ox-md.el 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. ;;; ox-md.el --- Markdown Back-End for Org Export Engine -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2012-2016 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. (require 'cl-lib)
  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. :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. (example-block . org-md-example-block)
  53. (export-block . org-md-export-block)
  54. (fixed-width . org-md-example-block)
  55. (headline . org-md-headline)
  56. (horizontal-rule . org-md-horizontal-rule)
  57. (inline-src-block . org-md-verbatim)
  58. (inner-template . org-md-inner-template)
  59. (italic . org-md-italic)
  60. (item . org-md-item)
  61. (keyword . org-md-keyword)
  62. (line-break . org-md-line-break)
  63. (link . org-md-link)
  64. (node-property . org-md-node-property)
  65. (paragraph . org-md-paragraph)
  66. (plain-list . org-md-plain-list)
  67. (plain-text . org-md-plain-text)
  68. (property-drawer . org-md-property-drawer)
  69. (quote-block . org-md-quote-block)
  70. (section . org-md-section)
  71. (src-block . org-md-example-block)
  72. (template . org-md-template)
  73. (verbatim . org-md-verbatim))
  74. :options-alist '((:md-headline-style nil nil org-md-headline-style)))
  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. Enforce a blank line between elements. There are two exceptions
  81. to this rule:
  82. 1. Preserve blank lines between sibling items in a plain list,
  83. 2. In an item, remove any blank line before the very first
  84. paragraph and the next sub-list when the latter ends the
  85. current item.
  86. Assume BACKEND is `md'."
  87. (org-element-map tree (remq 'item org-element-all-elements)
  88. (lambda (e)
  89. (org-element-put-property
  90. e :post-blank
  91. (if (and (eq (org-element-type e) 'paragraph)
  92. (eq (org-element-type (org-element-property :parent e)) 'item)
  93. (org-export-first-sibling-p e info)
  94. (let ((next (org-export-get-next-element e info)))
  95. (and (eq (org-element-type next) 'plain-list)
  96. (not (org-export-get-next-element next 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-export-format-code-default example-block info))))
  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. (anchor
  158. (and (plist-get info :with-toc)
  159. (format "<a id=\"%s\"></a>"
  160. (or (org-element-property :CUSTOM_ID headline)
  161. (org-export-get-reference headline info)))))
  162. ;; Headline text without tags.
  163. (heading (concat todo priority title))
  164. (style (plist-get info :md-headline-style)))
  165. (cond
  166. ;; Cannot create a headline. Fall-back to a list.
  167. ((or (org-export-low-level-p headline info)
  168. (not (memq style '(atx setext)))
  169. (and (eq style 'atx) (> level 6))
  170. (and (eq style 'setext) (> level 2)))
  171. (let ((bullet
  172. (if (not (org-export-numbered-headline-p headline info)) "-"
  173. (concat (number-to-string
  174. (car (last (org-export-get-headline-number
  175. headline info))))
  176. "."))))
  177. (concat bullet (make-string (- 4 (length bullet)) ?\s) heading tags
  178. "\n\n"
  179. (and contents
  180. (replace-regexp-in-string "^" " " contents)))))
  181. ;; Use "Setext" style.
  182. ((eq style 'setext)
  183. (concat heading tags anchor "\n"
  184. (make-string (length heading) (if (= level 1) ?= ?-))
  185. "\n\n"
  186. contents))
  187. ;; Use "atx" style.
  188. (t (concat (make-string level ?#) " " heading tags anchor "\n\n"
  189. contents))))))
  190. ;;;; Horizontal Rule
  191. (defun org-md-horizontal-rule (_horizontal-rule _contents _info)
  192. "Transcode HORIZONTAL-RULE element into Markdown format.
  193. CONTENTS is the horizontal rule contents. INFO is a plist used
  194. as a communication channel."
  195. "---")
  196. ;;;; Italic
  197. (defun org-md-italic (_italic contents _info)
  198. "Transcode ITALIC object into Markdown format.
  199. CONTENTS is the text within italic markup. INFO is a plist used
  200. as a communication channel."
  201. (format "*%s*" contents))
  202. ;;;; Item
  203. (defun org-md-item (item contents info)
  204. "Transcode ITEM element into Markdown format.
  205. CONTENTS is the item contents. INFO is a plist used as
  206. a communication channel."
  207. (let* ((type (org-element-property :type (org-export-get-parent item)))
  208. (struct (org-element-property :structure item))
  209. (bullet (if (not (eq type 'ordered)) "-"
  210. (concat (number-to-string
  211. (car (last (org-list-get-item-number
  212. (org-element-property :begin item)
  213. struct
  214. (org-list-prevs-alist struct)
  215. (org-list-parents-alist struct)))))
  216. "."))))
  217. (concat bullet
  218. (make-string (- 4 (length bullet)) ? )
  219. (pcase (org-element-property :checkbox item)
  220. (`on "[X] ")
  221. (`trans "[-] ")
  222. (`off "[ ] "))
  223. (let ((tag (org-element-property :tag item)))
  224. (and tag (format "**%s:** "(org-export-data tag info))))
  225. (and contents
  226. (org-trim (replace-regexp-in-string "^" " " contents))))))
  227. ;;;; Keyword
  228. (defun org-md-keyword (keyword contents info)
  229. "Transcode a KEYWORD element into Markdown format.
  230. CONTENTS is nil. INFO is a plist used as a communication
  231. channel."
  232. (if (member (org-element-property :key keyword) '("MARKDOWN" "MD"))
  233. (org-element-property :value keyword)
  234. (org-export-with-backend 'html keyword contents info)))
  235. ;;;; Line Break
  236. (defun org-md-line-break (_line-break _contents _info)
  237. "Transcode LINE-BREAK object into Markdown format.
  238. CONTENTS is nil. INFO is a plist used as a communication
  239. channel."
  240. " \n")
  241. ;;;; Link
  242. (defun org-md-link (link contents info)
  243. "Transcode LINE-BREAK object into Markdown format.
  244. CONTENTS is the link's description. INFO is a plist used as
  245. a communication channel."
  246. (let ((link-org-files-as-md
  247. (lambda (raw-path)
  248. ;; Treat links to `file.org' as links to `file.md'.
  249. (if (string= ".org" (downcase (file-name-extension raw-path ".")))
  250. (concat (file-name-sans-extension raw-path) ".md")
  251. raw-path)))
  252. (type (org-element-property :type link)))
  253. (cond
  254. ;; Link type is handled by a special function.
  255. ((org-export-custom-protocol-maybe link contents 'md))
  256. ((member type '("custom-id" "id" "fuzzy"))
  257. (let ((destination (if (string= type "fuzzy")
  258. (org-export-resolve-fuzzy-link link info)
  259. (org-export-resolve-id-link link info))))
  260. (pcase (org-element-type destination)
  261. (`plain-text ; External file.
  262. (let ((path (funcall link-org-files-as-md destination)))
  263. (if (not contents) (format "<%s>" path)
  264. (format "[%s](%s)" contents path))))
  265. (`headline
  266. (format
  267. "[%s](#%s)"
  268. ;; Description.
  269. (cond ((org-string-nw-p contents))
  270. ((org-export-numbered-headline-p destination info)
  271. (mapconcat #'number-to-string
  272. (org-export-get-headline-number destination info)
  273. "."))
  274. (t (org-export-data (org-element-property :title destination)
  275. info)))
  276. ;; Reference.
  277. (or (org-element-property :CUSTOM_ID destination)
  278. (org-export-get-reference destination info))))
  279. (_
  280. (let ((description
  281. (or (org-string-nw-p contents)
  282. (let ((number (org-export-get-ordinal destination info)))
  283. (cond
  284. ((not number) nil)
  285. ((atom number) (number-to-string number))
  286. (t (mapconcat #'number-to-string number ".")))))))
  287. (when description
  288. (format "[%s](#%s)"
  289. description
  290. (org-export-get-reference destination info))))))))
  291. ((org-export-inline-image-p link org-html-inline-image-rules)
  292. (let ((path (let ((raw-path (org-element-property :path link)))
  293. (if (not (file-name-absolute-p raw-path)) raw-path
  294. (expand-file-name raw-path))))
  295. (caption (org-export-data
  296. (org-export-get-caption
  297. (org-export-get-parent-element link)) info)))
  298. (format "![img](%s)"
  299. (if (not (org-string-nw-p caption)) path
  300. (format "%s \"%s\"" path caption)))))
  301. ((string= type "coderef")
  302. (let ((ref (org-element-property :path link)))
  303. (format (org-export-get-coderef-format ref contents)
  304. (org-export-resolve-coderef ref info))))
  305. ((equal type "radio") contents)
  306. (t (let* ((raw-path (org-element-property :path link))
  307. (path
  308. (cond
  309. ((member type '("http" "https" "ftp"))
  310. (concat type ":" raw-path))
  311. ((string= type "file")
  312. (org-export-file-uri (funcall link-org-files-as-md raw-path)))
  313. (t raw-path))))
  314. (if (not contents) (format "<%s>" path)
  315. (format "[%s](%s)" contents path)))))))
  316. ;;;; Node Property
  317. (defun org-md-node-property (node-property _contents _info)
  318. "Transcode a NODE-PROPERTY element into Markdown syntax.
  319. CONTENTS is nil. INFO is a plist holding contextual
  320. information."
  321. (format "%s:%s"
  322. (org-element-property :key node-property)
  323. (let ((value (org-element-property :value node-property)))
  324. (if value (concat " " value) ""))))
  325. ;;;; Paragraph
  326. (defun org-md-paragraph (paragraph contents _info)
  327. "Transcode PARAGRAPH element into Markdown format.
  328. CONTENTS is the paragraph contents. INFO is a plist used as
  329. a communication channel."
  330. (let ((first-object (car (org-element-contents paragraph))))
  331. ;; If paragraph starts with a #, protect it.
  332. (if (and (stringp first-object) (string-match "\\`#" first-object))
  333. (replace-regexp-in-string "\\`#" "\\#" contents nil t)
  334. contents)))
  335. ;;;; Plain List
  336. (defun org-md-plain-list (_plain-list contents _info)
  337. "Transcode PLAIN-LIST element into Markdown format.
  338. CONTENTS is the plain-list contents. INFO is a plist used as
  339. a communication channel."
  340. contents)
  341. ;;;; Plain Text
  342. (defun org-md-plain-text (text info)
  343. "Transcode a TEXT string into Markdown format.
  344. TEXT is the string to transcode. INFO is a plist holding
  345. contextual information."
  346. (when (plist-get info :with-smart-quotes)
  347. (setq text (org-export-activate-smart-quotes text :html info)))
  348. ;; Protect ambiguous #. This will protect # at the beginning of
  349. ;; a line, but not at the beginning of a paragraph. See
  350. ;; `org-md-paragraph'.
  351. (setq text (replace-regexp-in-string "\n#" "\n\\\\#" text))
  352. ;; Protect ambiguous !
  353. (setq text (replace-regexp-in-string "\\(!\\)\\[" "\\\\!" text nil nil 1))
  354. ;; Protect `, *, _ and \
  355. (setq text (replace-regexp-in-string "[`*_\\]" "\\\\\\&" text))
  356. ;; Handle special strings, if required.
  357. (when (plist-get info :with-special-strings)
  358. (setq text (org-html-convert-special-strings text)))
  359. ;; Handle break preservation, if required.
  360. (when (plist-get info :preserve-breaks)
  361. (setq text (replace-regexp-in-string "[ \t]*\n" " \n" text)))
  362. ;; Return value.
  363. text)
  364. ;;;; Property Drawer
  365. (defun org-md-property-drawer (_property-drawer contents _info)
  366. "Transcode a PROPERTY-DRAWER element into Markdown format.
  367. CONTENTS holds the contents of the drawer. INFO is a plist
  368. holding contextual information."
  369. (and (org-string-nw-p contents)
  370. (replace-regexp-in-string "^" " " contents)))
  371. ;;;; Quote Block
  372. (defun org-md-quote-block (_quote-block contents _info)
  373. "Transcode QUOTE-BLOCK element into Markdown format.
  374. CONTENTS is the quote-block contents. INFO is a plist used as
  375. a communication channel."
  376. (replace-regexp-in-string
  377. "^" "> "
  378. (replace-regexp-in-string "\n\\'" "" contents)))
  379. ;;;; Section
  380. (defun org-md-section (_section contents _info)
  381. "Transcode SECTION element into Markdown format.
  382. CONTENTS is the section contents. INFO is a plist used as
  383. a communication channel."
  384. contents)
  385. ;;;; Template
  386. (defun org-md-inner-template (contents info)
  387. "Return body of document after converting it to Markdown syntax.
  388. CONTENTS is the transcoded contents string. INFO is a plist
  389. holding export options."
  390. ;; Make sure CONTENTS is separated from table of contents and
  391. ;; footnotes with at least a blank line.
  392. (org-trim (org-html-inner-template (concat "\n" contents "\n") info)))
  393. (defun org-md-template (contents _info)
  394. "Return complete document string after Markdown conversion.
  395. CONTENTS is the transcoded contents string. INFO is a plist used
  396. as a communication channel."
  397. contents)
  398. ;;; Interactive function
  399. ;;;###autoload
  400. (defun org-md-export-as-markdown (&optional async subtreep visible-only)
  401. "Export current buffer to a Markdown buffer.
  402. If narrowing is active in the current buffer, only export its
  403. narrowed part.
  404. If a region is active, export that region.
  405. A non-nil optional argument ASYNC means the process should happen
  406. asynchronously. The resulting buffer should be accessible
  407. through the `org-export-stack' interface.
  408. When optional argument SUBTREEP is non-nil, export the sub-tree
  409. at point, extracting information from the headline properties
  410. first.
  411. When optional argument VISIBLE-ONLY is non-nil, don't export
  412. contents of hidden elements.
  413. Export is done in a buffer named \"*Org MD Export*\", which will
  414. be displayed when `org-export-show-temporary-export-buffer' is
  415. non-nil."
  416. (interactive)
  417. (org-export-to-buffer 'md "*Org MD Export*"
  418. async subtreep visible-only nil nil (lambda () (text-mode))))
  419. ;;;###autoload
  420. (defun org-md-convert-region-to-md ()
  421. "Assume the current region has Org syntax, and convert it to Markdown.
  422. This can be used in any buffer. For example, you can write an
  423. itemized list in Org syntax in a Markdown buffer and use
  424. this command to convert it."
  425. (interactive)
  426. (org-export-replace-region-by 'md))
  427. ;;;###autoload
  428. (defun org-md-export-to-markdown (&optional async subtreep visible-only)
  429. "Export current buffer to a Markdown file.
  430. If narrowing is active in the current buffer, only export its
  431. narrowed part.
  432. If a region is active, export that region.
  433. A non-nil optional argument ASYNC means the process should happen
  434. asynchronously. The resulting file should be accessible through
  435. the `org-export-stack' interface.
  436. When optional argument SUBTREEP is non-nil, export the sub-tree
  437. at point, extracting information from the headline properties
  438. first.
  439. When optional argument VISIBLE-ONLY is non-nil, don't export
  440. contents of hidden elements.
  441. Return output file's name."
  442. (interactive)
  443. (let ((outfile (org-export-output-file-name ".md" subtreep)))
  444. (org-export-to-file 'md outfile async subtreep visible-only)))
  445. ;;;###autoload
  446. (defun org-md-publish-to-md (plist filename pub-dir)
  447. "Publish an org file to Markdown.
  448. FILENAME is the filename of the Org file to be published. PLIST
  449. is the property list for the given project. PUB-DIR is the
  450. publishing directory.
  451. Return output file name."
  452. (org-publish-org-to 'md filename ".md" plist pub-dir))
  453. (provide 'ox-md)
  454. ;; Local variables:
  455. ;; generated-autoload-file: "org-loaddefs.el"
  456. ;; End:
  457. ;;; ox-md.el ends here