ox-md.el: Fix a library keyword in the comment section
[org-mode.git] / lisp / ox-md.el
blobcfbd3d3acbee7ce7c2b2fe84e1ac14165e152aec
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)
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 (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)))
97 ;;; Filters
99 (defun org-md-separate-elements (tree backend info)
100 "Make sure elements are separated by at least one blank line.
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 Assume BACKEND is `md'."
106 (org-element-map tree org-element-all-elements
107 (lambda (elem)
108 (unless (eq (org-element-type elem) 'org-data)
109 (org-element-put-property
110 elem :post-blank
111 (let ((post-blank (org-element-property :post-blank elem)))
112 (if (not post-blank) 1 (max 1 post-blank)))))))
113 ;; Return updated tree.
114 tree)
118 ;;; Transcode Functions
120 ;;;; Bold
122 (defun org-md-bold (bold contents info)
123 "Transcode BOLD object into Markdown format.
124 CONTENTS is the text within bold markup. INFO is a plist used as
125 a communication channel."
126 (format "**%s**" contents))
129 ;;;; Code and Verbatim
131 (defun org-md-verbatim (verbatim contents info)
132 "Transcode VERBATIM object into Markdown format.
133 CONTENTS is nil. INFO is a plist used as a communication
134 channel."
135 (let ((value (org-element-property :value verbatim)))
136 (format (cond ((not (string-match "`" value)) "`%s`")
137 ((or (string-match "\\``" value)
138 (string-match "`\\'" value))
139 "`` %s ``")
140 (t "``%s``"))
141 value)))
144 ;;;; Example Block and Src Block
146 (defun org-md-example-block (example-block contents info)
147 "Transcode EXAMPLE-BLOCK element into Markdown format.
148 CONTENTS is nil. INFO is a plist used as a communication
149 channel."
150 (replace-regexp-in-string
151 "^" " "
152 (org-remove-indentation
153 (org-element-property :value example-block))))
156 ;;;; Headline
158 (defun org-md-headline (headline contents info)
159 "Transcode HEADLINE element into Markdown format.
160 CONTENTS is the headline contents. INFO is a plist used as
161 a communication channel."
162 (unless (org-element-property :footnote-section-p headline)
163 (let* ((level (org-export-get-relative-level headline info))
164 (title (org-export-data (org-element-property :title headline) info))
165 (todo (and (plist-get info :with-todo-keywords)
166 (let ((todo (org-element-property :todo-keyword
167 headline)))
168 (and todo (concat (org-export-data todo info) " ")))))
169 (tags (and (plist-get info :with-tags)
170 (let ((tag-list (org-export-get-tags headline info)))
171 (and tag-list
172 (format " :%s:"
173 (mapconcat 'identity tag-list ":"))))))
174 (priority
175 (and (plist-get info :with-priority)
176 (let ((char (org-element-property :priority headline)))
177 (and char (format "[#%c] " char)))))
178 ;; Headline text without tags.
179 (heading (concat todo priority title)))
180 (cond
181 ;; Cannot create a headline. Fall-back to a list.
182 ((or (org-export-low-level-p headline info)
183 (not (memq org-md-headline-style '(atx setext)))
184 (and (eq org-md-headline-style 'atx) (> level 6))
185 (and (eq org-md-headline-style 'setext) (> level 2)))
186 (let ((bullet
187 (if (not (org-export-numbered-headline-p headline info)) "-"
188 (concat (number-to-string
189 (car (last (org-export-get-headline-number
190 headline info))))
191 "."))))
192 (concat bullet (make-string (- 4 (length bullet)) ? ) heading tags
193 "\n\n"
194 (and contents
195 (replace-regexp-in-string "^" " " contents)))))
196 ;; Use "Setext" style.
197 ((eq org-md-headline-style 'setext)
198 (concat heading tags "\n"
199 (make-string (length heading) (if (= level 1) ?= ?-))
200 "\n\n"
201 contents))
202 ;; Use "atx" style.
203 (t (concat (make-string level ?#) " " heading tags "\n\n" contents))))))
206 ;;;; Horizontal Rule
208 (defun org-md-horizontal-rule (horizontal-rule contents info)
209 "Transcode HORIZONTAL-RULE element into Markdown format.
210 CONTENTS is the horizontal rule contents. INFO is a plist used
211 as a communication channel."
212 "---")
215 ;;;; Italic
217 (defun org-md-italic (italic contents info)
218 "Transcode ITALIC object into Markdown format.
219 CONTENTS is the text within italic markup. INFO is a plist used
220 as a communication channel."
221 (format "*%s*" contents))
224 ;;;; Item
226 (defun org-md-item (item contents info)
227 "Transcode ITEM element into Markdown format.
228 CONTENTS is the item contents. INFO is a plist used as
229 a communication channel."
230 (let* ((type (org-element-property :type (org-export-get-parent item)))
231 (struct (org-element-property :structure item))
232 (bullet (if (not (eq type 'ordered)) "-"
233 (concat (number-to-string
234 (car (last (org-list-get-item-number
235 (org-element-property :begin item)
236 struct
237 (org-list-prevs-alist struct)
238 (org-list-parents-alist struct)))))
239 "."))))
240 (concat bullet
241 (make-string (- 4 (length bullet)) ? )
242 (case (org-element-property :checkbox item)
243 (on "[X] ")
244 (trans "[-] ")
245 (off "[ ] "))
246 (let ((tag (org-element-property :tag item)))
247 (and tag (format "**%s:** "(org-export-data tag info))))
248 (org-trim (replace-regexp-in-string "^" " " contents)))))
251 ;;;; Line Break
253 (defun org-md-line-break (line-break contents info)
254 "Transcode LINE-BREAK object into Markdown format.
255 CONTENTS is nil. INFO is a plist used as a communication
256 channel."
257 " \n")
260 ;;;; Link
262 (defun org-md-link (link contents info)
263 "Transcode LINE-BREAK object into Markdown format.
264 CONTENTS is the link's description. INFO is a plist used as
265 a communication channel."
266 (let ((--link-org-files-as-html-maybe
267 (function
268 (lambda (raw-path info)
269 ;; Treat links to `file.org' as links to `file.html', if
270 ;; needed. See `org-html-link-org-files-as-html'.
271 (cond
272 ((and org-html-link-org-files-as-html
273 (string= ".org"
274 (downcase (file-name-extension raw-path "."))))
275 (concat (file-name-sans-extension raw-path) "."
276 (plist-get info :html-extension)))
277 (t raw-path)))))
278 (type (org-element-property :type link)))
279 (cond ((member type '("custom-id" "id"))
280 (let ((destination (org-export-resolve-id-link link info)))
281 (if (stringp destination) ; External file.
282 (let ((path (funcall --link-org-files-as-html-maybe
283 destination info)))
284 (if (not contents) (format "<%s>" path)
285 (format "[%s](%s)" contents path)))
286 (concat
287 (and contents (concat contents " "))
288 (format "(%s)"
289 (format (org-export-translate "See section %s" :html info)
290 (mapconcat 'number-to-string
291 (org-export-get-headline-number
292 destination info)
293 ".")))))))
294 ((org-export-inline-image-p link org-html-inline-image-rules)
295 (let ((path (let ((raw-path (org-element-property :path link)))
296 (if (not (file-name-absolute-p raw-path)) raw-path
297 (expand-file-name raw-path)))))
298 (format "![%s](%s)"
299 (let ((caption (org-export-get-caption
300 (org-export-get-parent-element link))))
301 (when caption (org-export-data caption info)))
302 path)))
303 ((string= type "coderef")
304 (let ((ref (org-element-property :path link)))
305 (format (org-export-get-coderef-format ref contents)
306 (org-export-resolve-coderef ref info))))
307 ((equal type "radio")
308 (let ((destination (org-export-resolve-radio-link link info)))
309 (org-export-data (org-element-contents destination) info)))
310 ((equal type "fuzzy")
311 (let ((destination (org-export-resolve-fuzzy-link link info)))
312 ;; Ignore invisible "#+TARGET: path".
313 (unless (eq (org-element-type destination) 'keyword)
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 ;;;; Paragraph
341 (defun org-md-paragraph (paragraph contents info)
342 "Transcode PARAGRAPH element into Markdown format.
343 CONTENTS is the paragraph contents. INFO is a plist used as
344 a communication channel."
345 (let ((first-object (car (org-element-contents paragraph))))
346 ;; If paragraph starts with a #, protect it.
347 (if (and (stringp first-object) (string-match "\\`#" first-object))
348 (replace-regexp-in-string "\\`#" "\\#" contents nil t)
349 contents)))
352 ;;;; Plain List
354 (defun org-md-plain-list (plain-list contents info)
355 "Transcode PLAIN-LIST element into Markdown format.
356 CONTENTS is the plain-list contents. INFO is a plist used as
357 a communication channel."
358 contents)
361 ;;;; Plain Text
363 (defun org-md-plain-text (text info)
364 "Transcode a TEXT string into Markdown format.
365 TEXT is the string to transcode. INFO is a plist holding
366 contextual information."
367 (when (plist-get info :with-smart-quotes)
368 (setq text (org-export-activate-smart-quotes text :html info)))
369 ;; Protect ambiguous #. This will protect # at the beginning of
370 ;; a line, but not at the beginning of a paragraph. See
371 ;; `org-md-paragraph'.
372 (setq text (replace-regexp-in-string "\n#" "\n\\\\#" text))
373 ;; Protect ambiguous !
374 (setq text (replace-regexp-in-string "\\(!\\)\\[" "\\\\!" text nil nil 1))
375 ;; Protect `, *, _ and \
376 (setq text (replace-regexp-in-string "[`*_\\]" "\\\\\\&" text))
377 ;; Handle special strings, if required.
378 (when (plist-get info :with-special-strings)
379 (setq text (org-html-convert-special-strings text)))
380 ;; Handle break preservation, if required.
381 (when (plist-get info :preserve-breaks)
382 (setq text (replace-regexp-in-string "[ \t]*\n" " \n" text)))
383 ;; Return value.
384 text)
387 ;;;; Quote Block
389 (defun org-md-quote-block (quote-block contents info)
390 "Transcode QUOTE-BLOCK element into Markdown format.
391 CONTENTS is the quote-block contents. INFO is a plist used as
392 a communication channel."
393 (replace-regexp-in-string
394 "^" "> "
395 (replace-regexp-in-string "\n\\'" "" contents)))
398 ;;;; Section
400 (defun org-md-section (section contents info)
401 "Transcode SECTION element into Markdown format.
402 CONTENTS is the section contents. INFO is a plist used as
403 a communication channel."
404 contents)
407 ;;;; Template
409 (defun org-md-template (contents info)
410 "Return complete document string after Markdown conversion.
411 CONTENTS is the transcoded contents string. INFO is a plist used
412 as a communication channel."
413 contents)
417 ;;; Interactive function
419 ;;;###autoload
420 (defun org-md-export-as-markdown (&optional async subtreep visible-only)
421 "Export current buffer to a text buffer.
423 If narrowing is active in the current buffer, only export its
424 narrowed part.
426 If a region is active, export that region.
428 A non-nil optional argument ASYNC means the process should happen
429 asynchronously. The resulting buffer should be accessible
430 through the `org-export-stack' interface.
432 When optional argument SUBTREEP is non-nil, export the sub-tree
433 at point, extracting information from the headline properties
434 first.
436 When optional argument VISIBLE-ONLY is non-nil, don't export
437 contents of hidden elements.
439 Export is done in a buffer named \"*Org MD Export*\", which will
440 be displayed when `org-export-show-temporary-export-buffer' is
441 non-nil."
442 (interactive)
443 (if async
444 (org-export-async-start
445 (lambda (output)
446 (with-current-buffer (get-buffer-create "*Org MD Export*")
447 (erase-buffer)
448 (insert output)
449 (goto-char (point-min))
450 (text-mode)
451 (org-export-add-to-stack (current-buffer) 'md)))
452 `(org-export-as 'md ,subtreep ,visible-only))
453 (let ((outbuf (org-export-to-buffer
454 'md "*Org MD Export*" subtreep visible-only)))
455 (with-current-buffer outbuf (text-mode))
456 (when org-export-show-temporary-export-buffer
457 (switch-to-buffer-other-window outbuf)))))
460 ;;;###autoload
461 (defun org-md-export-to-markdown (&optional async subtreep visible-only)
462 "Export current buffer to a Markdown file.
464 If narrowing is active in the current buffer, only export its
465 narrowed part.
467 If a region is active, export that region.
469 A non-nil optional argument ASYNC means the process should happen
470 asynchronously. The resulting file should be accessible through
471 the `org-export-stack' interface.
473 When optional argument SUBTREEP is non-nil, export the sub-tree
474 at point, extracting information from the headline properties
475 first.
477 When optional argument VISIBLE-ONLY is non-nil, don't export
478 contents of hidden elements.
480 Return output file's name."
481 (interactive)
482 (let ((outfile (org-export-output-file-name ".md" subtreep)))
483 (if async
484 (org-export-async-start
485 (lambda (f) (org-export-add-to-stack f 'md))
486 `(expand-file-name
487 (org-export-to-file 'md ,outfile ,subtreep ,visible-only)))
488 (org-export-to-file 'md outfile subtreep visible-only))))
491 (provide 'ox-md)
493 ;; Local variables:
494 ;; generated-autoload-file: "org-loaddefs.el"
495 ;; End:
497 ;;; ox-md.el ends here