-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathorg-html-themify.el
More file actions
181 lines (157 loc) · 7.05 KB
/
org-html-themify.el
File metadata and controls
181 lines (157 loc) · 7.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
;;; org-html-themify.el --- Themify org-mode export with Emacs color theme
;;; -*- lexical-binding: t -*-
;; Author: Shi Tianshu
;; Keywords: org-mode
;; Package-Requires: ((emacs "27.1") (org "9.4.4") (htmlize "1.5.6") (dash "2.17.0") (hexrgb "0") (s "1.12.0"))
;; Version: 1.0.0
;; URL: https://www.github.com/DogLooksGood/org-html-themify
;;
;; This file is not part of GNU Emacs.
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License
;; as published by the Free Software Foundation; either version 3
;; of the License, or (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; 1. Specify a light and a dark theme.
;;
;; (setq org-html-themify-themes '((dark . solarized-dark) (light . solarized-light)))
;;
;; 2. Enable org-html-themify-themes in org-mode.
;;
;; (add-hook 'org-mode-hook #'org-html-themify-mode)
;;
;; That's all, now you can export HTML.
;;; Code:
(require 'hexrgb)
(require 'htmlize)
(require 'dash)
(require 'org)
(require 'ox-html)
(require 's)
(defvar org-html-themify-themes
'((dark . tango-dark)
(light . leuven))
"Themes used to generate inline stylesheet.")
(defvar org-html-themify--current-theme nil)
(defvar org-html-themify--backup-htmlize-face-overrides nil)
(defvar org-html-themify--backup-org-html-preamble nil)
(defvar org-html-themify--backup-org-html-head-extra "")
(defvar org-html-themify--backup-org-html-postamble nil)
(defvar org-html-themify-css-path
(expand-file-name
"org-html-themify.css"
(file-name-directory (or load-file-name (buffer-file-name)))))
(defvar org-html-themify-js-path
(expand-file-name
"org-html-themify.js"
(file-name-directory (or load-file-name (buffer-file-name)))))
(defun org-html-themify--parse-clause (clause)
(-let* (((th f a k) (s-split ":" clause))
(fs (intern f))
(as (intern (concat ":" a)))
(req-theme (alist-get (intern th) org-html-themify-themes))
(_ (unless (equal req-theme org-html-themify--current-theme)
(when org-html-themify--current-theme (disable-theme org-html-themify--current-theme))
(load-theme req-theme t)
(setq org-html-themify--current-theme req-theme))))
(let ((v (face-attribute fs as)))
(unless (equal v 'unspecified)
(if k
(plist-get v (intern (concat ":" k)))
v)))))
(defun org-html-themify--get-interpolate-value (s)
(let* ((clauses (-> s
(substring 2 -1)
(split-string "|")))
(vals (-keep #'org-html-themify--parse-clause clauses))
(val (car vals)))
(cond
((null val) "initial")
((hexrgb-rgb-hex-string-p val) val)
((hexrgb-color-name-to-hex val 2)))))
(defun org-html-themify--interpolate ()
(let ((inhibit-redisplay t)
(orig-themes custom-enabled-themes))
(mapc (lambda (th) (disable-theme th)) orig-themes)
(goto-char (point-min))
(while (re-search-forward "#{.+?}" nil t)
(-let* ((beg (match-beginning 0))
(end (match-end 0))
(s (buffer-substring-no-properties beg end))
(v (org-html-themify--get-interpolate-value s)))
(delete-region beg end)
(insert v)))
(disable-theme org-html-themify--current-theme)
(mapc (lambda (th) (load-theme th t)) orig-themes)))
(defun org-html-themify--setup-inlines (exporter)
"Insert custom inline css"
(when (eq exporter 'html)
(setq org-html-preamble
(concat
"<div id=\"toggle-theme\">dark theme</div>"
"<div id=\"toggle-toc\">☰</div>"))
(setq org-html-head-extra
(concat
org-html-themify--backup-org-html-head-extra
"<style type=\"text/css\">\n"
"<!--/*--><![CDATA[/*><!--*/\n"
(with-temp-buffer
(insert-file-contents org-html-themify-css-path)
(org-html-themify--interpolate)
(buffer-string))
"/*]]>*/-->\n"
"</style>\n"))
(setq org-html-postamble
(concat
"<script type=\"text/javascript\">\n"
"<!--/*--><![CDATA[/*><!--*/\n"
(with-temp-buffer
(insert-file-contents org-html-themify-js-path)
(buffer-string))
"/*]]>*/-->\n"
"</script>"))))
(defun org-html-themify--init ()
(add-hook 'org-export-before-processing-hook 'org-html-themify--setup-inlines)
(setq org-html-themify--backup-htmlize-face-overrides htmlize-face-overrides
org-html-themify--backup-org-html-head-extra org-html-head-extra
org-html-themify--backup-org-html-postamble org-html-postamble
org-html-themify--backup-org-html-preamble org-html-preamble)
(setq org-html-head-include-default-style nil)
(setq htmlize-face-overrides
(-concat
org-html-themify--backup-htmlize-face-overrides
'(font-lock-keyword-face (:foreground "var(--clr-keyword)" :background "var(--bg-keyword)")
font-lock-constant-face (:foreground "var(--clr-constant)" :background "var(--bg-constant)")
font-lock-comment-face (:foreground "var(--clr-comment)" :background "var(--bg-comment)")
font-lock-comment-delimiter-face (:foreground "var(--clr-comment-delimiter)" :background "var(--bg-comment-delimiter)")
font-lock-function-name-face (:foreground "var(--function-clr-name)" :background "var(--function-bg-name)")
font-lock-variable-name-face (:foreground "var(--clr-variable)" :background "var(--bg-variable)")
font-lock-preprocessor-face (:foreground "var(--clr-preprocessor)" :background "var(--bg-preprocessor)")
font-lock-doc-face (:foreground "var(--clr-doc)" :background "var(--bg-doc)")
font-lock-builtin-face (:foreground "var(--clr-builtin)" :background "var(--bg-builtin)")
font-lock-string-face (:foreground "var(--clr-string)" :background "var(--bg-string)")))))
(defun org-html-themify--uninit ()
(remove-hook 'org-export-before-processing-hook 'org-html-themify--setup-inlines)
(setq org-html-head-include-default-style t)
(setq htmlize-face-overrides org-html-themify--backup-htmlize-face-overrides
org-html-head-extra org-html-themify--backup-org-html-head-extra
org-html-postamble org-html-themify--backup-org-html-postamble
org-html-preamble org-html-themify--backup-org-html-preamble))
(define-minor-mode org-html-themify-mode
"Themify org-mode HTML export with Emacs color theme."
nil
nil
nil
(if org-html-themify-mode
(org-html-themify--init)
(org-html-themify--uninit)))
(provide 'org-html-themify)
;;; org-html-themify ends here.