ox-gfm.el 6.2 KB

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