org.el (org-align-tags-here): Fix bug: move to the correct position
[org-mode.git] / lisp / ox-md.el
blob7d540787d98ca2c47c9c36462e04800713323ac9
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)
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 (paragraph . org-md-paragraph)
83 (plain-list . org-md-plain-list)
84 (plain-text . org-md-plain-text)
85 (quote-block . org-md-quote-block)
86 (quote-section . org-md-example-block)
87 (section . org-md-section)
88 (src-block . org-md-example-block)
89 (template . org-md-template)
90 (verbatim . org-md-verbatim)))
94 ;;; Filters
96 (defun org-md-separate-elements (tree backend info)
97 "Make sure elements are separated by at least one blank line.
99 TREE is the parse tree being exported. BACKEND is the export
100 back-end used. INFO is a plist used as a communication channel.
102 Assume BACKEND is `md'."
103 (org-element-map tree org-element-all-elements
104 (lambda (elem)
105 (unless (eq (org-element-type elem) 'org-data)
106 (org-element-put-property
107 elem :post-blank
108 (let ((post-blank (org-element-property :post-blank elem)))
109 (if (not post-blank) 1 (max 1 post-blank)))))))
110 ;; Return updated tree.
111 tree)
115 ;;; Transcode Functions
117 ;;;; Bold
119 (defun org-md-bold (bold contents info)
120 "Transcode BOLD object into Markdown format.
121 CONTENTS is the text within bold markup. INFO is a plist used as
122 a communication channel."
123 (format "**%s**" contents))
126 ;;;; Code and Verbatim
128 (defun org-md-verbatim (verbatim contents info)
129 "Transcode VERBATIM object into Markdown format.
130 CONTENTS is nil. INFO is a plist used as a communication
131 channel."
132 (let ((value (org-element-property :value verbatim)))
133 (format (cond ((not (string-match "`" value)) "`%s`")
134 ((or (string-match "\\``" value)
135 (string-match "`\\'" value))
136 "`` %s ``")
137 (t "``%s``"))
138 value)))
141 ;;;; Example Block and Src Block
143 (defun org-md-example-block (example-block contents info)
144 "Transcode EXAMPLE-BLOCK element into Markdown format.
145 CONTENTS is nil. INFO is a plist used as a communication
146 channel."
147 (replace-regexp-in-string
148 "^" " "
149 (org-remove-indentation
150 (org-element-property :value example-block))))
153 ;;;; Headline
155 (defun org-md-headline (headline contents info)
156 "Transcode HEADLINE element into Markdown format.
157 CONTENTS is the headline contents. INFO is a plist used as
158 a communication channel."
159 (unless (org-element-property :footnote-section-p headline)
160 (let* ((level (org-export-get-relative-level headline info))
161 (title (org-export-data (org-element-property :title headline) info))
162 (todo (and (plist-get info :with-todo-keywords)
163 (let ((todo (org-element-property :todo-keyword
164 headline)))
165 (and todo (concat (org-export-data todo info) " ")))))
166 (tags (and (plist-get info :with-tags)
167 (let ((tag-list (org-export-get-tags headline info)))
168 (and tag-list
169 (format " :%s:"
170 (mapconcat 'identity tag-list ":"))))))
171 (priority
172 (and (plist-get info :with-priority)
173 (let ((char (org-element-property :priority headline)))
174 (and char (format "[#%c] " char)))))
175 ;; Headline text without tags.
176 (heading (concat todo priority title)))
177 (cond
178 ;; Cannot create a headline. Fall-back to a list.
179 ((or (org-export-low-level-p headline info)
180 (not (memq org-md-headline-style '(atx setext)))
181 (and (eq org-md-headline-style 'atx) (> level 6))
182 (and (eq org-md-headline-style 'setext) (> level 2)))
183 (let ((bullet
184 (if (not (org-export-numbered-headline-p headline info)) "-"
185 (concat (number-to-string
186 (car (last (org-export-get-headline-number
187 headline info))))
188 "."))))
189 (concat bullet (make-string (- 4 (length bullet)) ? ) heading tags
190 "\n\n"
191 (and contents
192 (replace-regexp-in-string "^" " " contents)))))
193 ;; Use "Setext" style.
194 ((eq org-md-headline-style 'setext)
195 (concat heading tags "\n"
196 (make-string (length heading) (if (= level 1) ?= ?-))
197 "\n\n"
198 contents))
199 ;; Use "atx" style.
200 (t (concat (make-string level ?#) " " heading tags "\n\n" contents))))))
203 ;;;; Horizontal Rule
205 (defun org-md-horizontal-rule (horizontal-rule contents info)
206 "Transcode HORIZONTAL-RULE element into Markdown format.
207 CONTENTS is the horizontal rule contents. INFO is a plist used
208 as a communication channel."
209 "---")
212 ;;;; Italic
214 (defun org-md-italic (italic contents info)
215 "Transcode ITALIC object into Markdown format.
216 CONTENTS is the text within italic markup. INFO is a plist used
217 as a communication channel."
218 (format "*%s*" contents))
221 ;;;; Item
223 (defun org-md-item (item contents info)
224 "Transcode ITEM element into Markdown format.
225 CONTENTS is the item contents. INFO is a plist used as
226 a communication channel."
227 (let* ((type (org-element-property :type (org-export-get-parent item)))
228 (struct (org-element-property :structure item))
229 (bullet (if (not (eq type 'ordered)) "-"
230 (concat (number-to-string
231 (car (last (org-list-get-item-number
232 (org-element-property :begin item)
233 struct
234 (org-list-prevs-alist struct)
235 (org-list-parents-alist struct)))))
236 "."))))
237 (concat bullet
238 (make-string (- 4 (length bullet)) ? )
239 (case (org-element-property :checkbox item)
240 (on "[X] ")
241 (trans "[-] ")
242 (off "[ ] "))
243 (let ((tag (org-element-property :tag item)))
244 (and tag (format "**%s:** "(org-export-data tag info))))
245 (org-trim (replace-regexp-in-string "^" " " contents)))))
248 ;;;; Line Break
250 (defun org-md-line-break (line-break contents info)
251 "Transcode LINE-BREAK object into Markdown format.
252 CONTENTS is nil. INFO is a plist used as a communication
253 channel."
254 " \n")
257 ;;;; Link
259 (defun org-md-link (link contents info)
260 "Transcode LINE-BREAK object into Markdown format.
261 CONTENTS is the link's description. INFO is a plist used as
262 a communication channel."
263 (let ((--link-org-files-as-html-maybe
264 (function
265 (lambda (raw-path info)
266 ;; Treat links to `file.org' as links to `file.html', if
267 ;; needed. See `org-html-link-org-files-as-html'.
268 (cond
269 ((and org-html-link-org-files-as-html
270 (string= ".org"
271 (downcase (file-name-extension raw-path "."))))
272 (concat (file-name-sans-extension raw-path) "."
273 (plist-get info :html-extension)))
274 (t raw-path)))))
275 (type (org-element-property :type link)))
276 (cond ((member type '("custom-id" "id"))
277 (let ((destination (org-export-resolve-id-link link info)))
278 (if (stringp destination) ; External file.
279 (let ((path (funcall --link-org-files-as-html-maybe
280 destination info)))
281 (if (not contents) (format "<%s>" path)
282 (format "[%s](%s)" contents path)))
283 (concat
284 (and contents (concat contents " "))
285 (format "(%s)"
286 (format (org-export-translate "See section %s" :html info)
287 (mapconcat 'number-to-string
288 (org-export-get-headline-number
289 destination info)
290 ".")))))))
291 ((org-export-inline-image-p link org-html-inline-image-rules)
292 (let ((path (let ((raw-path (org-element-property :path link)))
293 (if (not (file-name-absolute-p raw-path)) raw-path
294 (expand-file-name raw-path)))))
295 (format "![%s](%s)"
296 (let ((caption (org-export-get-caption
297 (org-export-get-parent-element link))))
298 (when caption (org-export-data caption info)))
299 path)))
300 ((string= type "coderef")
301 (let ((ref (org-element-property :path link)))
302 (format (org-export-get-coderef-format ref contents)
303 (org-export-resolve-coderef ref info))))
304 ((equal type "radio")
305 (let ((destination (org-export-resolve-radio-link link info)))
306 (org-export-data (org-element-contents destination) info)))
307 ((equal type "fuzzy")
308 (let ((destination (org-export-resolve-fuzzy-link link info)))
309 (if (org-string-nw-p contents) contents
310 (when destination
311 (let ((number (org-export-get-ordinal destination info)))
312 (when number
313 (if (atom number) (number-to-string number)
314 (mapconcat 'number-to-string number "."))))))))
315 (t (let* ((raw-path (org-element-property :path link))
316 (path (cond
317 ((member type '("http" "https" "ftp"))
318 (concat type ":" raw-path))
319 ((equal type "file")
320 ;; Treat links to ".org" files as ".html",
321 ;; if needed.
322 (setq raw-path
323 (funcall --link-org-files-as-html-maybe
324 raw-path info))
325 ;; If file path is absolute, prepend it
326 ;; with protocol component - "file://".
327 (if (not (file-name-absolute-p raw-path)) raw-path
328 (concat "file://" (expand-file-name raw-path))))
329 (t raw-path))))
330 (if (not contents) (format "<%s>" path)
331 (format "[%s](%s)" contents path)))))))
334 ;;;; Paragraph
336 (defun org-md-paragraph (paragraph contents info)
337 "Transcode PARAGRAPH element into Markdown format.
338 CONTENTS is the paragraph contents. INFO is a plist used as
339 a communication channel."
340 (let ((first-object (car (org-element-contents paragraph))))
341 ;; If paragraph starts with a #, protect it.
342 (if (and (stringp first-object) (string-match "\\`#" first-object))
343 (replace-regexp-in-string "\\`#" "\\#" contents nil t)
344 contents)))
347 ;;;; Plain List
349 (defun org-md-plain-list (plain-list contents info)
350 "Transcode PLAIN-LIST element into Markdown format.
351 CONTENTS is the plain-list contents. INFO is a plist used as
352 a communication channel."
353 contents)
356 ;;;; Plain Text
358 (defun org-md-plain-text (text info)
359 "Transcode a TEXT string into Markdown format.
360 TEXT is the string to transcode. INFO is a plist holding
361 contextual information."
362 (when (plist-get info :with-smart-quotes)
363 (setq text (org-export-activate-smart-quotes text :html info)))
364 ;; Protect ambiguous #. This will protect # at the beginning of
365 ;; a line, but not at the beginning of a paragraph. See
366 ;; `org-md-paragraph'.
367 (setq text (replace-regexp-in-string "\n#" "\n\\\\#" text))
368 ;; Protect ambiguous !
369 (setq text (replace-regexp-in-string "\\(!\\)\\[" "\\\\!" text nil nil 1))
370 ;; Protect `, *, _ and \
371 (setq text (replace-regexp-in-string "[`*_\\]" "\\\\\\&" text))
372 ;; Handle special strings, if required.
373 (when (plist-get info :with-special-strings)
374 (setq text (org-html-convert-special-strings text)))
375 ;; Handle break preservation, if required.
376 (when (plist-get info :preserve-breaks)
377 (setq text (replace-regexp-in-string "[ \t]*\n" " \n" text)))
378 ;; Return value.
379 text)
382 ;;;; Quote Block
384 (defun org-md-quote-block (quote-block contents info)
385 "Transcode QUOTE-BLOCK element into Markdown format.
386 CONTENTS is the quote-block contents. INFO is a plist used as
387 a communication channel."
388 (replace-regexp-in-string
389 "^" "> "
390 (replace-regexp-in-string "\n\\'" "" contents)))
393 ;;;; Section
395 (defun org-md-section (section contents info)
396 "Transcode SECTION element into Markdown format.
397 CONTENTS is the section contents. INFO is a plist used as
398 a communication channel."
399 contents)
402 ;;;; Template
404 (defun org-md-template (contents info)
405 "Return complete document string after Markdown conversion.
406 CONTENTS is the transcoded contents string. INFO is a plist used
407 as a communication channel."
408 contents)
412 ;;; Interactive function
414 ;;;###autoload
415 (defun org-md-export-as-markdown (&optional async subtreep visible-only)
416 "Export current buffer to a Markdown buffer.
418 If narrowing is active in the current buffer, only export its
419 narrowed part.
421 If a region is active, export that region.
423 A non-nil optional argument ASYNC means the process should happen
424 asynchronously. The resulting buffer should be accessible
425 through the `org-export-stack' interface.
427 When optional argument SUBTREEP is non-nil, export the sub-tree
428 at point, extracting information from the headline properties
429 first.
431 When optional argument VISIBLE-ONLY is non-nil, don't export
432 contents of hidden elements.
434 Export is done in a buffer named \"*Org MD Export*\", which will
435 be displayed when `org-export-show-temporary-export-buffer' is
436 non-nil."
437 (interactive)
438 (org-export-to-buffer 'md "*Org MD Export*"
439 async subtreep visible-only nil nil (lambda () (text-mode))))
441 ;;;###autoload
442 (defun org-md-convert-region-to-md ()
443 "Assume the current region has org-mode syntax, and convert it to Markdown.
444 This can be used in any buffer. For example, you can write an
445 itemized list in org-mode syntax in a Markdown buffer and use
446 this command to convert it."
447 (interactive)
448 (org-export-replace-region-by 'md))
451 ;;;###autoload
452 (defun org-md-export-to-markdown (&optional async subtreep visible-only)
453 "Export current buffer to a Markdown file.
455 If narrowing is active in the current buffer, only export its
456 narrowed part.
458 If a region is active, export that region.
460 A non-nil optional argument ASYNC means the process should happen
461 asynchronously. The resulting file should be accessible through
462 the `org-export-stack' interface.
464 When optional argument SUBTREEP is non-nil, export the sub-tree
465 at point, extracting information from the headline properties
466 first.
468 When optional argument VISIBLE-ONLY is non-nil, don't export
469 contents of hidden elements.
471 Return output file's name."
472 (interactive)
473 (let ((outfile (org-export-output-file-name ".md" subtreep)))
474 (org-export-to-file 'md outfile async subtreep visible-only)))
477 (provide 'ox-md)
479 ;; Local variables:
480 ;; generated-autoload-file: "org-loaddefs.el"
481 ;; End:
483 ;;; ox-md.el ends here