ox-md.el 18 KB

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