ox-md.el 16 KB

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