1 ;;; ox-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 `html' back-end.
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).
32 (eval-when-compile (require 'cl
))
37 ;;; User-Configurable Variables
39 (defgroup org-export-md nil
40 "Options specific to Markdown export back-end."
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 (example-block . org-md-example-block
)
72 (fixed-width . org-md-example-block
)
73 (footnote-definition . ignore
)
74 (footnote-reference . ignore
)
75 (headline . org-md-headline
)
76 (horizontal-rule . org-md-horizontal-rule
)
77 (inline-src-block . org-md-verbatim
)
78 (italic . org-md-italic
)
80 (line-break . org-md-line-break
)
82 (paragraph . org-md-paragraph
)
83 (plain-list . org-md-plain-list
)
84 (plain-text . org-md-plain-text
)
85 (quote-block . org-md-quote-block
)
86 (quote-section . org-md-example-block
)
87 (section . org-md-section
)
88 (src-block . org-md-example-block
)
89 (template . org-md-template
)
90 (verbatim . org-md-verbatim
)))
96 (defun org-md-separate-elements (tree backend info
)
97 "Make sure elements are separated by at least one blank line.
99 TREE is the parse tree being exported. BACKEND is the export
100 back-end used. INFO is a plist used as a communication channel.
102 Assume BACKEND is `md'."
103 (org-element-map tree org-element-all-elements
105 (unless (eq (org-element-type elem
) 'org-data
)
106 (org-element-put-property
108 (let ((post-blank (org-element-property :post-blank elem
)))
109 (if (not post-blank
) 1 (max 1 post-blank
)))))))
110 ;; Return updated tree.
115 ;;; Transcode Functions
119 (defun org-md-bold (bold contents info
)
120 "Transcode BOLD object into Markdown format.
121 CONTENTS is the text within bold markup. INFO is a plist used as
122 a communication channel."
123 (format "**%s**" contents
))
126 ;;;; Code and Verbatim
128 (defun org-md-verbatim (verbatim contents info
)
129 "Transcode VERBATIM object into Markdown format.
130 CONTENTS is nil. INFO is a plist used as a communication
132 (let ((value (org-element-property :value verbatim
)))
133 (format (cond ((not (string-match "`" value
)) "`%s`")
134 ((or (string-match "\\``" value
)
135 (string-match "`\\'" value
))
141 ;;;; Example Block and Src Block
143 (defun org-md-example-block (example-block contents info
)
144 "Transcode EXAMPLE-BLOCK element into Markdown format.
145 CONTENTS is nil. INFO is a plist used as a communication
147 (replace-regexp-in-string
149 (org-remove-indentation
150 (org-element-property :value example-block
))))
155 (defun org-md-headline (headline contents info
)
156 "Transcode HEADLINE element into Markdown format.
157 CONTENTS is the headline contents. INFO is a plist used as
158 a communication channel."
159 (unless (org-element-property :footnote-section-p headline
)
160 (let* ((level (org-export-get-relative-level headline info
))
161 (title (org-export-data (org-element-property :title headline
) info
))
162 (todo (and (plist-get info
:with-todo-keywords
)
163 (let ((todo (org-element-property :todo-keyword
165 (and todo
(concat (org-export-data todo info
) " ")))))
166 (tags (and (plist-get info
:with-tags
)
167 (let ((tag-list (org-export-get-tags headline info
)))
170 (mapconcat 'identity tag-list
":"))))))
172 (and (plist-get info
:with-priority
)
173 (let ((char (org-element-property :priority headline
)))
174 (and char
(format "[#%c] " char
)))))
175 ;; Headline text without tags.
176 (heading (concat todo priority title
)))
178 ;; Cannot create an headline. Fall-back to a list.
179 ((or (org-export-low-level-p headline info
)
180 (not (memq org-md-headline-style
'(atx setext
)))
181 (and (eq org-md-headline-style
'atx
) (> level
6))
182 (and (eq org-md-headline-style
'setext
) (> level
2)))
184 (if (not (org-export-numbered-headline-p headline info
)) "-"
185 (concat (number-to-string
186 (car (last (org-export-get-headline-number
189 (concat bullet
(make-string (- 4 (length bullet
)) ?
) heading tags
192 (replace-regexp-in-string "^" " " contents
)))))
193 ;; Use "Setext" style.
194 ((eq org-md-headline-style
'setext
)
195 (concat heading tags
"\n"
196 (make-string (length heading
) (if (= level
1) ?
= ?-
))
200 (t (concat (make-string level ?
#) " " heading tags
"\n\n" contents
))))))
205 (defun org-md-horizontal-rule (horizontal-rule contents info
)
206 "Transcode HORIZONTAL-RULE element into Markdown format.
207 CONTENTS is the horizontal rule contents. INFO is a plist used
208 as a communication channel."
214 (defun org-md-italic (italic contents info
)
215 "Transcode ITALIC object into Markdown format.
216 CONTENTS is the text within italic markup. INFO is a plist used
217 as a communication channel."
218 (format "*%s*" contents
))
223 (defun org-md-item (item contents info
)
224 "Transcode ITEM element into Markdown format.
225 CONTENTS is the item contents. INFO is a plist used as
226 a communication channel."
227 (let* ((type (org-element-property :type
(org-export-get-parent item
)))
228 (struct (org-element-property :structure item
))
229 (bullet (if (not (eq type
'ordered
)) "-"
230 (concat (number-to-string
231 (car (last (org-list-get-item-number
232 (org-element-property :begin item
)
234 (org-list-prevs-alist struct
)
235 (org-list-parents-alist struct
)))))
238 (make-string (- 4 (length bullet
)) ?
)
239 (case (org-element-property :checkbox item
)
243 (let ((tag (org-element-property :tag item
)))
244 (and tag
(format "**%s:** "(org-export-data tag info
))))
245 (org-trim (replace-regexp-in-string "^" " " contents
)))))
250 (defun org-md-line-break (line-break contents info
)
251 "Transcode LINE-BREAK object into Markdown format.
252 CONTENTS is nil. INFO is a plist used as a communication
259 (defun org-md-link (link contents info
)
260 "Transcode LINE-BREAK object into Markdown format.
261 CONTENTS is the link's description. INFO is a plist used as
262 a communication channel."
263 (let ((--link-org-files-as-html-maybe
265 (lambda (raw-path info
)
266 ;; Treat links to `file.org' as links to `file.html', if
267 ;; needed. See `org-html-link-org-files-as-html'.
269 ((and org-html-link-org-files-as-html
271 (downcase (file-name-extension raw-path
"."))))
272 (concat (file-name-sans-extension raw-path
) "."
273 (plist-get info
:html-extension
)))
275 (type (org-element-property :type link
)))
276 (cond ((member type
'("custom-id" "id"))
277 (let ((destination (org-export-resolve-id-link link info
)))
278 (if (stringp destination
) ; External file.
279 (let ((path (funcall --link-org-files-as-html-maybe
281 (if (not contents
) (format "<%s>" path
)
282 (format "[%s](%s)" contents path
)))
284 (and contents
(concat contents
" "))
286 (format (org-export-translate "See section %s" :html info
)
287 (mapconcat 'number-to-string
288 (org-export-get-headline-number
291 ((org-export-inline-image-p link org-html-inline-image-rules
)
292 (let ((path (let ((raw-path (org-element-property :path link
)))
293 (if (not (file-name-absolute-p raw-path
)) raw-path
294 (expand-file-name raw-path
)))))
296 (let ((caption (org-export-get-caption
297 (org-export-get-parent-element link
))))
298 (when caption
(org-export-data caption info
)))
300 ((string= type
"coderef")
301 (let ((ref (org-element-property :path link
)))
302 (format (org-export-get-coderef-format ref contents
)
303 (org-export-resolve-coderef ref info
))))
304 ((equal type
"radio")
305 (let ((destination (org-export-resolve-radio-link link info
)))
306 (org-export-data (org-element-contents destination
) info
)))
307 ((equal type
"fuzzy")
308 (let ((destination (org-export-resolve-fuzzy-link link info
)))
309 ;; Ignore invisible "#+TARGET: path".
310 (unless (eq (org-element-type destination
) 'keyword
)
311 (if (org-string-nw-p contents
) contents
313 (let ((number (org-export-get-ordinal destination info
)))
315 (if (atom number
) (number-to-string number
)
316 (mapconcat 'number-to-string number
".")))))))))
317 (t (let* ((raw-path (org-element-property :path link
))
319 ((member type
'("http" "https" "ftp"))
320 (concat type
":" raw-path
))
322 ;; Treat links to ".org" files as ".html",
325 (funcall --link-org-files-as-html-maybe
327 ;; If file path is absolute, prepend it
328 ;; with protocol component - "file://".
329 (if (not (file-name-absolute-p raw-path
)) raw-path
330 (concat "file://" (expand-file-name raw-path
))))
332 (if (not contents
) (format "<%s>" path
)
333 (format "[%s](%s)" contents path
)))))))
338 (defun org-md-paragraph (paragraph contents info
)
339 "Transcode PARAGRAPH element into Markdown format.
340 CONTENTS is the paragraph contents. INFO is a plist used as
341 a communication channel."
342 (let ((first-object (car (org-element-contents paragraph
))))
343 ;; If paragraph starts with a #, protect it.
344 (if (and (stringp first-object
) (string-match "\\`#" first-object
))
345 (replace-regexp-in-string "\\`#" "\\#" contents nil t
)
351 (defun org-md-plain-list (plain-list contents info
)
352 "Transcode PLAIN-LIST element into Markdown format.
353 CONTENTS is the plain-list contents. INFO is a plist used as
354 a communication channel."
360 (defun org-md-plain-text (text info
)
361 "Transcode a TEXT string into Markdown format.
362 TEXT is the string to transcode. INFO is a plist holding
363 contextual information."
364 (when (plist-get info
:with-smart-quotes
)
365 (setq text
(org-export-activate-smart-quotes text
:html info
)))
366 ;; Protect ambiguous #. This will protect # at the beginning of
367 ;; a line, but not at the beginning of a paragraph. See
368 ;; `org-md-paragraph'.
369 (setq text
(replace-regexp-in-string "\n#" "\n\\\\#" text
))
370 ;; Protect ambiguous !
371 (setq text
(replace-regexp-in-string "\\(!\\)\\[" "\\\\!" text nil nil
1))
372 ;; Protect `, *, _ and \
373 (setq text
(replace-regexp-in-string "[`*_\\]" "\\\\\\&" text
))
374 ;; Handle special strings, if required.
375 (when (plist-get info
:with-special-strings
)
376 (setq text
(org-html-convert-special-strings text
)))
377 ;; Handle break preservation, if required.
378 (when (plist-get info
:preserve-breaks
)
379 (setq text
(replace-regexp-in-string "[ \t]*\n" " \n" text
)))
386 (defun org-md-quote-block (quote-block contents info
)
387 "Transcode QUOTE-BLOCK element into Markdown format.
388 CONTENTS is the quote-block contents. INFO is a plist used as
389 a communication channel."
390 (replace-regexp-in-string
392 (replace-regexp-in-string "\n\\'" "" contents
)))
397 (defun org-md-section (section contents info
)
398 "Transcode SECTION element into Markdown format.
399 CONTENTS is the section contents. INFO is a plist used as
400 a communication channel."
406 (defun org-md-template (contents info
)
407 "Return complete document string after Markdown conversion.
408 CONTENTS is the transcoded contents string. INFO is a plist used
409 as a communication channel."
414 ;;; Interactive function
417 (defun org-md-export-as-markdown (&optional async subtreep visible-only
)
418 "Export current buffer to a text buffer.
420 If narrowing is active in the current buffer, only export its
423 If a region is active, export that region.
425 A non-nil optional argument ASYNC means the process should happen
426 asynchronously. The resulting buffer should be accessible
427 through the `org-export-stack' interface.
429 When optional argument SUBTREEP is non-nil, export the sub-tree
430 at point, extracting information from the headline properties
433 When optional argument VISIBLE-ONLY is non-nil, don't export
434 contents of hidden elements.
436 Export is done in a buffer named \"*Org MD Export*\", which will
437 be displayed when `org-export-show-temporary-export-buffer' is
441 (org-export-async-start
443 (with-current-buffer (get-buffer-create "*Org MD Export*")
446 (goto-char (point-min))
448 (org-export-add-to-stack (current-buffer) 'md
)))
449 `(org-export-as 'md
,subtreep
,visible-only
))
450 (let ((outbuf (org-export-to-buffer
451 'md
"*Org MD Export*" subtreep visible-only
)))
452 (with-current-buffer outbuf
(text-mode))
453 (when org-export-show-temporary-export-buffer
454 (switch-to-buffer-other-window outbuf
)))))
458 (defun org-md-export-to-markdown (&optional async subtreep visible-only
)
459 "Export current buffer to a Markdown file.
461 If narrowing is active in the current buffer, only export its
464 If a region is active, export that region.
466 A non-nil optional argument ASYNC means the process should happen
467 asynchronously. The resulting file should be accessible through
468 the `org-export-stack' interface.
470 When optional argument SUBTREEP is non-nil, export the sub-tree
471 at point, extracting information from the headline properties
474 When optional argument VISIBLE-ONLY is non-nil, don't export
475 contents of hidden elements.
477 Return output file's name."
479 (let ((outfile (org-export-output-file-name ".md" subtreep
)))
481 (org-export-async-start
482 (lambda (f) (org-export-add-to-stack f
'md
))
484 (org-export-to-file 'md
,outfile
,subtreep
,visible-only
)))
485 (org-export-to-file 'md outfile subtreep visible-only
))))
491 ;; generated-autoload-file: "org-loaddefs.el"
494 ;;; ox-md.el ends here