ox-md.el 18 KB

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