ox-md.el 18 KB

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