org-md.el 14 KB

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