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 (quote-section . org-md-example-block
)
94 (section . org-md-section
)
95 (src-block . org-md-example-block
)
96 (template . org-md-template
)
97 (verbatim . org-md-verbatim
)))
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 Make sure there's no blank line before a plain list, unless it is
109 located right after a paragraph. Otherwise, add a blank line
110 between elements. Blank lines between items are preserved.
112 Assume BACKEND is `md'."
113 (org-element-map tree
(remq 'item org-element-all-elements
)
115 (org-element-put-property
117 (if (and (eq (org-element-type (org-export-get-next-element elem info
))
119 (not (and (eq (org-element-type elem
) 'paragraph
)
120 (org-export-get-previous-element elem info
))))
123 ;; Return updated tree.
128 ;;; Transcode Functions
132 (defun org-md-bold (bold contents info
)
133 "Transcode BOLD object into Markdown format.
134 CONTENTS is the text within bold markup. INFO is a plist used as
135 a communication channel."
136 (format "**%s**" contents
))
139 ;;;; Code and Verbatim
141 (defun org-md-verbatim (verbatim contents info
)
142 "Transcode VERBATIM object into Markdown format.
143 CONTENTS is nil. INFO is a plist used as a communication
145 (let ((value (org-element-property :value verbatim
)))
146 (format (cond ((not (string-match "`" value
)) "`%s`")
147 ((or (string-match "\\``" value
)
148 (string-match "`\\'" value
))
154 ;;;; Example Block, Src Block and export Block
156 (defun org-md-example-block (example-block contents info
)
157 "Transcode EXAMPLE-BLOCK element into Markdown format.
158 CONTENTS is nil. INFO is a plist used as a communication
160 (replace-regexp-in-string
162 (org-remove-indentation
163 (org-element-property :value example-block
))))
165 (defun org-md-export-block (export-block contents info
)
166 "Transcode a EXPORT-BLOCK element from Org to Markdown.
167 CONTENTS is nil. INFO is a plist holding contextual information."
168 (if (member (org-element-property :type export-block
) '("MARKDOWN" "MD"))
169 (org-remove-indentation (org-element-property :value export-block
))
170 ;; Also include HTML export blocks.
171 (org-export-with-backend 'html export-block contents info
)))
176 (defun org-md-headline (headline contents info
)
177 "Transcode HEADLINE element into Markdown format.
178 CONTENTS is the headline contents. INFO is a plist used as
179 a communication channel."
180 (unless (org-element-property :footnote-section-p headline
)
181 (let* ((level (org-export-get-relative-level headline info
))
182 (title (org-export-data (org-element-property :title headline
) info
))
183 (todo (and (plist-get info
:with-todo-keywords
)
184 (let ((todo (org-element-property :todo-keyword
186 (and todo
(concat (org-export-data todo info
) " ")))))
187 (tags (and (plist-get info
:with-tags
)
188 (let ((tag-list (org-export-get-tags headline info
)))
191 (mapconcat 'identity tag-list
":"))))))
193 (and (plist-get info
:with-priority
)
194 (let ((char (org-element-property :priority headline
)))
195 (and char
(format "[#%c] " char
)))))
196 ;; Headline text without tags.
197 (heading (concat todo priority title
)))
199 ;; Cannot create a headline. Fall-back to a list.
200 ((or (org-export-low-level-p headline info
)
201 (not (memq org-md-headline-style
'(atx setext
)))
202 (and (eq org-md-headline-style
'atx
) (> level
6))
203 (and (eq org-md-headline-style
'setext
) (> level
2)))
205 (if (not (org-export-numbered-headline-p headline info
)) "-"
206 (concat (number-to-string
207 (car (last (org-export-get-headline-number
210 (concat bullet
(make-string (- 4 (length bullet
)) ?
) heading tags
213 (replace-regexp-in-string "^" " " contents
)))))
214 ;; Use "Setext" style.
215 ((eq org-md-headline-style
'setext
)
216 (concat heading tags
"\n"
217 (make-string (length heading
) (if (= level
1) ?
= ?-
))
221 (t (concat (make-string level ?
#) " " heading tags
"\n\n" contents
))))))
226 (defun org-md-horizontal-rule (horizontal-rule contents info
)
227 "Transcode HORIZONTAL-RULE element into Markdown format.
228 CONTENTS is the horizontal rule contents. INFO is a plist used
229 as a communication channel."
235 (defun org-md-italic (italic contents info
)
236 "Transcode ITALIC object into Markdown format.
237 CONTENTS is the text within italic markup. INFO is a plist used
238 as a communication channel."
239 (format "*%s*" contents
))
244 (defun org-md-item (item contents info
)
245 "Transcode ITEM element into Markdown format.
246 CONTENTS is the item contents. INFO is a plist used as
247 a communication channel."
248 (let* ((type (org-element-property :type
(org-export-get-parent item
)))
249 (struct (org-element-property :structure item
))
250 (bullet (if (not (eq type
'ordered
)) "-"
251 (concat (number-to-string
252 (car (last (org-list-get-item-number
253 (org-element-property :begin item
)
255 (org-list-prevs-alist struct
)
256 (org-list-parents-alist struct
)))))
259 (make-string (- 4 (length bullet
)) ?
)
260 (case (org-element-property :checkbox item
)
264 (let ((tag (org-element-property :tag item
)))
265 (and tag
(format "**%s:** "(org-export-data tag info
))))
267 (org-trim (replace-regexp-in-string "^" " " contents
))))))
273 (defun org-md-keyword (keyword contents info
)
274 "Transcode a KEYWORD element into Markdown format.
275 CONTENTS is nil. INFO is a plist used as a communication
277 (if (member (org-element-property :key keyword
) '("MARKDOWN" "MD"))
278 (org-element-property :value keyword
)
279 (org-export-with-backend 'html keyword contents info
)))
284 (defun org-md-line-break (line-break contents info
)
285 "Transcode LINE-BREAK object into Markdown format.
286 CONTENTS is nil. INFO is a plist used as a communication
293 (defun org-md-link (link contents info
)
294 "Transcode LINE-BREAK object into Markdown format.
295 CONTENTS is the link's description. INFO is a plist used as
296 a communication channel."
297 (let ((--link-org-files-as-html-maybe
299 (lambda (raw-path info
)
300 ;; Treat links to `file.org' as links to `file.html', if
301 ;; needed. See `org-html-link-org-files-as-html'.
303 ((and org-html-link-org-files-as-html
305 (downcase (file-name-extension raw-path
"."))))
306 (concat (file-name-sans-extension raw-path
) "."
307 (plist-get info
:html-extension
)))
309 (type (org-element-property :type link
)))
310 (cond ((member type
'("custom-id" "id"))
311 (let ((destination (org-export-resolve-id-link link info
)))
312 (if (stringp destination
) ; External file.
313 (let ((path (funcall --link-org-files-as-html-maybe
315 (if (not contents
) (format "<%s>" path
)
316 (format "[%s](%s)" contents path
)))
318 (and contents
(concat contents
" "))
320 (format (org-export-translate "See section %s" :html info
)
321 (mapconcat 'number-to-string
322 (org-export-get-headline-number
325 ((org-export-inline-image-p link org-html-inline-image-rules
)
326 (let ((path (let ((raw-path (org-element-property :path link
)))
327 (if (not (file-name-absolute-p raw-path
)) raw-path
328 (expand-file-name raw-path
)))))
330 (let ((caption (org-export-get-caption
331 (org-export-get-parent-element link
))))
332 (when caption
(org-export-data caption info
)))
334 ((string= type
"coderef")
335 (let ((ref (org-element-property :path link
)))
336 (format (org-export-get-coderef-format ref contents
)
337 (org-export-resolve-coderef ref info
))))
338 ((equal type
"radio")
339 (let ((destination (org-export-resolve-radio-link link info
)))
340 (org-export-data (org-element-contents destination
) info
)))
341 ((equal type
"fuzzy")
342 (let ((destination (org-export-resolve-fuzzy-link link info
)))
343 (if (org-string-nw-p contents
) contents
345 (let ((number (org-export-get-ordinal destination info
)))
347 (if (atom number
) (number-to-string number
)
348 (mapconcat 'number-to-string number
"."))))))))
349 (t (let* ((raw-path (org-element-property :path link
))
351 ((member type
'("http" "https" "ftp"))
352 (concat type
":" raw-path
))
354 ;; Treat links to ".org" files as ".html",
357 (funcall --link-org-files-as-html-maybe
359 ;; If file path is absolute, prepend it
360 ;; with protocol component - "file://".
361 (if (not (file-name-absolute-p raw-path
)) raw-path
362 (concat "file://" (expand-file-name raw-path
))))
364 (if (not contents
) (format "<%s>" path
)
365 (format "[%s](%s)" contents path
)))))))
370 (defun org-md-node-property (node-property contents info
)
371 "Transcode a NODE-PROPERTY element into Markdown syntax.
372 CONTENTS is nil. INFO is a plist holding contextual
375 (org-element-property :key node-property
)
376 (let ((value (org-element-property :value node-property
)))
377 (if value
(concat " " value
) ""))))
382 (defun org-md-paragraph (paragraph contents info
)
383 "Transcode PARAGRAPH element into Markdown format.
384 CONTENTS is the paragraph contents. INFO is a plist used as
385 a communication channel."
386 (let ((first-object (car (org-element-contents paragraph
))))
387 ;; If paragraph starts with a #, protect it.
388 (if (and (stringp first-object
) (string-match "\\`#" first-object
))
389 (replace-regexp-in-string "\\`#" "\\#" contents nil t
)
395 (defun org-md-plain-list (plain-list contents info
)
396 "Transcode PLAIN-LIST element into Markdown format.
397 CONTENTS is the plain-list contents. INFO is a plist used as
398 a communication channel."
404 (defun org-md-plain-text (text info
)
405 "Transcode a TEXT string into Markdown format.
406 TEXT is the string to transcode. INFO is a plist holding
407 contextual information."
408 (when (plist-get info
:with-smart-quotes
)
409 (setq text
(org-export-activate-smart-quotes text
:html info
)))
410 ;; Protect ambiguous #. This will protect # at the beginning of
411 ;; a line, but not at the beginning of a paragraph. See
412 ;; `org-md-paragraph'.
413 (setq text
(replace-regexp-in-string "\n#" "\n\\\\#" text
))
414 ;; Protect ambiguous !
415 (setq text
(replace-regexp-in-string "\\(!\\)\\[" "\\\\!" text nil nil
1))
416 ;; Protect `, *, _ and \
417 (setq text
(replace-regexp-in-string "[`*_\\]" "\\\\\\&" text
))
418 ;; Handle special strings, if required.
419 (when (plist-get info
:with-special-strings
)
420 (setq text
(org-html-convert-special-strings text
)))
421 ;; Handle break preservation, if required.
422 (when (plist-get info
:preserve-breaks
)
423 (setq text
(replace-regexp-in-string "[ \t]*\n" " \n" text
)))
430 (defun org-md-property-drawer (property-drawer contents info
)
431 "Transcode a PROPERTY-DRAWER element into Markdown format.
432 CONTENTS holds the contents of the drawer. INFO is a plist
433 holding contextual information."
434 (and (org-string-nw-p contents
)
435 (replace-regexp-in-string "^" " " contents
)))
440 (defun org-md-quote-block (quote-block contents info
)
441 "Transcode QUOTE-BLOCK element into Markdown format.
442 CONTENTS is the quote-block contents. INFO is a plist used as
443 a communication channel."
444 (replace-regexp-in-string
446 (replace-regexp-in-string "\n\\'" "" contents
)))
451 (defun org-md-section (section contents info
)
452 "Transcode SECTION element into Markdown format.
453 CONTENTS is the section contents. INFO is a plist used as
454 a communication channel."
460 (defun org-md-inner-template (contents info
)
461 "Return body of document after converting it to Markdown syntax.
462 CONTENTS is the transcoded contents string. INFO is a plist
463 holding export options."
464 ;; Make sure CONTENTS is separated from table of contents and
465 ;; footnotes with at least a blank line.
466 (org-trim (org-html-inner-template (concat "\n" contents
"\n") info
)))
468 (defun org-md-template (contents info
)
469 "Return complete document string after Markdown conversion.
470 CONTENTS is the transcoded contents string. INFO is a plist used
471 as a communication channel."
476 ;;; Interactive function
479 (defun org-md-export-as-markdown (&optional async subtreep visible-only
)
480 "Export current buffer to a Markdown buffer.
482 If narrowing is active in the current buffer, only export its
485 If a region is active, export that region.
487 A non-nil optional argument ASYNC means the process should happen
488 asynchronously. The resulting buffer should be accessible
489 through the `org-export-stack' interface.
491 When optional argument SUBTREEP is non-nil, export the sub-tree
492 at point, extracting information from the headline properties
495 When optional argument VISIBLE-ONLY is non-nil, don't export
496 contents of hidden elements.
498 Export is done in a buffer named \"*Org MD Export*\", which will
499 be displayed when `org-export-show-temporary-export-buffer' is
502 (org-export-to-buffer 'md
"*Org MD Export*"
503 async subtreep visible-only nil nil
(lambda () (text-mode))))
506 (defun org-md-convert-region-to-md ()
507 "Assume the current region has org-mode syntax, and convert it to Markdown.
508 This can be used in any buffer. For example, you can write an
509 itemized list in org-mode syntax in a Markdown buffer and use
510 this command to convert it."
512 (org-export-replace-region-by 'md
))
516 (defun org-md-export-to-markdown (&optional async subtreep visible-only
)
517 "Export current buffer to a Markdown file.
519 If narrowing is active in the current buffer, only export its
522 If a region is active, export that region.
524 A non-nil optional argument ASYNC means the process should happen
525 asynchronously. The resulting file should be accessible through
526 the `org-export-stack' interface.
528 When optional argument SUBTREEP is non-nil, export the sub-tree
529 at point, extracting information from the headline properties
532 When optional argument VISIBLE-ONLY is non-nil, don't export
533 contents of hidden elements.
535 Return output file's name."
537 (let ((outfile (org-export-output-file-name ".md" subtreep
)))
538 (org-export-to-file 'md outfile async subtreep visible-only
)))
541 (defun org-md-publish-to-md (plist filename pub-dir
)
542 "Publish an org file to Markdown.
544 FILENAME is the filename of the Org file to be published. PLIST
545 is the property list for the given project. PUB-DIR is the
546 publishing directory.
548 Return output file name."
549 (org-publish-org-to 'md filename
".md" plist pub-dir
))
554 ;; generated-autoload-file: "org-loaddefs.el"
557 ;;; ox-md.el ends here