ox-md.el 16 KB

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