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/>.
25 ;; This library implements a Markdown back-end (vanilla flavor) for
26 ;; Org exporter, based on `html' back-end. See Org manual for more
31 (eval-when-compile (require 'cl
))
36 ;;; User-Configurable Variables
38 (defgroup org-export-md nil
39 "Options specific to Markdown export back-end."
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'."
50 (const :tag
"Use \"atx\" style" atx
)
51 (const :tag
"Use \"Setext\" style" setext
)))
57 (org-export-define-derived-backend 'md
'html
58 :export-block
'("MD" "MARKDOWN")
59 :filters-alist
'((:filter-parse-tree . org-md-separate-elements
))
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"
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 (export-block . org-md-export-block
)
75 (fixed-width . org-md-example-block
)
76 (footnote-definition . ignore
)
77 (footnote-reference . ignore
)
78 (headline . org-md-headline
)
79 (horizontal-rule . org-md-horizontal-rule
)
80 (inline-src-block . org-md-verbatim
)
81 (inner-template . org-md-inner-template
)
82 (italic . org-md-italic
)
84 (keyword . org-md-keyword
)
85 (line-break . org-md-line-break
)
87 (node-property . org-md-node-property
)
88 (paragraph . org-md-paragraph
)
89 (plain-list . org-md-plain-list
)
90 (plain-text . org-md-plain-text
)
91 (property-drawer . org-md-property-drawer
)
92 (quote-block . org-md-quote-block
)
93 (section . org-md-section
)
94 (src-block . org-md-example-block
)
95 (template . org-md-template
)
96 (verbatim . org-md-verbatim
))
97 :options-alist
'((:md-headline-style nil nil org-md-headline-style
)))
102 (defun org-md-separate-elements (tree backend info
)
103 "Fix blank lines between elements.
105 TREE is the parse tree being exported. BACKEND is the export
106 back-end used. INFO is a plist used as a communication channel.
108 Enforce a blank line between elements. There are three
109 exceptions to this rule:
111 1. Preserve blank lines between sibling items in a plain list,
113 2. Outside of plain lists, preserve blank lines between
114 a paragraph and a plain list,
116 3. In an item, remove any blank line before the very first
117 paragraph and the next sub-list.
119 Assume BACKEND is `md'."
120 (org-element-map tree
(remq 'item org-element-all-elements
)
123 ((not (and (eq (org-element-type e
) 'paragraph
)
124 (eq (org-element-type (org-export-get-next-element e info
))
126 (org-element-put-property e
:post-blank
1))
127 ((not (eq (org-element-type (org-element-property :parent e
)) 'item
)))
128 (t (org-element-put-property
129 e
:post-blank
(if (org-export-get-previous-element e info
) 1 0))))))
130 ;; Return updated tree.
135 ;;; Transcode Functions
139 (defun org-md-bold (bold contents info
)
140 "Transcode BOLD object into Markdown format.
141 CONTENTS is the text within bold markup. INFO is a plist used as
142 a communication channel."
143 (format "**%s**" contents
))
146 ;;;; Code and Verbatim
148 (defun org-md-verbatim (verbatim contents info
)
149 "Transcode VERBATIM object into Markdown format.
150 CONTENTS is nil. INFO is a plist used as a communication
152 (let ((value (org-element-property :value verbatim
)))
153 (format (cond ((not (string-match "`" value
)) "`%s`")
154 ((or (string-match "\\``" value
)
155 (string-match "`\\'" value
))
161 ;;;; Example Block, Src Block and export Block
163 (defun org-md-example-block (example-block contents info
)
164 "Transcode EXAMPLE-BLOCK element into Markdown format.
165 CONTENTS is nil. INFO is a plist used as a communication
167 (replace-regexp-in-string
169 (org-remove-indentation
170 (org-export-format-code-default example-block info
))))
172 (defun org-md-export-block (export-block contents info
)
173 "Transcode a EXPORT-BLOCK element from Org to Markdown.
174 CONTENTS is nil. INFO is a plist holding contextual information."
175 (if (member (org-element-property :type export-block
) '("MARKDOWN" "MD"))
176 (org-remove-indentation (org-element-property :value export-block
))
177 ;; Also include HTML export blocks.
178 (org-export-with-backend 'html export-block contents info
)))
183 (defun org-md-headline (headline contents info
)
184 "Transcode HEADLINE element into Markdown format.
185 CONTENTS is the headline contents. INFO is a plist used as
186 a communication channel."
187 (unless (org-element-property :footnote-section-p headline
)
188 (let* ((level (org-export-get-relative-level headline info
))
189 (title (org-export-data (org-element-property :title headline
) info
))
190 (todo (and (plist-get info
:with-todo-keywords
)
191 (let ((todo (org-element-property :todo-keyword
193 (and todo
(concat (org-export-data todo info
) " ")))))
194 (tags (and (plist-get info
:with-tags
)
195 (let ((tag-list (org-export-get-tags headline info
)))
198 (mapconcat 'identity tag-list
":"))))))
200 (and (plist-get info
:with-priority
)
201 (let ((char (org-element-property :priority headline
)))
202 (and char
(format "[#%c] " char
)))))
204 (when (plist-get info
:with-toc
)
206 (or (org-element-property :CUSTOM_ID headline
)
207 (org-export-get-headline-id headline info
))
209 ;; Headline text without tags.
210 (heading (concat todo priority title
))
211 (style (plist-get info
:md-headline-style
)))
213 ;; Cannot create a headline. Fall-back to a list.
214 ((or (org-export-low-level-p headline info
)
215 (not (memq style
'(atx setext
)))
216 (and (eq style
'atx
) (> level
6))
217 (and (eq style
'setext
) (> level
2)))
219 (if (not (org-export-numbered-headline-p headline info
)) "-"
220 (concat (number-to-string
221 (car (last (org-export-get-headline-number
224 (concat bullet
(make-string (- 4 (length bullet
)) ?
) heading tags
227 (replace-regexp-in-string "^" " " contents
)))))
228 ;; Use "Setext" style.
230 (concat heading tags anchor
"\n"
231 (make-string (length heading
) (if (= level
1) ?
= ?-
))
235 (t (concat (make-string level ?
#) " " heading tags anchor
"\n\n" contents
))))))
240 (defun org-md-horizontal-rule (horizontal-rule contents info
)
241 "Transcode HORIZONTAL-RULE element into Markdown format.
242 CONTENTS is the horizontal rule contents. INFO is a plist used
243 as a communication channel."
249 (defun org-md-italic (italic contents info
)
250 "Transcode ITALIC object into Markdown format.
251 CONTENTS is the text within italic markup. INFO is a plist used
252 as a communication channel."
253 (format "*%s*" contents
))
258 (defun org-md-item (item contents info
)
259 "Transcode ITEM element into Markdown format.
260 CONTENTS is the item contents. INFO is a plist used as
261 a communication channel."
262 (let* ((type (org-element-property :type
(org-export-get-parent item
)))
263 (struct (org-element-property :structure item
))
264 (bullet (if (not (eq type
'ordered
)) "-"
265 (concat (number-to-string
266 (car (last (org-list-get-item-number
267 (org-element-property :begin item
)
269 (org-list-prevs-alist struct
)
270 (org-list-parents-alist struct
)))))
273 (make-string (- 4 (length bullet
)) ?
)
274 (case (org-element-property :checkbox item
)
278 (let ((tag (org-element-property :tag item
)))
279 (and tag
(format "**%s:** "(org-export-data tag info
))))
281 (org-trim (replace-regexp-in-string "^" " " contents
))))))
287 (defun org-md-keyword (keyword contents info
)
288 "Transcode a KEYWORD element into Markdown format.
289 CONTENTS is nil. INFO is a plist used as a communication
291 (if (member (org-element-property :key keyword
) '("MARKDOWN" "MD"))
292 (org-element-property :value keyword
)
293 (org-export-with-backend 'html keyword contents info
)))
298 (defun org-md-line-break (line-break contents info
)
299 "Transcode LINE-BREAK object into Markdown format.
300 CONTENTS is nil. INFO is a plist used as a communication
307 (defun org-md-link (link contents info
)
308 "Transcode LINE-BREAK object into Markdown format.
309 CONTENTS is the link's description. INFO is a plist used as
310 a communication channel."
311 (let ((link-org-files-as-md
314 ;; Treat links to `file.org' as links to `file.md'.
315 (if (string= ".org" (downcase (file-name-extension raw-path
".")))
316 (concat (file-name-sans-extension raw-path
) ".md")
318 (type (org-element-property :type link
)))
320 ((member type
'("custom-id" "id"))
321 (let ((destination (org-export-resolve-id-link link info
)))
322 (if (stringp destination
) ; External file.
323 (let ((path (funcall link-org-files-as-md destination
)))
324 (if (not contents
) (format "<%s>" path
)
325 (format "[%s](%s)" contents path
)))
327 (and contents
(concat contents
" "))
329 (format (org-export-translate "See section %s" :html info
)
330 (if (org-export-numbered-headline-p destination info
)
331 (mapconcat #'number-to-string
332 (org-export-get-headline-number
336 (org-element-property :title destination
) info
))))))))
337 ((org-export-inline-image-p link org-html-inline-image-rules
)
338 (let ((path (let ((raw-path (org-element-property :path link
)))
339 (if (not (file-name-absolute-p raw-path
)) raw-path
340 (expand-file-name raw-path
))))
341 (caption (org-export-data
342 (org-export-get-caption
343 (org-export-get-parent-element link
)) info
)))
345 (if (not (org-string-nw-p caption
)) path
346 (format "%s \"%s\"" path caption
)))))
347 ((string= type
"coderef")
348 (let ((ref (org-element-property :path link
)))
349 (format (org-export-get-coderef-format ref contents
)
350 (org-export-resolve-coderef ref info
))))
351 ((equal type
"radio") contents
)
352 ((equal type
"fuzzy")
353 (let ((destination (org-export-resolve-fuzzy-link link info
)))
354 (if (org-string-nw-p contents
) contents
356 (let ((number (org-export-get-ordinal destination info
)))
358 (if (atom number
) (number-to-string number
)
359 (mapconcat #'number-to-string number
"."))
360 ;; Unnumbered headline.
361 (and (eq 'headline
(org-element-type destination
))
362 ;; BUG: shouldn't headlines have a form like [ref](name) in md?
364 (org-element-property :title destination
) info
))))))))
365 ;; Link type is handled by a special function.
366 ((let ((protocol (nth 2 (assoc type org-link-protocols
))))
367 (and (functionp protocol
)
369 (org-link-unescape (org-element-property :path link
))
372 (t (let* ((raw-path (org-element-property :path link
))
375 ((member type
'("http" "https" "ftp"))
376 (concat type
":" raw-path
))
377 ((string= type
"file")
378 (let ((path (funcall link-org-files-as-md raw-path
)))
379 (if (not (file-name-absolute-p path
)) path
380 ;; If file path is absolute, prepend it
381 ;; with "file:" component.
382 (concat "file:" path
))))
384 (if (not contents
) (format "<%s>" path
)
385 (format "[%s](%s)" contents path
)))))))
390 (defun org-md-node-property (node-property contents info
)
391 "Transcode a NODE-PROPERTY element into Markdown syntax.
392 CONTENTS is nil. INFO is a plist holding contextual
395 (org-element-property :key node-property
)
396 (let ((value (org-element-property :value node-property
)))
397 (if value
(concat " " value
) ""))))
402 (defun org-md-paragraph (paragraph contents info
)
403 "Transcode PARAGRAPH element into Markdown format.
404 CONTENTS is the paragraph contents. INFO is a plist used as
405 a communication channel."
406 (let ((first-object (car (org-element-contents paragraph
))))
407 ;; If paragraph starts with a #, protect it.
408 (if (and (stringp first-object
) (string-match "\\`#" first-object
))
409 (replace-regexp-in-string "\\`#" "\\#" contents nil t
)
415 (defun org-md-plain-list (plain-list contents info
)
416 "Transcode PLAIN-LIST element into Markdown format.
417 CONTENTS is the plain-list contents. INFO is a plist used as
418 a communication channel."
424 (defun org-md-plain-text (text info
)
425 "Transcode a TEXT string into Markdown format.
426 TEXT is the string to transcode. INFO is a plist holding
427 contextual information."
428 (when (plist-get info
:with-smart-quotes
)
429 (setq text
(org-export-activate-smart-quotes text
:html info
)))
430 ;; Protect ambiguous #. This will protect # at the beginning of
431 ;; a line, but not at the beginning of a paragraph. See
432 ;; `org-md-paragraph'.
433 (setq text
(replace-regexp-in-string "\n#" "\n\\\\#" text
))
434 ;; Protect ambiguous !
435 (setq text
(replace-regexp-in-string "\\(!\\)\\[" "\\\\!" text nil nil
1))
436 ;; Protect `, *, _ and \
437 (setq text
(replace-regexp-in-string "[`*_\\]" "\\\\\\&" text
))
438 ;; Handle special strings, if required.
439 (when (plist-get info
:with-special-strings
)
440 (setq text
(org-html-convert-special-strings text
)))
441 ;; Handle break preservation, if required.
442 (when (plist-get info
:preserve-breaks
)
443 (setq text
(replace-regexp-in-string "[ \t]*\n" " \n" text
)))
450 (defun org-md-property-drawer (property-drawer contents info
)
451 "Transcode a PROPERTY-DRAWER element into Markdown format.
452 CONTENTS holds the contents of the drawer. INFO is a plist
453 holding contextual information."
454 (and (org-string-nw-p contents
)
455 (replace-regexp-in-string "^" " " contents
)))
460 (defun org-md-quote-block (quote-block contents info
)
461 "Transcode QUOTE-BLOCK element into Markdown format.
462 CONTENTS is the quote-block contents. INFO is a plist used as
463 a communication channel."
464 (replace-regexp-in-string
466 (replace-regexp-in-string "\n\\'" "" contents
)))
471 (defun org-md-section (section contents info
)
472 "Transcode SECTION element into Markdown format.
473 CONTENTS is the section contents. INFO is a plist used as
474 a communication channel."
480 (defun org-md-inner-template (contents info
)
481 "Return body of document after converting it to Markdown syntax.
482 CONTENTS is the transcoded contents string. INFO is a plist
483 holding export options."
484 ;; Make sure CONTENTS is separated from table of contents and
485 ;; footnotes with at least a blank line.
486 (org-trim (org-html-inner-template (concat "\n" contents
"\n") info
)))
488 (defun org-md-template (contents info
)
489 "Return complete document string after Markdown conversion.
490 CONTENTS is the transcoded contents string. INFO is a plist used
491 as a communication channel."
496 ;;; Interactive function
499 (defun org-md-export-as-markdown (&optional async subtreep visible-only
)
500 "Export current buffer to a Markdown buffer.
502 If narrowing is active in the current buffer, only export its
505 If a region is active, export that region.
507 A non-nil optional argument ASYNC means the process should happen
508 asynchronously. The resulting buffer should be accessible
509 through the `org-export-stack' interface.
511 When optional argument SUBTREEP is non-nil, export the sub-tree
512 at point, extracting information from the headline properties
515 When optional argument VISIBLE-ONLY is non-nil, don't export
516 contents of hidden elements.
518 Export is done in a buffer named \"*Org MD Export*\", which will
519 be displayed when `org-export-show-temporary-export-buffer' is
522 (org-export-to-buffer 'md
"*Org MD Export*"
523 async subtreep visible-only nil nil
(lambda () (text-mode))))
526 (defun org-md-convert-region-to-md ()
527 "Assume the current region has org-mode syntax, and convert it to Markdown.
528 This can be used in any buffer. For example, you can write an
529 itemized list in org-mode syntax in a Markdown buffer and use
530 this command to convert it."
532 (org-export-replace-region-by 'md
))
536 (defun org-md-export-to-markdown (&optional async subtreep visible-only
)
537 "Export current buffer to a Markdown file.
539 If narrowing is active in the current buffer, only export its
542 If a region is active, export that region.
544 A non-nil optional argument ASYNC means the process should happen
545 asynchronously. The resulting file should be accessible through
546 the `org-export-stack' interface.
548 When optional argument SUBTREEP is non-nil, export the sub-tree
549 at point, extracting information from the headline properties
552 When optional argument VISIBLE-ONLY is non-nil, don't export
553 contents of hidden elements.
555 Return output file's name."
557 (let ((outfile (org-export-output-file-name ".md" subtreep
)))
558 (org-export-to-file 'md outfile async subtreep visible-only
)))
561 (defun org-md-publish-to-md (plist filename pub-dir
)
562 "Publish an org file to Markdown.
564 FILENAME is the filename of the Org file to be published. PLIST
565 is the property list for the given project. PUB-DIR is the
566 publishing directory.
568 Return output file name."
569 (org-publish-org-to 'md filename
".md" plist pub-dir
))
574 ;; generated-autoload-file: "org-loaddefs.el"
577 ;;; ox-md.el ends here