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
)
208 (mapconcat 'number-to-string
209 (org-export-get-headline-number
210 headline info
) "-")))
212 ;; Headline text without tags.
213 (heading (concat todo priority title
))
214 (style (plist-get info
:md-headline-style
)))
216 ;; Cannot create a headline. Fall-back to a list.
217 ((or (org-export-low-level-p headline info
)
218 (not (memq style
'(atx setext
)))
219 (and (eq style
'atx
) (> level
6))
220 (and (eq style
'setext
) (> level
2)))
222 (if (not (org-export-numbered-headline-p headline info
)) "-"
223 (concat (number-to-string
224 (car (last (org-export-get-headline-number
227 (concat bullet
(make-string (- 4 (length bullet
)) ?
) heading tags
230 (replace-regexp-in-string "^" " " contents
)))))
231 ;; Use "Setext" style.
233 (concat heading tags anchor
"\n"
234 (make-string (length heading
) (if (= level
1) ?
= ?-
))
238 (t (concat (make-string level ?
#) " " heading tags anchor
"\n\n" contents
))))))
243 (defun org-md-horizontal-rule (horizontal-rule contents info
)
244 "Transcode HORIZONTAL-RULE element into Markdown format.
245 CONTENTS is the horizontal rule contents. INFO is a plist used
246 as a communication channel."
252 (defun org-md-italic (italic contents info
)
253 "Transcode ITALIC object into Markdown format.
254 CONTENTS is the text within italic markup. INFO is a plist used
255 as a communication channel."
256 (format "*%s*" contents
))
261 (defun org-md-item (item contents info
)
262 "Transcode ITEM element into Markdown format.
263 CONTENTS is the item contents. INFO is a plist used as
264 a communication channel."
265 (let* ((type (org-element-property :type
(org-export-get-parent item
)))
266 (struct (org-element-property :structure item
))
267 (bullet (if (not (eq type
'ordered
)) "-"
268 (concat (number-to-string
269 (car (last (org-list-get-item-number
270 (org-element-property :begin item
)
272 (org-list-prevs-alist struct
)
273 (org-list-parents-alist struct
)))))
276 (make-string (- 4 (length bullet
)) ?
)
277 (case (org-element-property :checkbox item
)
281 (let ((tag (org-element-property :tag item
)))
282 (and tag
(format "**%s:** "(org-export-data tag info
))))
284 (org-trim (replace-regexp-in-string "^" " " contents
))))))
290 (defun org-md-keyword (keyword contents info
)
291 "Transcode a KEYWORD element into Markdown format.
292 CONTENTS is nil. INFO is a plist used as a communication
294 (if (member (org-element-property :key keyword
) '("MARKDOWN" "MD"))
295 (org-element-property :value keyword
)
296 (org-export-with-backend 'html keyword contents info
)))
301 (defun org-md-line-break (line-break contents info
)
302 "Transcode LINE-BREAK object into Markdown format.
303 CONTENTS is nil. INFO is a plist used as a communication
310 (defun org-md-link (link contents info
)
311 "Transcode LINE-BREAK object into Markdown format.
312 CONTENTS is the link's description. INFO is a plist used as
313 a communication channel."
314 (let ((link-org-files-as-md
317 ;; Treat links to `file.org' as links to `file.md'.
318 (if (string= ".org" (downcase (file-name-extension raw-path
".")))
319 (concat (file-name-sans-extension raw-path
) ".md")
321 (type (org-element-property :type link
)))
323 ((member type
'("custom-id" "id"))
324 (let ((destination (org-export-resolve-id-link link info
)))
325 (if (stringp destination
) ; External file.
326 (let ((path (funcall link-org-files-as-md destination
)))
327 (if (not contents
) (format "<%s>" path
)
328 (format "[%s](%s)" contents path
)))
330 (and contents
(concat contents
" "))
332 (format (org-export-translate "See section %s" :html info
)
333 (mapconcat 'number-to-string
334 (org-export-get-headline-number
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 ;; Link type is handled by a special function.
361 ((let ((protocol (nth 2 (assoc type org-link-protocols
))))
362 (and (functionp protocol
)
364 (org-link-unescape (org-element-property :path link
))
367 (t (let* ((raw-path (org-element-property :path link
))
370 ((member type
'("http" "https" "ftp"))
371 (concat type
":" raw-path
))
372 ((string= type
"file")
373 (let ((path (funcall link-org-files-as-md raw-path
)))
374 (if (not (file-name-absolute-p path
)) path
375 ;; If file path is absolute, prepend it
376 ;; with "file:" component.
377 (concat "file:" path
))))
379 (if (not contents
) (format "<%s>" path
)
380 (format "[%s](%s)" contents path
)))))))
385 (defun org-md-node-property (node-property contents info
)
386 "Transcode a NODE-PROPERTY element into Markdown syntax.
387 CONTENTS is nil. INFO is a plist holding contextual
390 (org-element-property :key node-property
)
391 (let ((value (org-element-property :value node-property
)))
392 (if value
(concat " " value
) ""))))
397 (defun org-md-paragraph (paragraph contents info
)
398 "Transcode PARAGRAPH element into Markdown format.
399 CONTENTS is the paragraph contents. INFO is a plist used as
400 a communication channel."
401 (let ((first-object (car (org-element-contents paragraph
))))
402 ;; If paragraph starts with a #, protect it.
403 (if (and (stringp first-object
) (string-match "\\`#" first-object
))
404 (replace-regexp-in-string "\\`#" "\\#" contents nil t
)
410 (defun org-md-plain-list (plain-list contents info
)
411 "Transcode PLAIN-LIST element into Markdown format.
412 CONTENTS is the plain-list contents. INFO is a plist used as
413 a communication channel."
419 (defun org-md-plain-text (text info
)
420 "Transcode a TEXT string into Markdown format.
421 TEXT is the string to transcode. INFO is a plist holding
422 contextual information."
423 (when (plist-get info
:with-smart-quotes
)
424 (setq text
(org-export-activate-smart-quotes text
:html info
)))
425 ;; Protect ambiguous #. This will protect # at the beginning of
426 ;; a line, but not at the beginning of a paragraph. See
427 ;; `org-md-paragraph'.
428 (setq text
(replace-regexp-in-string "\n#" "\n\\\\#" text
))
429 ;; Protect ambiguous !
430 (setq text
(replace-regexp-in-string "\\(!\\)\\[" "\\\\!" text nil nil
1))
431 ;; Protect `, *, _ and \
432 (setq text
(replace-regexp-in-string "[`*_\\]" "\\\\\\&" text
))
433 ;; Handle special strings, if required.
434 (when (plist-get info
:with-special-strings
)
435 (setq text
(org-html-convert-special-strings text
)))
436 ;; Handle break preservation, if required.
437 (when (plist-get info
:preserve-breaks
)
438 (setq text
(replace-regexp-in-string "[ \t]*\n" " \n" text
)))
445 (defun org-md-property-drawer (property-drawer contents info
)
446 "Transcode a PROPERTY-DRAWER element into Markdown format.
447 CONTENTS holds the contents of the drawer. INFO is a plist
448 holding contextual information."
449 (and (org-string-nw-p contents
)
450 (replace-regexp-in-string "^" " " contents
)))
455 (defun org-md-quote-block (quote-block contents info
)
456 "Transcode QUOTE-BLOCK element into Markdown format.
457 CONTENTS is the quote-block contents. INFO is a plist used as
458 a communication channel."
459 (replace-regexp-in-string
461 (replace-regexp-in-string "\n\\'" "" contents
)))
466 (defun org-md-section (section contents info
)
467 "Transcode SECTION element into Markdown format.
468 CONTENTS is the section contents. INFO is a plist used as
469 a communication channel."
475 (defun org-md-inner-template (contents info
)
476 "Return body of document after converting it to Markdown syntax.
477 CONTENTS is the transcoded contents string. INFO is a plist
478 holding export options."
479 ;; Make sure CONTENTS is separated from table of contents and
480 ;; footnotes with at least a blank line.
481 (org-trim (org-html-inner-template (concat "\n" contents
"\n") info
)))
483 (defun org-md-template (contents info
)
484 "Return complete document string after Markdown conversion.
485 CONTENTS is the transcoded contents string. INFO is a plist used
486 as a communication channel."
491 ;;; Interactive function
494 (defun org-md-export-as-markdown (&optional async subtreep visible-only
)
495 "Export current buffer to a Markdown buffer.
497 If narrowing is active in the current buffer, only export its
500 If a region is active, export that region.
502 A non-nil optional argument ASYNC means the process should happen
503 asynchronously. The resulting buffer should be accessible
504 through the `org-export-stack' interface.
506 When optional argument SUBTREEP is non-nil, export the sub-tree
507 at point, extracting information from the headline properties
510 When optional argument VISIBLE-ONLY is non-nil, don't export
511 contents of hidden elements.
513 Export is done in a buffer named \"*Org MD Export*\", which will
514 be displayed when `org-export-show-temporary-export-buffer' is
517 (org-export-to-buffer 'md
"*Org MD Export*"
518 async subtreep visible-only nil nil
(lambda () (text-mode))))
521 (defun org-md-convert-region-to-md ()
522 "Assume the current region has org-mode syntax, and convert it to Markdown.
523 This can be used in any buffer. For example, you can write an
524 itemized list in org-mode syntax in a Markdown buffer and use
525 this command to convert it."
527 (org-export-replace-region-by 'md
))
531 (defun org-md-export-to-markdown (&optional async subtreep visible-only
)
532 "Export current buffer to a Markdown file.
534 If narrowing is active in the current buffer, only export its
537 If a region is active, export that region.
539 A non-nil optional argument ASYNC means the process should happen
540 asynchronously. The resulting file should be accessible through
541 the `org-export-stack' interface.
543 When optional argument SUBTREEP is non-nil, export the sub-tree
544 at point, extracting information from the headline properties
547 When optional argument VISIBLE-ONLY is non-nil, don't export
548 contents of hidden elements.
550 Return output file's name."
552 (let ((outfile (org-export-output-file-name ".md" subtreep
)))
553 (org-export-to-file 'md outfile async subtreep visible-only
)))
556 (defun org-md-publish-to-md (plist filename pub-dir
)
557 "Publish an org file to Markdown.
559 FILENAME is the filename of the Org file to be published. PLIST
560 is the property list for the given project. PUB-DIR is the
561 publishing directory.
563 Return output file name."
564 (org-publish-org-to 'md filename
".md" plist pub-dir
))
569 ;; generated-autoload-file: "org-loaddefs.el"
572 ;;; ox-md.el ends here