org-md.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. ;;; org-md.el --- Markdown Back-End for Org Export Engine
  2. ;; Copyright (C) 2012 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <n.goaziou@gmail.com>
  4. ;; Keywords: org, wp, tex
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; This library implements a Markdown back-end (vanilla flavour) for
  17. ;; Org exporter, based on `e-html'.
  18. ;;
  19. ;; It provides two commands for export, depending on the desired
  20. ;; output: `org-md-export-as-markdown' (temporary buffer) and
  21. ;; `org-md-export-to-markdown' ("md" file).
  22. ;;; Code:
  23. (require 'org-e-html)
  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.2")
  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 e-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" org-md-export-as-markdown)
  44. (?m "To file" org-md-export-to-markdown)
  45. (?o "To file and open"
  46. (lambda (s v b) (org-open-file (org-md-export-to-markdown s v b))))))
  47. :translate-alist ((bold . org-md-bold)
  48. (code . org-md-verbatim)
  49. (example-block . org-md-example-block)
  50. (footnote-definition . ignore)
  51. (footnote-reference . ignore)
  52. (headline . org-md-headline)
  53. (horizontal-rule . org-md-horizontal-rule)
  54. (inline-src-block . org-md-verbatim)
  55. (italic . org-md-italic)
  56. (item . org-md-item)
  57. (line-break . org-md-line-break)
  58. (link . org-md-link)
  59. (paragraph . org-md-paragraph)
  60. (plain-list . org-md-plain-list)
  61. (plain-text . org-md-plain-text)
  62. (quote-block . org-md-quote-block)
  63. (quote-section . org-md-example-block)
  64. (section . org-md-section)
  65. (src-block . org-md-example-block)
  66. (template . org-md-template)
  67. (verbatim . org-md-verbatim)))
  68. ;;; Filters
  69. (defun org-md-separate-elements (tree backend info)
  70. "Make sure elements are separated by at least one blank line.
  71. TREE is the parse tree being exported. BACKEND is the export
  72. back-end used. INFO is a plist used as a communication channel.
  73. Assume BACKEND is `md'."
  74. (org-element-map
  75. tree org-element-all-elements
  76. (lambda (elem)
  77. (unless (eq (org-element-type elem) 'org-data)
  78. (org-element-put-property
  79. elem :post-blank
  80. (let ((post-blank (org-element-property :post-blank elem)))
  81. (if (not post-blank) 1 (max 1 post-blank)))))))
  82. ;; Return updated tree.
  83. tree)
  84. ;;; Transcode Functions
  85. ;;;; Bold
  86. (defun org-md-bold (bold contents info)
  87. "Transcode BOLD object into Markdown format.
  88. CONTENTS is the text within bold markup. INFO is a plist used as
  89. a communication channel."
  90. (format "**%s**" contents))
  91. ;;;; Code and Verbatim
  92. (defun org-md-verbatim (verbatim contents info)
  93. "Transcode VERBATIM object into Markdown format.
  94. CONTENTS is nil. INFO is a plist used as a communication
  95. channel."
  96. (let ((value (org-element-property :value verbatim)))
  97. (format (cond ((not (string-match "`" value)) "`%s`")
  98. ((or (string-match "\\``" value)
  99. (string-match "`\\'" value))
  100. "`` %s ``")
  101. (t "``%s``"))
  102. value)))
  103. ;;;; Example Block and Src Block
  104. (defun org-md-example-block (example-block contents info)
  105. "Transcode EXAMPLE-BLOCK element into Markdown format.
  106. CONTENTS is nil. INFO is a plist used as a communication
  107. channel."
  108. (replace-regexp-in-string
  109. "^" " "
  110. (org-remove-indentation
  111. (org-element-property :value example-block))))
  112. ;;;; Headline
  113. (defun org-md-headline (headline contents info)
  114. "Transcode HEADLINE element into Markdown format.
  115. CONTENTS is the headline contents. INFO is a plist used as
  116. a communication channel."
  117. (unless (org-element-property :footnote-section-p headline)
  118. (let* ((level (org-export-get-relative-level headline info))
  119. (title (org-export-data (org-element-property :title headline) info))
  120. (todo (and (plist-get info :with-todo-keywords)
  121. (let ((todo (org-element-property :todo-keyword
  122. headline)))
  123. (and todo (concat (org-export-data todo info) " ")))))
  124. (tags (and (plist-get info :with-tags)
  125. (let ((tag-list (org-export-get-tags headline info)))
  126. (and tag-list
  127. (format " :%s:"
  128. (mapconcat 'identity tag-list ":"))))))
  129. (priority
  130. (and (plist-get info :with-priority)
  131. (let ((char (org-element-property :priority headline)))
  132. (and char (format "[#%c] " char)))))
  133. ;; Headline text without tags.
  134. (heading (concat todo priority title)))
  135. (cond
  136. ;; Cannot create an headline. Fall-back to a list.
  137. ((or (org-export-low-level-p headline info)
  138. (not (memq org-md-headline-style '(atx setext)))
  139. (and (eq org-md-headline-style 'atx) (> level 6))
  140. (and (eq org-md-headline-style 'setext) (> level 2)))
  141. (let ((bullet
  142. (if (not (org-export-numbered-headline-p headline info)) "-"
  143. (concat (number-to-string
  144. (car (last (org-export-get-headline-number
  145. headline info))))
  146. "."))))
  147. (concat bullet (make-string (- 4 (length bullet)) ? ) heading tags
  148. "\n\n"
  149. (and contents
  150. (replace-regexp-in-string "^" " " contents)))))
  151. ;; Use "Setext" style.
  152. ((eq org-md-headline-style 'setext)
  153. (concat heading tags "\n"
  154. (make-string (length heading) (if (= level 1) ?= ?-))
  155. "\n\n"
  156. contents))
  157. ;; Use "atx" style.
  158. (t (concat (make-string level ?#) " " heading tags "\n\n" contents))))))
  159. ;;;; Horizontal Rule
  160. (defun org-md-horizontal-rule (horizontal-rule contents info)
  161. "Transcode HORIZONTAL-RULE element into Markdown format.
  162. CONTENTS is the horizontal rule contents. INFO is a plist used
  163. as a communication channel."
  164. "---")
  165. ;;;; Italic
  166. (defun org-md-italic (italic contents info)
  167. "Transcode ITALIC object into Markdown format.
  168. CONTENTS is the text within italic markup. INFO is a plist used
  169. as a communication channel."
  170. (format "*%s*" contents))
  171. ;;;; Item
  172. (defun org-md-item (item contents info)
  173. "Transcode ITEM element into Markdown format.
  174. CONTENTS is the item contents. INFO is a plist used as
  175. a communication channel."
  176. (let* ((type (org-element-property :type (org-export-get-parent item)))
  177. (struct (org-element-property :structure item))
  178. (bullet (if (not (eq type 'ordered)) "-"
  179. (concat (number-to-string
  180. (car (last (org-list-get-item-number
  181. (org-element-property :begin item)
  182. struct
  183. (org-list-prevs-alist struct)
  184. (org-list-parents-alist struct)))))
  185. "."))))
  186. (concat bullet
  187. (make-string (- 4 (length bullet)) ? )
  188. (case (org-element-property :checkbox item)
  189. (on "[X] ")
  190. (trans "[-] ")
  191. (off "[ ] "))
  192. (let ((tag (org-element-property :tag item)))
  193. (and tag (format "**%s:** "(org-export-data tag info))))
  194. (org-trim (replace-regexp-in-string "^" " " contents)))))
  195. ;;;; Line Break
  196. (defun org-md-line-break (line-break contents info)
  197. "Transcode LINE-BREAK object into Markdown format.
  198. CONTENTS is nil. INFO is a plist used as a communication
  199. channel."
  200. " ")
  201. ;;;; Link
  202. (defun org-md-link (link contents info)
  203. "Transcode LINE-BREAK object into Markdown format.
  204. CONTENTS is the link's description. INFO is a plist used as
  205. a communication channel."
  206. (let ((--link-org-files-as-html-maybe
  207. (function
  208. (lambda (raw-path info)
  209. ;; Treat links to `file.org' as links to `file.html', if
  210. ;; needed. See `org-e-html-link-org-files-as-html'.
  211. (cond
  212. ((and org-e-html-link-org-files-as-html
  213. (string= ".org"
  214. (downcase (file-name-extension raw-path "."))))
  215. (concat (file-name-sans-extension raw-path) "."
  216. (plist-get info :html-extension)))
  217. (t raw-path)))))
  218. (type (org-element-property :type link)))
  219. (cond ((member type '("custom-id" "id"))
  220. (let ((destination (org-export-resolve-id-link link info)))
  221. (if (stringp destination) ; External file.
  222. (let ((path (funcall --link-org-files-as-html-maybe
  223. destination info)))
  224. (if (not contents) (format "<%s>" path)
  225. (format "[%s](%s)" contents path)))
  226. (concat
  227. (and contents (concat contents " "))
  228. (format "(%s)"
  229. (format (org-export-translate "See section %s" :html info)
  230. (mapconcat 'number-to-string
  231. (org-export-get-headline-number
  232. destination info)
  233. ".")))))))
  234. ((org-export-inline-image-p link org-e-html-inline-image-rules)
  235. (format "![%s](%s)"
  236. (let ((caption (org-export-get-caption
  237. (org-export-get-parent-element link))))
  238. (when caption (org-export-data caption info)))
  239. path))
  240. ((string= type "coderef")
  241. (let ((ref (org-element-property :path link)))
  242. (format (org-export-get-coderef-format ref contents)
  243. (org-export-resolve-coderef ref info))))
  244. ((equal type "radio")
  245. (let ((destination (org-export-resolve-radio-link link info)))
  246. (org-export-data (org-element-contents destination) info)))
  247. ((equal type "fuzzy")
  248. (let ((destination (org-export-resolve-fuzzy-link link info)))
  249. ;; Ignore invisible "#+TARGET: path".
  250. (unless (eq (org-element-type destination) 'keyword)
  251. (if (org-string-nw-p contents) contents
  252. (when destination
  253. (let ((number (org-export-get-ordinal destination info)))
  254. (when number
  255. (if (atom number) (number-to-string number)
  256. (mapconcat 'number-to-string number ".")))))))))
  257. (t (let* ((raw-path (org-element-property :path link))
  258. (path (cond
  259. ((member type '("http" "https" "ftp"))
  260. (concat type ":" raw-path))
  261. ((equal type "file")
  262. ;; Treat links to ".org" files as ".html",
  263. ;; if needed.
  264. (setq raw-path
  265. (funcall --link-org-files-as-html-maybe
  266. raw-path info))
  267. ;; If file path is absolute, prepend it
  268. ;; with protocol component - "file://".
  269. (if (not (file-name-absolute-p raw-path)) raw-path
  270. (concat "file://" (expand-file-name raw-path))))
  271. (t raw-path))))
  272. (if (not contents) (format "<%s>" path)
  273. (format "[%s](%s)" contents path)))))))
  274. ;;;; Paragraph
  275. (defun org-md-paragraph (paragraph contents info)
  276. "Transcode PARAGRAPH element into Markdown format.
  277. CONTENTS is the paragraph contents. INFO is a plist used as
  278. a communication channel."
  279. (let ((first-object (car (org-element-contents paragraph))))
  280. ;; If paragraph starts with a #, protect it.
  281. (if (and (stringp first-object) (string-match "\\`#" first-object))
  282. (replace-regexp-in-string "\\`#" "\\#" contents nil t)
  283. contents)))
  284. ;;;; Plain List
  285. (defun org-md-plain-list (plain-list contents info)
  286. "Transcode PLAIN-LIST element into Markdown format.
  287. CONTENTS is the plain-list contents. INFO is a plist used as
  288. a communication channel."
  289. contents)
  290. ;;;; Plain Text
  291. (defun org-md-plain-text (text info)
  292. "Transcode a TEXT string into Markdown format.
  293. TEXT is the string to transcode. INFO is a plist holding
  294. contextual information."
  295. ;; Protect ambiguous #. This will protect # at the beginning of
  296. ;; a line, but not at the beginning of a paragraph. See
  297. ;; `org-md-paragraph'.
  298. (setq text (replace-regexp-in-string "\n#" "\n\\\\#" text))
  299. ;; Protect ambiguous !
  300. (setq text (replace-regexp-in-string "\\(!\\)\\[" "\\\\!" text nil nil 1))
  301. ;; Protect `, *, _ and \
  302. (setq text
  303. (replace-regexp-in-string
  304. "[`*_\\]" (lambda (rep) (concat "\\\\" (match-string 1 rep))) text))
  305. ;; Handle special strings, if required.
  306. (when (plist-get info :with-special-strings)
  307. (setq text (org-e-html-convert-special-strings text)))
  308. ;; Handle break preservation, if required.
  309. (when (plist-get info :preserve-breaks)
  310. (setq text (replace-regexp-in-string "[ \t]*\n" " \n" text)))
  311. ;; Return value.
  312. text)
  313. ;;;; Quote Block
  314. (defun org-md-quote-block (quote-block contents info)
  315. "Transcode QUOTE-BLOCK element into Markdown format.
  316. CONTENTS is the quote-block contents. INFO is a plist used as
  317. a communication channel."
  318. (replace-regexp-in-string
  319. "^" "> "
  320. (replace-regexp-in-string "\n\\'" "" contents)))
  321. ;;;; Section
  322. (defun org-md-section (section contents info)
  323. "Transcode SECTION element into Markdown format.
  324. CONTENTS is the section contents. INFO is a plist used as
  325. a communication channel."
  326. contents)
  327. ;;;; Template
  328. (defun org-md-template (contents info)
  329. "Return complete document string after Markdown conversion.
  330. CONTENTS is the transcoded contents string. INFO is a plist used
  331. as a communication channel."
  332. contents)
  333. ;;; Interactive function
  334. ;;;###autoload
  335. (defun org-md-export-as-markdown (&optional subtreep visible-only)
  336. "Export current buffer to a text buffer.
  337. If narrowing is active in the current buffer, only export its
  338. narrowed part.
  339. If a region is active, export that region.
  340. When optional argument SUBTREEP is non-nil, export the sub-tree
  341. at point, extracting information from the headline properties
  342. first.
  343. When optional argument VISIBLE-ONLY is non-nil, don't export
  344. contents of hidden elements.
  345. Export is done in a buffer named \"*Org MD Export*\", which will
  346. be displayed when `org-export-show-temporary-export-buffer' is
  347. non-nil."
  348. (interactive)
  349. (let ((outbuf (org-export-to-buffer
  350. 'md "*Org MD Export*" subtreep visible-only)))
  351. (with-current-buffer outbuf (text-mode))
  352. (when org-export-show-temporary-export-buffer
  353. (switch-to-buffer-other-window outbuf))))
  354. ;;;###autoload
  355. (defun org-md-export-to-markdown (&optional subtreep visible-only pub-dir)
  356. "Export current buffer to a Markdown file.
  357. If narrowing is active in the current buffer, only export its
  358. narrowed part.
  359. If a region is active, export that region.
  360. When optional argument SUBTREEP is non-nil, export the sub-tree
  361. at point, extracting information from the headline properties
  362. first.
  363. When optional argument VISIBLE-ONLY is non-nil, don't export
  364. contents of hidden elements.
  365. When optional argument PUB-DIR is set, use it as the publishing
  366. directory.
  367. Return output file's name."
  368. (interactive)
  369. (let ((outfile (org-export-output-file-name ".md" subtreep pub-dir)))
  370. (org-export-to-file 'md outfile subtreep visible-only)))
  371. (provide 'org-md)
  372. ;;; org-md.el ends here