ob-R: declare org-every
[org-mode.git] / lisp / ox-md.el
blobb082e82633159962cf7f156a774cbe165434d86f
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 ;; 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/>.
23 ;;; Commentary:
25 ;; This library implements a Markdown back-end (vanilla flavour) for
26 ;; Org exporter, based on `html' back-end. See Org manual for more
27 ;; information.
29 ;;; Code:
31 (eval-when-compile (require 'cl))
32 (require 'ox-html)
33 (require 'ox-publish)
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.4"
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'."
48 :group 'org-export-md
49 :type '(choice
50 (const :tag "Use \"atx\" style" atx)
51 (const :tag "Use \"Setext\" style" setext)))
55 ;;; Define Back-End
57 (org-export-define-derived-backend 'md 'html
58 :export-block '("MD" "MARKDOWN")
59 :filters-alist '((:filter-parse-tree . org-md-separate-elements))
60 :menu-entry
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"
66 (lambda (a s v b)
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 (italic . org-md-italic)
81 (item . org-md-item)
82 (line-break . org-md-line-break)
83 (link . org-md-link)
84 (node-property . org-md-node-property)
85 (paragraph . org-md-paragraph)
86 (plain-list . org-md-plain-list)
87 (plain-text . org-md-plain-text)
88 (property-drawer . org-md-property-drawer)
89 (quote-block . org-md-quote-block)
90 (quote-section . org-md-example-block)
91 (section . org-md-section)
92 (src-block . org-md-example-block)
93 (template . org-md-template)
94 (verbatim . org-md-verbatim)))
98 ;;; Filters
100 (defun org-md-separate-elements (tree backend info)
101 "Make sure elements are separated by at least one blank line.
103 TREE is the parse tree being exported. BACKEND is the export
104 back-end used. INFO is a plist used as a communication channel.
106 Assume BACKEND is `md'."
107 (org-element-map tree org-element-all-elements
108 (lambda (elem)
109 (unless (eq (org-element-type elem) 'org-data)
110 (org-element-put-property
111 elem :post-blank
112 (let ((post-blank (org-element-property :post-blank elem)))
113 (if (not post-blank) 1 (max 1 post-blank)))))))
114 ;; Return updated tree.
115 tree)
119 ;;; Transcode Functions
121 ;;;; Bold
123 (defun org-md-bold (bold contents info)
124 "Transcode BOLD object into Markdown format.
125 CONTENTS is the text within bold markup. INFO is a plist used as
126 a communication channel."
127 (format "**%s**" contents))
130 ;;;; Code and Verbatim
132 (defun org-md-verbatim (verbatim contents info)
133 "Transcode VERBATIM object into Markdown format.
134 CONTENTS is nil. INFO is a plist used as a communication
135 channel."
136 (let ((value (org-element-property :value verbatim)))
137 (format (cond ((not (string-match "`" value)) "`%s`")
138 ((or (string-match "\\``" value)
139 (string-match "`\\'" value))
140 "`` %s ``")
141 (t "``%s``"))
142 value)))
145 ;;;; Example Block and Src Block
147 (defun org-md-example-block (example-block contents info)
148 "Transcode EXAMPLE-BLOCK element into Markdown format.
149 CONTENTS is nil. INFO is a plist used as a communication
150 channel."
151 (replace-regexp-in-string
152 "^" " "
153 (org-remove-indentation
154 (org-element-property :value example-block))))
157 ;;;; Headline
159 (defun org-md-headline (headline contents info)
160 "Transcode HEADLINE element into Markdown format.
161 CONTENTS is the headline contents. INFO is a plist used as
162 a communication channel."
163 (unless (org-element-property :footnote-section-p headline)
164 (let* ((level (org-export-get-relative-level headline info))
165 (title (org-export-data (org-element-property :title headline) info))
166 (todo (and (plist-get info :with-todo-keywords)
167 (let ((todo (org-element-property :todo-keyword
168 headline)))
169 (and todo (concat (org-export-data todo info) " ")))))
170 (tags (and (plist-get info :with-tags)
171 (let ((tag-list (org-export-get-tags headline info)))
172 (and tag-list
173 (format " :%s:"
174 (mapconcat 'identity tag-list ":"))))))
175 (priority
176 (and (plist-get info :with-priority)
177 (let ((char (org-element-property :priority headline)))
178 (and char (format "[#%c] " char)))))
179 ;; Headline text without tags.
180 (heading (concat todo priority title)))
181 (cond
182 ;; Cannot create a headline. Fall-back to a list.
183 ((or (org-export-low-level-p headline info)
184 (not (memq org-md-headline-style '(atx setext)))
185 (and (eq org-md-headline-style 'atx) (> level 6))
186 (and (eq org-md-headline-style 'setext) (> level 2)))
187 (let ((bullet
188 (if (not (org-export-numbered-headline-p headline info)) "-"
189 (concat (number-to-string
190 (car (last (org-export-get-headline-number
191 headline info))))
192 "."))))
193 (concat bullet (make-string (- 4 (length bullet)) ? ) heading tags
194 "\n\n"
195 (and contents
196 (replace-regexp-in-string "^" " " contents)))))
197 ;; Use "Setext" style.
198 ((eq org-md-headline-style 'setext)
199 (concat heading tags "\n"
200 (make-string (length heading) (if (= level 1) ?= ?-))
201 "\n\n"
202 contents))
203 ;; Use "atx" style.
204 (t (concat (make-string level ?#) " " heading tags "\n\n" contents))))))
207 ;;;; Horizontal Rule
209 (defun org-md-horizontal-rule (horizontal-rule contents info)
210 "Transcode HORIZONTAL-RULE element into Markdown format.
211 CONTENTS is the horizontal rule contents. INFO is a plist used
212 as a communication channel."
213 "---")
216 ;;;; Italic
218 (defun org-md-italic (italic contents info)
219 "Transcode ITALIC object into Markdown format.
220 CONTENTS is the text within italic markup. INFO is a plist used
221 as a communication channel."
222 (format "*%s*" contents))
225 ;;;; Item
227 (defun org-md-item (item contents info)
228 "Transcode ITEM element into Markdown format.
229 CONTENTS is the item contents. INFO is a plist used as
230 a communication channel."
231 (let* ((type (org-element-property :type (org-export-get-parent item)))
232 (struct (org-element-property :structure item))
233 (bullet (if (not (eq type 'ordered)) "-"
234 (concat (number-to-string
235 (car (last (org-list-get-item-number
236 (org-element-property :begin item)
237 struct
238 (org-list-prevs-alist struct)
239 (org-list-parents-alist struct)))))
240 "."))))
241 (concat bullet
242 (make-string (- 4 (length bullet)) ? )
243 (case (org-element-property :checkbox item)
244 (on "[X] ")
245 (trans "[-] ")
246 (off "[ ] "))
247 (let ((tag (org-element-property :tag item)))
248 (and tag (format "**%s:** "(org-export-data tag info))))
249 (org-trim (replace-regexp-in-string "^" " " contents)))))
252 ;;;; Line Break
254 (defun org-md-line-break (line-break contents info)
255 "Transcode LINE-BREAK object into Markdown format.
256 CONTENTS is nil. INFO is a plist used as a communication
257 channel."
258 " \n")
261 ;;;; Link
263 (defun org-md-link (link contents info)
264 "Transcode LINE-BREAK object into Markdown format.
265 CONTENTS is the link's description. INFO is a plist used as
266 a communication channel."
267 (let ((--link-org-files-as-html-maybe
268 (function
269 (lambda (raw-path info)
270 ;; Treat links to `file.org' as links to `file.html', if
271 ;; needed. See `org-html-link-org-files-as-html'.
272 (cond
273 ((and org-html-link-org-files-as-html
274 (string= ".org"
275 (downcase (file-name-extension raw-path "."))))
276 (concat (file-name-sans-extension raw-path) "."
277 (plist-get info :html-extension)))
278 (t raw-path)))))
279 (type (org-element-property :type link)))
280 (cond ((member type '("custom-id" "id"))
281 (let ((destination (org-export-resolve-id-link link info)))
282 (if (stringp destination) ; External file.
283 (let ((path (funcall --link-org-files-as-html-maybe
284 destination info)))
285 (if (not contents) (format "<%s>" path)
286 (format "[%s](%s)" contents path)))
287 (concat
288 (and contents (concat contents " "))
289 (format "(%s)"
290 (format (org-export-translate "See section %s" :html info)
291 (mapconcat 'number-to-string
292 (org-export-get-headline-number
293 destination info)
294 ".")))))))
295 ((org-export-inline-image-p link org-html-inline-image-rules)
296 (let ((path (let ((raw-path (org-element-property :path link)))
297 (if (not (file-name-absolute-p raw-path)) raw-path
298 (expand-file-name raw-path)))))
299 (format "![%s](%s)"
300 (let ((caption (org-export-get-caption
301 (org-export-get-parent-element link))))
302 (when caption (org-export-data caption info)))
303 path)))
304 ((string= type "coderef")
305 (let ((ref (org-element-property :path link)))
306 (format (org-export-get-coderef-format ref contents)
307 (org-export-resolve-coderef ref info))))
308 ((equal type "radio")
309 (let ((destination (org-export-resolve-radio-link link info)))
310 (org-export-data (org-element-contents destination) info)))
311 ((equal type "fuzzy")
312 (let ((destination (org-export-resolve-fuzzy-link link info)))
313 (if (org-string-nw-p contents) contents
314 (when destination
315 (let ((number (org-export-get-ordinal destination info)))
316 (when number
317 (if (atom number) (number-to-string number)
318 (mapconcat 'number-to-string number "."))))))))
319 (t (let* ((raw-path (org-element-property :path link))
320 (path (cond
321 ((member type '("http" "https" "ftp"))
322 (concat type ":" raw-path))
323 ((equal type "file")
324 ;; Treat links to ".org" files as ".html",
325 ;; if needed.
326 (setq raw-path
327 (funcall --link-org-files-as-html-maybe
328 raw-path info))
329 ;; If file path is absolute, prepend it
330 ;; with protocol component - "file://".
331 (if (not (file-name-absolute-p raw-path)) raw-path
332 (concat "file://" (expand-file-name raw-path))))
333 (t raw-path))))
334 (if (not contents) (format "<%s>" path)
335 (format "[%s](%s)" contents path)))))))
338 ;;;; Node Property
340 (defun org-md-node-property (node-property contents info)
341 "Transcode a NODE-PROPERTY element into Markdown syntax.
342 CONTENTS is nil. INFO is a plist holding contextual
343 information."
344 (format "%s:%s"
345 (org-element-property :key node-property)
346 (let ((value (org-element-property :value node-property)))
347 (if value (concat " " value) ""))))
350 ;;;; Paragraph
352 (defun org-md-paragraph (paragraph contents info)
353 "Transcode PARAGRAPH element into Markdown format.
354 CONTENTS is the paragraph contents. INFO is a plist used as
355 a communication channel."
356 (let ((first-object (car (org-element-contents paragraph))))
357 ;; If paragraph starts with a #, protect it.
358 (if (and (stringp first-object) (string-match "\\`#" first-object))
359 (replace-regexp-in-string "\\`#" "\\#" contents nil t)
360 contents)))
363 ;;;; Plain List
365 (defun org-md-plain-list (plain-list contents info)
366 "Transcode PLAIN-LIST element into Markdown format.
367 CONTENTS is the plain-list contents. INFO is a plist used as
368 a communication channel."
369 contents)
372 ;;;; Plain Text
374 (defun org-md-plain-text (text info)
375 "Transcode a TEXT string into Markdown format.
376 TEXT is the string to transcode. INFO is a plist holding
377 contextual information."
378 (when (plist-get info :with-smart-quotes)
379 (setq text (org-export-activate-smart-quotes text :html info)))
380 ;; Protect ambiguous #. This will protect # at the beginning of
381 ;; a line, but not at the beginning of a paragraph. See
382 ;; `org-md-paragraph'.
383 (setq text (replace-regexp-in-string "\n#" "\n\\\\#" text))
384 ;; Protect ambiguous !
385 (setq text (replace-regexp-in-string "\\(!\\)\\[" "\\\\!" text nil nil 1))
386 ;; Protect `, *, _ and \
387 (setq text (replace-regexp-in-string "[`*_\\]" "\\\\\\&" text))
388 ;; Handle special strings, if required.
389 (when (plist-get info :with-special-strings)
390 (setq text (org-html-convert-special-strings text)))
391 ;; Handle break preservation, if required.
392 (when (plist-get info :preserve-breaks)
393 (setq text (replace-regexp-in-string "[ \t]*\n" " \n" text)))
394 ;; Return value.
395 text)
398 ;;;; Property Drawer
400 (defun org-md-property-drawer (property-drawer contents info)
401 "Transcode a PROPERTY-DRAWER element into Markdown format.
402 CONTENTS holds the contents of the drawer. INFO is a plist
403 holding contextual information."
404 (and (org-string-nw-p contents)
405 (replace-regexp-in-string "^" " " contents)))
408 ;;;; Quote Block
410 (defun org-md-quote-block (quote-block contents info)
411 "Transcode QUOTE-BLOCK element into Markdown format.
412 CONTENTS is the quote-block contents. INFO is a plist used as
413 a communication channel."
414 (replace-regexp-in-string
415 "^" "> "
416 (replace-regexp-in-string "\n\\'" "" contents)))
419 ;;;; Section
421 (defun org-md-section (section contents info)
422 "Transcode SECTION element into Markdown format.
423 CONTENTS is the section contents. INFO is a plist used as
424 a communication channel."
425 contents)
428 ;;;; Template
430 (defun org-md-template (contents info)
431 "Return complete document string after Markdown conversion.
432 CONTENTS is the transcoded contents string. INFO is a plist used
433 as a communication channel."
434 contents)
438 ;;; Interactive function
440 ;;;###autoload
441 (defun org-md-export-as-markdown (&optional async subtreep visible-only)
442 "Export current buffer to a Markdown buffer.
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 A non-nil optional argument ASYNC means the process should happen
450 asynchronously. The resulting buffer should be accessible
451 through the `org-export-stack' interface.
453 When optional argument SUBTREEP is non-nil, export the sub-tree
454 at point, extracting information from the headline properties
455 first.
457 When optional argument VISIBLE-ONLY is non-nil, don't export
458 contents of hidden elements.
460 Export is done in a buffer named \"*Org MD Export*\", which will
461 be displayed when `org-export-show-temporary-export-buffer' is
462 non-nil."
463 (interactive)
464 (org-export-to-buffer 'md "*Org MD Export*"
465 async subtreep visible-only nil nil (lambda () (text-mode))))
467 ;;;###autoload
468 (defun org-md-convert-region-to-md ()
469 "Assume the current region has org-mode syntax, and convert it to Markdown.
470 This can be used in any buffer. For example, you can write an
471 itemized list in org-mode syntax in a Markdown buffer and use
472 this command to convert it."
473 (interactive)
474 (org-export-replace-region-by 'md))
477 ;;;###autoload
478 (defun org-md-export-to-markdown (&optional async subtreep visible-only)
479 "Export current buffer to a Markdown file.
481 If narrowing is active in the current buffer, only export its
482 narrowed part.
484 If a region is active, export that region.
486 A non-nil optional argument ASYNC means the process should happen
487 asynchronously. The resulting file should be accessible through
488 the `org-export-stack' interface.
490 When optional argument SUBTREEP is non-nil, export the sub-tree
491 at point, extracting information from the headline properties
492 first.
494 When optional argument VISIBLE-ONLY is non-nil, don't export
495 contents of hidden elements.
497 Return output file's name."
498 (interactive)
499 (let ((outfile (org-export-output-file-name ".md" subtreep)))
500 (org-export-to-file 'md outfile async subtreep visible-only)))
502 ;;;###autoload
503 (defun org-md-publish-to-md (plist filename pub-dir)
504 "Publish an org file to Markdown.
506 FILENAME is the filename of the Org file to be published. PLIST
507 is the property list for the given project. PUB-DIR is the
508 publishing directory.
510 Return output file name."
511 (org-publish-org-to 'md filename ".md" plist pub-dir))
513 (provide 'ox-md)
515 ;; Local variables:
516 ;; generated-autoload-file: "org-loaddefs.el"
517 ;; End:
519 ;;; ox-md.el ends here