ox-md.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. ;;; ox-md.el --- Markdown Back-End for Org Export Engine
  2. ;; Copyright (C) 2012, 2013 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <n.goaziou@gmail.com>
  4. ;; Keywords: org, wp, markdown
  5. ;; GNU Emacs 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. ;; GNU Emacs 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 GNU Emacs. 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 `html' back-end. See Org manual for more
  18. ;; information.
  19. ;;; Code:
  20. (eval-when-compile (require 'cl))
  21. (require 'ox-html)
  22. ;;; User-Configurable Variables
  23. (defgroup org-export-md nil
  24. "Options specific to Markdown export back-end."
  25. :tag "Org Markdown"
  26. :group 'org-export
  27. :version "24.4"
  28. :package-version '(Org . "8.0"))
  29. (defcustom org-md-headline-style 'atx
  30. "Style used to format headlines.
  31. This variable can be set to either `atx' or `setext'."
  32. :group 'org-export-md
  33. :type '(choice
  34. (const :tag "Use \"atx\" style" atx)
  35. (const :tag "Use \"Setext\" style" setext)))
  36. ;;; Define Back-End
  37. (org-export-define-derived-backend 'md 'html
  38. :export-block '("MD" "MARKDOWN")
  39. :filters-alist '((:filter-parse-tree . org-md-separate-elements))
  40. :menu-entry
  41. '(?m "Export to Markdown"
  42. ((?M "To temporary buffer"
  43. (lambda (a s v b) (org-md-export-as-markdown a s v)))
  44. (?m "To file" (lambda (a s v b) (org-md-export-to-markdown a s v)))
  45. (?o "To file and open"
  46. (lambda (a s v b)
  47. (if a (org-md-export-to-markdown t s v)
  48. (org-open-file (org-md-export-to-markdown nil s v)))))))
  49. :translate-alist '((bold . org-md-bold)
  50. (code . org-md-verbatim)
  51. (comment . (lambda (&rest args) ""))
  52. (comment-block . (lambda (&rest args) ""))
  53. (example-block . org-md-example-block)
  54. (fixed-width . org-md-example-block)
  55. (footnote-definition . ignore)
  56. (footnote-reference . ignore)
  57. (headline . org-md-headline)
  58. (horizontal-rule . org-md-horizontal-rule)
  59. (inline-src-block . org-md-verbatim)
  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. "Make sure elements are separated by at least one blank line.
  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. Assume BACKEND is `md'."
  79. (org-element-map tree org-element-all-elements
  80. (lambda (elem)
  81. (unless (eq (org-element-type elem) 'org-data)
  82. (org-element-put-property
  83. elem :post-blank
  84. (let ((post-blank (org-element-property :post-blank elem)))
  85. (if (not post-blank) 1 (max 1 post-blank)))))))
  86. ;; Return updated tree.
  87. tree)
  88. ;;; Transcode Functions
  89. ;;;; Bold
  90. (defun org-md-bold (bold contents info)
  91. "Transcode BOLD object into Markdown format.
  92. CONTENTS is the text within bold markup. INFO is a plist used as
  93. a communication channel."
  94. (format "**%s**" contents))
  95. ;;;; Code and Verbatim
  96. (defun org-md-verbatim (verbatim contents info)
  97. "Transcode VERBATIM object into Markdown format.
  98. CONTENTS is nil. INFO is a plist used as a communication
  99. channel."
  100. (let ((value (org-element-property :value verbatim)))
  101. (format (cond ((not (string-match "`" value)) "`%s`")
  102. ((or (string-match "\\``" value)
  103. (string-match "`\\'" value))
  104. "`` %s ``")
  105. (t "``%s``"))
  106. value)))
  107. ;;;; Example Block and Src Block
  108. (defun org-md-example-block (example-block contents info)
  109. "Transcode EXAMPLE-BLOCK element into Markdown format.
  110. CONTENTS is nil. INFO is a plist used as a communication
  111. channel."
  112. (replace-regexp-in-string
  113. "^" " "
  114. (org-remove-indentation
  115. (org-element-property :value example-block))))
  116. ;;;; Headline
  117. (defun org-md-headline (headline contents info)
  118. "Transcode HEADLINE element into Markdown format.
  119. CONTENTS is the headline contents. INFO is a plist used as
  120. a communication channel."
  121. (unless (org-element-property :footnote-section-p headline)
  122. (let* ((level (org-export-get-relative-level headline info))
  123. (title (org-export-data (org-element-property :title headline) info))
  124. (todo (and (plist-get info :with-todo-keywords)
  125. (let ((todo (org-element-property :todo-keyword
  126. headline)))
  127. (and todo (concat (org-export-data todo info) " ")))))
  128. (tags (and (plist-get info :with-tags)
  129. (let ((tag-list (org-export-get-tags headline info)))
  130. (and tag-list
  131. (format " :%s:"
  132. (mapconcat 'identity tag-list ":"))))))
  133. (priority
  134. (and (plist-get info :with-priority)
  135. (let ((char (org-element-property :priority headline)))
  136. (and char (format "[#%c] " char)))))
  137. ;; Headline text without tags.
  138. (heading (concat todo priority title)))
  139. (cond
  140. ;; Cannot create a headline. Fall-back to a list.
  141. ((or (org-export-low-level-p headline info)
  142. (not (memq org-md-headline-style '(atx setext)))
  143. (and (eq org-md-headline-style 'atx) (> level 6))
  144. (and (eq org-md-headline-style 'setext) (> level 2)))
  145. (let ((bullet
  146. (if (not (org-export-numbered-headline-p headline info)) "-"
  147. (concat (number-to-string
  148. (car (last (org-export-get-headline-number
  149. headline info))))
  150. "."))))
  151. (concat bullet (make-string (- 4 (length bullet)) ? ) heading tags
  152. "\n\n"
  153. (and contents
  154. (replace-regexp-in-string "^" " " contents)))))
  155. ;; Use "Setext" style.
  156. ((eq org-md-headline-style 'setext)
  157. (concat heading tags "\n"
  158. (make-string (length heading) (if (= level 1) ?= ?-))
  159. "\n\n"
  160. contents))
  161. ;; Use "atx" style.
  162. (t (concat (make-string level ?#) " " heading tags "\n\n" contents))))))
  163. ;;;; Horizontal Rule
  164. (defun org-md-horizontal-rule (horizontal-rule contents info)
  165. "Transcode HORIZONTAL-RULE element into Markdown format.
  166. CONTENTS is the horizontal rule contents. INFO is a plist used
  167. as a communication channel."
  168. "---")
  169. ;;;; Italic
  170. (defun org-md-italic (italic contents info)
  171. "Transcode ITALIC object into Markdown format.
  172. CONTENTS is the text within italic markup. INFO is a plist used
  173. as a communication channel."
  174. (format "*%s*" contents))
  175. ;;;; Item
  176. (defun org-md-item (item contents info)
  177. "Transcode ITEM element into Markdown format.
  178. CONTENTS is the item contents. INFO is a plist used as
  179. a communication channel."
  180. (let* ((type (org-element-property :type (org-export-get-parent item)))
  181. (struct (org-element-property :structure item))
  182. (bullet (if (not (eq type 'ordered)) "-"
  183. (concat (number-to-string
  184. (car (last (org-list-get-item-number
  185. (org-element-property :begin item)
  186. struct
  187. (org-list-prevs-alist struct)
  188. (org-list-parents-alist struct)))))
  189. "."))))
  190. (concat bullet
  191. (make-string (- 4 (length bullet)) ? )
  192. (case (org-element-property :checkbox item)
  193. (on "[X] ")
  194. (trans "[-] ")
  195. (off "[ ] "))
  196. (let ((tag (org-element-property :tag item)))
  197. (and tag (format "**%s:** "(org-export-data tag info))))
  198. (org-trim (replace-regexp-in-string "^" " " contents)))))
  199. ;;;; Line Break
  200. (defun org-md-line-break (line-break contents info)
  201. "Transcode LINE-BREAK object into Markdown format.
  202. CONTENTS is nil. INFO is a plist used as a communication
  203. channel."
  204. " \n")
  205. ;;;; Link
  206. (defun org-md-link (link contents info)
  207. "Transcode LINE-BREAK object into Markdown format.
  208. CONTENTS is the link's description. INFO is a plist used as
  209. a communication channel."
  210. (let ((--link-org-files-as-html-maybe
  211. (function
  212. (lambda (raw-path info)
  213. ;; Treat links to `file.org' as links to `file.html', if
  214. ;; needed. See `org-html-link-org-files-as-html'.
  215. (cond
  216. ((and org-html-link-org-files-as-html
  217. (string= ".org"
  218. (downcase (file-name-extension raw-path "."))))
  219. (concat (file-name-sans-extension raw-path) "."
  220. (plist-get info :html-extension)))
  221. (t raw-path)))))
  222. (type (org-element-property :type link)))
  223. (cond ((member type '("custom-id" "id"))
  224. (let ((destination (org-export-resolve-id-link link info)))
  225. (if (stringp destination) ; External file.
  226. (let ((path (funcall --link-org-files-as-html-maybe
  227. destination info)))
  228. (if (not contents) (format "<%s>" path)
  229. (format "[%s](%s)" contents path)))
  230. (concat
  231. (and contents (concat contents " "))
  232. (format "(%s)"
  233. (format (org-export-translate "See section %s" :html info)
  234. (mapconcat 'number-to-string
  235. (org-export-get-headline-number
  236. destination info)
  237. ".")))))))
  238. ((org-export-inline-image-p link org-html-inline-image-rules)
  239. (let ((path (let ((raw-path (org-element-property :path link)))
  240. (if (not (file-name-absolute-p raw-path)) raw-path
  241. (expand-file-name raw-path)))))
  242. (format "![%s](%s)"
  243. (let ((caption (org-export-get-caption
  244. (org-export-get-parent-element link))))
  245. (when caption (org-export-data caption info)))
  246. path)))
  247. ((string= type "coderef")
  248. (let ((ref (org-element-property :path link)))
  249. (format (org-export-get-coderef-format ref contents)
  250. (org-export-resolve-coderef ref info))))
  251. ((equal type "radio")
  252. (let ((destination (org-export-resolve-radio-link link info)))
  253. (org-export-data (org-element-contents destination) info)))
  254. ((equal type "fuzzy")
  255. (let ((destination (org-export-resolve-fuzzy-link link info)))
  256. (if (org-string-nw-p contents) contents
  257. (when destination
  258. (let ((number (org-export-get-ordinal destination info)))
  259. (when number
  260. (if (atom number) (number-to-string number)
  261. (mapconcat 'number-to-string number "."))))))))
  262. (t (let* ((raw-path (org-element-property :path link))
  263. (path (cond
  264. ((member type '("http" "https" "ftp"))
  265. (concat type ":" raw-path))
  266. ((equal type "file")
  267. ;; Treat links to ".org" files as ".html",
  268. ;; if needed.
  269. (setq raw-path
  270. (funcall --link-org-files-as-html-maybe
  271. raw-path info))
  272. ;; If file path is absolute, prepend it
  273. ;; with protocol component - "file://".
  274. (if (not (file-name-absolute-p raw-path)) raw-path
  275. (concat "file://" (expand-file-name raw-path))))
  276. (t raw-path))))
  277. (if (not contents) (format "<%s>" path)
  278. (format "[%s](%s)" contents path)))))))
  279. ;;;; Paragraph
  280. (defun org-md-paragraph (paragraph contents info)
  281. "Transcode PARAGRAPH element into Markdown format.
  282. CONTENTS is the paragraph contents. INFO is a plist used as
  283. a communication channel."
  284. (let ((first-object (car (org-element-contents paragraph))))
  285. ;; If paragraph starts with a #, protect it.
  286. (if (and (stringp first-object) (string-match "\\`#" first-object))
  287. (replace-regexp-in-string "\\`#" "\\#" contents nil t)
  288. contents)))
  289. ;;;; Plain List
  290. (defun org-md-plain-list (plain-list contents info)
  291. "Transcode PLAIN-LIST element into Markdown format.
  292. CONTENTS is the plain-list contents. INFO is a plist used as
  293. a communication channel."
  294. contents)
  295. ;;;; Plain Text
  296. (defun org-md-plain-text (text info)
  297. "Transcode a TEXT string into Markdown format.
  298. TEXT is the string to transcode. INFO is a plist holding
  299. contextual information."
  300. (when (plist-get info :with-smart-quotes)
  301. (setq text (org-export-activate-smart-quotes text :html info)))
  302. ;; Protect ambiguous #. This will protect # at the beginning of
  303. ;; a line, but not at the beginning of a paragraph. See
  304. ;; `org-md-paragraph'.
  305. (setq text (replace-regexp-in-string "\n#" "\n\\\\#" text))
  306. ;; Protect ambiguous !
  307. (setq text (replace-regexp-in-string "\\(!\\)\\[" "\\\\!" text nil nil 1))
  308. ;; Protect `, *, _ and \
  309. (setq text (replace-regexp-in-string "[`*_\\]" "\\\\\\&" text))
  310. ;; Handle special strings, if required.
  311. (when (plist-get info :with-special-strings)
  312. (setq text (org-html-convert-special-strings text)))
  313. ;; Handle break preservation, if required.
  314. (when (plist-get info :preserve-breaks)
  315. (setq text (replace-regexp-in-string "[ \t]*\n" " \n" text)))
  316. ;; Return value.
  317. text)
  318. ;;;; Quote Block
  319. (defun org-md-quote-block (quote-block contents info)
  320. "Transcode QUOTE-BLOCK element into Markdown format.
  321. CONTENTS is the quote-block contents. INFO is a plist used as
  322. a communication channel."
  323. (replace-regexp-in-string
  324. "^" "> "
  325. (replace-regexp-in-string "\n\\'" "" contents)))
  326. ;;;; Section
  327. (defun org-md-section (section contents info)
  328. "Transcode SECTION element into Markdown format.
  329. CONTENTS is the section contents. INFO is a plist used as
  330. a communication channel."
  331. contents)
  332. ;;;; Template
  333. (defun org-md-template (contents info)
  334. "Return complete document string after Markdown conversion.
  335. CONTENTS is the transcoded contents string. INFO is a plist used
  336. as a communication channel."
  337. contents)
  338. ;;; Interactive function
  339. ;;;###autoload
  340. (defun org-md-export-as-markdown (&optional async subtreep visible-only)
  341. "Export current buffer to a Markdown buffer.
  342. If narrowing is active in the current buffer, only export its
  343. narrowed part.
  344. If a region is active, export that region.
  345. A non-nil optional argument ASYNC means the process should happen
  346. asynchronously. The resulting buffer should be accessible
  347. through the `org-export-stack' interface.
  348. When optional argument SUBTREEP is non-nil, export the sub-tree
  349. at point, extracting information from the headline properties
  350. first.
  351. When optional argument VISIBLE-ONLY is non-nil, don't export
  352. contents of hidden elements.
  353. Export is done in a buffer named \"*Org MD Export*\", which will
  354. be displayed when `org-export-show-temporary-export-buffer' is
  355. non-nil."
  356. (interactive)
  357. (org-export-to-buffer 'md "*Org MD Export*"
  358. async subtreep visible-only nil nil (lambda () (text-mode))))
  359. ;;;###autoload
  360. (defun org-md-convert-region-to-md ()
  361. "Assume the current region has org-mode syntax, and convert it to Markdown.
  362. This can be used in any buffer. For example, you can write an
  363. itemized list in org-mode syntax in a Markdown buffer and use
  364. this command to convert it."
  365. (interactive)
  366. (org-export-replace-region-by 'md))
  367. ;;;###autoload
  368. (defun org-md-export-to-markdown (&optional async subtreep visible-only)
  369. "Export current buffer to a Markdown file.
  370. If narrowing is active in the current buffer, only export its
  371. narrowed part.
  372. If a region is active, export that region.
  373. A non-nil optional argument ASYNC means the process should happen
  374. asynchronously. The resulting file should be accessible through
  375. the `org-export-stack' interface.
  376. When optional argument SUBTREEP is non-nil, export the sub-tree
  377. at point, extracting information from the headline properties
  378. first.
  379. When optional argument VISIBLE-ONLY is non-nil, don't export
  380. contents of hidden elements.
  381. Return output file's name."
  382. (interactive)
  383. (let ((outfile (org-export-output-file-name ".md" subtreep)))
  384. (org-export-to-file 'md outfile async subtreep visible-only)))
  385. (provide 'ox-md)
  386. ;; Local variables:
  387. ;; generated-autoload-file: "org-loaddefs.el"
  388. ;; End:
  389. ;;; ox-md.el ends here