|
| 1 | +;;; ox-gfm.el --- Github Flavored Markdown Back-End for Org Export Engine |
| 2 | + |
| 3 | +;; Copyright (C) 2014 Lars Tveito |
| 4 | + |
| 5 | +;; Author: Lars Tveito |
| 6 | +;; Keywords: org, wp, markdown, github |
| 7 | +;; Package-Version: 20141211.240 |
| 8 | + |
| 9 | +;; This file is not part of GNU Emacs. |
| 10 | + |
| 11 | +;; GNU Emacs is free software: you can redistribute it and/or modify |
| 12 | +;; it under the terms of the GNU General Public License as published by |
| 13 | +;; the Free Software Foundation, either version 3 of the License, or |
| 14 | +;; (at your option) any later version. |
| 15 | + |
| 16 | +;; GNU Emacs is distributed in the hope that it will be useful, |
| 17 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | +;; GNU General Public License for more details. |
| 20 | + |
| 21 | +;; You should have received a copy of the GNU General Public License |
| 22 | +;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
| 23 | + |
| 24 | +;;; Commentary: |
| 25 | + |
| 26 | +;; This library implements a Markdown back-end (github flavor) for Org |
| 27 | +;; exporter, based on the `md' back-end. |
| 28 | + |
| 29 | +;;; Code: |
| 30 | + |
| 31 | +(require 'ox-md) |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +;;; User-Configurable Variables |
| 36 | + |
| 37 | +(defgroup org-export-gfm nil |
| 38 | + "Options specific to Markdown export back-end." |
| 39 | + :tag "Org Github Flavored Markdown" |
| 40 | + :group 'org-export |
| 41 | + :version "24.4" |
| 42 | + :package-version '(Org . "8.0")) |
| 43 | + |
| 44 | + |
| 45 | +;;; Define Back-End |
| 46 | + |
| 47 | +(org-export-define-derived-backend 'gfm 'md |
| 48 | + :export-block '("GFM" "GITHUB FLAVORED MARKDOWN") |
| 49 | + :filters-alist '((:filter-parse-tree . org-md-separate-elements)) |
| 50 | + :menu-entry |
| 51 | + '(?g "Export to Github Flavored Markdown" |
| 52 | + ((?G "To temporary buffer" |
| 53 | + (lambda (a s v b) (org-gfm-export-as-markdown a s v))) |
| 54 | + (?g "To file" (lambda (a s v b) (org-gfm-export-to-markdown a s v))) |
| 55 | + (?o "To file and open" |
| 56 | + (lambda (a s v b) |
| 57 | + (if a (org-gfm-export-to-markdown t s v) |
| 58 | + (org-open-file (org-gfm-export-to-markdown nil s v))))))) |
| 59 | + :translate-alist '((inner-template . org-gfm-inner-template) |
| 60 | + (strike-through . org-gfm-strike-through) |
| 61 | + (src-block . org-gfm-src-block))) |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +;;; Transcode Functions |
| 66 | + |
| 67 | +;;;; Src Block |
| 68 | + |
| 69 | +(defun org-gfm-src-block (src-block contents info) |
| 70 | + "Transcode SRC-BLOCK element into Github Flavored Markdown |
| 71 | +format. CONTENTS is nil. INFO is a plist used as a communication |
| 72 | +channel." |
| 73 | + (let* ((lang (org-element-property :language src-block)) |
| 74 | + (code (org-export-format-code-default src-block info)) |
| 75 | + (prefix (concat "```" lang "\n")) |
| 76 | + (suffix "```")) |
| 77 | + (concat prefix code suffix))) |
| 78 | + |
| 79 | + |
| 80 | +;;;; Strike-Through |
| 81 | + |
| 82 | +(defun org-gfm-strike-through (strike-through contents info) |
| 83 | + "Transcode STRIKE-THROUGH from Org to Markdown (GFM). |
| 84 | +CONTENTS is the text with strike-through markup. INFO is a plist |
| 85 | +holding contextual information." |
| 86 | + (format "~~%s~~" contents)) |
| 87 | + |
| 88 | +;;;; Table of contents |
| 89 | + |
| 90 | +(defun org-gfm-format-toc (headline) |
| 91 | + "Return an appropriate table of contents entry for HEADLINE. INFO is a |
| 92 | +plist used as a communication channel." |
| 93 | + (let* ((title (org-export-data |
| 94 | + (org-export-get-alt-title headline info) info)) |
| 95 | + (level (1- (org-element-property :level headline))) |
| 96 | + (indent (concat (make-string (* level 2) ? ))) |
| 97 | + (anchor (or (org-element-property :custom_id headline) |
| 98 | + (concat "sec-" (mapconcat 'number-to-string |
| 99 | + (org-export-get-headline-number |
| 100 | + headline info) "-"))))) |
| 101 | + (concat indent "- [" title "]" "(#" anchor ")"))) |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | +;;;; Template |
| 107 | + |
| 108 | +(defun org-gfm-inner-template (contents info) |
| 109 | + "Return body of document after converting it to Markdown syntax. |
| 110 | +CONTENTS is the transcoded contents string. INFO is a plist |
| 111 | +holding export options." |
| 112 | + (let* ((depth (plist-get info :with-toc)) |
| 113 | + (headlines (and depth (org-export-collect-headlines info depth))) |
| 114 | + (toc-string (or (mapconcat 'org-gfm-format-toc headlines "\n") "")) |
| 115 | + (toc-tail (if headlines "\n\n" ""))) |
| 116 | + (concat toc-string toc-tail contents))) |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | +;;; Interactive function |
| 121 | + |
| 122 | +;;;###autoload |
| 123 | +(defun org-gfm-export-as-markdown (&optional async subtreep visible-only) |
| 124 | + "Export current buffer to a Github Flavored Markdown buffer. |
| 125 | +
|
| 126 | +If narrowing is active in the current buffer, only export its |
| 127 | +narrowed part. |
| 128 | +
|
| 129 | +If a region is active, export that region. |
| 130 | +
|
| 131 | +A non-nil optional argument ASYNC means the process should happen |
| 132 | +asynchronously. The resulting buffer should be accessible |
| 133 | +through the `org-export-stack' interface. |
| 134 | +
|
| 135 | +When optional argument SUBTREEP is non-nil, export the sub-tree |
| 136 | +at point, extracting information from the headline properties |
| 137 | +first. |
| 138 | +
|
| 139 | +When optional argument VISIBLE-ONLY is non-nil, don't export |
| 140 | +contents of hidden elements. |
| 141 | +
|
| 142 | +Export is done in a buffer named \"*Org GFM Export*\", which will |
| 143 | +be displayed when `org-export-show-temporary-export-buffer' is |
| 144 | +non-nil." |
| 145 | + (interactive) |
| 146 | + (org-export-to-buffer 'gfm "*Org GFM Export*" |
| 147 | + async subtreep visible-only nil nil (lambda () (text-mode)))) |
| 148 | + |
| 149 | + |
| 150 | +;;;###autoload |
| 151 | +(defun org-gfm-convert-region-to-md () |
| 152 | + "Assume the current region has org-mode syntax, and convert it |
| 153 | +to Github Flavored Markdown. This can be used in any buffer. |
| 154 | +For example, you can write an itemized list in org-mode syntax in |
| 155 | +a Markdown buffer and use this command to convert it." |
| 156 | + (interactive) |
| 157 | + (org-export-replace-region-by 'gfm)) |
| 158 | + |
| 159 | + |
| 160 | +;;;###autoload |
| 161 | +(defun org-gfm-export-to-markdown (&optional async subtreep visible-only) |
| 162 | + "Export current buffer to a Github Flavored Markdown file. |
| 163 | +
|
| 164 | +If narrowing is active in the current buffer, only export its |
| 165 | +narrowed part. |
| 166 | +
|
| 167 | +If a region is active, export that region. |
| 168 | +
|
| 169 | +A non-nil optional argument ASYNC means the process should happen |
| 170 | +asynchronously. The resulting file should be accessible through |
| 171 | +the `org-export-stack' interface. |
| 172 | +
|
| 173 | +When optional argument SUBTREEP is non-nil, export the sub-tree |
| 174 | +at point, extracting information from the headline properties |
| 175 | +first. |
| 176 | +
|
| 177 | +When optional argument VISIBLE-ONLY is non-nil, don't export |
| 178 | +contents of hidden elements. |
| 179 | +
|
| 180 | +Return output file's name." |
| 181 | + (interactive) |
| 182 | + (let ((outfile (org-export-output-file-name ".md" subtreep))) |
| 183 | + (org-export-to-file 'gfm outfile async subtreep visible-only))) |
| 184 | + |
| 185 | +(provide 'ox-gfm) |
| 186 | + |
| 187 | +;; Local variables: |
| 188 | +;; generated-autoload-file: "org-loaddefs.el" |
| 189 | +;; End: |
| 190 | + |
| 191 | +;;; ox-gfm.el ends here |
0 commit comments