1 ;;; org-md.el --- Markdown Back-End for Org Export Engine
3 ;; Copyright (C) 2012, 2013 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou@gmail.com>
6 ;; Keywords: org, wp, tex
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
23 ;; This library implements a Markdown back-end (vanilla flavour) for
24 ;; Org exporter, based on `e-html'.
26 ;; It provides two commands for export, depending on the desired
27 ;; output: `org-md-export-as-markdown' (temporary buffer) and
28 ;; `org-md-export-to-markdown' ("md" file).
36 ;;; User-Configurable Variables
38 (defgroup org-export-md nil
39 "Options specific to Markdown export back-end."
44 (defcustom org-md-headline-style
'atx
45 "Style used to format headlines.
46 This variable can be set to either `atx' or `setext'."
49 (const :tag
"Use \"atx\" style" atx
)
50 (const :tag
"Use \"Setext\" style" setext
)))
56 (org-export-define-derived-backend md e-html
57 :export-block
("MD" "MARKDOWN")
58 :filters-alist
((:filter-parse-tree . org-md-separate-elements
))
60 (?m
"Export to Markdown"
61 ((?M
"To temporary buffer"
62 (lambda (a s v b
) (org-md-export-as-markdown a s v
)))
63 (?m
"To file" (lambda (a s v b
) (org-md-export-to-markdown a s v
)))
64 (?o
"To file and open"
66 (if a
(org-md-export-to-markdown t s v
)
67 (org-open-file (org-md-export-to-markdown nil s v
)))))))
68 :translate-alist
((bold . org-md-bold
)
69 (code . org-md-verbatim
)
70 (example-block . org-md-example-block
)
71 (footnote-definition . ignore
)
72 (footnote-reference . ignore
)
73 (headline . org-md-headline
)
74 (horizontal-rule . org-md-horizontal-rule
)
75 (inline-src-block . org-md-verbatim
)
76 (italic . org-md-italic
)
78 (line-break . org-md-line-break
)
80 (paragraph . org-md-paragraph
)
81 (plain-list . org-md-plain-list
)
82 (plain-text . org-md-plain-text
)
83 (quote-block . org-md-quote-block
)
84 (quote-section . org-md-example-block
)
85 (section . org-md-section
)
86 (src-block . org-md-example-block
)
87 (template . org-md-template
)
88 (verbatim . org-md-verbatim
)))
94 (defun org-md-separate-elements (tree backend info
)
95 "Make sure elements are separated by at least one blank line.
97 TREE is the parse tree being exported. BACKEND is the export
98 back-end used. INFO is a plist used as a communication channel.
100 Assume BACKEND is `md'."
102 tree org-element-all-elements
104 (unless (eq (org-element-type elem
) 'org-data
)
105 (org-element-put-property
107 (let ((post-blank (org-element-property :post-blank elem
)))
108 (if (not post-blank
) 1 (max 1 post-blank
)))))))
109 ;; Return updated tree.
114 ;;; Transcode Functions
118 (defun org-md-bold (bold contents info
)
119 "Transcode BOLD object into Markdown format.
120 CONTENTS is the text within bold markup. INFO is a plist used as
121 a communication channel."
122 (format "**%s**" contents
))
125 ;;;; Code and Verbatim
127 (defun org-md-verbatim (verbatim contents info
)
128 "Transcode VERBATIM object into Markdown format.
129 CONTENTS is nil. INFO is a plist used as a communication
131 (let ((value (org-element-property :value verbatim
)))
132 (format (cond ((not (string-match "`" value
)) "`%s`")
133 ((or (string-match "\\``" value
)
134 (string-match "`\\'" value
))
140 ;;;; Example Block and Src Block
142 (defun org-md-example-block (example-block contents info
)
143 "Transcode EXAMPLE-BLOCK element into Markdown format.
144 CONTENTS is nil. INFO is a plist used as a communication
146 (replace-regexp-in-string
148 (org-remove-indentation
149 (org-element-property :value example-block
))))
154 (defun org-md-headline (headline contents info
)
155 "Transcode HEADLINE element into Markdown format.
156 CONTENTS is the headline contents. INFO is a plist used as
157 a communication channel."
158 (unless (org-element-property :footnote-section-p headline
)
159 (let* ((level (org-export-get-relative-level headline info
))
160 (title (org-export-data (org-element-property :title headline
) info
))
161 (todo (and (plist-get info
:with-todo-keywords
)
162 (let ((todo (org-element-property :todo-keyword
164 (and todo
(concat (org-export-data todo info
) " ")))))
165 (tags (and (plist-get info
:with-tags
)
166 (let ((tag-list (org-export-get-tags headline info
)))
169 (mapconcat 'identity tag-list
":"))))))
171 (and (plist-get info
:with-priority
)
172 (let ((char (org-element-property :priority headline
)))
173 (and char
(format "[#%c] " char
)))))
174 ;; Headline text without tags.
175 (heading (concat todo priority title
)))
177 ;; Cannot create an headline. Fall-back to a list.
178 ((or (org-export-low-level-p headline info
)
179 (not (memq org-md-headline-style
'(atx setext
)))
180 (and (eq org-md-headline-style
'atx
) (> level
6))
181 (and (eq org-md-headline-style
'setext
) (> level
2)))
183 (if (not (org-export-numbered-headline-p headline info
)) "-"
184 (concat (number-to-string
185 (car (last (org-export-get-headline-number
188 (concat bullet
(make-string (- 4 (length bullet
)) ?
) heading tags
191 (replace-regexp-in-string "^" " " contents
)))))
192 ;; Use "Setext" style.
193 ((eq org-md-headline-style
'setext
)
194 (concat heading tags
"\n"
195 (make-string (length heading
) (if (= level
1) ?
= ?-
))
199 (t (concat (make-string level ?
#) " " heading tags
"\n\n" contents
))))))
204 (defun org-md-horizontal-rule (horizontal-rule contents info
)
205 "Transcode HORIZONTAL-RULE element into Markdown format.
206 CONTENTS is the horizontal rule contents. INFO is a plist used
207 as a communication channel."
213 (defun org-md-italic (italic contents info
)
214 "Transcode ITALIC object into Markdown format.
215 CONTENTS is the text within italic markup. INFO is a plist used
216 as a communication channel."
217 (format "*%s*" contents
))
222 (defun org-md-item (item contents info
)
223 "Transcode ITEM element into Markdown format.
224 CONTENTS is the item contents. INFO is a plist used as
225 a communication channel."
226 (let* ((type (org-element-property :type
(org-export-get-parent item
)))
227 (struct (org-element-property :structure item
))
228 (bullet (if (not (eq type
'ordered
)) "-"
229 (concat (number-to-string
230 (car (last (org-list-get-item-number
231 (org-element-property :begin item
)
233 (org-list-prevs-alist struct
)
234 (org-list-parents-alist struct
)))))
237 (make-string (- 4 (length bullet
)) ?
)
238 (case (org-element-property :checkbox item
)
242 (let ((tag (org-element-property :tag item
)))
243 (and tag
(format "**%s:** "(org-export-data tag info
))))
244 (org-trim (replace-regexp-in-string "^" " " contents
)))))
249 (defun org-md-line-break (line-break contents info
)
250 "Transcode LINE-BREAK object into Markdown format.
251 CONTENTS is nil. INFO is a plist used as a communication
258 (defun org-md-link (link contents info
)
259 "Transcode LINE-BREAK object into Markdown format.
260 CONTENTS is the link's description. INFO is a plist used as
261 a communication channel."
262 (let ((--link-org-files-as-html-maybe
264 (lambda (raw-path info
)
265 ;; Treat links to `file.org' as links to `file.html', if
266 ;; needed. See `org-e-html-link-org-files-as-html'.
268 ((and org-e-html-link-org-files-as-html
270 (downcase (file-name-extension raw-path
"."))))
271 (concat (file-name-sans-extension raw-path
) "."
272 (plist-get info
:html-extension
)))
274 (type (org-element-property :type link
)))
275 (cond ((member type
'("custom-id" "id"))
276 (let ((destination (org-export-resolve-id-link link info
)))
277 (if (stringp destination
) ; External file.
278 (let ((path (funcall --link-org-files-as-html-maybe
280 (if (not contents
) (format "<%s>" path
)
281 (format "[%s](%s)" contents path
)))
283 (and contents
(concat contents
" "))
285 (format (org-export-translate "See section %s" :html info
)
286 (mapconcat 'number-to-string
287 (org-export-get-headline-number
290 ((org-export-inline-image-p link org-e-html-inline-image-rules
)
291 (let ((path (let ((raw-path (org-element-property :path link
)))
292 (if (not (file-name-absolute-p raw-path
)) raw-path
293 (expand-file-name raw-path
)))))
295 (let ((caption (org-export-get-caption
296 (org-export-get-parent-element link
))))
297 (when caption
(org-export-data caption info
)))
299 ((string= type
"coderef")
300 (let ((ref (org-element-property :path link
)))
301 (format (org-export-get-coderef-format ref contents
)
302 (org-export-resolve-coderef ref info
))))
303 ((equal type
"radio")
304 (let ((destination (org-export-resolve-radio-link link info
)))
305 (org-export-data (org-element-contents destination
) info
)))
306 ((equal type
"fuzzy")
307 (let ((destination (org-export-resolve-fuzzy-link link info
)))
308 ;; Ignore invisible "#+TARGET: path".
309 (unless (eq (org-element-type destination
) 'keyword
)
310 (if (org-string-nw-p contents
) contents
312 (let ((number (org-export-get-ordinal destination info
)))
314 (if (atom number
) (number-to-string number
)
315 (mapconcat 'number-to-string number
".")))))))))
316 (t (let* ((raw-path (org-element-property :path link
))
318 ((member type
'("http" "https" "ftp"))
319 (concat type
":" raw-path
))
321 ;; Treat links to ".org" files as ".html",
324 (funcall --link-org-files-as-html-maybe
326 ;; If file path is absolute, prepend it
327 ;; with protocol component - "file://".
328 (if (not (file-name-absolute-p raw-path
)) raw-path
329 (concat "file://" (expand-file-name raw-path
))))
331 (if (not contents
) (format "<%s>" path
)
332 (format "[%s](%s)" contents path
)))))))
337 (defun org-md-paragraph (paragraph contents info
)
338 "Transcode PARAGRAPH element into Markdown format.
339 CONTENTS is the paragraph contents. INFO is a plist used as
340 a communication channel."
341 (let ((first-object (car (org-element-contents paragraph
))))
342 ;; If paragraph starts with a #, protect it.
343 (if (and (stringp first-object
) (string-match "\\`#" first-object
))
344 (replace-regexp-in-string "\\`#" "\\#" contents nil t
)
350 (defun org-md-plain-list (plain-list contents info
)
351 "Transcode PLAIN-LIST element into Markdown format.
352 CONTENTS is the plain-list contents. INFO is a plist used as
353 a communication channel."
359 (defun org-md-plain-text (text info
)
360 "Transcode a TEXT string into Markdown format.
361 TEXT is the string to transcode. INFO is a plist holding
362 contextual information."
363 (when (plist-get info
:with-smart-quotes
)
364 (setq text
(org-export-activate-smart-quotes text
:html info
)))
365 ;; Protect ambiguous #. This will protect # at the beginning of
366 ;; a line, but not at the beginning of a paragraph. See
367 ;; `org-md-paragraph'.
368 (setq text
(replace-regexp-in-string "\n#" "\n\\\\#" text
))
369 ;; Protect ambiguous !
370 (setq text
(replace-regexp-in-string "\\(!\\)\\[" "\\\\!" text nil nil
1))
371 ;; Protect `, *, _ and \
372 (setq text
(replace-regexp-in-string "[`*_\\]" "\\\\\\&" text
))
373 ;; Handle special strings, if required.
374 (when (plist-get info
:with-special-strings
)
375 (setq text
(org-e-html-convert-special-strings text
)))
376 ;; Handle break preservation, if required.
377 (when (plist-get info
:preserve-breaks
)
378 (setq text
(replace-regexp-in-string "[ \t]*\n" " \n" text
)))
385 (defun org-md-quote-block (quote-block contents info
)
386 "Transcode QUOTE-BLOCK element into Markdown format.
387 CONTENTS is the quote-block contents. INFO is a plist used as
388 a communication channel."
389 (replace-regexp-in-string
391 (replace-regexp-in-string "\n\\'" "" contents
)))
396 (defun org-md-section (section contents info
)
397 "Transcode SECTION element into Markdown format.
398 CONTENTS is the section contents. INFO is a plist used as
399 a communication channel."
405 (defun org-md-template (contents info
)
406 "Return complete document string after Markdown conversion.
407 CONTENTS is the transcoded contents string. INFO is a plist used
408 as a communication channel."
413 ;;; Interactive function
416 (defun org-md-export-as-markdown (&optional async subtreep visible-only
)
417 "Export current buffer to a text buffer.
419 If narrowing is active in the current buffer, only export its
422 If a region is active, export that region.
424 A non-nil optional argument ASYNC means the process should happen
425 asynchronously. The resulting buffer should be accessible
426 through the `org-export-stack' interface.
428 When optional argument SUBTREEP is non-nil, export the sub-tree
429 at point, extracting information from the headline properties
432 When optional argument VISIBLE-ONLY is non-nil, don't export
433 contents of hidden elements.
435 Export is done in a buffer named \"*Org MD Export*\", which will
436 be displayed when `org-export-show-temporary-export-buffer' is
440 (org-export-async-start
442 (with-current-buffer (get-buffer-create "*Org MD Export*")
445 (goto-char (point-min))
447 (org-export-add-to-stack (current-buffer) 'md
)))
448 `(org-export-as 'md
,subtreep
,visible-only
))
449 (let ((outbuf (org-export-to-buffer
450 'md
"*Org MD Export*" subtreep visible-only
)))
451 (with-current-buffer outbuf
(text-mode))
452 (when org-export-show-temporary-export-buffer
453 (switch-to-buffer-other-window outbuf
)))))
457 (defun org-md-export-to-markdown (&optional async subtreep visible-only
)
458 "Export current buffer to a Markdown file.
460 If narrowing is active in the current buffer, only export its
463 If a region is active, export that region.
465 A non-nil optional argument ASYNC means the process should happen
466 asynchronously. The resulting file should be accessible through
467 the `org-export-stack' interface.
469 When optional argument SUBTREEP is non-nil, export the sub-tree
470 at point, extracting information from the headline properties
473 When optional argument VISIBLE-ONLY is non-nil, don't export
474 contents of hidden elements.
476 Return output file's name."
478 (let ((outfile (org-export-output-file-name ".md" subtreep
)))
480 (org-export-async-start
481 (lambda (f) (org-export-add-to-stack f
'md
))
483 (org-export-to-file 'md
,outfile
,subtreep
,visible-only
)))
484 (org-export-to-file 'md outfile subtreep visible-only
))))
488 ;;; org-md.el ends here