ox-md.el 16 KB

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