ox-md.el 16 KB

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