Skip to content

Commit be0ce89

Browse files
committed
org layer: move ox-gfm to extensions and add org-enable-github-support
Fixes syl20bnr#1800
1 parent 8ce1fad commit be0ce89

File tree

5 files changed

+276
-4
lines changed

5 files changed

+276
-4
lines changed

contrib/org/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- [Description](#description)
1010
- [Install](#install)
1111
- [Layer](#layer)
12+
- [Github support](#github-support)
1213
- [Different bullets](#different-bullets)
1314
- [Key bindings](#key-bindings)
1415
- [Org with evil-org-mode](#org-with-evil-org-mode)
@@ -41,6 +42,17 @@ To use this contribution add it to your `~/.spacemacs`
4142
(setq-default dotspacemacs-configuration-layers '(org))
4243
```
4344

45+
### Github support
46+
47+
To install Github related extensions like [ox-gfm][] to export to Github
48+
flavored markdown set the variable `org-enable-github-support` to `t`.
49+
50+
```elisp
51+
(setq-default dotspacemacs-configuration-layers '(
52+
(org :variables
53+
org-enable-github-support t)))
54+
```
55+
4456
### Different bullets
4557

4658
You can tweak the bullets displayed in the org buffer in the function

contrib/org/config.el

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
;;; config.el --- Org configuration File for Spacemacs
2+
;;
3+
;; Copyright (c) 2012-2014 Sylvain Benner
4+
;; Copyright (c) 2014-2015 Sylvain Benner & Contributors
5+
;;
6+
;; Author: Sylvain Benner <[email protected]>
7+
;; URL: https://github.com/syl20bnr/spacemacs
8+
;;
9+
;; This file is not part of GNU Emacs.
10+
;;
11+
;;; License: GPLv3
12+
13+
;; Variables
14+
15+
(defvar org-enable-github-support nil
16+
"If non-nil Github related packages are configured.")

contrib/org/extensions.el

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
;;; extensions.el --- Org Extensions File for Spacemacs
2+
;;
3+
;; Copyright (c) 2012-2014 Sylvain Benner
4+
;; Copyright (c) 2014-2015 Sylvain Benner & Contributors
5+
;;
6+
;; Author: Sylvain Benner <[email protected]>
7+
;; URL: https://github.com/syl20bnr/spacemacs
8+
;;
9+
;; This file is not part of GNU Emacs.
10+
;;
11+
;;; License: GPLv3
12+
13+
(setq org-post-extensions '(ox-gfm))
14+
15+
(defun org/init-ox-gfm ()
16+
;; installing this package from melpa is buggy,
17+
;; so we install it as an extension for now.
18+
(use-package ox-gfm
19+
:if org-enable-github-support
20+
:defer t
21+
:init
22+
(progn
23+
;; seems to be required otherwise the extension is not
24+
;; loaded properly by org
25+
(eval-after-load 'org '(require 'ox-gfm))
26+
(autoload 'org-gfm-export-as-markdown "ox-gfm" "\
27+
Export current buffer to a Github Flavored Markdown buffer.
28+
29+
If narrowing is active in the current buffer, only export its
30+
narrowed part.
31+
32+
If a region is active, export that region.
33+
34+
A non-nil optional argument ASYNC means the process should happen
35+
asynchronously. The resulting buffer should be accessible
36+
through the `org-export-stack' interface.
37+
38+
When optional argument SUBTREEP is non-nil, export the sub-tree
39+
at point, extracting information from the headline properties
40+
first.
41+
42+
When optional argument VISIBLE-ONLY is non-nil, don't export
43+
contents of hidden elements.
44+
45+
Export is done in a buffer named \"*Org GFM Export*\", which will
46+
be displayed when `org-export-show-temporary-export-buffer' is
47+
non-nil.
48+
49+
\(fn &optional ASYNC SUBTREEP VISIBLE-ONLY)" t nil)
50+
51+
(autoload 'org-gfm-convert-region-to-md "ox-gfm" "\
52+
Assume the current region has org-mode syntax, and convert it
53+
to Github Flavored Markdown. This can be used in any buffer.
54+
For example, you can write an itemized list in org-mode syntax in
55+
a Markdown buffer and use this command to convert it.
56+
57+
\(fn)" t nil))))
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
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

contrib/org/packages.el

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
org-pomodoro
2020
org-present
2121
org-repo-todo
22-
ox-gfm
2322
))
2423

2524
(defun org/init-evil-org ()
@@ -187,9 +186,6 @@ Will work on both org-mode and any mode that accepts plain html."
187186
(evil-leader/set-key-for-mode 'org-mode
188187
"mgt" 'ort/goto-todos))))
189188

190-
(defun org/init-ox-gfm ()
191-
(eval-after-load 'org '(require 'ox-gfm)))
192-
193189
(defun org/init-htmlize ()
194190
(use-package htmlize
195191
:defer t))

0 commit comments

Comments
 (0)