org-md.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. ;;; org-md.el --- Markdown Back-End for Org Export Engine
  2. ;; Copyright (C) 2012, 2013 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <n.goaziou@gmail.com>
  4. ;; Keywords: org, wp, tex
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; This library implements a Markdown back-end (vanilla flavour) for
  17. ;; Org exporter, based on `e-html'.
  18. ;;
  19. ;; It provides two commands for export, depending on the desired
  20. ;; output: `org-md-export-as-markdown' (temporary buffer) and
  21. ;; `org-md-export-to-markdown' ("md" file).
  22. ;;; Code:
  23. (require 'org-e-html)
  24. ;;; User-Configurable Variables
  25. (defgroup org-export-md nil
  26. "Options specific to Markdown export back-end."
  27. :tag "Org Markdown"
  28. :group 'org-export
  29. :version "24.2")
  30. (defcustom org-md-headline-style 'atx
  31. "Style used to format headlines.
  32. This variable can be set to either `atx' or `setext'."
  33. :group 'org-export-md
  34. :type '(choice
  35. (const :tag "Use \"atx\" style" atx)
  36. (const :tag "Use \"Setext\" style" setext)))
  37. ;;; Define Back-End
  38. (org-export-define-derived-backend md e-html
  39. :export-block ("MD" "MARKDOWN")
  40. :filters-alist ((:filter-parse-tree . org-md-separate-elements))
  41. :menu-entry
  42. (?m "Export to Markdown"
  43. ((?M "To temporary buffer"
  44. (lambda (a s v b) (org-md-export-as-markdown a s v)))
  45. (?m "To file" (lambda (a s v b) (org-md-export-to-markdown a s v)))
  46. (?o "To file and open"
  47. (lambda (a s v b)
  48. (if a (org-md-export-to-markdown t s v)
  49. (org-open-file (org-md-export-to-markdown nil s v)))))))
  50. :translate-alist ((bold . org-md-bold)
  51. (code . org-md-verbatim)
  52. (example-block . org-md-example-block)
  53. (footnote-definition . ignore)
  54. (footnote-reference . ignore)
  55. (headline . org-md-headline)
  56. (horizontal-rule . org-md-horizontal-rule)
  57. (inline-src-block . org-md-verbatim)
  58. (italic . org-md-italic)
  59. (item . org-md-item)
  60. (line-break . org-md-line-break)
  61. (link . org-md-link)
  62. (paragraph . org-md-paragraph)
  63. (plain-list . org-md-plain-list)
  64. (plain-text . org-md-plain-text)
  65. (quote-block . org-md-quote-block)
  66. (quote-section . org-md-example-block)
  67. (section . org-md-section)
  68. (src-block . org-md-example-block)
  69. (template . org-md-template)
  70. (verbatim . org-md-verbatim)))
  71. ;;; Filters
  72. (defun org-md-separate-elements (tree backend info)
  73. "Make sure elements are separated by at least one blank line.
  74. TREE is the parse tree being exported. BACKEND is the export
  75. back-end used. INFO is a plist used as a communication channel.
  76. Assume BACKEND is `md'."
  77. (org-element-map
  78. tree org-element-all-elements
  79. (lambda (elem)
  80. (unless (eq (org-element-type elem) 'org-data)
  81. (org-element-put-property
  82. elem :post-blank
  83. (let ((post-blank (org-element-property :post-blank elem)))
  84. (if (not post-blank) 1 (max 1 post-blank)))))))
  85. ;; Return updated tree.
  86. tree)
  87. ;;; Transcode Functions
  88. ;;;; Bold
  89. (defun org-md-bold (bold contents info)
  90. "Transcode BOLD object into Markdown format.
  91. CONTENTS is the text within bold markup. INFO is a plist used as
  92. a communication channel."
  93. (format "**%s**" contents))
  94. ;;;; Code and Verbatim
  95. (defun org-md-verbatim (verbatim contents info)
  96. "Transcode VERBATIM object into Markdown format.
  97. CONTENTS is nil. INFO is a plist used as a communication
  98. channel."
  99. (let ((value (org-element-property :value verbatim)))
  100. (format (cond ((not (string-match "`" value)) "`%s`")
  101. ((or (string-match "\\``" value)
  102. (string-match "`\\'" value))
  103. "`` %s ``")
  104. (t "``%s``"))
  105. value)))
  106. ;;;; Example Block and Src Block
  107. (defun org-md-example-block (example-block contents info)
  108. "Transcode EXAMPLE-BLOCK element into Markdown format.
  109. CONTENTS is nil. INFO is a plist used as a communication
  110. channel."
  111. (replace-regexp-in-string
  112. "^" " "
  113. (org-remove-indentation
  114. (org-element-property :value example-block))))
  115. ;;;; Headline
  116. (defun org-md-headline (headline contents info)
  117. "Transcode HEADLINE element into Markdown format.
  118. CONTENTS is the headline contents. INFO is a plist used as
  119. a communication channel."
  120. (unless (org-element-property :footnote-section-p headline)
  121. (let* ((level (org-export-get-relative-level headline info))
  122. (title (org-export-data (org-element-property :title headline) info))
  123. (todo (and (plist-get info :with-todo-keywords)
  124. (let ((todo (org-element-property :todo-keyword
  125. headline)))
  126. (and todo (concat (org-export-data todo info) " ")))))
  127. (tags (and (plist-get info :with-tags)
  128. (let ((tag-list (org-export-get-tags headline info)))
  129. (and tag-list
  130. (format " :%s:"
  131. (mapconcat 'identity tag-list ":"))))))
  132. (priority
  133. (and (plist-get info :with-priority)
  134. (let ((char (org-element-property :priority headline)))
  135. (and char (format "[#%c] " char)))))
  136. ;; Headline text without tags.
  137. (heading (concat todo priority title)))
  138. (cond
  139. ;; Cannot create an headline. Fall-back to a list.
  140. ((or (org-export-low-level-p headline info)
  141. (not (memq org-md-headline-style '(atx setext)))
  142. (and (eq org-md-headline-style 'atx) (> level 6))
  143. (and (eq org-md-headline-style 'setext) (> level 2)))
  144. (let ((bullet
  145. (if (not (org-export-numbered-headline-p headline info)) "-"
  146. (concat (number-to-string
  147. (car (last (org-export-get-headline-number
  148. headline info))))
  149. "."))))
  150. (concat bullet (make-string (- 4 (length bullet)) ? ) heading tags
  151. "\n\n"
  152. (and contents
  153. (replace-regexp-in-string "^" " " contents)))))
  154. ;; Use "Setext" style.
  155. ((eq org-md-headline-style 'setext)
  156. (concat heading tags "\n"
  157. (make-string (length heading) (if (= level 1) ?= ?-))
  158. "\n\n"
  159. contents))
  160. ;; Use "atx" style.
  161. (t (concat (make-string level ?#) " " heading tags "\n\n" contents))))))
  162. ;;;; Horizontal Rule
  163. (defun org-md-horizontal-rule (horizontal-rule contents info)
  164. "Transcode HORIZONTAL-RULE element into Markdown format.
  165. CONTENTS is the horizontal rule contents. INFO is a plist used
  166. as a communication channel."
  167. "---")
  168. ;;;; Italic
  169. (defun org-md-italic (italic contents info)
  170. "Transcode ITALIC object into Markdown format.
  171. CONTENTS is the text within italic markup. INFO is a plist used
  172. as a communication channel."
  173. (format "*%s*" contents))
  174. ;;;; Item
  175. (defun org-md-item (item contents info)
  176. "Transcode ITEM element into Markdown format.
  177. CONTENTS is the item contents. INFO is a plist used as
  178. a communication channel."
  179. (let* ((type (org-element-property :type (org-export-get-parent item)))
  180. (struct (org-element-property :structure item))
  181. (bullet (if (not (eq type 'ordered)) "-"
  182. (concat (number-to-string
  183. (car (last (org-list-get-item-number
  184. (org-element-property :begin item)
  185. struct
  186. (org-list-prevs-alist struct)
  187. (org-list-parents-alist struct)))))
  188. "."))))
  189. (concat bullet
  190. (make-string (- 4 (length bullet)) ? )
  191. (case (org-element-property :checkbox item)
  192. (on "[X] ")
  193. (trans "[-] ")
  194. (off "[ ] "))
  195. (let ((tag (org-element-property :tag item)))
  196. (and tag (format "**%s:** "(org-export-data tag info))))
  197. (org-trim (replace-regexp-in-string "^" " " contents)))))
  198. ;;;; Line Break
  199. (defun org-md-line-break (line-break contents info)
  200. "Transcode LINE-BREAK object into Markdown format.
  201. CONTENTS is nil. INFO is a plist used as a communication
  202. channel."
  203. " \n")
  204. ;;;; Link
  205. (defun org-md-link (link contents info)
  206. "Transcode LINE-BREAK object into Markdown format.
  207. CONTENTS is the link's description. INFO is a plist used as
  208. a communication channel."
  209. (let ((--link-org-files-as-html-maybe
  210. (function
  211. (lambda (raw-path info)
  212. ;; Treat links to `file.org' as links to `file.html', if
  213. ;; needed. See `org-e-html-link-org-files-as-html'.
  214. (cond
  215. ((and org-e-html-link-org-files-as-html
  216. (string= ".org"
  217. (downcase (file-name-extension raw-path "."))))
  218. (concat (file-name-sans-extension raw-path) "."
  219. (plist-get info :html-extension)))
  220. (t raw-path)))))
  221. (type (org-element-property :type link)))
  222. (cond ((member type '("custom-id" "id"))
  223. (let ((destination (org-export-resolve-id-link link info)))
  224. (if (stringp destination) ; External file.
  225. (let ((path (funcall --link-org-files-as-html-maybe
  226. destination info)))
  227. (if (not contents) (format "<%s>" path)
  228. (format "[%s](%s)" contents path)))
  229. (concat
  230. (and contents (concat contents " "))
  231. (format "(%s)"
  232. (format (org-export-translate "See section %s" :html info)
  233. (mapconcat 'number-to-string
  234. (org-export-get-headline-number
  235. destination info)
  236. ".")))))))
  237. ((org-export-inline-image-p link org-e-html-inline-image-rules)
  238. (let ((path (let ((raw-path (org-element-property :path link)))
  239. (if (not (file-name-absolute-p raw-path)) raw-path
  240. (expand-file-name raw-path)))))
  241. (format "![%s](%s)"
  242. (let ((caption (org-export-get-caption
  243. (org-export-get-parent-element link))))
  244. (when caption (org-export-data caption info)))
  245. path)))
  246. ((string= type "coderef")
  247. (let ((ref (org-element-property :path link)))
  248. (format (org-export-get-coderef-format ref contents)
  249. (org-export-resolve-coderef ref info))))
  250. ((equal type "radio")
  251. (let ((destination (org-export-resolve-radio-link link info)))
  252. (org-export-data (org-element-contents destination) info)))
  253. ((equal type "fuzzy")
  254. (let ((destination (org-export-resolve-fuzzy-link link info)))
  255. ;; Ignore invisible "#+TARGET: path".
  256. (unless (eq (org-element-type destination) 'keyword)
  257. (if (org-string-nw-p contents) contents
  258. (when destination
  259. (let ((number (org-export-get-ordinal destination info)))
  260. (when number
  261. (if (atom number) (number-to-string number)
  262. (mapconcat 'number-to-string number ".")))))))))
  263. (t (let* ((raw-path (org-element-property :path link))
  264. (path (cond
  265. ((member type '("http" "https" "ftp"))
  266. (concat type ":" raw-path))
  267. ((equal type "file")
  268. ;; Treat links to ".org" files as ".html",
  269. ;; if needed.
  270. (setq raw-path
  271. (funcall --link-org-files-as-html-maybe
  272. raw-path info))
  273. ;; If file path is absolute, prepend it
  274. ;; with protocol component - "file://".
  275. (if (not (file-name-absolute-p raw-path)) raw-path
  276. (concat "file://" (expand-file-name raw-path))))
  277. (t raw-path))))
  278. (if (not contents) (format "<%s>" path)
  279. (format "[%s](%s)" contents path)))))))
  280. ;;;; Paragraph
  281. (defun org-md-paragraph (paragraph contents info)
  282. "Transcode PARAGRAPH element into Markdown format.
  283. CONTENTS is the paragraph contents. INFO is a plist used as
  284. a communication channel."
  285. (let ((first-object (car (org-element-contents paragraph))))
  286. ;; If paragraph starts with a #, protect it.
  287. (if (and (stringp first-object) (string-match "\\`#" first-object))
  288. (replace-regexp-in-string "\\`#" "\\#" contents nil t)
  289. contents)))
  290. ;;;; Plain List
  291. (defun org-md-plain-list (plain-list contents info)
  292. "Transcode PLAIN-LIST element into Markdown format.
  293. CONTENTS is the plain-list contents. INFO is a plist used as
  294. a communication channel."
  295. contents)
  296. ;;;; Plain Text
  297. (defun org-md-plain-text (text info)
  298. "Transcode a TEXT string into Markdown format.
  299. TEXT is the string to transcode. INFO is a plist holding
  300. contextual information."
  301. (when (plist-get info :with-smart-quotes)
  302. (setq text (org-export-activate-smart-quotes text :html info)))
  303. ;; Protect ambiguous #. This will protect # at the beginning of
  304. ;; a line, but not at the beginning of a paragraph. See
  305. ;; `org-md-paragraph'.
  306. (setq text (replace-regexp-in-string "\n#" "\n\\\\#" text))
  307. ;; Protect ambiguous !
  308. (setq text (replace-regexp-in-string "\\(!\\)\\[" "\\\\!" text nil nil 1))
  309. ;; Protect `, *, _ and \
  310. (setq text (replace-regexp-in-string "[`*_\\]" "\\\\\\&" text))
  311. ;; Handle special strings, if required.
  312. (when (plist-get info :with-special-strings)
  313. (setq text (org-e-html-convert-special-strings text)))
  314. ;; Handle break preservation, if required.
  315. (when (plist-get info :preserve-breaks)
  316. (setq text (replace-regexp-in-string "[ \t]*\n" " \n" text)))
  317. ;; Return value.
  318. text)
  319. ;;;; Quote Block
  320. (defun org-md-quote-block (quote-block contents info)
  321. "Transcode QUOTE-BLOCK element into Markdown format.
  322. CONTENTS is the quote-block contents. INFO is a plist used as
  323. a communication channel."
  324. (replace-regexp-in-string
  325. "^" "> "
  326. (replace-regexp-in-string "\n\\'" "" contents)))
  327. ;;;; Section
  328. (defun org-md-section (section contents info)
  329. "Transcode SECTION element into Markdown format.
  330. CONTENTS is the section contents. INFO is a plist used as
  331. a communication channel."
  332. contents)
  333. ;;;; Template
  334. (defun org-md-template (contents info)
  335. "Return complete document string after Markdown conversion.
  336. CONTENTS is the transcoded contents string. INFO is a plist used
  337. as a communication channel."
  338. contents)
  339. ;;; Interactive function
  340. ;;;###autoload
  341. (defun org-md-export-as-markdown (&optional async subtreep visible-only)
  342. "Export current buffer to a text buffer.
  343. If narrowing is active in the current buffer, only export its
  344. narrowed part.
  345. If a region is active, export that region.
  346. A non-nil optional argument ASYNC means the process should happen
  347. asynchronously. The resulting buffer should be accessible
  348. through the `org-export-stack' interface.
  349. When optional argument SUBTREEP is non-nil, export the sub-tree
  350. at point, extracting information from the headline properties
  351. first.
  352. When optional argument VISIBLE-ONLY is non-nil, don't export
  353. contents of hidden elements.
  354. Export is done in a buffer named \"*Org MD Export*\", which will
  355. be displayed when `org-export-show-temporary-export-buffer' is
  356. non-nil."
  357. (interactive)
  358. (if async
  359. (org-export-async-start
  360. (lambda (output)
  361. (with-current-buffer (get-buffer-create "*Org MD Export*")
  362. (erase-buffer)
  363. (insert output)
  364. (goto-char (point-min))
  365. (text-mode)
  366. (org-export-add-to-stack (current-buffer) 'md)))
  367. `(org-export-as 'md ,subtreep ,visible-only))
  368. (let ((outbuf (org-export-to-buffer
  369. 'md "*Org MD Export*" subtreep visible-only)))
  370. (with-current-buffer outbuf (text-mode))
  371. (when org-export-show-temporary-export-buffer
  372. (switch-to-buffer-other-window outbuf)))))
  373. ;;;###autoload
  374. (defun org-md-export-to-markdown (&optional async subtreep visible-only)
  375. "Export current buffer to a Markdown file.
  376. If narrowing is active in the current buffer, only export its
  377. narrowed part.
  378. If a region is active, export that region.
  379. A non-nil optional argument ASYNC means the process should happen
  380. asynchronously. The resulting file should be accessible through
  381. the `org-export-stack' interface.
  382. When optional argument SUBTREEP is non-nil, export the sub-tree
  383. at point, extracting information from the headline properties
  384. first.
  385. When optional argument VISIBLE-ONLY is non-nil, don't export
  386. contents of hidden elements.
  387. Return output file's name."
  388. (interactive)
  389. (let ((outfile (org-export-output-file-name ".md" subtreep)))
  390. (if async
  391. (org-export-async-start
  392. (lambda (f) (org-export-add-to-stack f 'md))
  393. `(expand-file-name
  394. (org-export-to-file 'md ,outfile ,subtreep ,visible-only)))
  395. (org-export-to-file 'md outfile subtreep visible-only))))
  396. (provide 'org-md)
  397. ;;; org-md.el ends here