reading tables from lisp code block output
[org-mode.git] / lisp / ox-md.el
blob1114c50ef28cef752c5263cfd0ffe8e7fc2e8fe6
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, markdown
8 ;; GNU Emacs 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 ;; GNU Emacs 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 GNU Emacs. 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 `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).
30 ;;; Code:
32 (eval-when-compile (require 'cl))
33 (require 'ox-html)
34 (require 'ox-publish)
37 ;;; User-Configurable Variables
39 (defgroup org-export-md nil
40 "Options specific to Markdown export back-end."
41 :tag "Org Markdown"
42 :group 'org-export
43 :version "24.4"
44 :package-version '(Org . "8.0"))
46 (defcustom org-md-headline-style 'atx
47 "Style used to format headlines.
48 This variable can be set to either `atx' or `setext'."
49 :group 'org-export-md
50 :type '(choice
51 (const :tag "Use \"atx\" style" atx)
52 (const :tag "Use \"Setext\" style" setext)))
56 ;;; Define Back-End
58 (org-export-define-derived-backend 'md 'html
59 :export-block '("MD" "MARKDOWN")
60 :filters-alist '((:filter-parse-tree . org-md-separate-elements))
61 :menu-entry
62 '(?m "Export to Markdown"
63 ((?M "To temporary buffer"
64 (lambda (a s v b) (org-md-export-as-markdown a s v)))
65 (?m "To file" (lambda (a s v b) (org-md-export-to-markdown a s v)))
66 (?o "To file and open"
67 (lambda (a s v b)
68 (if a (org-md-export-to-markdown t s v)
69 (org-open-file (org-md-export-to-markdown nil s v)))))))
70 :translate-alist '((bold . org-md-bold)
71 (code . org-md-verbatim)
72 (comment . (lambda (&rest args) ""))
73 (comment-block . (lambda (&rest args) ""))
74 (example-block . org-md-example-block)
75 (fixed-width . org-md-example-block)
76 (footnote-definition . ignore)
77 (footnote-reference . ignore)
78 (headline . org-md-headline)
79 (horizontal-rule . org-md-horizontal-rule)
80 (inline-src-block . org-md-verbatim)
81 (italic . org-md-italic)
82 (item . org-md-item)
83 (line-break . org-md-line-break)
84 (link . org-md-link)
85 (node-property . org-md-node-property)
86 (paragraph . org-md-paragraph)
87 (plain-list . org-md-plain-list)
88 (plain-text . org-md-plain-text)
89 (property-drawer . org-md-property-drawer)
90 (quote-block . org-md-quote-block)
91 (quote-section . org-md-example-block)
92 (section . org-md-section)
93 (src-block . org-md-example-block)
94 (template . org-md-template)
95 (verbatim . org-md-verbatim)))
99 ;;; Filters
101 (defun org-md-separate-elements (tree backend info)
102 "Make sure elements are separated by at least one blank line.
104 TREE is the parse tree being exported. BACKEND is the export
105 back-end used. INFO is a plist used as a communication channel.
107 Assume BACKEND is `md'."
108 (org-element-map tree org-element-all-elements
109 (lambda (elem)
110 (unless (eq (org-element-type elem) 'org-data)
111 (org-element-put-property
112 elem :post-blank
113 (let ((post-blank (org-element-property :post-blank elem)))
114 (if (not post-blank) 1 (max 1 post-blank)))))))
115 ;; Return updated tree.
116 tree)
120 ;;; Transcode Functions
122 ;;;; Bold
124 (defun org-md-bold (bold contents info)
125 "Transcode BOLD object into Markdown format.
126 CONTENTS is the text within bold markup. INFO is a plist used as
127 a communication channel."
128 (format "**%s**" contents))
131 ;;;; Code and Verbatim
133 (defun org-md-verbatim (verbatim contents info)
134 "Transcode VERBATIM object into Markdown format.
135 CONTENTS is nil. INFO is a plist used as a communication
136 channel."
137 (let ((value (org-element-property :value verbatim)))
138 (format (cond ((not (string-match "`" value)) "`%s`")
139 ((or (string-match "\\``" value)
140 (string-match "`\\'" value))
141 "`` %s ``")
142 (t "``%s``"))
143 value)))
146 ;;;; Example Block and Src Block
148 (defun org-md-example-block (example-block contents info)
149 "Transcode EXAMPLE-BLOCK element into Markdown format.
150 CONTENTS is nil. INFO is a plist used as a communication
151 channel."
152 (replace-regexp-in-string
153 "^" " "
154 (org-remove-indentation
155 (org-element-property :value example-block))))
158 ;;;; Headline
160 (defun org-md-headline (headline contents info)
161 "Transcode HEADLINE element into Markdown format.
162 CONTENTS is the headline contents. INFO is a plist used as
163 a communication channel."
164 (unless (org-element-property :footnote-section-p headline)
165 (let* ((level (org-export-get-relative-level headline info))
166 (title (org-export-data (org-element-property :title headline) info))
167 (todo (and (plist-get info :with-todo-keywords)
168 (let ((todo (org-element-property :todo-keyword
169 headline)))
170 (and todo (concat (org-export-data todo info) " ")))))
171 (tags (and (plist-get info :with-tags)
172 (let ((tag-list (org-export-get-tags headline info)))
173 (and tag-list
174 (format " :%s:"
175 (mapconcat 'identity tag-list ":"))))))
176 (priority
177 (and (plist-get info :with-priority)
178 (let ((char (org-element-property :priority headline)))
179 (and char (format "[#%c] " char)))))
180 ;; Headline text without tags.
181 (heading (concat todo priority title)))
182 (cond
183 ;; Cannot create a headline. Fall-back to a list.
184 ((or (org-export-low-level-p headline info)
185 (not (memq org-md-headline-style '(atx setext)))
186 (and (eq org-md-headline-style 'atx) (> level 6))
187 (and (eq org-md-headline-style 'setext) (> level 2)))
188 (let ((bullet
189 (if (not (org-export-numbered-headline-p headline info)) "-"
190 (concat (number-to-string
191 (car (last (org-export-get-headline-number
192 headline info))))
193 "."))))
194 (concat bullet (make-string (- 4 (length bullet)) ? ) heading tags
195 "\n\n"
196 (and contents
197 (replace-regexp-in-string "^" " " contents)))))
198 ;; Use "Setext" style.
199 ((eq org-md-headline-style 'setext)
200 (concat heading tags "\n"
201 (make-string (length heading) (if (= level 1) ?= ?-))
202 "\n\n"
203 contents))
204 ;; Use "atx" style.
205 (t (concat (make-string level ?#) " " heading tags "\n\n" contents))))))
208 ;;;; Horizontal Rule
210 (defun org-md-horizontal-rule (horizontal-rule contents info)
211 "Transcode HORIZONTAL-RULE element into Markdown format.
212 CONTENTS is the horizontal rule contents. INFO is a plist used
213 as a communication channel."
214 "---")
217 ;;;; Italic
219 (defun org-md-italic (italic contents info)
220 "Transcode ITALIC object into Markdown format.
221 CONTENTS is the text within italic markup. INFO is a plist used
222 as a communication channel."
223 (format "*%s*" contents))
226 ;;;; Item
228 (defun org-md-item (item contents info)
229 "Transcode ITEM element into Markdown format.
230 CONTENTS is the item contents. INFO is a plist used as
231 a communication channel."
232 (let* ((type (org-element-property :type (org-export-get-parent item)))
233 (struct (org-element-property :structure item))
234 (bullet (if (not (eq type 'ordered)) "-"
235 (concat (number-to-string
236 (car (last (org-list-get-item-number
237 (org-element-property :begin item)
238 struct
239 (org-list-prevs-alist struct)
240 (org-list-parents-alist struct)))))
241 "."))))
242 (concat bullet
243 (make-string (- 4 (length bullet)) ? )
244 (case (org-element-property :checkbox item)
245 (on "[X] ")
246 (trans "[-] ")
247 (off "[ ] "))
248 (let ((tag (org-element-property :tag item)))
249 (and tag (format "**%s:** "(org-export-data tag info))))
250 (org-trim (replace-regexp-in-string "^" " " contents)))))
253 ;;;; Line Break
255 (defun org-md-line-break (line-break contents info)
256 "Transcode LINE-BREAK object into Markdown format.
257 CONTENTS is nil. INFO is a plist used as a communication
258 channel."
259 " \n")
262 ;;;; Link
264 (defun org-md-link (link contents info)
265 "Transcode LINE-BREAK object into Markdown format.
266 CONTENTS is the link's description. INFO is a plist used as
267 a communication channel."
268 (let ((--link-org-files-as-html-maybe
269 (function
270 (lambda (raw-path info)
271 ;; Treat links to `file.org' as links to `file.html', if
272 ;; needed. See `org-html-link-org-files-as-html'.
273 (cond
274 ((and org-html-link-org-files-as-html
275 (string= ".org"
276 (downcase (file-name-extension raw-path "."))))
277 (concat (file-name-sans-extension raw-path) "."
278 (plist-get info :html-extension)))
279 (t raw-path)))))
280 (type (org-element-property :type link)))
281 (cond ((member type '("custom-id" "id"))
282 (let ((destination (org-export-resolve-id-link link info)))
283 (if (stringp destination) ; External file.
284 (let ((path (funcall --link-org-files-as-html-maybe
285 destination info)))
286 (if (not contents) (format "<%s>" path)
287 (format "[%s](%s)" contents path)))
288 (concat
289 (and contents (concat contents " "))
290 (format "(%s)"
291 (format (org-export-translate "See section %s" :html info)
292 (mapconcat 'number-to-string
293 (org-export-get-headline-number
294 destination info)
295 ".")))))))
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 (format "![%s](%s)"
301 (let ((caption (org-export-get-caption
302 (org-export-get-parent-element link))))
303 (when caption (org-export-data caption info)))
304 path)))
305 ((string= type "coderef")
306 (let ((ref (org-element-property :path link)))
307 (format (org-export-get-coderef-format ref contents)
308 (org-export-resolve-coderef ref info))))
309 ((equal type "radio")
310 (let ((destination (org-export-resolve-radio-link link info)))
311 (org-export-data (org-element-contents destination) info)))
312 ((equal type "fuzzy")
313 (let ((destination (org-export-resolve-fuzzy-link link info)))
314 (if (org-string-nw-p contents) contents
315 (when destination
316 (let ((number (org-export-get-ordinal destination info)))
317 (when number
318 (if (atom number) (number-to-string number)
319 (mapconcat 'number-to-string number "."))))))))
320 (t (let* ((raw-path (org-element-property :path link))
321 (path (cond
322 ((member type '("http" "https" "ftp"))
323 (concat type ":" raw-path))
324 ((equal type "file")
325 ;; Treat links to ".org" files as ".html",
326 ;; if needed.
327 (setq raw-path
328 (funcall --link-org-files-as-html-maybe
329 raw-path info))
330 ;; If file path is absolute, prepend it
331 ;; with protocol component - "file://".
332 (if (not (file-name-absolute-p raw-path)) raw-path
333 (concat "file://" (expand-file-name raw-path))))
334 (t raw-path))))
335 (if (not contents) (format "<%s>" path)
336 (format "[%s](%s)" contents path)))))))
339 ;;;; Node Property
341 (defun org-md-node-property (node-property contents info)
342 "Transcode a NODE-PROPERTY element into Markdown syntax.
343 CONTENTS is nil. INFO is a plist holding contextual
344 information."
345 (format "%s:%s"
346 (org-element-property :key node-property)
347 (let ((value (org-element-property :value node-property)))
348 (if value (concat " " value) ""))))
351 ;;;; Paragraph
353 (defun org-md-paragraph (paragraph contents info)
354 "Transcode PARAGRAPH element into Markdown format.
355 CONTENTS is the paragraph contents. INFO is a plist used as
356 a communication channel."
357 (let ((first-object (car (org-element-contents paragraph))))
358 ;; If paragraph starts with a #, protect it.
359 (if (and (stringp first-object) (string-match "\\`#" first-object))
360 (replace-regexp-in-string "\\`#" "\\#" contents nil t)
361 contents)))
364 ;;;; Plain List
366 (defun org-md-plain-list (plain-list contents info)
367 "Transcode PLAIN-LIST element into Markdown format.
368 CONTENTS is the plain-list contents. INFO is a plist used as
369 a communication channel."
370 contents)
373 ;;;; Plain Text
375 (defun org-md-plain-text (text info)
376 "Transcode a TEXT string into Markdown format.
377 TEXT is the string to transcode. INFO is a plist holding
378 contextual information."
379 (when (plist-get info :with-smart-quotes)
380 (setq text (org-export-activate-smart-quotes text :html info)))
381 ;; Protect ambiguous #. This will protect # at the beginning of
382 ;; a line, but not at the beginning of a paragraph. See
383 ;; `org-md-paragraph'.
384 (setq text (replace-regexp-in-string "\n#" "\n\\\\#" text))
385 ;; Protect ambiguous !
386 (setq text (replace-regexp-in-string "\\(!\\)\\[" "\\\\!" text nil nil 1))
387 ;; Protect `, *, _ and \
388 (setq text (replace-regexp-in-string "[`*_\\]" "\\\\\\&" text))
389 ;; Handle special strings, if required.
390 (when (plist-get info :with-special-strings)
391 (setq text (org-html-convert-special-strings text)))
392 ;; Handle break preservation, if required.
393 (when (plist-get info :preserve-breaks)
394 (setq text (replace-regexp-in-string "[ \t]*\n" " \n" text)))
395 ;; Return value.
396 text)
399 ;;;; Property Drawer
401 (defun org-md-property-drawer (property-drawer contents info)
402 "Transcode a PROPERTY-DRAWER element into Markdown format.
403 CONTENTS holds the contents of the drawer. INFO is a plist
404 holding contextual information."
405 (and (org-string-nw-p contents)
406 (replace-regexp-in-string "^" " " contents)))
409 ;;;; Quote Block
411 (defun org-md-quote-block (quote-block contents info)
412 "Transcode QUOTE-BLOCK element into Markdown format.
413 CONTENTS is the quote-block contents. INFO is a plist used as
414 a communication channel."
415 (replace-regexp-in-string
416 "^" "> "
417 (replace-regexp-in-string "\n\\'" "" contents)))
420 ;;;; Section
422 (defun org-md-section (section contents info)
423 "Transcode SECTION element into Markdown format.
424 CONTENTS is the section contents. INFO is a plist used as
425 a communication channel."
426 contents)
429 ;;;; Template
431 (defun org-md-template (contents info)
432 "Return complete document string after Markdown conversion.
433 CONTENTS is the transcoded contents string. INFO is a plist used
434 as a communication channel."
435 contents)
439 ;;; Interactive function
441 ;;;###autoload
442 (defun org-md-export-as-markdown (&optional async subtreep visible-only)
443 "Export current buffer to a Markdown buffer.
445 If narrowing is active in the current buffer, only export its
446 narrowed part.
448 If a region is active, export that region.
450 A non-nil optional argument ASYNC means the process should happen
451 asynchronously. The resulting buffer should be accessible
452 through the `org-export-stack' interface.
454 When optional argument SUBTREEP is non-nil, export the sub-tree
455 at point, extracting information from the headline properties
456 first.
458 When optional argument VISIBLE-ONLY is non-nil, don't export
459 contents of hidden elements.
461 Export is done in a buffer named \"*Org MD Export*\", which will
462 be displayed when `org-export-show-temporary-export-buffer' is
463 non-nil."
464 (interactive)
465 (org-export-to-buffer 'md "*Org MD Export*"
466 async subtreep visible-only nil nil (lambda () (text-mode))))
468 ;;;###autoload
469 (defun org-md-convert-region-to-md ()
470 "Assume the current region has org-mode syntax, and convert it to Markdown.
471 This can be used in any buffer. For example, you can write an
472 itemized list in org-mode syntax in a Markdown buffer and use
473 this command to convert it."
474 (interactive)
475 (org-export-replace-region-by 'md))
478 ;;;###autoload
479 (defun org-md-export-to-markdown (&optional async subtreep visible-only)
480 "Export current buffer to a Markdown file.
482 If narrowing is active in the current buffer, only export its
483 narrowed part.
485 If a region is active, export that region.
487 A non-nil optional argument ASYNC means the process should happen
488 asynchronously. The resulting file should be accessible through
489 the `org-export-stack' interface.
491 When optional argument SUBTREEP is non-nil, export the sub-tree
492 at point, extracting information from the headline properties
493 first.
495 When optional argument VISIBLE-ONLY is non-nil, don't export
496 contents of hidden elements.
498 Return output file's name."
499 (interactive)
500 (let ((outfile (org-export-output-file-name ".md" subtreep)))
501 (org-export-to-file 'md outfile async subtreep visible-only)))
503 ;;;###autoload
504 (defun org-md-publish-to-md (plist filename pub-dir)
505 "Publish an org file to Markdown.
507 FILENAME is the filename of the Org file to be published. PLIST
508 is the property list for the given project. PUB-DIR is the
509 publishing directory.
511 Return output file name."
512 (org-publish-org-to 'md filename ".md" plist pub-dir))
514 (provide 'ox-md)
516 ;; Local variables:
517 ;; generated-autoload-file: "org-loaddefs.el"
518 ;; End:
520 ;;; ox-md.el ends here