Move new export framework files into core
[org-mode.git] / lisp / ox-md.el
blob24ca0a10f896b0a04996510938f2caf5ff157ea7
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, tex
8 ;; This program 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 ;; This program 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 this program. 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.
26 ;; It provides two commands for export, depending on the desired
27 ;; output: `org-md-export-as-markdown' (temporary buffer) and
28 ;; `org-md-export-to-markdown' ("md" file).
30 ;;; Code:
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.2")
44 (defcustom org-md-headline-style 'atx
45 "Style used to format headlines.
46 This variable can be set to either `atx' or `setext'."
47 :group 'org-export-md
48 :type '(choice
49 (const :tag "Use \"atx\" style" atx)
50 (const :tag "Use \"Setext\" style" setext)))
54 ;;; Define Back-End
56 (org-export-define-derived-backend md html
57 :export-block ("MD" "MARKDOWN")
58 :filters-alist ((:filter-parse-tree . org-md-separate-elements))
59 :menu-entry
60 (?m "Export to Markdown"
61 ((?M "To temporary buffer"
62 (lambda (a s v b) (org-md-export-as-markdown a s v)))
63 (?m "To file" (lambda (a s v b) (org-md-export-to-markdown a s v)))
64 (?o "To file and open"
65 (lambda (a s v b)
66 (if a (org-md-export-to-markdown t s v)
67 (org-open-file (org-md-export-to-markdown nil s v)))))))
68 :translate-alist ((bold . org-md-bold)
69 (code . org-md-verbatim)
70 (example-block . org-md-example-block)
71 (fixed-width . org-md-example-block)
72 (footnote-definition . ignore)
73 (footnote-reference . ignore)
74 (headline . org-md-headline)
75 (horizontal-rule . org-md-horizontal-rule)
76 (inline-src-block . org-md-verbatim)
77 (italic . org-md-italic)
78 (item . org-md-item)
79 (line-break . org-md-line-break)
80 (link . org-md-link)
81 (paragraph . org-md-paragraph)
82 (plain-list . org-md-plain-list)
83 (plain-text . org-md-plain-text)
84 (quote-block . org-md-quote-block)
85 (quote-section . org-md-example-block)
86 (section . org-md-section)
87 (src-block . org-md-example-block)
88 (template . org-md-template)
89 (verbatim . org-md-verbatim)))
93 ;;; Filters
95 (defun org-md-separate-elements (tree backend info)
96 "Make sure elements are separated by at least one blank line.
98 TREE is the parse tree being exported. BACKEND is the export
99 back-end used. INFO is a plist used as a communication channel.
101 Assume BACKEND is `md'."
102 (org-element-map tree org-element-all-elements
103 (lambda (elem)
104 (unless (eq (org-element-type elem) 'org-data)
105 (org-element-put-property
106 elem :post-blank
107 (let ((post-blank (org-element-property :post-blank elem)))
108 (if (not post-blank) 1 (max 1 post-blank)))))))
109 ;; Return updated tree.
110 tree)
114 ;;; Transcode Functions
116 ;;;; Bold
118 (defun org-md-bold (bold contents info)
119 "Transcode BOLD object into Markdown format.
120 CONTENTS is the text within bold markup. INFO is a plist used as
121 a communication channel."
122 (format "**%s**" contents))
125 ;;;; Code and Verbatim
127 (defun org-md-verbatim (verbatim contents info)
128 "Transcode VERBATIM object into Markdown format.
129 CONTENTS is nil. INFO is a plist used as a communication
130 channel."
131 (let ((value (org-element-property :value verbatim)))
132 (format (cond ((not (string-match "`" value)) "`%s`")
133 ((or (string-match "\\``" value)
134 (string-match "`\\'" value))
135 "`` %s ``")
136 (t "``%s``"))
137 value)))
140 ;;;; Example Block and Src Block
142 (defun org-md-example-block (example-block contents info)
143 "Transcode EXAMPLE-BLOCK element into Markdown format.
144 CONTENTS is nil. INFO is a plist used as a communication
145 channel."
146 (replace-regexp-in-string
147 "^" " "
148 (org-remove-indentation
149 (org-element-property :value example-block))))
152 ;;;; Headline
154 (defun org-md-headline (headline contents info)
155 "Transcode HEADLINE element into Markdown format.
156 CONTENTS is the headline contents. INFO is a plist used as
157 a communication channel."
158 (unless (org-element-property :footnote-section-p headline)
159 (let* ((level (org-export-get-relative-level headline info))
160 (title (org-export-data (org-element-property :title headline) info))
161 (todo (and (plist-get info :with-todo-keywords)
162 (let ((todo (org-element-property :todo-keyword
163 headline)))
164 (and todo (concat (org-export-data todo info) " ")))))
165 (tags (and (plist-get info :with-tags)
166 (let ((tag-list (org-export-get-tags headline info)))
167 (and tag-list
168 (format " :%s:"
169 (mapconcat 'identity tag-list ":"))))))
170 (priority
171 (and (plist-get info :with-priority)
172 (let ((char (org-element-property :priority headline)))
173 (and char (format "[#%c] " char)))))
174 ;; Headline text without tags.
175 (heading (concat todo priority title)))
176 (cond
177 ;; Cannot create an headline. Fall-back to a list.
178 ((or (org-export-low-level-p headline info)
179 (not (memq org-md-headline-style '(atx setext)))
180 (and (eq org-md-headline-style 'atx) (> level 6))
181 (and (eq org-md-headline-style 'setext) (> level 2)))
182 (let ((bullet
183 (if (not (org-export-numbered-headline-p headline info)) "-"
184 (concat (number-to-string
185 (car (last (org-export-get-headline-number
186 headline info))))
187 "."))))
188 (concat bullet (make-string (- 4 (length bullet)) ? ) heading tags
189 "\n\n"
190 (and contents
191 (replace-regexp-in-string "^" " " contents)))))
192 ;; Use "Setext" style.
193 ((eq org-md-headline-style 'setext)
194 (concat heading tags "\n"
195 (make-string (length heading) (if (= level 1) ?= ?-))
196 "\n\n"
197 contents))
198 ;; Use "atx" style.
199 (t (concat (make-string level ?#) " " heading tags "\n\n" contents))))))
202 ;;;; Horizontal Rule
204 (defun org-md-horizontal-rule (horizontal-rule contents info)
205 "Transcode HORIZONTAL-RULE element into Markdown format.
206 CONTENTS is the horizontal rule contents. INFO is a plist used
207 as a communication channel."
208 "---")
211 ;;;; Italic
213 (defun org-md-italic (italic contents info)
214 "Transcode ITALIC object into Markdown format.
215 CONTENTS is the text within italic markup. INFO is a plist used
216 as a communication channel."
217 (format "*%s*" contents))
220 ;;;; Item
222 (defun org-md-item (item contents info)
223 "Transcode ITEM element into Markdown format.
224 CONTENTS is the item contents. INFO is a plist used as
225 a communication channel."
226 (let* ((type (org-element-property :type (org-export-get-parent item)))
227 (struct (org-element-property :structure item))
228 (bullet (if (not (eq type 'ordered)) "-"
229 (concat (number-to-string
230 (car (last (org-list-get-item-number
231 (org-element-property :begin item)
232 struct
233 (org-list-prevs-alist struct)
234 (org-list-parents-alist struct)))))
235 "."))))
236 (concat bullet
237 (make-string (- 4 (length bullet)) ? )
238 (case (org-element-property :checkbox item)
239 (on "[X] ")
240 (trans "[-] ")
241 (off "[ ] "))
242 (let ((tag (org-element-property :tag item)))
243 (and tag (format "**%s:** "(org-export-data tag info))))
244 (org-trim (replace-regexp-in-string "^" " " contents)))))
247 ;;;; Line Break
249 (defun org-md-line-break (line-break contents info)
250 "Transcode LINE-BREAK object into Markdown format.
251 CONTENTS is nil. INFO is a plist used as a communication
252 channel."
253 " \n")
256 ;;;; Link
258 (defun org-md-link (link contents info)
259 "Transcode LINE-BREAK object into Markdown format.
260 CONTENTS is the link's description. INFO is a plist used as
261 a communication channel."
262 (let ((--link-org-files-as-html-maybe
263 (function
264 (lambda (raw-path info)
265 ;; Treat links to `file.org' as links to `file.html', if
266 ;; needed. See `org-html-link-org-files-as-html'.
267 (cond
268 ((and org-html-link-org-files-as-html
269 (string= ".org"
270 (downcase (file-name-extension raw-path "."))))
271 (concat (file-name-sans-extension raw-path) "."
272 (plist-get info :html-extension)))
273 (t raw-path)))))
274 (type (org-element-property :type link)))
275 (cond ((member type '("custom-id" "id"))
276 (let ((destination (org-export-resolve-id-link link info)))
277 (if (stringp destination) ; External file.
278 (let ((path (funcall --link-org-files-as-html-maybe
279 destination info)))
280 (if (not contents) (format "<%s>" path)
281 (format "[%s](%s)" contents path)))
282 (concat
283 (and contents (concat contents " "))
284 (format "(%s)"
285 (format (org-export-translate "See section %s" :html info)
286 (mapconcat 'number-to-string
287 (org-export-get-headline-number
288 destination info)
289 ".")))))))
290 ((org-export-inline-image-p link org-html-inline-image-rules)
291 (let ((path (let ((raw-path (org-element-property :path link)))
292 (if (not (file-name-absolute-p raw-path)) raw-path
293 (expand-file-name raw-path)))))
294 (format "![%s](%s)"
295 (let ((caption (org-export-get-caption
296 (org-export-get-parent-element link))))
297 (when caption (org-export-data caption info)))
298 path)))
299 ((string= type "coderef")
300 (let ((ref (org-element-property :path link)))
301 (format (org-export-get-coderef-format ref contents)
302 (org-export-resolve-coderef ref info))))
303 ((equal type "radio")
304 (let ((destination (org-export-resolve-radio-link link info)))
305 (org-export-data (org-element-contents destination) info)))
306 ((equal type "fuzzy")
307 (let ((destination (org-export-resolve-fuzzy-link link info)))
308 ;; Ignore invisible "#+TARGET: path".
309 (unless (eq (org-element-type destination) 'keyword)
310 (if (org-string-nw-p contents) contents
311 (when destination
312 (let ((number (org-export-get-ordinal destination info)))
313 (when number
314 (if (atom number) (number-to-string number)
315 (mapconcat 'number-to-string number ".")))))))))
316 (t (let* ((raw-path (org-element-property :path link))
317 (path (cond
318 ((member type '("http" "https" "ftp"))
319 (concat type ":" raw-path))
320 ((equal type "file")
321 ;; Treat links to ".org" files as ".html",
322 ;; if needed.
323 (setq raw-path
324 (funcall --link-org-files-as-html-maybe
325 raw-path info))
326 ;; If file path is absolute, prepend it
327 ;; with protocol component - "file://".
328 (if (not (file-name-absolute-p raw-path)) raw-path
329 (concat "file://" (expand-file-name raw-path))))
330 (t raw-path))))
331 (if (not contents) (format "<%s>" path)
332 (format "[%s](%s)" contents path)))))))
335 ;;;; Paragraph
337 (defun org-md-paragraph (paragraph contents info)
338 "Transcode PARAGRAPH element into Markdown format.
339 CONTENTS is the paragraph contents. INFO is a plist used as
340 a communication channel."
341 (let ((first-object (car (org-element-contents paragraph))))
342 ;; If paragraph starts with a #, protect it.
343 (if (and (stringp first-object) (string-match "\\`#" first-object))
344 (replace-regexp-in-string "\\`#" "\\#" contents nil t)
345 contents)))
348 ;;;; Plain List
350 (defun org-md-plain-list (plain-list contents info)
351 "Transcode PLAIN-LIST element into Markdown format.
352 CONTENTS is the plain-list contents. INFO is a plist used as
353 a communication channel."
354 contents)
357 ;;;; Plain Text
359 (defun org-md-plain-text (text info)
360 "Transcode a TEXT string into Markdown format.
361 TEXT is the string to transcode. INFO is a plist holding
362 contextual information."
363 (when (plist-get info :with-smart-quotes)
364 (setq text (org-export-activate-smart-quotes text :html info)))
365 ;; Protect ambiguous #. This will protect # at the beginning of
366 ;; a line, but not at the beginning of a paragraph. See
367 ;; `org-md-paragraph'.
368 (setq text (replace-regexp-in-string "\n#" "\n\\\\#" text))
369 ;; Protect ambiguous !
370 (setq text (replace-regexp-in-string "\\(!\\)\\[" "\\\\!" text nil nil 1))
371 ;; Protect `, *, _ and \
372 (setq text (replace-regexp-in-string "[`*_\\]" "\\\\\\&" text))
373 ;; Handle special strings, if required.
374 (when (plist-get info :with-special-strings)
375 (setq text (org-html-convert-special-strings text)))
376 ;; Handle break preservation, if required.
377 (when (plist-get info :preserve-breaks)
378 (setq text (replace-regexp-in-string "[ \t]*\n" " \n" text)))
379 ;; Return value.
380 text)
383 ;;;; Quote Block
385 (defun org-md-quote-block (quote-block contents info)
386 "Transcode QUOTE-BLOCK element into Markdown format.
387 CONTENTS is the quote-block contents. INFO is a plist used as
388 a communication channel."
389 (replace-regexp-in-string
390 "^" "> "
391 (replace-regexp-in-string "\n\\'" "" contents)))
394 ;;;; Section
396 (defun org-md-section (section contents info)
397 "Transcode SECTION element into Markdown format.
398 CONTENTS is the section contents. INFO is a plist used as
399 a communication channel."
400 contents)
403 ;;;; Template
405 (defun org-md-template (contents info)
406 "Return complete document string after Markdown conversion.
407 CONTENTS is the transcoded contents string. INFO is a plist used
408 as a communication channel."
409 contents)
413 ;;; Interactive function
415 ;;;###autoload
416 (defun org-md-export-as-markdown (&optional async subtreep visible-only)
417 "Export current buffer to a text buffer.
419 If narrowing is active in the current buffer, only export its
420 narrowed part.
422 If a region is active, export that region.
424 A non-nil optional argument ASYNC means the process should happen
425 asynchronously. The resulting buffer should be accessible
426 through the `org-export-stack' interface.
428 When optional argument SUBTREEP is non-nil, export the sub-tree
429 at point, extracting information from the headline properties
430 first.
432 When optional argument VISIBLE-ONLY is non-nil, don't export
433 contents of hidden elements.
435 Export is done in a buffer named \"*Org MD Export*\", which will
436 be displayed when `org-export-show-temporary-export-buffer' is
437 non-nil."
438 (interactive)
439 (if async
440 (org-export-async-start
441 (lambda (output)
442 (with-current-buffer (get-buffer-create "*Org MD Export*")
443 (erase-buffer)
444 (insert output)
445 (goto-char (point-min))
446 (text-mode)
447 (org-export-add-to-stack (current-buffer) 'md)))
448 `(org-export-as 'md ,subtreep ,visible-only))
449 (let ((outbuf (org-export-to-buffer
450 'md "*Org MD Export*" subtreep visible-only)))
451 (with-current-buffer outbuf (text-mode))
452 (when org-export-show-temporary-export-buffer
453 (switch-to-buffer-other-window outbuf)))))
456 ;;;###autoload
457 (defun org-md-export-to-markdown (&optional async subtreep visible-only)
458 "Export current buffer to a Markdown file.
460 If narrowing is active in the current buffer, only export its
461 narrowed part.
463 If a region is active, export that region.
465 A non-nil optional argument ASYNC means the process should happen
466 asynchronously. The resulting file should be accessible through
467 the `org-export-stack' interface.
469 When optional argument SUBTREEP is non-nil, export the sub-tree
470 at point, extracting information from the headline properties
471 first.
473 When optional argument VISIBLE-ONLY is non-nil, don't export
474 contents of hidden elements.
476 Return output file's name."
477 (interactive)
478 (let ((outfile (org-export-output-file-name ".md" subtreep)))
479 (if async
480 (org-export-async-start
481 (lambda (f) (org-export-add-to-stack f 'md))
482 `(expand-file-name
483 (org-export-to-file 'md ,outfile ,subtreep ,visible-only)))
484 (org-export-to-file 'md outfile subtreep visible-only))))
487 (provide 'ox-md)
489 ;; Local variables:
490 ;; generated-autoload-file: "org-loaddefs.el"
491 ;; End:
493 ;;; ox-md.el ends here