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 (headline . org-md-headline
)
77 (horizontal-rule . org-md-horizontal-rule
)
78 (inline-src-block . org-md-verbatim
)
79 (inner-template . org-md-inner-template
)
80 (italic . org-md-italic
)
82 (keyword . org-md-keyword
)
83 (line-break . org-md-line-break
)
85 (node-property . org-md-node-property
)
86 (paragraph . org-md-paragraph
)
87 (plain-list . org-md-plain-list
)
88 (plain-text . org-md-plain-text
)
89 (property-drawer . org-md-property-drawer
)
90 (quote-block . org-md-quote-block
)
91 (section . org-md-section
)
92 (src-block . org-md-example-block
)
93 (template . org-md-template
)
94 (verbatim . org-md-verbatim
))
95 :options-alist
'((:md-headline-style nil nil org-md-headline-style
)))
100 (defun org-md-separate-elements (tree backend info
)
101 "Fix blank lines between elements.
103 TREE is the parse tree being exported. BACKEND is the export
104 back-end used. INFO is a plist used as a communication channel.
106 Enforce a blank line between elements. There are two exceptions
109 1. Preserve blank lines between sibling items in a plain list,
111 2. In an item, remove any blank line before the very first
112 paragraph and the next sub-list.
114 Assume BACKEND is `md'."
115 (org-element-map tree
(remq 'item org-element-all-elements
)
117 (org-element-put-property
119 (if (and (eq (org-element-type e
) 'paragraph
)
120 (eq (org-element-type (org-element-property :parent e
)) 'item
)
121 (eq (org-element-type (org-export-get-next-element e info
))
123 (not (org-export-get-previous-element e info
)))
126 ;; Return updated tree.
131 ;;; Transcode Functions
135 (defun org-md-bold (bold contents info
)
136 "Transcode BOLD object into Markdown format.
137 CONTENTS is the text within bold markup. INFO is a plist used as
138 a communication channel."
139 (format "**%s**" contents
))
142 ;;;; Code and Verbatim
144 (defun org-md-verbatim (verbatim contents info
)
145 "Transcode VERBATIM object into Markdown format.
146 CONTENTS is nil. INFO is a plist used as a communication
148 (let ((value (org-element-property :value verbatim
)))
149 (format (cond ((not (string-match "`" value
)) "`%s`")
150 ((or (string-match "\\``" value
)
151 (string-match "`\\'" value
))
157 ;;;; Example Block, Src Block and export Block
159 (defun org-md-example-block (example-block contents info
)
160 "Transcode EXAMPLE-BLOCK element into Markdown format.
161 CONTENTS is nil. INFO is a plist used as a communication
163 (replace-regexp-in-string
165 (org-remove-indentation
166 (org-export-format-code-default example-block info
))))
168 (defun org-md-export-block (export-block contents info
)
169 "Transcode a EXPORT-BLOCK element from Org to Markdown.
170 CONTENTS is nil. INFO is a plist holding contextual information."
171 (if (member (org-element-property :type export-block
) '("MARKDOWN" "MD"))
172 (org-remove-indentation (org-element-property :value export-block
))
173 ;; Also include HTML export blocks.
174 (org-export-with-backend 'html export-block contents info
)))
179 (defun org-md-headline (headline contents info
)
180 "Transcode HEADLINE element into Markdown format.
181 CONTENTS is the headline contents. INFO is a plist used as
182 a communication channel."
183 (unless (org-element-property :footnote-section-p headline
)
184 (let* ((level (org-export-get-relative-level headline info
))
185 (title (org-export-data (org-element-property :title headline
) info
))
186 (todo (and (plist-get info
:with-todo-keywords
)
187 (let ((todo (org-element-property :todo-keyword
189 (and todo
(concat (org-export-data todo info
) " ")))))
190 (tags (and (plist-get info
:with-tags
)
191 (let ((tag-list (org-export-get-tags headline info
)))
194 (mapconcat 'identity tag-list
":"))))))
196 (and (plist-get info
:with-priority
)
197 (let ((char (org-element-property :priority headline
)))
198 (and char
(format "[#%c] " char
)))))
200 (when (plist-get info
:with-toc
)
202 (or (org-element-property :CUSTOM_ID headline
)
203 (org-export-get-headline-id headline info
))
205 ;; Headline text without tags.
206 (heading (concat todo priority title
))
207 (style (plist-get info
:md-headline-style
)))
209 ;; Cannot create a headline. Fall-back to a list.
210 ((or (org-export-low-level-p headline info
)
211 (not (memq style
'(atx setext
)))
212 (and (eq style
'atx
) (> level
6))
213 (and (eq style
'setext
) (> level
2)))
215 (if (not (org-export-numbered-headline-p headline info
)) "-"
216 (concat (number-to-string
217 (car (last (org-export-get-headline-number
220 (concat bullet
(make-string (- 4 (length bullet
)) ?
) heading tags
223 (replace-regexp-in-string "^" " " contents
)))))
224 ;; Use "Setext" style.
226 (concat heading tags anchor
"\n"
227 (make-string (length heading
) (if (= level
1) ?
= ?-
))
231 (t (concat (make-string level ?
#) " " heading tags anchor
"\n\n" contents
))))))
236 (defun org-md-horizontal-rule (horizontal-rule contents info
)
237 "Transcode HORIZONTAL-RULE element into Markdown format.
238 CONTENTS is the horizontal rule contents. INFO is a plist used
239 as a communication channel."
245 (defun org-md-italic (italic contents info
)
246 "Transcode ITALIC object into Markdown format.
247 CONTENTS is the text within italic markup. INFO is a plist used
248 as a communication channel."
249 (format "*%s*" contents
))
254 (defun org-md-item (item contents info
)
255 "Transcode ITEM element into Markdown format.
256 CONTENTS is the item contents. INFO is a plist used as
257 a communication channel."
258 (let* ((type (org-element-property :type
(org-export-get-parent item
)))
259 (struct (org-element-property :structure item
))
260 (bullet (if (not (eq type
'ordered
)) "-"
261 (concat (number-to-string
262 (car (last (org-list-get-item-number
263 (org-element-property :begin item
)
265 (org-list-prevs-alist struct
)
266 (org-list-parents-alist struct
)))))
269 (make-string (- 4 (length bullet
)) ?
)
270 (case (org-element-property :checkbox item
)
274 (let ((tag (org-element-property :tag item
)))
275 (and tag
(format "**%s:** "(org-export-data tag info
))))
277 (org-trim (replace-regexp-in-string "^" " " contents
))))))
283 (defun org-md-keyword (keyword contents info
)
284 "Transcode a KEYWORD element into Markdown format.
285 CONTENTS is nil. INFO is a plist used as a communication
287 (if (member (org-element-property :key keyword
) '("MARKDOWN" "MD"))
288 (org-element-property :value keyword
)
289 (org-export-with-backend 'html keyword contents info
)))
294 (defun org-md-line-break (line-break contents info
)
295 "Transcode LINE-BREAK object into Markdown format.
296 CONTENTS is nil. INFO is a plist used as a communication
303 (defun org-md-link (link contents info
)
304 "Transcode LINE-BREAK object into Markdown format.
305 CONTENTS is the link's description. INFO is a plist used as
306 a communication channel."
307 (let ((link-org-files-as-md
310 ;; Treat links to `file.org' as links to `file.md'.
311 (if (string= ".org" (downcase (file-name-extension raw-path
".")))
312 (concat (file-name-sans-extension raw-path
) ".md")
314 (type (org-element-property :type link
)))
316 ;; Link type is handled by a special function.
317 ((org-export-custom-protocol-maybe link contents
'md
))
318 ((member type
'("custom-id" "id"))
319 (let ((destination (org-export-resolve-id-link link info
)))
320 (if (stringp destination
) ; External file.
321 (let ((path (funcall link-org-files-as-md destination
)))
322 (if (not contents
) (format "<%s>" path
)
323 (format "[%s](%s)" contents path
)))
325 (and contents
(concat contents
" "))
327 (format (org-export-translate "See section %s" :html info
)
328 (if (org-export-numbered-headline-p destination info
)
329 (mapconcat #'number-to-string
330 (org-export-get-headline-number
334 (org-element-property :title destination
) info
))))))))
335 ((org-export-inline-image-p link org-html-inline-image-rules
)
336 (let ((path (let ((raw-path (org-element-property :path link
)))
337 (if (not (file-name-absolute-p raw-path
)) raw-path
338 (expand-file-name raw-path
))))
339 (caption (org-export-data
340 (org-export-get-caption
341 (org-export-get-parent-element link
)) info
)))
343 (if (not (org-string-nw-p caption
)) path
344 (format "%s \"%s\"" path caption
)))))
345 ((string= type
"coderef")
346 (let ((ref (org-element-property :path link
)))
347 (format (org-export-get-coderef-format ref contents
)
348 (org-export-resolve-coderef ref info
))))
349 ((equal type
"radio") contents
)
350 ((equal type
"fuzzy")
351 (let ((destination (org-export-resolve-fuzzy-link link info
)))
352 (if (org-string-nw-p contents
) contents
354 (let ((number (org-export-get-ordinal destination info
)))
356 (if (atom number
) (number-to-string number
)
357 (mapconcat #'number-to-string number
"."))
358 ;; Unnumbered headline.
359 (and (eq 'headline
(org-element-type destination
))
360 ;; BUG: shouldn't headlines have a form like [ref](name) in md?
362 (org-element-property :title destination
) info
))))))))
363 (t (let* ((raw-path (org-element-property :path link
))
366 ((member type
'("http" "https" "ftp"))
367 (concat type
":" raw-path
))
368 ((string= type
"file")
369 (let ((path (funcall link-org-files-as-md raw-path
)))
370 (if (not (file-name-absolute-p path
)) path
371 ;; If file path is absolute, prepend it
372 ;; with "file:" component.
373 (concat "file:" path
))))
375 (if (not contents
) (format "<%s>" path
)
376 (format "[%s](%s)" contents path
)))))))
381 (defun org-md-node-property (node-property contents info
)
382 "Transcode a NODE-PROPERTY element into Markdown syntax.
383 CONTENTS is nil. INFO is a plist holding contextual
386 (org-element-property :key node-property
)
387 (let ((value (org-element-property :value node-property
)))
388 (if value
(concat " " value
) ""))))
393 (defun org-md-paragraph (paragraph contents info
)
394 "Transcode PARAGRAPH element into Markdown format.
395 CONTENTS is the paragraph contents. INFO is a plist used as
396 a communication channel."
397 (let ((first-object (car (org-element-contents paragraph
))))
398 ;; If paragraph starts with a #, protect it.
399 (if (and (stringp first-object
) (string-match "\\`#" first-object
))
400 (replace-regexp-in-string "\\`#" "\\#" contents nil t
)
406 (defun org-md-plain-list (plain-list contents info
)
407 "Transcode PLAIN-LIST element into Markdown format.
408 CONTENTS is the plain-list contents. INFO is a plist used as
409 a communication channel."
415 (defun org-md-plain-text (text info
)
416 "Transcode a TEXT string into Markdown format.
417 TEXT is the string to transcode. INFO is a plist holding
418 contextual information."
419 (when (plist-get info
:with-smart-quotes
)
420 (setq text
(org-export-activate-smart-quotes text
:html info
)))
421 ;; Protect ambiguous #. This will protect # at the beginning of
422 ;; a line, but not at the beginning of a paragraph. See
423 ;; `org-md-paragraph'.
424 (setq text
(replace-regexp-in-string "\n#" "\n\\\\#" text
))
425 ;; Protect ambiguous !
426 (setq text
(replace-regexp-in-string "\\(!\\)\\[" "\\\\!" text nil nil
1))
427 ;; Protect `, *, _ and \
428 (setq text
(replace-regexp-in-string "[`*_\\]" "\\\\\\&" text
))
429 ;; Handle special strings, if required.
430 (when (plist-get info
:with-special-strings
)
431 (setq text
(org-html-convert-special-strings text
)))
432 ;; Handle break preservation, if required.
433 (when (plist-get info
:preserve-breaks
)
434 (setq text
(replace-regexp-in-string "[ \t]*\n" " \n" text
)))
441 (defun org-md-property-drawer (property-drawer contents info
)
442 "Transcode a PROPERTY-DRAWER element into Markdown format.
443 CONTENTS holds the contents of the drawer. INFO is a plist
444 holding contextual information."
445 (and (org-string-nw-p contents
)
446 (replace-regexp-in-string "^" " " contents
)))
451 (defun org-md-quote-block (quote-block contents info
)
452 "Transcode QUOTE-BLOCK element into Markdown format.
453 CONTENTS is the quote-block contents. INFO is a plist used as
454 a communication channel."
455 (replace-regexp-in-string
457 (replace-regexp-in-string "\n\\'" "" contents
)))
462 (defun org-md-section (section contents info
)
463 "Transcode SECTION element into Markdown format.
464 CONTENTS is the section contents. INFO is a plist used as
465 a communication channel."
471 (defun org-md-inner-template (contents info
)
472 "Return body of document after converting it to Markdown syntax.
473 CONTENTS is the transcoded contents string. INFO is a plist
474 holding export options."
475 ;; Make sure CONTENTS is separated from table of contents and
476 ;; footnotes with at least a blank line.
477 (org-trim (org-html-inner-template (concat "\n" contents
"\n") info
)))
479 (defun org-md-template (contents info
)
480 "Return complete document string after Markdown conversion.
481 CONTENTS is the transcoded contents string. INFO is a plist used
482 as a communication channel."
487 ;;; Interactive function
490 (defun org-md-export-as-markdown (&optional async subtreep visible-only
)
491 "Export current buffer to a Markdown buffer.
493 If narrowing is active in the current buffer, only export its
496 If a region is active, export that region.
498 A non-nil optional argument ASYNC means the process should happen
499 asynchronously. The resulting buffer should be accessible
500 through the `org-export-stack' interface.
502 When optional argument SUBTREEP is non-nil, export the sub-tree
503 at point, extracting information from the headline properties
506 When optional argument VISIBLE-ONLY is non-nil, don't export
507 contents of hidden elements.
509 Export is done in a buffer named \"*Org MD Export*\", which will
510 be displayed when `org-export-show-temporary-export-buffer' is
513 (org-export-to-buffer 'md
"*Org MD Export*"
514 async subtreep visible-only nil nil
(lambda () (text-mode))))
517 (defun org-md-convert-region-to-md ()
518 "Assume the current region has org-mode syntax, and convert it to Markdown.
519 This can be used in any buffer. For example, you can write an
520 itemized list in org-mode syntax in a Markdown buffer and use
521 this command to convert it."
523 (org-export-replace-region-by 'md
))
527 (defun org-md-export-to-markdown (&optional async subtreep visible-only
)
528 "Export current buffer to a Markdown file.
530 If narrowing is active in the current buffer, only export its
533 If a region is active, export that region.
535 A non-nil optional argument ASYNC means the process should happen
536 asynchronously. The resulting file should be accessible through
537 the `org-export-stack' interface.
539 When optional argument SUBTREEP is non-nil, export the sub-tree
540 at point, extracting information from the headline properties
543 When optional argument VISIBLE-ONLY is non-nil, don't export
544 contents of hidden elements.
546 Return output file's name."
548 (let ((outfile (org-export-output-file-name ".md" subtreep
)))
549 (org-export-to-file 'md outfile async subtreep visible-only
)))
552 (defun org-md-publish-to-md (plist filename pub-dir
)
553 "Publish an org file to Markdown.
555 FILENAME is the filename of the Org file to be published. PLIST
556 is the property list for the given project. PUB-DIR is the
557 publishing directory.
559 Return output file name."
560 (org-publish-org-to 'md filename
".md" plist pub-dir
))
565 ;; generated-autoload-file: "org-loaddefs.el"
568 ;;; ox-md.el ends here