org.el (org-self-insert-command): Use `yas-expand'
[org-mode.git] / lisp / ox-md.el
blob3b61c1ff559f46c506b514516d07eedb73db75d8
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)
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 (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 (inner-template . org-md-inner-template)
81 (italic . org-md-italic)
82 (item . org-md-item)
83 (keyword . org-md-keyword)
84 (line-break . org-md-line-break)
85 (link . org-md-link)
86 (node-property . org-md-node-property)
87 (paragraph . org-md-paragraph)
88 (plain-list . org-md-plain-list)
89 (plain-text . org-md-plain-text)
90 (property-drawer . org-md-property-drawer)
91 (quote-block . org-md-quote-block)
92 (section . org-md-section)
93 (src-block . org-md-example-block)
94 (template . org-md-template)
95 (verbatim . org-md-verbatim))
96 :options-alist '((:md-headline-style nil nil org-md-headline-style)))
99 ;;; Filters
101 (defun org-md-separate-elements (tree backend info)
102 "Fix blank lines between elements.
104 TREE is the parse tree being exported. BACKEND is the export
105 back-end used. INFO is a plist used as a communication channel.
107 Make sure there's no blank line before a plain list, unless it is
108 located right after a paragraph. Otherwise, add a blank line
109 between elements. Blank lines between items are preserved.
111 Assume BACKEND is `md'."
112 (org-element-map tree (remq 'item org-element-all-elements)
113 (lambda (elem)
114 (org-element-put-property
115 elem :post-blank
116 (if (and (eq (org-element-type (org-export-get-next-element elem info))
117 'plain-list)
118 (not (and (eq (org-element-type elem) 'paragraph)
119 (org-export-get-previous-element elem info))))
121 1))))
122 ;; Return updated tree.
123 tree)
127 ;;; Transcode Functions
129 ;;;; Bold
131 (defun org-md-bold (bold contents info)
132 "Transcode BOLD object into Markdown format.
133 CONTENTS is the text within bold markup. INFO is a plist used as
134 a communication channel."
135 (format "**%s**" contents))
138 ;;;; Code and Verbatim
140 (defun org-md-verbatim (verbatim contents info)
141 "Transcode VERBATIM object into Markdown format.
142 CONTENTS is nil. INFO is a plist used as a communication
143 channel."
144 (let ((value (org-element-property :value verbatim)))
145 (format (cond ((not (string-match "`" value)) "`%s`")
146 ((or (string-match "\\``" value)
147 (string-match "`\\'" value))
148 "`` %s ``")
149 (t "``%s``"))
150 value)))
153 ;;;; Example Block and Src Block
155 (defun org-md-example-block (example-block contents info)
156 "Transcode EXAMPLE-BLOCK element into Markdown format.
157 CONTENTS is nil. INFO is a plist used as a communication
158 channel."
159 (replace-regexp-in-string
160 "^" " "
161 (org-remove-indentation
162 (org-export-format-code-default example-block info))))
165 ;;;; Headline
167 (defun org-md-headline (headline contents info)
168 "Transcode HEADLINE element into Markdown format.
169 CONTENTS is the headline contents. INFO is a plist used as
170 a communication channel."
171 (unless (org-element-property :footnote-section-p headline)
172 (let* ((level (org-export-get-relative-level headline info))
173 (title (org-export-data (org-element-property :title headline) info))
174 (todo (and (plist-get info :with-todo-keywords)
175 (let ((todo (org-element-property :todo-keyword
176 headline)))
177 (and todo (concat (org-export-data todo info) " ")))))
178 (tags (and (plist-get info :with-tags)
179 (let ((tag-list (org-export-get-tags headline info)))
180 (and tag-list
181 (format " :%s:"
182 (mapconcat 'identity tag-list ":"))))))
183 (priority
184 (and (plist-get info :with-priority)
185 (let ((char (org-element-property :priority headline)))
186 (and char (format "[#%c] " char)))))
187 (anchor
188 (when (plist-get info :with-toc)
189 (org-html--anchor
190 (or (org-element-property :CUSTOM_ID headline)
191 (concat "sec-"
192 (mapconcat 'number-to-string
193 (org-export-get-headline-number
194 headline info) "-"))))))
195 ;; Headline text without tags.
196 (heading (concat todo priority title)))
197 (cond
198 ;; Cannot create a headline. Fall-back to a list.
199 ((or (org-export-low-level-p headline info)
200 (not (memq org-md-headline-style '(atx setext)))
201 (and (eq org-md-headline-style 'atx) (> level 6))
202 (and (eq org-md-headline-style 'setext) (> level 2)))
203 (let ((bullet
204 (if (not (org-export-numbered-headline-p headline info)) "-"
205 (concat (number-to-string
206 (car (last (org-export-get-headline-number
207 headline info))))
208 "."))))
209 (concat bullet (make-string (- 4 (length bullet)) ? ) heading tags
210 "\n\n"
211 (and contents
212 (replace-regexp-in-string "^" " " contents)))))
213 ;; Use "Setext" style.
214 ((eq org-md-headline-style 'setext)
215 (concat heading tags anchor "\n"
216 (make-string (length heading) (if (= level 1) ?= ?-))
217 "\n\n"
218 contents))
219 ;; Use "atx" style.
220 (t (concat (make-string level ?#) " " heading tags anchor "\n\n" contents))))))
223 ;;;; Horizontal Rule
225 (defun org-md-horizontal-rule (horizontal-rule contents info)
226 "Transcode HORIZONTAL-RULE element into Markdown format.
227 CONTENTS is the horizontal rule contents. INFO is a plist used
228 as a communication channel."
229 "---")
232 ;;;; Italic
234 (defun org-md-italic (italic contents info)
235 "Transcode ITALIC object into Markdown format.
236 CONTENTS is the text within italic markup. INFO is a plist used
237 as a communication channel."
238 (format "*%s*" contents))
241 ;;;; Item
243 (defun org-md-item (item contents info)
244 "Transcode ITEM element into Markdown format.
245 CONTENTS is the item contents. INFO is a plist used as
246 a communication channel."
247 (let* ((type (org-element-property :type (org-export-get-parent item)))
248 (struct (org-element-property :structure item))
249 (bullet (if (not (eq type 'ordered)) "-"
250 (concat (number-to-string
251 (car (last (org-list-get-item-number
252 (org-element-property :begin item)
253 struct
254 (org-list-prevs-alist struct)
255 (org-list-parents-alist struct)))))
256 "."))))
257 (concat bullet
258 (make-string (- 4 (length bullet)) ? )
259 (case (org-element-property :checkbox item)
260 (on "[X] ")
261 (trans "[-] ")
262 (off "[ ] "))
263 (let ((tag (org-element-property :tag item)))
264 (and tag (format "**%s:** "(org-export-data tag info))))
265 (and contents
266 (org-trim (replace-regexp-in-string "^" " " contents))))))
270 ;;;; Keyword
272 (defun org-md-keyword (keyword contents info)
273 "Transcode a KEYWORD element into Markdown format.
274 CONTENTS is nil. INFO is a plist used as a communication
275 channel."
276 (if (member (org-element-property :key keyword) '("MARKDOWN" "MD"))
277 (org-element-property :value keyword)
278 (org-export-with-backend 'html keyword contents info)))
281 ;;;; Line Break
283 (defun org-md-line-break (line-break contents info)
284 "Transcode LINE-BREAK object into Markdown format.
285 CONTENTS is nil. INFO is a plist used as a communication
286 channel."
287 " \n")
290 ;;;; Link
292 (defun org-md-link (link contents info)
293 "Transcode LINE-BREAK object into Markdown format.
294 CONTENTS is the link's description. INFO is a plist used as
295 a communication channel."
296 (let ((link-org-files-as-md
297 (function
298 (lambda (raw-path)
299 ;; Treat links to `file.org' as links to `file.md'.
300 (if (string= ".org" (downcase (file-name-extension raw-path ".")))
301 (concat (file-name-sans-extension raw-path) ".md")
302 raw-path))))
303 (type (org-element-property :type link)))
304 (cond ((member type '("custom-id" "id"))
305 (let ((destination (org-export-resolve-id-link link info)))
306 (if (stringp destination) ; External file.
307 (let ((path (funcall link-org-files-as-md destination)))
308 (if (not contents) (format "<%s>" path)
309 (format "[%s](%s)" contents path)))
310 (concat
311 (and contents (concat contents " "))
312 (format "(%s)"
313 (format (org-export-translate "See section %s" :html info)
314 (mapconcat 'number-to-string
315 (org-export-get-headline-number
316 destination info)
317 ".")))))))
318 ((org-export-inline-image-p link org-html-inline-image-rules)
319 (let ((path (let ((raw-path (org-element-property :path link)))
320 (if (not (file-name-absolute-p raw-path)) raw-path
321 (expand-file-name raw-path))))
322 (caption (org-export-data
323 (org-export-get-caption
324 (org-export-get-parent-element link)) info)))
325 (format "![img](%s)"
326 (if (not (org-string-nw-p caption)) path
327 (format "%s \"%s\"" path caption)))))
328 ((string= type "coderef")
329 (let ((ref (org-element-property :path link)))
330 (format (org-export-get-coderef-format ref contents)
331 (org-export-resolve-coderef ref info))))
332 ((equal type "radio") contents)
333 ((equal type "fuzzy")
334 (let ((destination (org-export-resolve-fuzzy-link link info)))
335 (if (org-string-nw-p contents) contents
336 (when destination
337 (let ((number (org-export-get-ordinal destination info)))
338 (when number
339 (if (atom number) (number-to-string number)
340 (mapconcat 'number-to-string number "."))))))))
341 (t (let* ((raw-path (org-element-property :path link))
342 (path
343 (cond
344 ((member type '("http" "https" "ftp"))
345 (concat type ":" raw-path))
346 ((string= type "file")
347 (let ((path (funcall link-org-files-as-md raw-path)))
348 (if (not (file-name-absolute-p path)) path
349 ;; If file path is absolute, prepend it
350 ;; with "file:" component.
351 (concat "file:" path))))
352 (t raw-path))))
353 (if (not contents) (format "<%s>" path)
354 (format "[%s](%s)" contents path)))))))
357 ;;;; Node Property
359 (defun org-md-node-property (node-property contents info)
360 "Transcode a NODE-PROPERTY element into Markdown syntax.
361 CONTENTS is nil. INFO is a plist holding contextual
362 information."
363 (format "%s:%s"
364 (org-element-property :key node-property)
365 (let ((value (org-element-property :value node-property)))
366 (if value (concat " " value) ""))))
369 ;;;; Paragraph
371 (defun org-md-paragraph (paragraph contents info)
372 "Transcode PARAGRAPH element into Markdown format.
373 CONTENTS is the paragraph contents. INFO is a plist used as
374 a communication channel."
375 (let ((first-object (car (org-element-contents paragraph))))
376 ;; If paragraph starts with a #, protect it.
377 (if (and (stringp first-object) (string-match "\\`#" first-object))
378 (replace-regexp-in-string "\\`#" "\\#" contents nil t)
379 contents)))
382 ;;;; Plain List
384 (defun org-md-plain-list (plain-list contents info)
385 "Transcode PLAIN-LIST element into Markdown format.
386 CONTENTS is the plain-list contents. INFO is a plist used as
387 a communication channel."
388 contents)
391 ;;;; Plain Text
393 (defun org-md-plain-text (text info)
394 "Transcode a TEXT string into Markdown format.
395 TEXT is the string to transcode. INFO is a plist holding
396 contextual information."
397 (when (plist-get info :with-smart-quotes)
398 (setq text (org-export-activate-smart-quotes text :html info)))
399 ;; Protect ambiguous #. This will protect # at the beginning of
400 ;; a line, but not at the beginning of a paragraph. See
401 ;; `org-md-paragraph'.
402 (setq text (replace-regexp-in-string "\n#" "\n\\\\#" text))
403 ;; Protect ambiguous !
404 (setq text (replace-regexp-in-string "\\(!\\)\\[" "\\\\!" text nil nil 1))
405 ;; Protect `, *, _ and \
406 (setq text (replace-regexp-in-string "[`*_\\]" "\\\\\\&" text))
407 ;; Handle special strings, if required.
408 (when (plist-get info :with-special-strings)
409 (setq text (org-html-convert-special-strings text)))
410 ;; Handle break preservation, if required.
411 (when (plist-get info :preserve-breaks)
412 (setq text (replace-regexp-in-string "[ \t]*\n" " \n" text)))
413 ;; Return value.
414 text)
417 ;;;; Property Drawer
419 (defun org-md-property-drawer (property-drawer contents info)
420 "Transcode a PROPERTY-DRAWER element into Markdown format.
421 CONTENTS holds the contents of the drawer. INFO is a plist
422 holding contextual information."
423 (and (org-string-nw-p contents)
424 (replace-regexp-in-string "^" " " contents)))
427 ;;;; Quote Block
429 (defun org-md-quote-block (quote-block contents info)
430 "Transcode QUOTE-BLOCK element into Markdown format.
431 CONTENTS is the quote-block contents. INFO is a plist used as
432 a communication channel."
433 (replace-regexp-in-string
434 "^" "> "
435 (replace-regexp-in-string "\n\\'" "" contents)))
438 ;;;; Section
440 (defun org-md-section (section contents info)
441 "Transcode SECTION element into Markdown format.
442 CONTENTS is the section contents. INFO is a plist used as
443 a communication channel."
444 contents)
447 ;;;; Template
449 (defun org-md-inner-template (contents info)
450 "Return body of document after converting it to Markdown syntax.
451 CONTENTS is the transcoded contents string. INFO is a plist
452 holding export options."
453 ;; Make sure CONTENTS is separated from table of contents and
454 ;; footnotes with at least a blank line.
455 (org-trim (org-html-inner-template (concat "\n" contents "\n") info)))
457 (defun org-md-template (contents info)
458 "Return complete document string after Markdown conversion.
459 CONTENTS is the transcoded contents string. INFO is a plist used
460 as a communication channel."
461 contents)
465 ;;; Interactive function
467 ;;;###autoload
468 (defun org-md-export-as-markdown (&optional async subtreep visible-only)
469 "Export current buffer to a Markdown buffer.
471 If narrowing is active in the current buffer, only export its
472 narrowed part.
474 If a region is active, export that region.
476 A non-nil optional argument ASYNC means the process should happen
477 asynchronously. The resulting buffer should be accessible
478 through the `org-export-stack' interface.
480 When optional argument SUBTREEP is non-nil, export the sub-tree
481 at point, extracting information from the headline properties
482 first.
484 When optional argument VISIBLE-ONLY is non-nil, don't export
485 contents of hidden elements.
487 Export is done in a buffer named \"*Org MD Export*\", which will
488 be displayed when `org-export-show-temporary-export-buffer' is
489 non-nil."
490 (interactive)
491 (org-export-to-buffer 'md "*Org MD Export*"
492 async subtreep visible-only nil nil (lambda () (text-mode))))
494 ;;;###autoload
495 (defun org-md-convert-region-to-md ()
496 "Assume the current region has org-mode syntax, and convert it to Markdown.
497 This can be used in any buffer. For example, you can write an
498 itemized list in org-mode syntax in a Markdown buffer and use
499 this command to convert it."
500 (interactive)
501 (org-export-replace-region-by 'md))
504 ;;;###autoload
505 (defun org-md-export-to-markdown (&optional async subtreep visible-only)
506 "Export current buffer to a Markdown file.
508 If narrowing is active in the current buffer, only export its
509 narrowed part.
511 If a region is active, export that region.
513 A non-nil optional argument ASYNC means the process should happen
514 asynchronously. The resulting file should be accessible through
515 the `org-export-stack' interface.
517 When optional argument SUBTREEP is non-nil, export the sub-tree
518 at point, extracting information from the headline properties
519 first.
521 When optional argument VISIBLE-ONLY is non-nil, don't export
522 contents of hidden elements.
524 Return output file's name."
525 (interactive)
526 (let ((outfile (org-export-output-file-name ".md" subtreep)))
527 (org-export-to-file 'md outfile async subtreep visible-only)))
529 ;;;###autoload
530 (defun org-md-publish-to-md (plist filename pub-dir)
531 "Publish an org file to Markdown.
533 FILENAME is the filename of the Org file to be published. PLIST
534 is the property list for the given project. PUB-DIR is the
535 publishing directory.
537 Return output file name."
538 (org-publish-org-to 'md filename ".md" plist pub-dir))
540 (provide 'ox-md)
542 ;; Local variables:
543 ;; generated-autoload-file: "org-loaddefs.el"
544 ;; End:
546 ;;; ox-md.el ends here