ox-md.el 17 KB

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