ox-md.el 16 KB

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