ox-md.el 17 KB

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