org-md: Also translate fixed-width areas
[org-mode.git] / contrib / lisp / org-md.el
blob770cbfc8b388bb286f4917ad9a2e4889f22dcfb3
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/>.
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 (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"
65 (lambda (a s v b)
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 (fixed-width . org-md-example-block)
72 (footnote-definition . ignore)
73 (footnote-reference . ignore)
74 (headline . org-md-headline)
75 (horizontal-rule . org-md-horizontal-rule)
76 (inline-src-block . org-md-verbatim)
77 (italic . org-md-italic)
78 (item . org-md-item)
79 (line-break . org-md-line-break)
80 (link . org-md-link)
81 (paragraph . org-md-paragraph)
82 (plain-list . org-md-plain-list)
83 (plain-text . org-md-plain-text)
84 (quote-block . org-md-quote-block)
85 (quote-section . org-md-example-block)
86 (section . org-md-section)
87 (src-block . org-md-example-block)
88 (template . org-md-template)
89 (verbatim . org-md-verbatim)))
93 ;;; Filters
95 (defun org-md-separate-elements (tree backend info)
96 "Make sure elements are separated by at least one blank line.
98 TREE is the parse tree being exported. BACKEND is the export
99 back-end used. INFO is a plist used as a communication channel.
101 Assume BACKEND is `md'."
102 (org-element-map
103 tree org-element-all-elements
104 (lambda (elem)
105 (unless (eq (org-element-type elem) 'org-data)
106 (org-element-put-property
107 elem :post-blank
108 (let ((post-blank (org-element-property :post-blank elem)))
109 (if (not post-blank) 1 (max 1 post-blank)))))))
110 ;; Return updated tree.
111 tree)
115 ;;; Transcode Functions
117 ;;;; Bold
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
131 channel."
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))
136 "`` %s ``")
137 (t "``%s``"))
138 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
146 channel."
147 (replace-regexp-in-string
148 "^" " "
149 (org-remove-indentation
150 (org-element-property :value example-block))))
153 ;;;; Headline
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
164 headline)))
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)))
168 (and tag-list
169 (format " :%s:"
170 (mapconcat 'identity tag-list ":"))))))
171 (priority
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)))
177 (cond
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)))
183 (let ((bullet
184 (if (not (org-export-numbered-headline-p headline info)) "-"
185 (concat (number-to-string
186 (car (last (org-export-get-headline-number
187 headline info))))
188 "."))))
189 (concat bullet (make-string (- 4 (length bullet)) ? ) heading tags
190 "\n\n"
191 (and contents
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) ?= ?-))
197 "\n\n"
198 contents))
199 ;; Use "atx" style.
200 (t (concat (make-string level ?#) " " heading tags "\n\n" contents))))))
203 ;;;; Horizontal Rule
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."
209 "---")
212 ;;;; Italic
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))
221 ;;;; Item
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)
233 struct
234 (org-list-prevs-alist struct)
235 (org-list-parents-alist struct)))))
236 "."))))
237 (concat bullet
238 (make-string (- 4 (length bullet)) ? )
239 (case (org-element-property :checkbox item)
240 (on "[X] ")
241 (trans "[-] ")
242 (off "[ ] "))
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)))))
248 ;;;; Line Break
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
253 channel."
254 " \n")
257 ;;;; Link
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
264 (function
265 (lambda (raw-path info)
266 ;; Treat links to `file.org' as links to `file.html', if
267 ;; needed. See `org-e-html-link-org-files-as-html'.
268 (cond
269 ((and org-e-html-link-org-files-as-html
270 (string= ".org"
271 (downcase (file-name-extension raw-path "."))))
272 (concat (file-name-sans-extension raw-path) "."
273 (plist-get info :html-extension)))
274 (t raw-path)))))
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
280 destination info)))
281 (if (not contents) (format "<%s>" path)
282 (format "[%s](%s)" contents path)))
283 (concat
284 (and contents (concat contents " "))
285 (format "(%s)"
286 (format (org-export-translate "See section %s" :html info)
287 (mapconcat 'number-to-string
288 (org-export-get-headline-number
289 destination info)
290 ".")))))))
291 ((org-export-inline-image-p link org-e-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)))))
295 (format "![%s](%s)"
296 (let ((caption (org-export-get-caption
297 (org-export-get-parent-element link))))
298 (when caption (org-export-data caption info)))
299 path)))
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
312 (when destination
313 (let ((number (org-export-get-ordinal destination info)))
314 (when number
315 (if (atom number) (number-to-string number)
316 (mapconcat 'number-to-string number ".")))))))))
317 (t (let* ((raw-path (org-element-property :path link))
318 (path (cond
319 ((member type '("http" "https" "ftp"))
320 (concat type ":" raw-path))
321 ((equal type "file")
322 ;; Treat links to ".org" files as ".html",
323 ;; if needed.
324 (setq raw-path
325 (funcall --link-org-files-as-html-maybe
326 raw-path info))
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))))
331 (t raw-path))))
332 (if (not contents) (format "<%s>" path)
333 (format "[%s](%s)" contents path)))))))
336 ;;;; Paragraph
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)
346 contents)))
349 ;;;; Plain List
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."
355 contents)
358 ;;;; Plain Text
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-e-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)))
380 ;; Return value.
381 text)
384 ;;;; Quote Block
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
391 "^" "> "
392 (replace-regexp-in-string "\n\\'" "" contents)))
395 ;;;; Section
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."
401 contents)
404 ;;;; Template
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."
410 contents)
414 ;;; Interactive function
416 ;;;###autoload
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
421 narrowed part.
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
431 first.
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
438 non-nil."
439 (interactive)
440 (if async
441 (org-export-async-start
442 (lambda (output)
443 (with-current-buffer (get-buffer-create "*Org MD Export*")
444 (erase-buffer)
445 (insert output)
446 (goto-char (point-min))
447 (text-mode)
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)))))
457 ;;;###autoload
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
462 narrowed part.
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
472 first.
474 When optional argument VISIBLE-ONLY is non-nil, don't export
475 contents of hidden elements.
477 Return output file's name."
478 (interactive)
479 (let ((outfile (org-export-output-file-name ".md" subtreep)))
480 (if async
481 (org-export-async-start
482 (lambda (f) (org-export-add-to-stack f 'md))
483 `(expand-file-name
484 (org-export-to-file 'md ,outfile ,subtreep ,visible-only)))
485 (org-export-to-file 'md outfile subtreep visible-only))))
488 (provide 'org-md)
489 ;;; org-md.el ends here