Merge branch 'maint'
[org-mode.git] / lisp / ox-md.el
blob14e881d249d1fc71b596cda478f4be3461ad17b0
1 ;;; ox-md.el --- Markdown Back-End for Org Export Engine
3 ;; Copyright (C) 2012, 2013 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou@gmail.com>
6 ;; Keywords: org, wp, markdown
8 ;; GNU Emacs is free software: you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21 ;;; Commentary:
23 ;; This library implements a Markdown back-end (vanilla flavour) for
24 ;; Org exporter, based on `html' back-end. See Org manual for more
25 ;; information.
27 ;;; Code:
29 (eval-when-compile (require 'cl))
30 (require 'ox-html)
31 (require 'ox-publish)
34 ;;; User-Configurable Variables
36 (defgroup org-export-md nil
37 "Options specific to Markdown export back-end."
38 :tag "Org Markdown"
39 :group 'org-export
40 :version "24.4"
41 :package-version '(Org . "8.0"))
43 (defcustom org-md-headline-style 'atx
44 "Style used to format headlines.
45 This variable can be set to either `atx' or `setext'."
46 :group 'org-export-md
47 :type '(choice
48 (const :tag "Use \"atx\" style" atx)
49 (const :tag "Use \"Setext\" style" setext)))
53 ;;; Define Back-End
55 (org-export-define-derived-backend 'md 'html
56 :export-block '("MD" "MARKDOWN")
57 :filters-alist '((:filter-parse-tree . org-md-separate-elements))
58 :menu-entry
59 '(?m "Export to Markdown"
60 ((?M "To temporary buffer"
61 (lambda (a s v b) (org-md-export-as-markdown a s v)))
62 (?m "To file" (lambda (a s v b) (org-md-export-to-markdown a s v)))
63 (?o "To file and open"
64 (lambda (a s v b)
65 (if a (org-md-export-to-markdown t s v)
66 (org-open-file (org-md-export-to-markdown nil s v)))))))
67 :translate-alist '((bold . org-md-bold)
68 (code . org-md-verbatim)
69 (comment . (lambda (&rest args) ""))
70 (comment-block . (lambda (&rest args) ""))
71 (example-block . org-md-example-block)
72 (fixed-width . org-md-example-block)
73 (footnote-definition . ignore)
74 (footnote-reference . ignore)
75 (headline . org-md-headline)
76 (horizontal-rule . org-md-horizontal-rule)
77 (inline-src-block . org-md-verbatim)
78 (italic . org-md-italic)
79 (item . org-md-item)
80 (line-break . org-md-line-break)
81 (link . org-md-link)
82 (node-property . org-md-node-property)
83 (paragraph . org-md-paragraph)
84 (plain-list . org-md-plain-list)
85 (plain-text . org-md-plain-text)
86 (property-drawer . org-md-property-drawer)
87 (quote-block . org-md-quote-block)
88 (quote-section . org-md-example-block)
89 (section . org-md-section)
90 (src-block . org-md-example-block)
91 (template . org-md-template)
92 (verbatim . org-md-verbatim)))
96 ;;; Filters
98 (defun org-md-separate-elements (tree backend info)
99 "Make sure elements are separated by at least one blank line.
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 Assume BACKEND is `md'."
105 (org-element-map tree org-element-all-elements
106 (lambda (elem)
107 (unless (eq (org-element-type elem) 'org-data)
108 (org-element-put-property
109 elem :post-blank
110 (let ((post-blank (org-element-property :post-blank elem)))
111 (if (not post-blank) 1 (max 1 post-blank)))))))
112 ;; Return updated tree.
113 tree)
117 ;;; Transcode Functions
119 ;;;; Bold
121 (defun org-md-bold (bold contents info)
122 "Transcode BOLD object into Markdown format.
123 CONTENTS is the text within bold markup. INFO is a plist used as
124 a communication channel."
125 (format "**%s**" contents))
128 ;;;; Code and Verbatim
130 (defun org-md-verbatim (verbatim contents info)
131 "Transcode VERBATIM object into Markdown format.
132 CONTENTS is nil. INFO is a plist used as a communication
133 channel."
134 (let ((value (org-element-property :value verbatim)))
135 (format (cond ((not (string-match "`" value)) "`%s`")
136 ((or (string-match "\\``" value)
137 (string-match "`\\'" value))
138 "`` %s ``")
139 (t "``%s``"))
140 value)))
143 ;;;; Example Block and Src Block
145 (defun org-md-example-block (example-block contents info)
146 "Transcode EXAMPLE-BLOCK element into Markdown format.
147 CONTENTS is nil. INFO is a plist used as a communication
148 channel."
149 (replace-regexp-in-string
150 "^" " "
151 (org-remove-indentation
152 (org-element-property :value example-block))))
155 ;;;; Headline
157 (defun org-md-headline (headline contents info)
158 "Transcode HEADLINE element into Markdown format.
159 CONTENTS is the headline contents. INFO is a plist used as
160 a communication channel."
161 (unless (org-element-property :footnote-section-p headline)
162 (let* ((level (org-export-get-relative-level headline info))
163 (title (org-export-data (org-element-property :title headline) info))
164 (todo (and (plist-get info :with-todo-keywords)
165 (let ((todo (org-element-property :todo-keyword
166 headline)))
167 (and todo (concat (org-export-data todo info) " ")))))
168 (tags (and (plist-get info :with-tags)
169 (let ((tag-list (org-export-get-tags headline info)))
170 (and tag-list
171 (format " :%s:"
172 (mapconcat 'identity tag-list ":"))))))
173 (priority
174 (and (plist-get info :with-priority)
175 (let ((char (org-element-property :priority headline)))
176 (and char (format "[#%c] " char)))))
177 ;; Headline text without tags.
178 (heading (concat todo priority title)))
179 (cond
180 ;; Cannot create a headline. Fall-back to a list.
181 ((or (org-export-low-level-p headline info)
182 (not (memq org-md-headline-style '(atx setext)))
183 (and (eq org-md-headline-style 'atx) (> level 6))
184 (and (eq org-md-headline-style 'setext) (> level 2)))
185 (let ((bullet
186 (if (not (org-export-numbered-headline-p headline info)) "-"
187 (concat (number-to-string
188 (car (last (org-export-get-headline-number
189 headline info))))
190 "."))))
191 (concat bullet (make-string (- 4 (length bullet)) ? ) heading tags
192 "\n\n"
193 (and contents
194 (replace-regexp-in-string "^" " " contents)))))
195 ;; Use "Setext" style.
196 ((eq org-md-headline-style 'setext)
197 (concat heading tags "\n"
198 (make-string (length heading) (if (= level 1) ?= ?-))
199 "\n\n"
200 contents))
201 ;; Use "atx" style.
202 (t (concat (make-string level ?#) " " heading tags "\n\n" contents))))))
205 ;;;; Horizontal Rule
207 (defun org-md-horizontal-rule (horizontal-rule contents info)
208 "Transcode HORIZONTAL-RULE element into Markdown format.
209 CONTENTS is the horizontal rule contents. INFO is a plist used
210 as a communication channel."
211 "---")
214 ;;;; Italic
216 (defun org-md-italic (italic contents info)
217 "Transcode ITALIC object into Markdown format.
218 CONTENTS is the text within italic markup. INFO is a plist used
219 as a communication channel."
220 (format "*%s*" contents))
223 ;;;; Item
225 (defun org-md-item (item contents info)
226 "Transcode ITEM element into Markdown format.
227 CONTENTS is the item contents. INFO is a plist used as
228 a communication channel."
229 (let* ((type (org-element-property :type (org-export-get-parent item)))
230 (struct (org-element-property :structure item))
231 (bullet (if (not (eq type 'ordered)) "-"
232 (concat (number-to-string
233 (car (last (org-list-get-item-number
234 (org-element-property :begin item)
235 struct
236 (org-list-prevs-alist struct)
237 (org-list-parents-alist struct)))))
238 "."))))
239 (concat bullet
240 (make-string (- 4 (length bullet)) ? )
241 (case (org-element-property :checkbox item)
242 (on "[X] ")
243 (trans "[-] ")
244 (off "[ ] "))
245 (let ((tag (org-element-property :tag item)))
246 (and tag (format "**%s:** "(org-export-data tag info))))
247 (org-trim (replace-regexp-in-string "^" " " contents)))))
250 ;;;; Line Break
252 (defun org-md-line-break (line-break contents info)
253 "Transcode LINE-BREAK object into Markdown format.
254 CONTENTS is nil. INFO is a plist used as a communication
255 channel."
256 " \n")
259 ;;;; Link
261 (defun org-md-link (link contents info)
262 "Transcode LINE-BREAK object into Markdown format.
263 CONTENTS is the link's description. INFO is a plist used as
264 a communication channel."
265 (let ((--link-org-files-as-html-maybe
266 (function
267 (lambda (raw-path info)
268 ;; Treat links to `file.org' as links to `file.html', if
269 ;; needed. See `org-html-link-org-files-as-html'.
270 (cond
271 ((and org-html-link-org-files-as-html
272 (string= ".org"
273 (downcase (file-name-extension raw-path "."))))
274 (concat (file-name-sans-extension raw-path) "."
275 (plist-get info :html-extension)))
276 (t raw-path)))))
277 (type (org-element-property :type link)))
278 (cond ((member type '("custom-id" "id"))
279 (let ((destination (org-export-resolve-id-link link info)))
280 (if (stringp destination) ; External file.
281 (let ((path (funcall --link-org-files-as-html-maybe
282 destination info)))
283 (if (not contents) (format "<%s>" path)
284 (format "[%s](%s)" contents path)))
285 (concat
286 (and contents (concat contents " "))
287 (format "(%s)"
288 (format (org-export-translate "See section %s" :html info)
289 (mapconcat 'number-to-string
290 (org-export-get-headline-number
291 destination info)
292 ".")))))))
293 ((org-export-inline-image-p link org-html-inline-image-rules)
294 (let ((path (let ((raw-path (org-element-property :path link)))
295 (if (not (file-name-absolute-p raw-path)) raw-path
296 (expand-file-name raw-path)))))
297 (format "![%s](%s)"
298 (let ((caption (org-export-get-caption
299 (org-export-get-parent-element link))))
300 (when caption (org-export-data caption info)))
301 path)))
302 ((string= type "coderef")
303 (let ((ref (org-element-property :path link)))
304 (format (org-export-get-coderef-format ref contents)
305 (org-export-resolve-coderef ref info))))
306 ((equal type "radio")
307 (let ((destination (org-export-resolve-radio-link link info)))
308 (org-export-data (org-element-contents destination) info)))
309 ((equal type "fuzzy")
310 (let ((destination (org-export-resolve-fuzzy-link link info)))
311 (if (org-string-nw-p contents) contents
312 (when destination
313 (let ((number (org-export-get-ordinal destination info)))
314 (when number
315 (if (atom number) (number-to-string number)
316 (mapconcat 'number-to-string number "."))))))))
317 (t (let* ((raw-path (org-element-property :path link))
318 (path (cond
319 ((member type '("http" "https" "ftp"))
320 (concat type ":" raw-path))
321 ((equal type "file")
322 ;; Treat links to ".org" files as ".html",
323 ;; if needed.
324 (setq raw-path
325 (funcall --link-org-files-as-html-maybe
326 raw-path info))
327 ;; If file path is absolute, prepend it
328 ;; with protocol component - "file://".
329 (if (not (file-name-absolute-p raw-path)) raw-path
330 (concat "file://" (expand-file-name raw-path))))
331 (t raw-path))))
332 (if (not contents) (format "<%s>" path)
333 (format "[%s](%s)" contents path)))))))
336 ;;;; Node Property
338 (defun org-md-node-property (node-property contents info)
339 "Transcode a NODE-PROPERTY element into Markdown syntax.
340 CONTENTS is nil. INFO is a plist holding contextual
341 information."
342 (format "%s:%s"
343 (org-element-property :key node-property)
344 (let ((value (org-element-property :value node-property)))
345 (if value (concat " " value) ""))))
348 ;;;; Paragraph
350 (defun org-md-paragraph (paragraph contents info)
351 "Transcode PARAGRAPH element into Markdown format.
352 CONTENTS is the paragraph contents. INFO is a plist used as
353 a communication channel."
354 (let ((first-object (car (org-element-contents paragraph))))
355 ;; If paragraph starts with a #, protect it.
356 (if (and (stringp first-object) (string-match "\\`#" first-object))
357 (replace-regexp-in-string "\\`#" "\\#" contents nil t)
358 contents)))
361 ;;;; Plain List
363 (defun org-md-plain-list (plain-list contents info)
364 "Transcode PLAIN-LIST element into Markdown format.
365 CONTENTS is the plain-list contents. INFO is a plist used as
366 a communication channel."
367 contents)
370 ;;;; Plain Text
372 (defun org-md-plain-text (text info)
373 "Transcode a TEXT string into Markdown format.
374 TEXT is the string to transcode. INFO is a plist holding
375 contextual information."
376 (when (plist-get info :with-smart-quotes)
377 (setq text (org-export-activate-smart-quotes text :html info)))
378 ;; Protect ambiguous #. This will protect # at the beginning of
379 ;; a line, but not at the beginning of a paragraph. See
380 ;; `org-md-paragraph'.
381 (setq text (replace-regexp-in-string "\n#" "\n\\\\#" text))
382 ;; Protect ambiguous !
383 (setq text (replace-regexp-in-string "\\(!\\)\\[" "\\\\!" text nil nil 1))
384 ;; Protect `, *, _ and \
385 (setq text (replace-regexp-in-string "[`*_\\]" "\\\\\\&" text))
386 ;; Handle special strings, if required.
387 (when (plist-get info :with-special-strings)
388 (setq text (org-html-convert-special-strings text)))
389 ;; Handle break preservation, if required.
390 (when (plist-get info :preserve-breaks)
391 (setq text (replace-regexp-in-string "[ \t]*\n" " \n" text)))
392 ;; Return value.
393 text)
396 ;;;; Property Drawer
398 (defun org-md-property-drawer (property-drawer contents info)
399 "Transcode a PROPERTY-DRAWER element into Markdown format.
400 CONTENTS holds the contents of the drawer. INFO is a plist
401 holding contextual information."
402 (and (org-string-nw-p contents)
403 (replace-regexp-in-string "^" " " contents)))
406 ;;;; Quote Block
408 (defun org-md-quote-block (quote-block contents info)
409 "Transcode QUOTE-BLOCK element into Markdown format.
410 CONTENTS is the quote-block contents. INFO is a plist used as
411 a communication channel."
412 (replace-regexp-in-string
413 "^" "> "
414 (replace-regexp-in-string "\n\\'" "" contents)))
417 ;;;; Section
419 (defun org-md-section (section contents info)
420 "Transcode SECTION element into Markdown format.
421 CONTENTS is the section contents. INFO is a plist used as
422 a communication channel."
423 contents)
426 ;;;; Template
428 (defun org-md-template (contents info)
429 "Return complete document string after Markdown conversion.
430 CONTENTS is the transcoded contents string. INFO is a plist used
431 as a communication channel."
432 contents)
436 ;;; Interactive function
438 ;;;###autoload
439 (defun org-md-export-as-markdown (&optional async subtreep visible-only)
440 "Export current buffer to a Markdown buffer.
442 If narrowing is active in the current buffer, only export its
443 narrowed part.
445 If a region is active, export that region.
447 A non-nil optional argument ASYNC means the process should happen
448 asynchronously. The resulting buffer should be accessible
449 through the `org-export-stack' interface.
451 When optional argument SUBTREEP is non-nil, export the sub-tree
452 at point, extracting information from the headline properties
453 first.
455 When optional argument VISIBLE-ONLY is non-nil, don't export
456 contents of hidden elements.
458 Export is done in a buffer named \"*Org MD Export*\", which will
459 be displayed when `org-export-show-temporary-export-buffer' is
460 non-nil."
461 (interactive)
462 (org-export-to-buffer 'md "*Org MD Export*"
463 async subtreep visible-only nil nil (lambda () (text-mode))))
465 ;;;###autoload
466 (defun org-md-convert-region-to-md ()
467 "Assume the current region has org-mode syntax, and convert it to Markdown.
468 This can be used in any buffer. For example, you can write an
469 itemized list in org-mode syntax in a Markdown buffer and use
470 this command to convert it."
471 (interactive)
472 (org-export-replace-region-by 'md))
475 ;;;###autoload
476 (defun org-md-export-to-markdown (&optional async subtreep visible-only)
477 "Export current buffer to a Markdown file.
479 If narrowing is active in the current buffer, only export its
480 narrowed part.
482 If a region is active, export that region.
484 A non-nil optional argument ASYNC means the process should happen
485 asynchronously. The resulting file should be accessible through
486 the `org-export-stack' interface.
488 When optional argument SUBTREEP is non-nil, export the sub-tree
489 at point, extracting information from the headline properties
490 first.
492 When optional argument VISIBLE-ONLY is non-nil, don't export
493 contents of hidden elements.
495 Return output file's name."
496 (interactive)
497 (let ((outfile (org-export-output-file-name ".md" subtreep)))
498 (org-export-to-file 'md outfile async subtreep visible-only)))
500 ;;;###autoload
501 (defun org-md-publish-to-md (plist filename pub-dir)
502 "Publish an org file to Markdown.
504 FILENAME is the filename of the Org file to be published. PLIST
505 is the property list for the given project. PUB-DIR is the
506 publishing directory.
508 Return output file name."
509 (org-publish-org-to 'md filename ".md" plist pub-dir))
511 (provide 'ox-md)
513 ;; Local variables:
514 ;; generated-autoload-file: "org-loaddefs.el"
515 ;; End:
517 ;;; ox-md.el ends here