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 (line-break . org-md-line-break
)
85 (paragraph . org-md-paragraph
)
86 (plain-list . org-md-plain-list
)
87 (plain-text . org-md-plain-text
)
88 (quote-block . org-md-quote-block
)
89 (quote-section . org-md-example-block
)
90 (section . org-md-section
)
91 (src-block . org-md-example-block
)
92 (template . org-md-template
)
93 (verbatim . org-md-verbatim
)))
99 (defun org-md-separate-elements (tree backend info
)
100 "Fix blank lines between elements.
102 TREE is the parse tree being exported. BACKEND is the export
103 back-end used. INFO is a plist used as a communication channel.
105 Make sure there's no blank line before a plain list, unless it is
106 located right after a paragraph. Otherwise, add a blank line
107 between elements. Blank lines between items are preserved.
109 Assume BACKEND is `md'."
110 (org-element-map tree
(remq 'item org-element-all-elements
)
112 (org-element-put-property
114 (if (and (eq (org-element-type (org-export-get-next-element elem info
))
116 (not (and (eq (org-element-type elem
) 'paragraph
)
117 (org-export-get-previous-element elem info
))))
120 ;; Return updated tree.
125 ;;; Transcode Functions
129 (defun org-md-bold (bold contents info
)
130 "Transcode BOLD object into Markdown format.
131 CONTENTS is the text within bold markup. INFO is a plist used as
132 a communication channel."
133 (format "**%s**" contents
))
136 ;;;; Code and Verbatim
138 (defun org-md-verbatim (verbatim contents info
)
139 "Transcode VERBATIM object into Markdown format.
140 CONTENTS is nil. INFO is a plist used as a communication
142 (let ((value (org-element-property :value verbatim
)))
143 (format (cond ((not (string-match "`" value
)) "`%s`")
144 ((or (string-match "\\``" value
)
145 (string-match "`\\'" value
))
151 ;;;; Example Block and Src Block
153 (defun org-md-example-block (example-block contents info
)
154 "Transcode EXAMPLE-BLOCK element into Markdown format.
155 CONTENTS is nil. INFO is a plist used as a communication
157 (replace-regexp-in-string
159 (org-remove-indentation
160 (org-element-property :value example-block
))))
165 (defun org-md-headline (headline contents info
)
166 "Transcode HEADLINE element into Markdown format.
167 CONTENTS is the headline contents. INFO is a plist used as
168 a communication channel."
169 (unless (org-element-property :footnote-section-p headline
)
170 (let* ((level (org-export-get-relative-level headline info
))
171 (title (org-export-data (org-element-property :title headline
) info
))
172 (todo (and (plist-get info
:with-todo-keywords
)
173 (let ((todo (org-element-property :todo-keyword
175 (and todo
(concat (org-export-data todo info
) " ")))))
176 (tags (and (plist-get info
:with-tags
)
177 (let ((tag-list (org-export-get-tags headline info
)))
180 (mapconcat 'identity tag-list
":"))))))
182 (and (plist-get info
:with-priority
)
183 (let ((char (org-element-property :priority headline
)))
184 (and char
(format "[#%c] " char
)))))
185 ;; Headline text without tags.
186 (heading (concat todo priority title
)))
188 ;; Cannot create a headline. Fall-back to a list.
189 ((or (org-export-low-level-p headline info
)
190 (not (memq org-md-headline-style
'(atx setext
)))
191 (and (eq org-md-headline-style
'atx
) (> level
6))
192 (and (eq org-md-headline-style
'setext
) (> level
2)))
194 (if (not (org-export-numbered-headline-p headline info
)) "-"
195 (concat (number-to-string
196 (car (last (org-export-get-headline-number
199 (concat bullet
(make-string (- 4 (length bullet
)) ?
) heading tags
202 (replace-regexp-in-string "^" " " contents
)))))
203 ;; Use "Setext" style.
204 ((eq org-md-headline-style
'setext
)
205 (concat heading tags
"\n"
206 (make-string (length heading
) (if (= level
1) ?
= ?-
))
210 (t (concat (make-string level ?
#) " " heading tags
"\n\n" contents
))))))
215 (defun org-md-horizontal-rule (horizontal-rule contents info
)
216 "Transcode HORIZONTAL-RULE element into Markdown format.
217 CONTENTS is the horizontal rule contents. INFO is a plist used
218 as a communication channel."
224 (defun org-md-italic (italic contents info
)
225 "Transcode ITALIC object into Markdown format.
226 CONTENTS is the text within italic markup. INFO is a plist used
227 as a communication channel."
228 (format "*%s*" contents
))
233 (defun org-md-item (item contents info
)
234 "Transcode ITEM element into Markdown format.
235 CONTENTS is the item contents. INFO is a plist used as
236 a communication channel."
237 (let* ((type (org-element-property :type
(org-export-get-parent item
)))
238 (struct (org-element-property :structure item
))
239 (bullet (if (not (eq type
'ordered
)) "-"
240 (concat (number-to-string
241 (car (last (org-list-get-item-number
242 (org-element-property :begin item
)
244 (org-list-prevs-alist struct
)
245 (org-list-parents-alist struct
)))))
248 (make-string (- 4 (length bullet
)) ?
)
249 (case (org-element-property :checkbox item
)
253 (let ((tag (org-element-property :tag item
)))
254 (and tag
(format "**%s:** "(org-export-data tag info
))))
256 (org-trim (replace-regexp-in-string "^" " " contents
))))))
261 (defun org-md-line-break (line-break contents info
)
262 "Transcode LINE-BREAK object into Markdown format.
263 CONTENTS is nil. INFO is a plist used as a communication
270 (defun org-md-link (link contents info
)
271 "Transcode LINE-BREAK object into Markdown format.
272 CONTENTS is the link's description. INFO is a plist used as
273 a communication channel."
274 (let ((link-org-files-as-md
277 ;; Treat links to `file.org' as links to `file.md'.
278 (if (string= ".org" (downcase (file-name-extension raw-path
".")))
279 (concat (file-name-sans-extension raw-path
) ".md")
281 (type (org-element-property :type link
)))
282 (cond ((member type
'("custom-id" "id"))
283 (let ((destination (org-export-resolve-id-link link info
)))
284 (if (stringp destination
) ; External file.
285 (let ((path (funcall link-org-files-as-md destination
)))
286 (if (not contents
) (format "<%s>" path
)
287 (format "[%s](%s)" contents path
)))
289 (and contents
(concat contents
" "))
291 (format (org-export-translate "See section %s" :html info
)
292 (mapconcat 'number-to-string
293 (org-export-get-headline-number
296 ((org-export-inline-image-p link org-html-inline-image-rules
)
297 (let ((path (let ((raw-path (org-element-property :path link
)))
298 (if (not (file-name-absolute-p raw-path
)) raw-path
299 (expand-file-name raw-path
))))
300 (caption (org-export-data
301 (org-export-get-caption
302 (org-export-get-parent-element link
)) info
)))
304 (if (not (org-string-nw-p caption
)) path
305 (format "%s \"%s\"" path caption
)))))
306 ((string= type
"coderef")
307 (let ((ref (org-element-property :path link
)))
308 (format (org-export-get-coderef-format ref contents
)
309 (org-export-resolve-coderef ref info
))))
310 ((equal type
"radio")
311 (let ((destination (org-export-resolve-radio-link link info
)))
312 (org-export-data (org-element-contents destination
) info
)))
313 ((equal type
"fuzzy")
314 (let ((destination (org-export-resolve-fuzzy-link link info
)))
315 (if (org-string-nw-p contents
) contents
317 (let ((number (org-export-get-ordinal destination info
)))
319 (if (atom number
) (number-to-string number
)
320 (mapconcat 'number-to-string number
"."))))))))
321 (t (let* ((raw-path (org-element-property :path link
))
324 ((member type
'("http" "https" "ftp"))
325 (concat type
":" raw-path
))
327 (let ((path (funcall link-org-files-as-md raw-path
)))
328 (if (not (file-name-absolute-p path
)) path
329 ;; If file path is absolute, prepend it
330 ;; with "file://" component.
331 (concat "file://" (expand-file-name raw-path
)))))
333 (if (not contents
) (format "<%s>" path
)
334 (format "[%s](%s)" contents path
)))))))
339 (defun org-md-paragraph (paragraph contents info
)
340 "Transcode PARAGRAPH element into Markdown format.
341 CONTENTS is the paragraph contents. INFO is a plist used as
342 a communication channel."
343 (let ((first-object (car (org-element-contents paragraph
))))
344 ;; If paragraph starts with a #, protect it.
345 (if (and (stringp first-object
) (string-match "\\`#" first-object
))
346 (replace-regexp-in-string "\\`#" "\\#" contents nil t
)
352 (defun org-md-plain-list (plain-list contents info
)
353 "Transcode PLAIN-LIST element into Markdown format.
354 CONTENTS is the plain-list contents. INFO is a plist used as
355 a communication channel."
361 (defun org-md-plain-text (text info
)
362 "Transcode a TEXT string into Markdown format.
363 TEXT is the string to transcode. INFO is a plist holding
364 contextual information."
365 (when (plist-get info
:with-smart-quotes
)
366 (setq text
(org-export-activate-smart-quotes text
:html info
)))
367 ;; Protect ambiguous #. This will protect # at the beginning of
368 ;; a line, but not at the beginning of a paragraph. See
369 ;; `org-md-paragraph'.
370 (setq text
(replace-regexp-in-string "\n#" "\n\\\\#" text
))
371 ;; Protect ambiguous !
372 (setq text
(replace-regexp-in-string "\\(!\\)\\[" "\\\\!" text nil nil
1))
373 ;; Protect `, *, _ and \
374 (setq text
(replace-regexp-in-string "[`*_\\]" "\\\\\\&" text
))
375 ;; Handle special strings, if required.
376 (when (plist-get info
:with-special-strings
)
377 (setq text
(org-html-convert-special-strings text
)))
378 ;; Handle break preservation, if required.
379 (when (plist-get info
:preserve-breaks
)
380 (setq text
(replace-regexp-in-string "[ \t]*\n" " \n" text
)))
387 (defun org-md-quote-block (quote-block contents info
)
388 "Transcode QUOTE-BLOCK element into Markdown format.
389 CONTENTS is the quote-block contents. INFO is a plist used as
390 a communication channel."
391 (replace-regexp-in-string
393 (replace-regexp-in-string "\n\\'" "" contents
)))
398 (defun org-md-section (section contents info
)
399 "Transcode SECTION element into Markdown format.
400 CONTENTS is the section contents. INFO is a plist used as
401 a communication channel."
407 (defun org-md-inner-template (contents info
)
408 "Return body of document after converting it to Markdown syntax.
409 CONTENTS is the transcoded contents string. INFO is a plist
410 holding export options."
411 ;; Make sure CONTENTS is separated from table of contents and
412 ;; footnotes with at least a blank line.
413 (org-trim (org-html-inner-template (concat "\n" contents
"\n") info
)))
415 (defun org-md-template (contents info
)
416 "Return complete document string after Markdown conversion.
417 CONTENTS is the transcoded contents string. INFO is a plist used
418 as a communication channel."
423 ;;; Interactive function
426 (defun org-md-export-as-markdown (&optional async subtreep visible-only
)
427 "Export current buffer to a Markdown buffer.
429 If narrowing is active in the current buffer, only export its
432 If a region is active, export that region.
434 A non-nil optional argument ASYNC means the process should happen
435 asynchronously. The resulting buffer should be accessible
436 through the `org-export-stack' interface.
438 When optional argument SUBTREEP is non-nil, export the sub-tree
439 at point, extracting information from the headline properties
442 When optional argument VISIBLE-ONLY is non-nil, don't export
443 contents of hidden elements.
445 Export is done in a buffer named \"*Org MD Export*\", which will
446 be displayed when `org-export-show-temporary-export-buffer' is
449 (org-export-to-buffer 'md
"*Org MD Export*"
450 async subtreep visible-only nil nil
(lambda () (text-mode))))
453 (defun org-md-convert-region-to-md ()
454 "Assume the current region has org-mode syntax, and convert it to Markdown.
455 This can be used in any buffer. For example, you can write an
456 itemized list in org-mode syntax in a Markdown buffer and use
457 this command to convert it."
459 (org-export-replace-region-by 'md
))
463 (defun org-md-export-to-markdown (&optional async subtreep visible-only
)
464 "Export current buffer to a Markdown file.
466 If narrowing is active in the current buffer, only export its
469 If a region is active, export that region.
471 A non-nil optional argument ASYNC means the process should happen
472 asynchronously. The resulting file should be accessible through
473 the `org-export-stack' interface.
475 When optional argument SUBTREEP is non-nil, export the sub-tree
476 at point, extracting information from the headline properties
479 When optional argument VISIBLE-ONLY is non-nil, don't export
480 contents of hidden elements.
482 Return output file's name."
484 (let ((outfile (org-export-output-file-name ".md" subtreep
)))
485 (org-export-to-file 'md outfile async subtreep visible-only
)))
491 ;; generated-autoload-file: "org-loaddefs.el"
494 ;;; ox-md.el ends here