Skip to content

Commit d0abe2e

Browse files
committed
Make export to html compatible with GitHub and toc-org
Add new html preprocessors to the org-html-publish-to-html pipeline: - `spacemacs//toc-org-unhrefify-toc` - remove the `toc-org` modifications for the TOC - `spacemacs//org-heading-annotate-custom-id` - annotate org headings with the indexes that GitHub uses for linking. `org-html-publish-to-html` will use them instead of the default `#orgheadline{N}`. This way the GitHub links and the http://spacemacs.org/ links will be compatible." - `spacemacs//reroot-links` - find the links that start with https://github.com/syl20bnr/spacemacs/blob/ and end with .org{#an-optional-heading-link} (i.e the links between the local org files). Change their root to http://spacemacs.org/ so the links will point at files located on the site. *For the "file to file" links to work properly the expor
1 parent bfe1d73 commit d0abe2e

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

core/core-documentation.el

+50-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
(require 's)
1515
(require 'dash)
1616
(require 'f)
17+
(require 'toc-org)
18+
(require 'org-id)
1719

1820
(defvar spacemacs--category-names
1921
'(("config-files" . "Configuration files")
@@ -86,6 +88,49 @@
8688
(format "%s\n%s%s" beginning-of-content toc-string rest-of-content)
8789
content)))
8890

91+
(defun spacemacs//toc-org-unhrefify-toc ()
92+
"Make TOC classical org-mode TOC."
93+
(let ((toc-org-hrefify-default "org"))
94+
(toc-org-insert-toc)))
95+
96+
(defun spacemacs//org-heading-annotate-custom-id ()
97+
"Annotate headings with the indexes that GitHub uses for linking.
98+
`org-html-publish-to-html' will use them instead of the default #orgheadline{N}.
99+
This way the GitHub links and the http://spacemacs.org/ links will be compatible."
100+
(progn (goto-char (point-min))
101+
(goto-char (point-min))
102+
(while (re-search-forward "^[\\*]+\s\\(.*\\).*$" nil t)
103+
(let ((heading (match-string 1)))
104+
(progn (move-end-of-line nil)
105+
(open-line 1)
106+
(next-line 1)
107+
(insert (format (concat " :PROPERTIES:\n"
108+
" :CUSTOM_ID: %s\n"
109+
" :END:\n")
110+
(substring (toc-org-hrefify-gh
111+
(replace-regexp-in-string
112+
toc-org-tags-regexp
113+
""
114+
heading))
115+
;; Remove # prefix added by `toc-org-hrefify-gh'.
116+
1))))))))
117+
118+
(defun spacemacs//reroot-links ()
119+
"Find the links that start with https://github.com/syl20bnr/spacemacs/blob/
120+
and end with .org{#an-optional-heading-link} (i.e the links between the local org files).
121+
Change their root to http://spacemacs.org/ so the links will point at files located on the site.
122+
For the file to file links to work properly the exported org files should be processed with
123+
the `spacemacs//org-heading-annotate-custom-id' function."
124+
(let ((git-url-root-regexp
125+
(concat "\\[\\[[\\s]*\\(https\\:\\/\\/github\\.com\\/syl20bnr"
126+
"\\/spacemacs\\/blob\\/[^/]+\\/\\)[^]]+\\(\\.org\\).*$"))
127+
(site-url "http://spacemacs.org/")
128+
(site-doc-postf ".html"))
129+
(progn (goto-char (point-min))
130+
(while (re-search-forward git-url-root-regexp nil t)
131+
(progn (replace-match site-url nil t nil 1)
132+
(replace-match site-doc-postf nil t nil 2))))))
133+
89134
(defun spacemacs//add-org-meta-readtheorg-css (filename)
90135
(let* ((head-css-extra-readtheorg-head (concat
91136
"#+HTML_HEAD_EXTRA:"
@@ -98,7 +143,8 @@
98143
(goto-char (point-min))
99144
(if (search-forward "#+TITLE:" nil t nil)
100145
(beginning-of-line 2)
101-
(error "Can't find #+TITLE:"))
146+
(error (format "Can't find #+TITLE: in %s"
147+
(buffer-file-name))))
102148
(insert (concat head-css-extra-readtheorg-head
103149
(f-relative user-emacs-directory
104150
(file-name-directory filename))
@@ -115,9 +161,9 @@
115161
(save-match-data
116162
(insert-file-contents filename t)
117163
(spacemacs//add-org-meta-readtheorg-css filename)
118-
119-
120-
164+
(spacemacs//toc-org-unhrefify-toc)
165+
(spacemacs//reroot-links)
166+
(spacemacs//org-heading-annotate-custom-id)
121167
(apply origfunc args)
122168
(not-modified)))
123169
;; Restore `buffer-file-name' for the buffers that previously visited the org files.

0 commit comments

Comments
 (0)