ox-gfm.el 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. ;;; ox-gfm.el --- Github Flavored Markdown Back-End for Org Export Engine
  2. ;; Copyright (C) 2014 Lars Tveito
  3. ;; Author: Lars Tveito
  4. ;; Keywords: org, wp, markdown, github
  5. ;; This file is not 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 (github flavor) for Org
  18. ;; exporter, based on the `md' back-end.
  19. ;;; Code:
  20. (require 'ox-md)
  21. ;;; User-Configurable Variables
  22. (defgroup org-export-gfm nil
  23. "Options specific to Markdown export back-end."
  24. :tag "Org Github Flavored Markdown"
  25. :group 'org-export
  26. :version "24.4"
  27. :package-version '(Org . "8.0"))
  28. (defcustom org-gfm-lang '(("emacs-lisp" . "lisp") ("elisp" . "lisp"))
  29. "Alist of languages that are not recognized by Github, to
  30. languages that are. Emacs lisp is a good example of this, where
  31. we can use lisp as a nice replacement."
  32. :group 'org-export-gfm)
  33. ;;; Define Back-End
  34. (org-export-define-derived-backend 'gfm 'md
  35. :export-block '("GFM" "GITHUB FLAVORED MARKDOWN")
  36. :filters-alist '((:filter-parse-tree . org-md-separate-elements))
  37. :menu-entry
  38. '(?g "Export to Github Flavored Markdown"
  39. ((?G "To temporary buffer"
  40. (lambda (a s v b) (org-gfm-export-as-markdown a s v)))
  41. (?g "To file" (lambda (a s v b) (org-gfm-export-to-markdown a s v)))
  42. (?o "To file and open"
  43. (lambda (a s v b)
  44. (if a (org-gfm-export-to-markdown t s v)
  45. (org-open-file (org-gfm-export-to-markdown nil s v)))))))
  46. :translate-alist '((inner-template . org-gfm-inner-template)
  47. (strike-through . org-gfm-strike-through)
  48. (src-block . org-gfm-src-block)))
  49. ;;; Transcode Functions
  50. ;;;; Src Block
  51. (defun org-gfm-src-block (src-block contents info)
  52. "Transcode SRC-BLOCK element into Github Flavored Markdown
  53. format. CONTENTS is nil. INFO is a plist used as a communication
  54. channel."
  55. (let* ((lang (org-element-property :language src-block))
  56. (lang (or (assoc-default lang org-gfm-lang) lang))
  57. (code (org-export-format-code-default src-block info))
  58. (prefix (concat "```" lang "\n"))
  59. (suffix "```"))
  60. (concat prefix code suffix)))
  61. ;;;; Strike-Through
  62. (defun org-html-strike-through (strike-through contents info)
  63. "Transcode STRIKE-THROUGH from Org to Markdown (GFM).
  64. CONTENTS is the text with strike-through markup. INFO is a plist
  65. holding contextual information."
  66. (format "~~%s~~" contents))
  67. ;;;; Table of contents
  68. (defun org-gfm-format-toc (headline)
  69. "Return an appropriate table of contents entry for HEADLINE. INFO is a
  70. plist used as a communication channel."
  71. (let* ((title (org-export-data
  72. (org-export-get-alt-title headline info) info))
  73. (level (1- (org-element-property :level headline)))
  74. (indent (concat (make-string (* level 2) ? )))
  75. (ref-str (replace-regexp-in-string " " "-" (downcase title))))
  76. (concat indent "- [" title "]" "(#" ref-str ")")))
  77. ;;;; Template
  78. (defun org-gfm-inner-template (contents info)
  79. "Return body of document after converting it to Markdown syntax.
  80. CONTENTS is the transcoded contents string. INFO is a plist
  81. holding export options."
  82. (let* ((depth (plist-get info :with-toc))
  83. (headlines (and depth (org-export-collect-headlines info depth)))
  84. (toc-string (or (mapconcat 'org-gfm-format-toc headlines "\n") ""))
  85. (toc-tail (if headlines "\n\n" "")))
  86. (concat toc-string toc-tail contents)))
  87. ;;; Interactive function
  88. ;;;###autoload
  89. (defun org-gfm-export-as-markdown (&optional async subtreep visible-only)
  90. "Export current buffer to a Github Flavored Markdown buffer.
  91. If narrowing is active in the current buffer, only export its
  92. narrowed part.
  93. If a region is active, export that region.
  94. A non-nil optional argument ASYNC means the process should happen
  95. asynchronously. The resulting buffer should be accessible
  96. through the `org-export-stack' interface.
  97. When optional argument SUBTREEP is non-nil, export the sub-tree
  98. at point, extracting information from the headline properties
  99. first.
  100. When optional argument VISIBLE-ONLY is non-nil, don't export
  101. contents of hidden elements.
  102. Export is done in a buffer named \"*Org GFM Export*\", which will
  103. be displayed when `org-export-show-temporary-export-buffer' is
  104. non-nil."
  105. (interactive)
  106. (org-export-to-buffer 'gfm "*Org GFM Export*"
  107. async subtreep visible-only nil nil (lambda () (text-mode))))
  108. ;;;###autoload
  109. (defun org-gfm-convert-region-to-md ()
  110. "Assume the current region has org-mode syntax, and convert it
  111. to Github Flavored Markdown. This can be used in any buffer.
  112. For example, you can write an itemized list in org-mode syntax in
  113. a Markdown buffer and use this command to convert it."
  114. (interactive)
  115. (org-export-replace-region-by 'gfm))
  116. ;;;###autoload
  117. (defun org-gfm-export-to-markdown (&optional async subtreep visible-only)
  118. "Export current buffer to a Github Flavored Markdown file.
  119. If narrowing is active in the current buffer, only export its
  120. narrowed part.
  121. If a region is active, export that region.
  122. A non-nil optional argument ASYNC means the process should happen
  123. asynchronously. The resulting file should be accessible through
  124. the `org-export-stack' interface.
  125. When optional argument SUBTREEP is non-nil, export the sub-tree
  126. at point, extracting information from the headline properties
  127. first.
  128. When optional argument VISIBLE-ONLY is non-nil, don't export
  129. contents of hidden elements.
  130. Return output file's name."
  131. (interactive)
  132. (let ((outfile (org-export-output-file-name ".md" subtreep)))
  133. (org-export-to-file 'gfm outfile async subtreep visible-only)))
  134. (provide 'ox-gfm)
  135. ;; Local variables:
  136. ;; generated-autoload-file: "org-loaddefs.el"
  137. ;; End:
  138. ;;; ox-gfm.el ends here