org-md.el 15 KB

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