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 (fixed-width . org-md-example-block
)
75 (footnote-definition . ignore
)
76 (footnote-reference . ignore
)
77 (headline . org-md-headline
)
78 (horizontal-rule . org-md-horizontal-rule
)
79 (inline-src-block . org-md-verbatim
)
80 (inner-template . org-md-inner-template
)
81 (italic . org-md-italic
)
83 (keyword . org-md-keyword
)
84 (line-break . org-md-line-break
)
86 (node-property . org-md-node-property
)
87 (paragraph . org-md-paragraph
)
88 (plain-list . org-md-plain-list
)
89 (plain-text . org-md-plain-text
)
90 (property-drawer . org-md-property-drawer
)
91 (quote-block . org-md-quote-block
)
92 (section . org-md-section
)
93 (src-block . org-md-example-block
)
94 (template . org-md-template
)
95 (verbatim . org-md-verbatim
))
96 :options-alist
'((:md-headline-style nil nil org-md-headline-style
)))
101 (defun org-md-separate-elements (tree backend info
)
102 "Fix blank lines between elements.
104 TREE is the parse tree being exported. BACKEND is the export
105 back-end used. INFO is a plist used as a communication channel.
107 Make sure there's no blank line before a plain list, unless it is
108 located right after a paragraph. Otherwise, add a blank line
109 between elements. Blank lines between items are preserved.
111 Assume BACKEND is `md'."
112 (org-element-map tree
(remq 'item org-element-all-elements
)
114 (org-element-put-property
116 (if (and (eq (org-element-type (org-export-get-next-element elem info
))
118 (not (and (eq (org-element-type elem
) 'paragraph
)
119 (org-export-get-previous-element elem info
))))
122 ;; Return updated tree.
127 ;;; Transcode Functions
131 (defun org-md-bold (bold contents info
)
132 "Transcode BOLD object into Markdown format.
133 CONTENTS is the text within bold markup. INFO is a plist used as
134 a communication channel."
135 (format "**%s**" contents
))
138 ;;;; Code and Verbatim
140 (defun org-md-verbatim (verbatim contents info
)
141 "Transcode VERBATIM object into Markdown format.
142 CONTENTS is nil. INFO is a plist used as a communication
144 (let ((value (org-element-property :value verbatim
)))
145 (format (cond ((not (string-match "`" value
)) "`%s`")
146 ((or (string-match "\\``" value
)
147 (string-match "`\\'" value
))
153 ;;;; Example Block and Src Block
155 (defun org-md-example-block (example-block contents info
)
156 "Transcode EXAMPLE-BLOCK element into Markdown format.
157 CONTENTS is nil. INFO is a plist used as a communication
159 (replace-regexp-in-string
161 (org-remove-indentation
162 (org-export-format-code-default example-block info
))))
167 (defun org-md-headline (headline contents info
)
168 "Transcode HEADLINE element into Markdown format.
169 CONTENTS is the headline contents. INFO is a plist used as
170 a communication channel."
171 (unless (org-element-property :footnote-section-p headline
)
172 (let* ((level (org-export-get-relative-level headline info
))
173 (title (org-export-data (org-element-property :title headline
) info
))
174 (todo (and (plist-get info
:with-todo-keywords
)
175 (let ((todo (org-element-property :todo-keyword
177 (and todo
(concat (org-export-data todo info
) " ")))))
178 (tags (and (plist-get info
:with-tags
)
179 (let ((tag-list (org-export-get-tags headline info
)))
182 (mapconcat 'identity tag-list
":"))))))
184 (and (plist-get info
:with-priority
)
185 (let ((char (org-element-property :priority headline
)))
186 (and char
(format "[#%c] " char
)))))
188 (when (plist-get info
:with-toc
)
190 (or (org-element-property :CUSTOM_ID headline
)
192 (mapconcat 'number-to-string
193 (org-export-get-headline-number
194 headline info
) "-")))
196 ;; Headline text without tags.
197 (heading (concat todo priority title
))
198 (style (plist-get info
:md-headline-style
)))
200 ;; Cannot create a headline. Fall-back to a list.
201 ((or (org-export-low-level-p headline info
)
202 (not (memq style
'(atx setext
)))
203 (and (eq style
'atx
) (> level
6))
204 (and (eq style
'setext
) (> level
2)))
206 (if (not (org-export-numbered-headline-p headline info
)) "-"
207 (concat (number-to-string
208 (car (last (org-export-get-headline-number
211 (concat bullet
(make-string (- 4 (length bullet
)) ?
) heading tags
214 (replace-regexp-in-string "^" " " contents
)))))
215 ;; Use "Setext" style.
217 (concat heading tags anchor
"\n"
218 (make-string (length heading
) (if (= level
1) ?
= ?-
))
222 (t (concat (make-string level ?
#) " " heading tags anchor
"\n\n" contents
))))))
227 (defun org-md-horizontal-rule (horizontal-rule contents info
)
228 "Transcode HORIZONTAL-RULE element into Markdown format.
229 CONTENTS is the horizontal rule contents. INFO is a plist used
230 as a communication channel."
236 (defun org-md-italic (italic contents info
)
237 "Transcode ITALIC object into Markdown format.
238 CONTENTS is the text within italic markup. INFO is a plist used
239 as a communication channel."
240 (format "*%s*" contents
))
245 (defun org-md-item (item contents info
)
246 "Transcode ITEM element into Markdown format.
247 CONTENTS is the item contents. INFO is a plist used as
248 a communication channel."
249 (let* ((type (org-element-property :type
(org-export-get-parent item
)))
250 (struct (org-element-property :structure item
))
251 (bullet (if (not (eq type
'ordered
)) "-"
252 (concat (number-to-string
253 (car (last (org-list-get-item-number
254 (org-element-property :begin item
)
256 (org-list-prevs-alist struct
)
257 (org-list-parents-alist struct
)))))
260 (make-string (- 4 (length bullet
)) ?
)
261 (case (org-element-property :checkbox item
)
265 (let ((tag (org-element-property :tag item
)))
266 (and tag
(format "**%s:** "(org-export-data tag info
))))
268 (org-trim (replace-regexp-in-string "^" " " contents
))))))
274 (defun org-md-keyword (keyword contents info
)
275 "Transcode a KEYWORD element into Markdown format.
276 CONTENTS is nil. INFO is a plist used as a communication
278 (if (member (org-element-property :key keyword
) '("MARKDOWN" "MD"))
279 (org-element-property :value keyword
)
280 (org-export-with-backend 'html keyword contents info
)))
285 (defun org-md-line-break (line-break contents info
)
286 "Transcode LINE-BREAK object into Markdown format.
287 CONTENTS is nil. INFO is a plist used as a communication
294 (defun org-md-link (link contents info
)
295 "Transcode LINE-BREAK object into Markdown format.
296 CONTENTS is the link's description. INFO is a plist used as
297 a communication channel."
298 (let ((link-org-files-as-md
301 ;; Treat links to `file.org' as links to `file.md'.
302 (if (string= ".org" (downcase (file-name-extension raw-path
".")))
303 (concat (file-name-sans-extension raw-path
) ".md")
305 (type (org-element-property :type link
)))
306 (cond ((member type
'("custom-id" "id"))
307 (let ((destination (org-export-resolve-id-link link info
)))
308 (if (stringp destination
) ; External file.
309 (let ((path (funcall link-org-files-as-md destination
)))
310 (if (not contents
) (format "<%s>" path
)
311 (format "[%s](%s)" contents path
)))
313 (and contents
(concat contents
" "))
315 (format (org-export-translate "See section %s" :html info
)
316 (mapconcat 'number-to-string
317 (org-export-get-headline-number
320 ((org-export-inline-image-p link org-html-inline-image-rules
)
321 (let ((path (let ((raw-path (org-element-property :path link
)))
322 (if (not (file-name-absolute-p raw-path
)) raw-path
323 (expand-file-name raw-path
))))
324 (caption (org-export-data
325 (org-export-get-caption
326 (org-export-get-parent-element link
)) info
)))
328 (if (not (org-string-nw-p caption
)) path
329 (format "%s \"%s\"" path caption
)))))
330 ((string= type
"coderef")
331 (let ((ref (org-element-property :path link
)))
332 (format (org-export-get-coderef-format ref contents
)
333 (org-export-resolve-coderef ref info
))))
334 ((equal type
"radio") contents
)
335 ((equal type
"fuzzy")
336 (let ((destination (org-export-resolve-fuzzy-link link info
)))
337 (if (org-string-nw-p contents
) contents
339 (let ((number (org-export-get-ordinal destination info
)))
341 (if (atom number
) (number-to-string number
)
342 (mapconcat 'number-to-string number
"."))))))))
343 (t (let* ((raw-path (org-element-property :path link
))
346 ((member type
'("http" "https" "ftp"))
347 (concat type
":" raw-path
))
348 ((string= type
"file")
349 (let ((path (funcall link-org-files-as-md raw-path
)))
350 (if (not (file-name-absolute-p path
)) path
351 ;; If file path is absolute, prepend it
352 ;; with "file:" component.
353 (concat "file:" path
))))
355 (if (not contents
) (format "<%s>" path
)
356 (format "[%s](%s)" contents path
)))))))
361 (defun org-md-node-property (node-property contents info
)
362 "Transcode a NODE-PROPERTY element into Markdown syntax.
363 CONTENTS is nil. INFO is a plist holding contextual
366 (org-element-property :key node-property
)
367 (let ((value (org-element-property :value node-property
)))
368 (if value
(concat " " value
) ""))))
373 (defun org-md-paragraph (paragraph contents info
)
374 "Transcode PARAGRAPH element into Markdown format.
375 CONTENTS is the paragraph contents. INFO is a plist used as
376 a communication channel."
377 (let ((first-object (car (org-element-contents paragraph
))))
378 ;; If paragraph starts with a #, protect it.
379 (if (and (stringp first-object
) (string-match "\\`#" first-object
))
380 (replace-regexp-in-string "\\`#" "\\#" contents nil t
)
386 (defun org-md-plain-list (plain-list contents info
)
387 "Transcode PLAIN-LIST element into Markdown format.
388 CONTENTS is the plain-list contents. INFO is a plist used as
389 a communication channel."
395 (defun org-md-plain-text (text info
)
396 "Transcode a TEXT string into Markdown format.
397 TEXT is the string to transcode. INFO is a plist holding
398 contextual information."
399 (when (plist-get info
:with-smart-quotes
)
400 (setq text
(org-export-activate-smart-quotes text
:html info
)))
401 ;; Protect ambiguous #. This will protect # at the beginning of
402 ;; a line, but not at the beginning of a paragraph. See
403 ;; `org-md-paragraph'.
404 (setq text
(replace-regexp-in-string "\n#" "\n\\\\#" text
))
405 ;; Protect ambiguous !
406 (setq text
(replace-regexp-in-string "\\(!\\)\\[" "\\\\!" text nil nil
1))
407 ;; Protect `, *, _ and \
408 (setq text
(replace-regexp-in-string "[`*_\\]" "\\\\\\&" text
))
409 ;; Handle special strings, if required.
410 (when (plist-get info
:with-special-strings
)
411 (setq text
(org-html-convert-special-strings text
)))
412 ;; Handle break preservation, if required.
413 (when (plist-get info
:preserve-breaks
)
414 (setq text
(replace-regexp-in-string "[ \t]*\n" " \n" text
)))
421 (defun org-md-property-drawer (property-drawer contents info
)
422 "Transcode a PROPERTY-DRAWER element into Markdown format.
423 CONTENTS holds the contents of the drawer. INFO is a plist
424 holding contextual information."
425 (and (org-string-nw-p contents
)
426 (replace-regexp-in-string "^" " " contents
)))
431 (defun org-md-quote-block (quote-block contents info
)
432 "Transcode QUOTE-BLOCK element into Markdown format.
433 CONTENTS is the quote-block contents. INFO is a plist used as
434 a communication channel."
435 (replace-regexp-in-string
437 (replace-regexp-in-string "\n\\'" "" contents
)))
442 (defun org-md-section (section contents info
)
443 "Transcode SECTION element into Markdown format.
444 CONTENTS is the section contents. INFO is a plist used as
445 a communication channel."
451 (defun org-md-inner-template (contents info
)
452 "Return body of document after converting it to Markdown syntax.
453 CONTENTS is the transcoded contents string. INFO is a plist
454 holding export options."
455 ;; Make sure CONTENTS is separated from table of contents and
456 ;; footnotes with at least a blank line.
457 (org-trim (org-html-inner-template (concat "\n" contents
"\n") info
)))
459 (defun org-md-template (contents info
)
460 "Return complete document string after Markdown conversion.
461 CONTENTS is the transcoded contents string. INFO is a plist used
462 as a communication channel."
467 ;;; Interactive function
470 (defun org-md-export-as-markdown (&optional async subtreep visible-only
)
471 "Export current buffer to a Markdown buffer.
473 If narrowing is active in the current buffer, only export its
476 If a region is active, export that region.
478 A non-nil optional argument ASYNC means the process should happen
479 asynchronously. The resulting buffer should be accessible
480 through the `org-export-stack' interface.
482 When optional argument SUBTREEP is non-nil, export the sub-tree
483 at point, extracting information from the headline properties
486 When optional argument VISIBLE-ONLY is non-nil, don't export
487 contents of hidden elements.
489 Export is done in a buffer named \"*Org MD Export*\", which will
490 be displayed when `org-export-show-temporary-export-buffer' is
493 (org-export-to-buffer 'md
"*Org MD Export*"
494 async subtreep visible-only nil nil
(lambda () (text-mode))))
497 (defun org-md-convert-region-to-md ()
498 "Assume the current region has org-mode syntax, and convert it to Markdown.
499 This can be used in any buffer. For example, you can write an
500 itemized list in org-mode syntax in a Markdown buffer and use
501 this command to convert it."
503 (org-export-replace-region-by 'md
))
507 (defun org-md-export-to-markdown (&optional async subtreep visible-only
)
508 "Export current buffer to a Markdown file.
510 If narrowing is active in the current buffer, only export its
513 If a region is active, export that region.
515 A non-nil optional argument ASYNC means the process should happen
516 asynchronously. The resulting file should be accessible through
517 the `org-export-stack' interface.
519 When optional argument SUBTREEP is non-nil, export the sub-tree
520 at point, extracting information from the headline properties
523 When optional argument VISIBLE-ONLY is non-nil, don't export
524 contents of hidden elements.
526 Return output file's name."
528 (let ((outfile (org-export-output-file-name ".md" subtreep
)))
529 (org-export-to-file 'md outfile async subtreep visible-only
)))
532 (defun org-md-publish-to-md (plist filename pub-dir
)
533 "Publish an org file to Markdown.
535 FILENAME is the filename of the Org file to be published. PLIST
536 is the property list for the given project. PUB-DIR is the
537 publishing directory.
539 Return output file name."
540 (org-publish-org-to 'md filename
".md" plist pub-dir
))
545 ;; generated-autoload-file: "org-loaddefs.el"
548 ;;; ox-md.el ends here