ox-koma-letter: Allow to disable place locally
[org-mode.git] / lisp / ox-md.el
blob2abf1895e1c23b19c7de7a470fdcc37aa1c904c5
1 ;;; ox-md.el --- Markdown Back-End for Org Export Engine
3 ;; Copyright (C) 2012-2015 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 (example-block . org-md-example-block)
72 (export-block . org-md-export-block)
73 (fixed-width . org-md-example-block)
74 (headline . org-md-headline)
75 (horizontal-rule . org-md-horizontal-rule)
76 (inline-src-block . org-md-verbatim)
77 (inner-template . org-md-inner-template)
78 (italic . org-md-italic)
79 (item . org-md-item)
80 (keyword . org-md-keyword)
81 (line-break . org-md-line-break)
82 (link . org-md-link)
83 (node-property . org-md-node-property)
84 (paragraph . org-md-paragraph)
85 (plain-list . org-md-plain-list)
86 (plain-text . org-md-plain-text)
87 (property-drawer . org-md-property-drawer)
88 (quote-block . org-md-quote-block)
89 (section . org-md-section)
90 (src-block . org-md-example-block)
91 (template . org-md-template)
92 (verbatim . org-md-verbatim))
93 :options-alist '((:md-headline-style nil nil org-md-headline-style)))
96 ;;; Filters
98 (defun org-md-separate-elements (tree backend info)
99 "Fix blank lines between elements.
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 Enforce a blank line between elements. There are two exceptions
105 to this rule:
107 1. Preserve blank lines between sibling items in a plain list,
109 2. In an item, remove any blank line before the very first
110 paragraph and the next sub-list.
112 Assume BACKEND is `md'."
113 (org-element-map tree (remq 'item org-element-all-elements)
114 (lambda (e)
115 (org-element-put-property
116 e :post-blank
117 (if (and (eq (org-element-type e) 'paragraph)
118 (eq (org-element-type (org-element-property :parent e)) 'item)
119 (eq (org-element-type (org-export-get-next-element e info))
120 'plain-list)
121 (not (org-export-get-previous-element e info)))
123 1))))
124 ;; Return updated tree.
125 tree)
129 ;;; Transcode Functions
131 ;;;; Bold
133 (defun org-md-bold (bold contents info)
134 "Transcode BOLD object into Markdown format.
135 CONTENTS is the text within bold markup. INFO is a plist used as
136 a communication channel."
137 (format "**%s**" contents))
140 ;;;; Code and Verbatim
142 (defun org-md-verbatim (verbatim contents info)
143 "Transcode VERBATIM object into Markdown format.
144 CONTENTS is nil. INFO is a plist used as a communication
145 channel."
146 (let ((value (org-element-property :value verbatim)))
147 (format (cond ((not (string-match "`" value)) "`%s`")
148 ((or (string-match "\\``" value)
149 (string-match "`\\'" value))
150 "`` %s ``")
151 (t "``%s``"))
152 value)))
155 ;;;; Example Block, Src Block and export Block
157 (defun org-md-example-block (example-block contents info)
158 "Transcode EXAMPLE-BLOCK element into Markdown format.
159 CONTENTS is nil. INFO is a plist used as a communication
160 channel."
161 (replace-regexp-in-string
162 "^" " "
163 (org-remove-indentation
164 (org-export-format-code-default example-block info))))
166 (defun org-md-export-block (export-block contents info)
167 "Transcode a EXPORT-BLOCK element from Org to Markdown.
168 CONTENTS is nil. INFO is a plist holding contextual information."
169 (if (member (org-element-property :type export-block) '("MARKDOWN" "MD"))
170 (org-remove-indentation (org-element-property :value export-block))
171 ;; Also include HTML export blocks.
172 (org-export-with-backend 'html export-block contents info)))
175 ;;;; Headline
177 (defun org-md-headline (headline contents info)
178 "Transcode HEADLINE element into Markdown format.
179 CONTENTS is the headline contents. INFO is a plist used as
180 a communication channel."
181 (unless (org-element-property :footnote-section-p headline)
182 (let* ((level (org-export-get-relative-level headline info))
183 (title (org-export-data (org-element-property :title headline) info))
184 (todo (and (plist-get info :with-todo-keywords)
185 (let ((todo (org-element-property :todo-keyword
186 headline)))
187 (and todo (concat (org-export-data todo info) " ")))))
188 (tags (and (plist-get info :with-tags)
189 (let ((tag-list (org-export-get-tags headline info)))
190 (and tag-list
191 (format " :%s:"
192 (mapconcat 'identity tag-list ":"))))))
193 (priority
194 (and (plist-get info :with-priority)
195 (let ((char (org-element-property :priority headline)))
196 (and char (format "[#%c] " char)))))
197 (anchor
198 (and (plist-get info :with-toc)
199 (org-html--anchor
200 (org-export-get-reference headline info) nil nil info)))
201 ;; Headline text without tags.
202 (heading (concat todo priority title))
203 (style (plist-get info :md-headline-style)))
204 (cond
205 ;; Cannot create a headline. Fall-back to a list.
206 ((or (org-export-low-level-p headline info)
207 (not (memq style '(atx setext)))
208 (and (eq style 'atx) (> level 6))
209 (and (eq style 'setext) (> level 2)))
210 (let ((bullet
211 (if (not (org-export-numbered-headline-p headline info)) "-"
212 (concat (number-to-string
213 (car (last (org-export-get-headline-number
214 headline info))))
215 "."))))
216 (concat bullet (make-string (- 4 (length bullet)) ? ) heading tags
217 "\n\n"
218 (and contents
219 (replace-regexp-in-string "^" " " contents)))))
220 ;; Use "Setext" style.
221 ((eq style 'setext)
222 (concat heading tags anchor "\n"
223 (make-string (length heading) (if (= level 1) ?= ?-))
224 "\n\n"
225 contents))
226 ;; Use "atx" style.
227 (t (concat (make-string level ?#) " " heading tags anchor "\n\n" contents))))))
230 ;;;; Horizontal Rule
232 (defun org-md-horizontal-rule (horizontal-rule contents info)
233 "Transcode HORIZONTAL-RULE element into Markdown format.
234 CONTENTS is the horizontal rule contents. INFO is a plist used
235 as a communication channel."
236 "---")
239 ;;;; Italic
241 (defun org-md-italic (italic contents info)
242 "Transcode ITALIC object into Markdown format.
243 CONTENTS is the text within italic markup. INFO is a plist used
244 as a communication channel."
245 (format "*%s*" contents))
248 ;;;; Item
250 (defun org-md-item (item contents info)
251 "Transcode ITEM element into Markdown format.
252 CONTENTS is the item contents. INFO is a plist used as
253 a communication channel."
254 (let* ((type (org-element-property :type (org-export-get-parent item)))
255 (struct (org-element-property :structure item))
256 (bullet (if (not (eq type 'ordered)) "-"
257 (concat (number-to-string
258 (car (last (org-list-get-item-number
259 (org-element-property :begin item)
260 struct
261 (org-list-prevs-alist struct)
262 (org-list-parents-alist struct)))))
263 "."))))
264 (concat bullet
265 (make-string (- 4 (length bullet)) ? )
266 (case (org-element-property :checkbox item)
267 (on "[X] ")
268 (trans "[-] ")
269 (off "[ ] "))
270 (let ((tag (org-element-property :tag item)))
271 (and tag (format "**%s:** "(org-export-data tag info))))
272 (and contents
273 (org-trim (replace-regexp-in-string "^" " " contents))))))
277 ;;;; Keyword
279 (defun org-md-keyword (keyword contents info)
280 "Transcode a KEYWORD element into Markdown format.
281 CONTENTS is nil. INFO is a plist used as a communication
282 channel."
283 (if (member (org-element-property :key keyword) '("MARKDOWN" "MD"))
284 (org-element-property :value keyword)
285 (org-export-with-backend 'html keyword contents info)))
288 ;;;; Line Break
290 (defun org-md-line-break (line-break contents info)
291 "Transcode LINE-BREAK object into Markdown format.
292 CONTENTS is nil. INFO is a plist used as a communication
293 channel."
294 " \n")
297 ;;;; Link
299 (defun org-md-link (link contents info)
300 "Transcode LINE-BREAK object into Markdown format.
301 CONTENTS is the link's description. INFO is a plist used as
302 a communication channel."
303 (let ((link-org-files-as-md
304 (lambda (raw-path)
305 ;; Treat links to `file.org' as links to `file.md'.
306 (if (string= ".org" (downcase (file-name-extension raw-path ".")))
307 (concat (file-name-sans-extension raw-path) ".md")
308 raw-path)))
309 (type (org-element-property :type link)))
310 (cond
311 ;; Link type is handled by a special function.
312 ((org-export-custom-protocol-maybe link contents 'md))
313 ((member type '("custom-id" "id"))
314 (let ((destination (org-export-resolve-id-link link info)))
315 (if (stringp destination) ; External file.
316 (let ((path (funcall link-org-files-as-md destination)))
317 (if (not contents) (format "<%s>" path)
318 (format "[%s](%s)" contents path)))
319 (concat
320 (and contents (concat contents " "))
321 (format "(%s)"
322 (format (org-export-translate "See section %s" :html info)
323 (if (org-export-numbered-headline-p destination info)
324 (mapconcat #'number-to-string
325 (org-export-get-headline-number
326 destination info)
327 ".")
328 (org-export-data
329 (org-element-property :title destination) info))))))))
330 ((org-export-inline-image-p link org-html-inline-image-rules)
331 (let ((path (let ((raw-path (org-element-property :path link)))
332 (if (not (file-name-absolute-p raw-path)) raw-path
333 (expand-file-name raw-path))))
334 (caption (org-export-data
335 (org-export-get-caption
336 (org-export-get-parent-element link)) info)))
337 (format "![img](%s)"
338 (if (not (org-string-nw-p caption)) path
339 (format "%s \"%s\"" path caption)))))
340 ((string= type "coderef")
341 (let ((ref (org-element-property :path link)))
342 (format (org-export-get-coderef-format ref contents)
343 (org-export-resolve-coderef ref info))))
344 ((equal type "radio") contents)
345 ((equal type "fuzzy")
346 (let* ((destination (org-export-resolve-fuzzy-link link info))
347 (description
348 (or (org-string-nw-p contents)
349 (let ((number (org-export-get-ordinal destination info)))
350 (cond
351 ((not number)
352 (and (eq 'headline (org-element-type destination))
353 (org-export-data
354 (org-element-property :title destination) info)))
355 ((atom number) (number-to-string number))
356 (t (mapconcat #'number-to-string number ".")))))))
357 (format "[%s](#%s)"
358 description
359 (org-export-get-reference destination info))))
360 (t (let* ((raw-path (org-element-property :path link))
361 (path
362 (cond
363 ((member type '("http" "https" "ftp"))
364 (concat type ":" raw-path))
365 ((string= type "file")
366 (org-export-file-uri (funcall link-org-files-as-md raw-path)))
367 (t raw-path))))
368 (if (not contents) (format "<%s>" path)
369 (format "[%s](%s)" contents path)))))))
372 ;;;; Node Property
374 (defun org-md-node-property (node-property contents info)
375 "Transcode a NODE-PROPERTY element into Markdown syntax.
376 CONTENTS is nil. INFO is a plist holding contextual
377 information."
378 (format "%s:%s"
379 (org-element-property :key node-property)
380 (let ((value (org-element-property :value node-property)))
381 (if value (concat " " value) ""))))
384 ;;;; Paragraph
386 (defun org-md-paragraph (paragraph contents info)
387 "Transcode PARAGRAPH element into Markdown format.
388 CONTENTS is the paragraph contents. INFO is a plist used as
389 a communication channel."
390 (let ((first-object (car (org-element-contents paragraph))))
391 ;; If paragraph starts with a #, protect it.
392 (if (and (stringp first-object) (string-match "\\`#" first-object))
393 (replace-regexp-in-string "\\`#" "\\#" contents nil t)
394 contents)))
397 ;;;; Plain List
399 (defun org-md-plain-list (plain-list contents info)
400 "Transcode PLAIN-LIST element into Markdown format.
401 CONTENTS is the plain-list contents. INFO is a plist used as
402 a communication channel."
403 contents)
406 ;;;; Plain Text
408 (defun org-md-plain-text (text info)
409 "Transcode a TEXT string into Markdown format.
410 TEXT is the string to transcode. INFO is a plist holding
411 contextual information."
412 (when (plist-get info :with-smart-quotes)
413 (setq text (org-export-activate-smart-quotes text :html info)))
414 ;; Protect ambiguous #. This will protect # at the beginning of
415 ;; a line, but not at the beginning of a paragraph. See
416 ;; `org-md-paragraph'.
417 (setq text (replace-regexp-in-string "\n#" "\n\\\\#" text))
418 ;; Protect ambiguous !
419 (setq text (replace-regexp-in-string "\\(!\\)\\[" "\\\\!" text nil nil 1))
420 ;; Protect `, *, _ and \
421 (setq text (replace-regexp-in-string "[`*_\\]" "\\\\\\&" text))
422 ;; Handle special strings, if required.
423 (when (plist-get info :with-special-strings)
424 (setq text (org-html-convert-special-strings text)))
425 ;; Handle break preservation, if required.
426 (when (plist-get info :preserve-breaks)
427 (setq text (replace-regexp-in-string "[ \t]*\n" " \n" text)))
428 ;; Return value.
429 text)
432 ;;;; Property Drawer
434 (defun org-md-property-drawer (property-drawer contents info)
435 "Transcode a PROPERTY-DRAWER element into Markdown format.
436 CONTENTS holds the contents of the drawer. INFO is a plist
437 holding contextual information."
438 (and (org-string-nw-p contents)
439 (replace-regexp-in-string "^" " " contents)))
442 ;;;; Quote Block
444 (defun org-md-quote-block (quote-block contents info)
445 "Transcode QUOTE-BLOCK element into Markdown format.
446 CONTENTS is the quote-block contents. INFO is a plist used as
447 a communication channel."
448 (replace-regexp-in-string
449 "^" "> "
450 (replace-regexp-in-string "\n\\'" "" contents)))
453 ;;;; Section
455 (defun org-md-section (section contents info)
456 "Transcode SECTION element into Markdown format.
457 CONTENTS is the section contents. INFO is a plist used as
458 a communication channel."
459 contents)
462 ;;;; Template
464 (defun org-md-inner-template (contents info)
465 "Return body of document after converting it to Markdown syntax.
466 CONTENTS is the transcoded contents string. INFO is a plist
467 holding export options."
468 ;; Make sure CONTENTS is separated from table of contents and
469 ;; footnotes with at least a blank line.
470 (org-trim (org-html-inner-template (concat "\n" contents "\n") info)))
472 (defun org-md-template (contents info)
473 "Return complete document string after Markdown conversion.
474 CONTENTS is the transcoded contents string. INFO is a plist used
475 as a communication channel."
476 contents)
480 ;;; Interactive function
482 ;;;###autoload
483 (defun org-md-export-as-markdown (&optional async subtreep visible-only)
484 "Export current buffer to a Markdown buffer.
486 If narrowing is active in the current buffer, only export its
487 narrowed part.
489 If a region is active, export that region.
491 A non-nil optional argument ASYNC means the process should happen
492 asynchronously. The resulting buffer should be accessible
493 through the `org-export-stack' interface.
495 When optional argument SUBTREEP is non-nil, export the sub-tree
496 at point, extracting information from the headline properties
497 first.
499 When optional argument VISIBLE-ONLY is non-nil, don't export
500 contents of hidden elements.
502 Export is done in a buffer named \"*Org MD Export*\", which will
503 be displayed when `org-export-show-temporary-export-buffer' is
504 non-nil."
505 (interactive)
506 (org-export-to-buffer 'md "*Org MD Export*"
507 async subtreep visible-only nil nil (lambda () (text-mode))))
509 ;;;###autoload
510 (defun org-md-convert-region-to-md ()
511 "Assume the current region has org-mode syntax, and convert it to Markdown.
512 This can be used in any buffer. For example, you can write an
513 itemized list in org-mode syntax in a Markdown buffer and use
514 this command to convert it."
515 (interactive)
516 (org-export-replace-region-by 'md))
519 ;;;###autoload
520 (defun org-md-export-to-markdown (&optional async subtreep visible-only)
521 "Export current buffer to a Markdown file.
523 If narrowing is active in the current buffer, only export its
524 narrowed part.
526 If a region is active, export that region.
528 A non-nil optional argument ASYNC means the process should happen
529 asynchronously. The resulting file should be accessible through
530 the `org-export-stack' interface.
532 When optional argument SUBTREEP is non-nil, export the sub-tree
533 at point, extracting information from the headline properties
534 first.
536 When optional argument VISIBLE-ONLY is non-nil, don't export
537 contents of hidden elements.
539 Return output file's name."
540 (interactive)
541 (let ((outfile (org-export-output-file-name ".md" subtreep)))
542 (org-export-to-file 'md outfile async subtreep visible-only)))
544 ;;;###autoload
545 (defun org-md-publish-to-md (plist filename pub-dir)
546 "Publish an org file to Markdown.
548 FILENAME is the filename of the Org file to be published. PLIST
549 is the property list for the given project. PUB-DIR is the
550 publishing directory.
552 Return output file name."
553 (org-publish-org-to 'md filename ".md" plist pub-dir))
555 (provide 'ox-md)
557 ;; Local variables:
558 ;; generated-autoload-file: "org-loaddefs.el"
559 ;; End:
561 ;;; ox-md.el ends here