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
This commit is contained in:
JAremko 2016-03-14 20:44:37 +02:00 committed by syl20bnr
parent b2aadd06a3
commit d776ee60b7
1 changed files with 53 additions and 4 deletions

View File

@ -14,6 +14,8 @@
(require 's)
(require 'dash)
(require 'f)
(require 'toc-org)
(require 'org-id)
(defvar spacemacs--category-names
'(("config-files" . "Configuration files")
@ -90,6 +92,49 @@
(format "%s\n%s%s" beginning-of-content toc-string rest-of-content)
content)))
(defun spacemacs//toc-org-unhrefify-toc ()
"Make TOC classical org-mode TOC."
(let ((toc-org-hrefify-default "org"))
(toc-org-insert-toc)))
(defun spacemacs//org-heading-annotate-custom-id ()
"Annotate 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."
(progn (goto-char (point-min))
(goto-char (point-min))
(while (re-search-forward "^[\\*]+\s\\(.*\\).*$" nil t)
(let ((heading (match-string 1)))
(progn (move-end-of-line nil)
(open-line 1)
(next-line 1)
(insert (format (concat " :PROPERTIES:\n"
" :CUSTOM_ID: %s\n"
" :END:\n")
(substring (toc-org-hrefify-gh
(replace-regexp-in-string
toc-org-tags-regexp
""
heading))
;; Remove # prefix added by `toc-org-hrefify-gh'.
1))))))))
(defun 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 exported org files should be processed with
the `spacemacs//org-heading-annotate-custom-id' function."
(let ((git-url-root-regexp
(concat "\\[\\[[\\s]*\\(https\\:\\/\\/github\\.com\\/syl20bnr"
"\\/spacemacs\\/blob\\/[^/]+\\/\\)[^]]+\\(\\.org\\).*$"))
(site-url "http://spacemacs.org/")
(site-doc-postf ".html"))
(progn (goto-char (point-min))
(while (re-search-forward git-url-root-regexp nil t)
(progn (replace-match site-url nil t nil 1)
(replace-match site-doc-postf nil t nil 2))))))
(defun spacemacs//add-org-meta-readtheorg-css (filename)
(let* ((head-css-extra-readtheorg-head (concat
"#+HTML_HEAD_EXTRA:"
@ -102,13 +147,16 @@
(goto-char (point-min))
(if (search-forward "#+TITLE:" nil t nil)
(beginning-of-line 2)
(error "Can't find #+TITLE:"))
(error (format "Can't find #+TITLE: in %s"
(buffer-file-name))))
(insert (concat head-css-extra-readtheorg-head
(f-relative user-emacs-directory
(file-name-directory filename))
head-css-extra-readtheorg-tail)))))
(defun spacemacs//pub-doc-html-advice (origfunc &rest args)
"Wrapper for `org-html-publish-to-html' use it to insert
preprocessors for the exported .org files."
(save-current-buffer
(save-excursion
(let* ((filename (car (nthcdr 1 args)))
@ -118,10 +166,11 @@
(with-temp-buffer
(save-match-data
(insert-file-contents filename t)
;; ===========Add preprocessors here===============
(spacemacs//add-org-meta-readtheorg-css filename)
(spacemacs//toc-org-unhrefify-toc)
(spacemacs//reroot-links)
(spacemacs//org-heading-annotate-custom-id)
(apply origfunc args)
(not-modified)))
;; Restore `buffer-file-name' for the buffers that previously visited the org files.