org-md.el 13 KB

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