ox-latex: Small refactor
[org-mode.git] / lisp / ox-md.el
blob602c14963f2bb9336dbcc2c1f26d556602b877e0
1 ;;; ox-md.el --- Markdown Back-End for Org Export Engine
3 ;; Copyright (C) 2012-2015 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou@gmail.com>
6 ;; Keywords: org, wp, markdown
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; This library implements a Markdown back-end (vanilla flavor) for
26 ;; Org exporter, based on `html' back-end. See Org manual for more
27 ;; information.
29 ;;; Code:
31 (eval-when-compile (require 'cl))
32 (require 'ox-html)
33 (require 'ox-publish)
36 ;;; User-Configurable Variables
38 (defgroup org-export-md nil
39 "Options specific to Markdown export back-end."
40 :tag "Org Markdown"
41 :group 'org-export
42 :version "24.4"
43 :package-version '(Org . "8.0"))
45 (defcustom org-md-headline-style 'atx
46 "Style used to format headlines.
47 This variable can be set to either `atx' or `setext'."
48 :group 'org-export-md
49 :type '(choice
50 (const :tag "Use \"atx\" style" atx)
51 (const :tag "Use \"Setext\" style" setext)))
55 ;;; Define Back-End
57 (org-export-define-derived-backend 'md 'html
58 :export-block '("MD" "MARKDOWN")
59 :filters-alist '((:filter-parse-tree . org-md-separate-elements))
60 :menu-entry
61 '(?m "Export to Markdown"
62 ((?M "To temporary buffer"
63 (lambda (a s v b) (org-md-export-as-markdown a s v)))
64 (?m "To file" (lambda (a s v b) (org-md-export-to-markdown a s v)))
65 (?o "To file and open"
66 (lambda (a s v b)
67 (if a (org-md-export-to-markdown t s v)
68 (org-open-file (org-md-export-to-markdown nil s v)))))))
69 :translate-alist '((bold . org-md-bold)
70 (code . org-md-verbatim)
71 (example-block . org-md-example-block)
72 (export-block . org-md-export-block)
73 (fixed-width . org-md-example-block)
74 (headline . org-md-headline)
75 (horizontal-rule . org-md-horizontal-rule)
76 (inline-src-block . org-md-verbatim)
77 (inner-template . org-md-inner-template)
78 (italic . org-md-italic)
79 (item . org-md-item)
80 (keyword . org-md-keyword)
81 (line-break . org-md-line-break)
82 (link . org-md-link)
83 (node-property . org-md-node-property)
84 (paragraph . org-md-paragraph)
85 (plain-list . org-md-plain-list)
86 (plain-text . org-md-plain-text)
87 (property-drawer . org-md-property-drawer)
88 (quote-block . org-md-quote-block)
89 (section . org-md-section)
90 (src-block . org-md-example-block)
91 (template . org-md-template)
92 (verbatim . org-md-verbatim))
93 :options-alist '((:md-headline-style nil nil org-md-headline-style)))
96 ;;; Filters
98 (defun org-md-separate-elements (tree backend info)
99 "Fix blank lines between elements.
101 TREE is the parse tree being exported. BACKEND is the export
102 back-end used. INFO is a plist used as a communication channel.
104 Enforce a blank line between elements. There are two exceptions
105 to this rule:
107 1. Preserve blank lines between sibling items in a plain list,
109 2. In an item, remove any blank line before the very first
110 paragraph and the next sub-list when the latter ends the
111 current item.
113 Assume BACKEND is `md'."
114 (org-element-map tree (remq 'item org-element-all-elements)
115 (lambda (e)
116 (org-element-put-property
117 e :post-blank
118 (if (and (eq (org-element-type e) 'paragraph)
119 (eq (org-element-type (org-element-property :parent e)) 'item)
120 (org-export-first-sibling-p e info)
121 (let ((next (org-export-get-next-element e info)))
122 (and (eq (org-element-type next) 'plain-list)
123 (not (org-export-get-next-element next info)))))
125 1))))
126 ;; Return updated tree.
127 tree)
131 ;;; Transcode Functions
133 ;;;; Bold
135 (defun org-md-bold (bold contents info)
136 "Transcode BOLD object into Markdown format.
137 CONTENTS is the text within bold markup. INFO is a plist used as
138 a communication channel."
139 (format "**%s**" contents))
142 ;;;; Code and Verbatim
144 (defun org-md-verbatim (verbatim contents info)
145 "Transcode VERBATIM object into Markdown format.
146 CONTENTS is nil. INFO is a plist used as a communication
147 channel."
148 (let ((value (org-element-property :value verbatim)))
149 (format (cond ((not (string-match "`" value)) "`%s`")
150 ((or (string-match "\\``" value)
151 (string-match "`\\'" value))
152 "`` %s ``")
153 (t "``%s``"))
154 value)))
157 ;;;; Example Block, Src Block and export Block
159 (defun org-md-example-block (example-block contents info)
160 "Transcode EXAMPLE-BLOCK element into Markdown format.
161 CONTENTS is nil. INFO is a plist used as a communication
162 channel."
163 (replace-regexp-in-string
164 "^" " "
165 (org-remove-indentation
166 (org-export-format-code-default example-block info))))
168 (defun org-md-export-block (export-block contents info)
169 "Transcode a EXPORT-BLOCK element from Org to Markdown.
170 CONTENTS is nil. INFO is a plist holding contextual information."
171 (if (member (org-element-property :type export-block) '("MARKDOWN" "MD"))
172 (org-remove-indentation (org-element-property :value export-block))
173 ;; Also include HTML export blocks.
174 (org-export-with-backend 'html export-block contents info)))
177 ;;;; Headline
179 (defun org-md-headline (headline contents info)
180 "Transcode HEADLINE element into Markdown format.
181 CONTENTS is the headline contents. INFO is a plist used as
182 a communication channel."
183 (unless (org-element-property :footnote-section-p headline)
184 (let* ((level (org-export-get-relative-level headline info))
185 (title (org-export-data (org-element-property :title headline) info))
186 (todo (and (plist-get info :with-todo-keywords)
187 (let ((todo (org-element-property :todo-keyword
188 headline)))
189 (and todo (concat (org-export-data todo info) " ")))))
190 (tags (and (plist-get info :with-tags)
191 (let ((tag-list (org-export-get-tags headline info)))
192 (and tag-list
193 (format " :%s:"
194 (mapconcat 'identity tag-list ":"))))))
195 (priority
196 (and (plist-get info :with-priority)
197 (let ((char (org-element-property :priority headline)))
198 (and char (format "[#%c] " char)))))
199 (anchor
200 (and (plist-get info :with-toc)
201 (format "<a id=\"%s\"></a>"
202 (or (org-element-property :CUSTOM_ID headline)
203 (org-export-get-reference headline info)))))
204 ;; Headline text without tags.
205 (heading (concat todo priority title))
206 (style (plist-get info :md-headline-style)))
207 (cond
208 ;; Cannot create a headline. Fall-back to a list.
209 ((or (org-export-low-level-p headline info)
210 (not (memq style '(atx setext)))
211 (and (eq style 'atx) (> level 6))
212 (and (eq style 'setext) (> level 2)))
213 (let ((bullet
214 (if (not (org-export-numbered-headline-p headline info)) "-"
215 (concat (number-to-string
216 (car (last (org-export-get-headline-number
217 headline info))))
218 "."))))
219 (concat bullet (make-string (- 4 (length bullet)) ?\s) heading tags
220 "\n\n"
221 (and contents
222 (replace-regexp-in-string "^" " " contents)))))
223 ;; Use "Setext" style.
224 ((eq style 'setext)
225 (concat heading tags anchor "\n"
226 (make-string (length heading) (if (= level 1) ?= ?-))
227 "\n\n"
228 contents))
229 ;; Use "atx" style.
230 (t (concat (make-string level ?#) " " heading tags anchor "\n\n"
231 contents))))))
234 ;;;; Horizontal Rule
236 (defun org-md-horizontal-rule (horizontal-rule contents info)
237 "Transcode HORIZONTAL-RULE element into Markdown format.
238 CONTENTS is the horizontal rule contents. INFO is a plist used
239 as a communication channel."
240 "---")
243 ;;;; Italic
245 (defun org-md-italic (italic contents info)
246 "Transcode ITALIC object into Markdown format.
247 CONTENTS is the text within italic markup. INFO is a plist used
248 as a communication channel."
249 (format "*%s*" contents))
252 ;;;; Item
254 (defun org-md-item (item contents info)
255 "Transcode ITEM element into Markdown format.
256 CONTENTS is the item contents. INFO is a plist used as
257 a communication channel."
258 (let* ((type (org-element-property :type (org-export-get-parent item)))
259 (struct (org-element-property :structure item))
260 (bullet (if (not (eq type 'ordered)) "-"
261 (concat (number-to-string
262 (car (last (org-list-get-item-number
263 (org-element-property :begin item)
264 struct
265 (org-list-prevs-alist struct)
266 (org-list-parents-alist struct)))))
267 "."))))
268 (concat bullet
269 (make-string (- 4 (length bullet)) ? )
270 (case (org-element-property :checkbox item)
271 (on "[X] ")
272 (trans "[-] ")
273 (off "[ ] "))
274 (let ((tag (org-element-property :tag item)))
275 (and tag (format "**%s:** "(org-export-data tag info))))
276 (and contents
277 (org-trim (replace-regexp-in-string "^" " " contents))))))
281 ;;;; Keyword
283 (defun org-md-keyword (keyword contents info)
284 "Transcode a KEYWORD element into Markdown format.
285 CONTENTS is nil. INFO is a plist used as a communication
286 channel."
287 (if (member (org-element-property :key keyword) '("MARKDOWN" "MD"))
288 (org-element-property :value keyword)
289 (org-export-with-backend 'html keyword contents info)))
292 ;;;; Line Break
294 (defun org-md-line-break (line-break contents info)
295 "Transcode LINE-BREAK object into Markdown format.
296 CONTENTS is nil. INFO is a plist used as a communication
297 channel."
298 " \n")
301 ;;;; Link
303 (defun org-md-link (link contents info)
304 "Transcode LINE-BREAK object into Markdown format.
305 CONTENTS is the link's description. INFO is a plist used as
306 a communication channel."
307 (let ((link-org-files-as-md
308 (lambda (raw-path)
309 ;; Treat links to `file.org' as links to `file.md'.
310 (if (string= ".org" (downcase (file-name-extension raw-path ".")))
311 (concat (file-name-sans-extension raw-path) ".md")
312 raw-path)))
313 (type (org-element-property :type link)))
314 (cond
315 ;; Link type is handled by a special function.
316 ((org-export-custom-protocol-maybe link contents 'md))
317 ((member type '("custom-id" "id" "fuzzy"))
318 (let ((destination (if (string= type "fuzzy")
319 (org-export-resolve-fuzzy-link link info)
320 (org-export-resolve-id-link link info))))
321 (case (org-element-type destination)
322 (plain-text ; External file.
323 (let ((path (funcall link-org-files-as-md destination)))
324 (if (not contents) (format "<%s>" path)
325 (format "[%s](%s)" contents path))))
326 (headline
327 (format
328 "[%s](#%s)"
329 ;; Description.
330 (cond ((org-string-nw-p contents))
331 ((org-export-numbered-headline-p destination info)
332 (mapconcat #'number-to-string
333 (org-export-get-headline-number destination info)
334 "."))
335 (t (org-export-data (org-element-property :title destination)
336 info)))
337 ;; Reference.
338 (or (org-element-property :CUSTOM_ID destination)
339 (org-export-get-reference destination info))))
341 (let ((description
342 (or (org-string-nw-p contents)
343 (let ((number (org-export-get-ordinal destination info)))
344 (cond
345 ((not number) nil)
346 ((atom number) (number-to-string number))
347 (t (mapconcat #'number-to-string number ".")))))))
348 (when description
349 (format "[%s](#%s)"
350 description
351 (org-export-get-reference destination info))))))))
352 ((org-export-inline-image-p link org-html-inline-image-rules)
353 (let ((path (let ((raw-path (org-element-property :path link)))
354 (if (not (file-name-absolute-p raw-path)) raw-path
355 (expand-file-name raw-path))))
356 (caption (org-export-data
357 (org-export-get-caption
358 (org-export-get-parent-element link)) info)))
359 (format "![img](%s)"
360 (if (not (org-string-nw-p caption)) path
361 (format "%s \"%s\"" path caption)))))
362 ((string= type "coderef")
363 (let ((ref (org-element-property :path link)))
364 (format (org-export-get-coderef-format ref contents)
365 (org-export-resolve-coderef ref info))))
366 ((equal type "radio") contents)
367 (t (let* ((raw-path (org-element-property :path link))
368 (path
369 (cond
370 ((member type '("http" "https" "ftp"))
371 (concat type ":" raw-path))
372 ((string= type "file")
373 (org-export-file-uri (funcall link-org-files-as-md raw-path)))
374 (t raw-path))))
375 (if (not contents) (format "<%s>" path)
376 (format "[%s](%s)" contents path)))))))
379 ;;;; Node Property
381 (defun org-md-node-property (node-property contents info)
382 "Transcode a NODE-PROPERTY element into Markdown syntax.
383 CONTENTS is nil. INFO is a plist holding contextual
384 information."
385 (format "%s:%s"
386 (org-element-property :key node-property)
387 (let ((value (org-element-property :value node-property)))
388 (if value (concat " " value) ""))))
391 ;;;; Paragraph
393 (defun org-md-paragraph (paragraph contents info)
394 "Transcode PARAGRAPH element into Markdown format.
395 CONTENTS is the paragraph contents. INFO is a plist used as
396 a communication channel."
397 (let ((first-object (car (org-element-contents paragraph))))
398 ;; If paragraph starts with a #, protect it.
399 (if (and (stringp first-object) (string-match "\\`#" first-object))
400 (replace-regexp-in-string "\\`#" "\\#" contents nil t)
401 contents)))
404 ;;;; Plain List
406 (defun org-md-plain-list (plain-list contents info)
407 "Transcode PLAIN-LIST element into Markdown format.
408 CONTENTS is the plain-list contents. INFO is a plist used as
409 a communication channel."
410 contents)
413 ;;;; Plain Text
415 (defun org-md-plain-text (text info)
416 "Transcode a TEXT string into Markdown format.
417 TEXT is the string to transcode. INFO is a plist holding
418 contextual information."
419 (when (plist-get info :with-smart-quotes)
420 (setq text (org-export-activate-smart-quotes text :html info)))
421 ;; Protect ambiguous #. This will protect # at the beginning of
422 ;; a line, but not at the beginning of a paragraph. See
423 ;; `org-md-paragraph'.
424 (setq text (replace-regexp-in-string "\n#" "\n\\\\#" text))
425 ;; Protect ambiguous !
426 (setq text (replace-regexp-in-string "\\(!\\)\\[" "\\\\!" text nil nil 1))
427 ;; Protect `, *, _ and \
428 (setq text (replace-regexp-in-string "[`*_\\]" "\\\\\\&" text))
429 ;; Handle special strings, if required.
430 (when (plist-get info :with-special-strings)
431 (setq text (org-html-convert-special-strings text)))
432 ;; Handle break preservation, if required.
433 (when (plist-get info :preserve-breaks)
434 (setq text (replace-regexp-in-string "[ \t]*\n" " \n" text)))
435 ;; Return value.
436 text)
439 ;;;; Property Drawer
441 (defun org-md-property-drawer (property-drawer contents info)
442 "Transcode a PROPERTY-DRAWER element into Markdown format.
443 CONTENTS holds the contents of the drawer. INFO is a plist
444 holding contextual information."
445 (and (org-string-nw-p contents)
446 (replace-regexp-in-string "^" " " contents)))
449 ;;;; Quote Block
451 (defun org-md-quote-block (quote-block contents info)
452 "Transcode QUOTE-BLOCK element into Markdown format.
453 CONTENTS is the quote-block contents. INFO is a plist used as
454 a communication channel."
455 (replace-regexp-in-string
456 "^" "> "
457 (replace-regexp-in-string "\n\\'" "" contents)))
460 ;;;; Section
462 (defun org-md-section (section contents info)
463 "Transcode SECTION element into Markdown format.
464 CONTENTS is the section contents. INFO is a plist used as
465 a communication channel."
466 contents)
469 ;;;; Template
471 (defun org-md-inner-template (contents info)
472 "Return body of document after converting it to Markdown syntax.
473 CONTENTS is the transcoded contents string. INFO is a plist
474 holding export options."
475 ;; Make sure CONTENTS is separated from table of contents and
476 ;; footnotes with at least a blank line.
477 (org-trim (org-html-inner-template (concat "\n" contents "\n") info)))
479 (defun org-md-template (contents info)
480 "Return complete document string after Markdown conversion.
481 CONTENTS is the transcoded contents string. INFO is a plist used
482 as a communication channel."
483 contents)
487 ;;; Interactive function
489 ;;;###autoload
490 (defun org-md-export-as-markdown (&optional async subtreep visible-only)
491 "Export current buffer to a Markdown buffer.
493 If narrowing is active in the current buffer, only export its
494 narrowed part.
496 If a region is active, export that region.
498 A non-nil optional argument ASYNC means the process should happen
499 asynchronously. The resulting buffer should be accessible
500 through the `org-export-stack' interface.
502 When optional argument SUBTREEP is non-nil, export the sub-tree
503 at point, extracting information from the headline properties
504 first.
506 When optional argument VISIBLE-ONLY is non-nil, don't export
507 contents of hidden elements.
509 Export is done in a buffer named \"*Org MD Export*\", which will
510 be displayed when `org-export-show-temporary-export-buffer' is
511 non-nil."
512 (interactive)
513 (org-export-to-buffer 'md "*Org MD Export*"
514 async subtreep visible-only nil nil (lambda () (text-mode))))
516 ;;;###autoload
517 (defun org-md-convert-region-to-md ()
518 "Assume the current region has org-mode syntax, and convert it to Markdown.
519 This can be used in any buffer. For example, you can write an
520 itemized list in org-mode syntax in a Markdown buffer and use
521 this command to convert it."
522 (interactive)
523 (org-export-replace-region-by 'md))
526 ;;;###autoload
527 (defun org-md-export-to-markdown (&optional async subtreep visible-only)
528 "Export current buffer to a Markdown file.
530 If narrowing is active in the current buffer, only export its
531 narrowed part.
533 If a region is active, export that region.
535 A non-nil optional argument ASYNC means the process should happen
536 asynchronously. The resulting file should be accessible through
537 the `org-export-stack' interface.
539 When optional argument SUBTREEP is non-nil, export the sub-tree
540 at point, extracting information from the headline properties
541 first.
543 When optional argument VISIBLE-ONLY is non-nil, don't export
544 contents of hidden elements.
546 Return output file's name."
547 (interactive)
548 (let ((outfile (org-export-output-file-name ".md" subtreep)))
549 (org-export-to-file 'md outfile async subtreep visible-only)))
551 ;;;###autoload
552 (defun org-md-publish-to-md (plist filename pub-dir)
553 "Publish an org file to Markdown.
555 FILENAME is the filename of the Org file to be published. PLIST
556 is the property list for the given project. PUB-DIR is the
557 publishing directory.
559 Return output file name."
560 (org-publish-org-to 'md filename ".md" plist pub-dir))
562 (provide 'ox-md)
564 ;; Local variables:
565 ;; generated-autoload-file: "org-loaddefs.el"
566 ;; End:
568 ;;; ox-md.el ends here