ox-md.el 16 KB

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