export back-ends: Apply line break changes
[org-mode.git] / contrib / lisp / org-md.el
blobd2bb7fc7942a940e47d1c5884a9bb612f9fb461c
1 ;;; org-md.el --- Markdown Back-End for Org Export Engine
3 ;; Copyright (C) 2012 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/>.
21 ;;; Commentary:
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).
30 ;;; Code:
32 (require 'org-e-html)
36 ;;; User-Configurable Variables
38 (defgroup org-export-md nil
39 "Options specific to Markdown export back-end."
40 :tag "Org Markdown"
41 :group 'org-export
42 :version "24.2")
44 (defcustom org-md-headline-style 'atx
45 "Style used to format headlines.
46 This variable can be set to either `atx' or `setext'."
47 :group 'org-export-md
48 :type '(choice
49 (const :tag "Use \"atx\" style" atx)
50 (const :tag "Use \"Setext\" style" setext)))
54 ;;; Define Back-End
56 (org-export-define-derived-backend md e-html
57 :export-block ("MD" "MARKDOWN")
58 :filters-alist ((:filter-parse-tree . org-md-separate-elements))
59 :menu-entry
60 (?m "Export to Markdown"
61 ((?M "To temporary buffer"
62 (lambda (s v b) (org-md-export-as-markdown s v)))
63 (?m "To file" (lambda (s v b) (org-md-export-to-markdown s v)))
64 (?o "To file and open"
65 (lambda (s v b) (org-open-file (org-md-export-to-markdown s v))))))
66 :translate-alist ((bold . org-md-bold)
67 (code . org-md-verbatim)
68 (example-block . org-md-example-block)
69 (footnote-definition . ignore)
70 (footnote-reference . ignore)
71 (headline . org-md-headline)
72 (horizontal-rule . org-md-horizontal-rule)
73 (inline-src-block . org-md-verbatim)
74 (italic . org-md-italic)
75 (item . org-md-item)
76 (line-break . org-md-line-break)
77 (link . org-md-link)
78 (paragraph . org-md-paragraph)
79 (plain-list . org-md-plain-list)
80 (plain-text . org-md-plain-text)
81 (quote-block . org-md-quote-block)
82 (quote-section . org-md-example-block)
83 (section . org-md-section)
84 (src-block . org-md-example-block)
85 (template . org-md-template)
86 (verbatim . org-md-verbatim)))
90 ;;; Filters
92 (defun org-md-separate-elements (tree backend info)
93 "Make sure elements are separated by at least one blank line.
95 TREE is the parse tree being exported. BACKEND is the export
96 back-end used. INFO is a plist used as a communication channel.
98 Assume BACKEND is `md'."
99 (org-element-map
100 tree org-element-all-elements
101 (lambda (elem)
102 (unless (eq (org-element-type elem) 'org-data)
103 (org-element-put-property
104 elem :post-blank
105 (let ((post-blank (org-element-property :post-blank elem)))
106 (if (not post-blank) 1 (max 1 post-blank)))))))
107 ;; Return updated tree.
108 tree)
112 ;;; Transcode Functions
114 ;;;; Bold
116 (defun org-md-bold (bold contents info)
117 "Transcode BOLD object into Markdown format.
118 CONTENTS is the text within bold markup. INFO is a plist used as
119 a communication channel."
120 (format "**%s**" contents))
123 ;;;; Code and Verbatim
125 (defun org-md-verbatim (verbatim contents info)
126 "Transcode VERBATIM object into Markdown format.
127 CONTENTS is nil. INFO is a plist used as a communication
128 channel."
129 (let ((value (org-element-property :value verbatim)))
130 (format (cond ((not (string-match "`" value)) "`%s`")
131 ((or (string-match "\\``" value)
132 (string-match "`\\'" value))
133 "`` %s ``")
134 (t "``%s``"))
135 value)))
138 ;;;; Example Block and Src Block
140 (defun org-md-example-block (example-block contents info)
141 "Transcode EXAMPLE-BLOCK element into Markdown format.
142 CONTENTS is nil. INFO is a plist used as a communication
143 channel."
144 (replace-regexp-in-string
145 "^" " "
146 (org-remove-indentation
147 (org-element-property :value example-block))))
150 ;;;; Headline
152 (defun org-md-headline (headline contents info)
153 "Transcode HEADLINE element into Markdown format.
154 CONTENTS is the headline contents. INFO is a plist used as
155 a communication channel."
156 (unless (org-element-property :footnote-section-p headline)
157 (let* ((level (org-export-get-relative-level headline info))
158 (title (org-export-data (org-element-property :title headline) info))
159 (todo (and (plist-get info :with-todo-keywords)
160 (let ((todo (org-element-property :todo-keyword
161 headline)))
162 (and todo (concat (org-export-data todo info) " ")))))
163 (tags (and (plist-get info :with-tags)
164 (let ((tag-list (org-export-get-tags headline info)))
165 (and tag-list
166 (format " :%s:"
167 (mapconcat 'identity tag-list ":"))))))
168 (priority
169 (and (plist-get info :with-priority)
170 (let ((char (org-element-property :priority headline)))
171 (and char (format "[#%c] " char)))))
172 ;; Headline text without tags.
173 (heading (concat todo priority title)))
174 (cond
175 ;; Cannot create an headline. Fall-back to a list.
176 ((or (org-export-low-level-p headline info)
177 (not (memq org-md-headline-style '(atx setext)))
178 (and (eq org-md-headline-style 'atx) (> level 6))
179 (and (eq org-md-headline-style 'setext) (> level 2)))
180 (let ((bullet
181 (if (not (org-export-numbered-headline-p headline info)) "-"
182 (concat (number-to-string
183 (car (last (org-export-get-headline-number
184 headline info))))
185 "."))))
186 (concat bullet (make-string (- 4 (length bullet)) ? ) heading tags
187 "\n\n"
188 (and contents
189 (replace-regexp-in-string "^" " " contents)))))
190 ;; Use "Setext" style.
191 ((eq org-md-headline-style 'setext)
192 (concat heading tags "\n"
193 (make-string (length heading) (if (= level 1) ?= ?-))
194 "\n\n"
195 contents))
196 ;; Use "atx" style.
197 (t (concat (make-string level ?#) " " heading tags "\n\n" contents))))))
200 ;;;; Horizontal Rule
202 (defun org-md-horizontal-rule (horizontal-rule contents info)
203 "Transcode HORIZONTAL-RULE element into Markdown format.
204 CONTENTS is the horizontal rule contents. INFO is a plist used
205 as a communication channel."
206 "---")
209 ;;;; Italic
211 (defun org-md-italic (italic contents info)
212 "Transcode ITALIC object into Markdown format.
213 CONTENTS is the text within italic markup. INFO is a plist used
214 as a communication channel."
215 (format "*%s*" contents))
218 ;;;; Item
220 (defun org-md-item (item contents info)
221 "Transcode ITEM element into Markdown format.
222 CONTENTS is the item contents. INFO is a plist used as
223 a communication channel."
224 (let* ((type (org-element-property :type (org-export-get-parent item)))
225 (struct (org-element-property :structure item))
226 (bullet (if (not (eq type 'ordered)) "-"
227 (concat (number-to-string
228 (car (last (org-list-get-item-number
229 (org-element-property :begin item)
230 struct
231 (org-list-prevs-alist struct)
232 (org-list-parents-alist struct)))))
233 "."))))
234 (concat bullet
235 (make-string (- 4 (length bullet)) ? )
236 (case (org-element-property :checkbox item)
237 (on "[X] ")
238 (trans "[-] ")
239 (off "[ ] "))
240 (let ((tag (org-element-property :tag item)))
241 (and tag (format "**%s:** "(org-export-data tag info))))
242 (org-trim (replace-regexp-in-string "^" " " contents)))))
245 ;;;; Line Break
247 (defun org-md-line-break (line-break contents info)
248 "Transcode LINE-BREAK object into Markdown format.
249 CONTENTS is nil. INFO is a plist used as a communication
250 channel."
251 " \n")
254 ;;;; Link
256 (defun org-md-link (link contents info)
257 "Transcode LINE-BREAK object into Markdown format.
258 CONTENTS is the link's description. INFO is a plist used as
259 a communication channel."
260 (let ((--link-org-files-as-html-maybe
261 (function
262 (lambda (raw-path info)
263 ;; Treat links to `file.org' as links to `file.html', if
264 ;; needed. See `org-e-html-link-org-files-as-html'.
265 (cond
266 ((and org-e-html-link-org-files-as-html
267 (string= ".org"
268 (downcase (file-name-extension raw-path "."))))
269 (concat (file-name-sans-extension raw-path) "."
270 (plist-get info :html-extension)))
271 (t raw-path)))))
272 (type (org-element-property :type link)))
273 (cond ((member type '("custom-id" "id"))
274 (let ((destination (org-export-resolve-id-link link info)))
275 (if (stringp destination) ; External file.
276 (let ((path (funcall --link-org-files-as-html-maybe
277 destination info)))
278 (if (not contents) (format "<%s>" path)
279 (format "[%s](%s)" contents path)))
280 (concat
281 (and contents (concat contents " "))
282 (format "(%s)"
283 (format (org-export-translate "See section %s" :html info)
284 (mapconcat 'number-to-string
285 (org-export-get-headline-number
286 destination info)
287 ".")))))))
288 ((org-export-inline-image-p link org-e-html-inline-image-rules)
289 (let ((path (let ((raw-path (org-element-property :path link)))
290 (if (not (file-name-absolute-p raw-path)) raw-path
291 (expand-file-name raw-path)))))
292 (format "![%s](%s)"
293 (let ((caption (org-export-get-caption
294 (org-export-get-parent-element link))))
295 (when caption (org-export-data caption info)))
296 path)))
297 ((string= type "coderef")
298 (let ((ref (org-element-property :path link)))
299 (format (org-export-get-coderef-format ref contents)
300 (org-export-resolve-coderef ref info))))
301 ((equal type "radio")
302 (let ((destination (org-export-resolve-radio-link link info)))
303 (org-export-data (org-element-contents destination) info)))
304 ((equal type "fuzzy")
305 (let ((destination (org-export-resolve-fuzzy-link link info)))
306 ;; Ignore invisible "#+TARGET: path".
307 (unless (eq (org-element-type destination) 'keyword)
308 (if (org-string-nw-p contents) contents
309 (when destination
310 (let ((number (org-export-get-ordinal destination info)))
311 (when number
312 (if (atom number) (number-to-string number)
313 (mapconcat 'number-to-string number ".")))))))))
314 (t (let* ((raw-path (org-element-property :path link))
315 (path (cond
316 ((member type '("http" "https" "ftp"))
317 (concat type ":" raw-path))
318 ((equal type "file")
319 ;; Treat links to ".org" files as ".html",
320 ;; if needed.
321 (setq raw-path
322 (funcall --link-org-files-as-html-maybe
323 raw-path info))
324 ;; If file path is absolute, prepend it
325 ;; with protocol component - "file://".
326 (if (not (file-name-absolute-p raw-path)) raw-path
327 (concat "file://" (expand-file-name raw-path))))
328 (t raw-path))))
329 (if (not contents) (format "<%s>" path)
330 (format "[%s](%s)" contents path)))))))
333 ;;;; Paragraph
335 (defun org-md-paragraph (paragraph contents info)
336 "Transcode PARAGRAPH element into Markdown format.
337 CONTENTS is the paragraph contents. INFO is a plist used as
338 a communication channel."
339 (let ((first-object (car (org-element-contents paragraph))))
340 ;; If paragraph starts with a #, protect it.
341 (if (and (stringp first-object) (string-match "\\`#" first-object))
342 (replace-regexp-in-string "\\`#" "\\#" contents nil t)
343 contents)))
346 ;;;; Plain List
348 (defun org-md-plain-list (plain-list contents info)
349 "Transcode PLAIN-LIST element into Markdown format.
350 CONTENTS is the plain-list contents. INFO is a plist used as
351 a communication channel."
352 contents)
355 ;;;; Plain Text
357 (defun org-md-plain-text (text info)
358 "Transcode a TEXT string into Markdown format.
359 TEXT is the string to transcode. INFO is a plist holding
360 contextual information."
361 (when (plist-get info :with-smart-quotes)
362 (setq text (org-export-activate-smart-quotes text :html info)))
363 ;; Protect ambiguous #. This will protect # at the beginning of
364 ;; a line, but not at the beginning of a paragraph. See
365 ;; `org-md-paragraph'.
366 (setq text (replace-regexp-in-string "\n#" "\n\\\\#" text))
367 ;; Protect ambiguous !
368 (setq text (replace-regexp-in-string "\\(!\\)\\[" "\\\\!" text nil nil 1))
369 ;; Protect `, *, _ and \
370 (setq text (replace-regexp-in-string "[`*_\\]" "\\\\\\&" text))
371 ;; Handle special strings, if required.
372 (when (plist-get info :with-special-strings)
373 (setq text (org-e-html-convert-special-strings text)))
374 ;; Handle break preservation, if required.
375 (when (plist-get info :preserve-breaks)
376 (setq text (replace-regexp-in-string "[ \t]*\n" " \n" text)))
377 ;; Return value.
378 text)
381 ;;;; Quote Block
383 (defun org-md-quote-block (quote-block contents info)
384 "Transcode QUOTE-BLOCK element into Markdown format.
385 CONTENTS is the quote-block contents. INFO is a plist used as
386 a communication channel."
387 (replace-regexp-in-string
388 "^" "> "
389 (replace-regexp-in-string "\n\\'" "" contents)))
392 ;;;; Section
394 (defun org-md-section (section contents info)
395 "Transcode SECTION element into Markdown format.
396 CONTENTS is the section contents. INFO is a plist used as
397 a communication channel."
398 contents)
401 ;;;; Template
403 (defun org-md-template (contents info)
404 "Return complete document string after Markdown conversion.
405 CONTENTS is the transcoded contents string. INFO is a plist used
406 as a communication channel."
407 contents)
411 ;;; Interactive function
413 ;;;###autoload
414 (defun org-md-export-as-markdown (&optional subtreep visible-only)
415 "Export current buffer to a text buffer.
417 If narrowing is active in the current buffer, only export its
418 narrowed part.
420 If a region is active, export that region.
422 When optional argument SUBTREEP is non-nil, export the sub-tree
423 at point, extracting information from the headline properties
424 first.
426 When optional argument VISIBLE-ONLY is non-nil, don't export
427 contents of hidden elements.
429 Export is done in a buffer named \"*Org MD Export*\", which will
430 be displayed when `org-export-show-temporary-export-buffer' is
431 non-nil."
432 (interactive)
433 (let ((outbuf (org-export-to-buffer
434 'md "*Org MD Export*" subtreep visible-only)))
435 (with-current-buffer outbuf (text-mode))
436 (when org-export-show-temporary-export-buffer
437 (switch-to-buffer-other-window outbuf))))
440 ;;;###autoload
441 (defun org-md-export-to-markdown (&optional subtreep visible-only)
442 "Export current buffer to a Markdown file.
444 If narrowing is active in the current buffer, only export its
445 narrowed part.
447 If a region is active, export that region.
449 When optional argument SUBTREEP is non-nil, export the sub-tree
450 at point, extracting information from the headline properties
451 first.
453 When optional argument VISIBLE-ONLY is non-nil, don't export
454 contents of hidden elements.
456 Return output file's name."
457 (interactive)
458 (let ((outfile (org-export-output-file-name ".md" subtreep)))
459 (org-export-to-file 'md outfile subtreep visible-only)))
462 (provide 'org-md)
463 ;;; org-md.el ends here