Fix bf0649
[org-mode.git] / lisp / ox-md.el
blobc77a4bcf022b70811c67f4e829520b5a691fb6c2
1 ;;; ox-md.el --- Markdown Back-End for Org Export Engine
3 ;; Copyright (C) 2012-2014 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)
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 (comment . (lambda (&rest args) ""))
72 (comment-block . (lambda (&rest args) ""))
73 (example-block . org-md-example-block)
74 (fixed-width . org-md-example-block)
75 (footnote-definition . ignore)
76 (footnote-reference . ignore)
77 (headline . org-md-headline)
78 (horizontal-rule . org-md-horizontal-rule)
79 (inline-src-block . org-md-verbatim)
80 (italic . org-md-italic)
81 (item . org-md-item)
82 (line-break . org-md-line-break)
83 (link . org-md-link)
84 (paragraph . org-md-paragraph)
85 (plain-list . org-md-plain-list)
86 (plain-text . org-md-plain-text)
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 (and contents
248 (org-trim (replace-regexp-in-string "^" " " contents))))))
251 ;;;; Line Break
253 (defun org-md-line-break (line-break contents info)
254 "Transcode LINE-BREAK object into Markdown format.
255 CONTENTS is nil. INFO is a plist used as a communication
256 channel."
257 " \n")
260 ;;;; Link
262 (defun org-md-link (link contents info)
263 "Transcode LINE-BREAK object into Markdown format.
264 CONTENTS is the link's description. INFO is a plist used as
265 a communication channel."
266 (let ((--link-org-files-as-html-maybe
267 (function
268 (lambda (raw-path info)
269 ;; Treat links to `file.org' as links to `file.html', if
270 ;; needed. See `org-html-link-org-files-as-html'.
271 (cond
272 ((and org-html-link-org-files-as-html
273 (string= ".org"
274 (downcase (file-name-extension raw-path "."))))
275 (concat (file-name-sans-extension raw-path) "."
276 (plist-get info :html-extension)))
277 (t raw-path)))))
278 (type (org-element-property :type link)))
279 (cond ((member type '("custom-id" "id"))
280 (let ((destination (org-export-resolve-id-link link info)))
281 (if (stringp destination) ; External file.
282 (let ((path (funcall --link-org-files-as-html-maybe
283 destination info)))
284 (if (not contents) (format "<%s>" path)
285 (format "[%s](%s)" contents path)))
286 (concat
287 (and contents (concat contents " "))
288 (format "(%s)"
289 (format (org-export-translate "See section %s" :html info)
290 (mapconcat 'number-to-string
291 (org-export-get-headline-number
292 destination info)
293 ".")))))))
294 ((org-export-inline-image-p link org-html-inline-image-rules)
295 (let ((path (let ((raw-path (org-element-property :path link)))
296 (if (not (file-name-absolute-p raw-path)) raw-path
297 (expand-file-name raw-path)))))
298 (format "![%s](%s)"
299 (let ((caption (org-export-get-caption
300 (org-export-get-parent-element link))))
301 (when caption (org-export-data caption info)))
302 path)))
303 ((string= type "coderef")
304 (let ((ref (org-element-property :path link)))
305 (format (org-export-get-coderef-format ref contents)
306 (org-export-resolve-coderef ref info))))
307 ((equal type "radio")
308 (let ((destination (org-export-resolve-radio-link link info)))
309 (org-export-data (org-element-contents destination) info)))
310 ((equal type "fuzzy")
311 (let ((destination (org-export-resolve-fuzzy-link link info)))
312 (if (org-string-nw-p contents) contents
313 (when destination
314 (let ((number (org-export-get-ordinal destination info)))
315 (when number
316 (if (atom number) (number-to-string number)
317 (mapconcat 'number-to-string number "."))))))))
318 (t (let* ((raw-path (org-element-property :path link))
319 (path (cond
320 ((member type '("http" "https" "ftp"))
321 (concat type ":" raw-path))
322 ((equal type "file")
323 ;; Treat links to ".org" files as ".html",
324 ;; if needed.
325 (setq raw-path
326 (funcall --link-org-files-as-html-maybe
327 raw-path info))
328 ;; If file path is absolute, prepend it
329 ;; with protocol component - "file://".
330 (if (not (file-name-absolute-p raw-path)) raw-path
331 (concat "file://" (expand-file-name raw-path))))
332 (t raw-path))))
333 (if (not contents) (format "<%s>" path)
334 (format "[%s](%s)" contents path)))))))
337 ;;;; Paragraph
339 (defun org-md-paragraph (paragraph contents info)
340 "Transcode PARAGRAPH element into Markdown format.
341 CONTENTS is the paragraph contents. INFO is a plist used as
342 a communication channel."
343 (let ((first-object (car (org-element-contents paragraph))))
344 ;; If paragraph starts with a #, protect it.
345 (if (and (stringp first-object) (string-match "\\`#" first-object))
346 (replace-regexp-in-string "\\`#" "\\#" contents nil t)
347 contents)))
350 ;;;; Plain List
352 (defun org-md-plain-list (plain-list contents info)
353 "Transcode PLAIN-LIST element into Markdown format.
354 CONTENTS is the plain-list contents. INFO is a plist used as
355 a communication channel."
356 contents)
359 ;;;; Plain Text
361 (defun org-md-plain-text (text info)
362 "Transcode a TEXT string into Markdown format.
363 TEXT is the string to transcode. INFO is a plist holding
364 contextual information."
365 (when (plist-get info :with-smart-quotes)
366 (setq text (org-export-activate-smart-quotes text :html info)))
367 ;; Protect ambiguous #. This will protect # at the beginning of
368 ;; a line, but not at the beginning of a paragraph. See
369 ;; `org-md-paragraph'.
370 (setq text (replace-regexp-in-string "\n#" "\n\\\\#" text))
371 ;; Protect ambiguous !
372 (setq text (replace-regexp-in-string "\\(!\\)\\[" "\\\\!" text nil nil 1))
373 ;; Protect `, *, _ and \
374 (setq text (replace-regexp-in-string "[`*_\\]" "\\\\\\&" text))
375 ;; Handle special strings, if required.
376 (when (plist-get info :with-special-strings)
377 (setq text (org-html-convert-special-strings text)))
378 ;; Handle break preservation, if required.
379 (when (plist-get info :preserve-breaks)
380 (setq text (replace-regexp-in-string "[ \t]*\n" " \n" text)))
381 ;; Return value.
382 text)
385 ;;;; Quote Block
387 (defun org-md-quote-block (quote-block contents info)
388 "Transcode QUOTE-BLOCK element into Markdown format.
389 CONTENTS is the quote-block contents. INFO is a plist used as
390 a communication channel."
391 (replace-regexp-in-string
392 "^" "> "
393 (replace-regexp-in-string "\n\\'" "" contents)))
396 ;;;; Section
398 (defun org-md-section (section contents info)
399 "Transcode SECTION element into Markdown format.
400 CONTENTS is the section contents. INFO is a plist used as
401 a communication channel."
402 contents)
405 ;;;; Template
407 (defun org-md-template (contents info)
408 "Return complete document string after Markdown conversion.
409 CONTENTS is the transcoded contents string. INFO is a plist used
410 as a communication channel."
411 contents)
415 ;;; Interactive function
417 ;;;###autoload
418 (defun org-md-export-as-markdown (&optional async subtreep visible-only)
419 "Export current buffer to a Markdown buffer.
421 If narrowing is active in the current buffer, only export its
422 narrowed part.
424 If a region is active, export that region.
426 A non-nil optional argument ASYNC means the process should happen
427 asynchronously. The resulting buffer should be accessible
428 through the `org-export-stack' interface.
430 When optional argument SUBTREEP is non-nil, export the sub-tree
431 at point, extracting information from the headline properties
432 first.
434 When optional argument VISIBLE-ONLY is non-nil, don't export
435 contents of hidden elements.
437 Export is done in a buffer named \"*Org MD Export*\", which will
438 be displayed when `org-export-show-temporary-export-buffer' is
439 non-nil."
440 (interactive)
441 (org-export-to-buffer 'md "*Org MD Export*"
442 async subtreep visible-only nil nil (lambda () (text-mode))))
444 ;;;###autoload
445 (defun org-md-convert-region-to-md ()
446 "Assume the current region has org-mode syntax, and convert it to Markdown.
447 This can be used in any buffer. For example, you can write an
448 itemized list in org-mode syntax in a Markdown buffer and use
449 this command to convert it."
450 (interactive)
451 (org-export-replace-region-by 'md))
454 ;;;###autoload
455 (defun org-md-export-to-markdown (&optional async subtreep visible-only)
456 "Export current buffer to a Markdown file.
458 If narrowing is active in the current buffer, only export its
459 narrowed part.
461 If a region is active, export that region.
463 A non-nil optional argument ASYNC means the process should happen
464 asynchronously. The resulting file should be accessible through
465 the `org-export-stack' interface.
467 When optional argument SUBTREEP is non-nil, export the sub-tree
468 at point, extracting information from the headline properties
469 first.
471 When optional argument VISIBLE-ONLY is non-nil, don't export
472 contents of hidden elements.
474 Return output file's name."
475 (interactive)
476 (let ((outfile (org-export-output-file-name ".md" subtreep)))
477 (org-export-to-file 'md outfile async subtreep visible-only)))
480 (provide 'ox-md)
482 ;; Local variables:
483 ;; generated-autoload-file: "org-loaddefs.el"
484 ;; End:
486 ;;; ox-md.el ends here