ox-md.el 17 KB

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