/
generate.el
75 lines (60 loc) · 2.63 KB
/
generate.el
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
;; requires s.el to be available and loaded
(require 's)
(require 'cl-lib)
(require 'helm-themes)
(require 'htmlize)
;; requires to keep htmlize happy about font faces
(require 'outline)
;; themes that cause errors. These are generally themes with a dark and light version
;; but no default version. For example hemisu has no deftheme, but hemisu-dark and
;; hemisu-light do
(setq bad-themes '("apropospriate" "hemisu" "solarized" "zonokai"))
;; utility functions
(defun get-string-from-file (filePath)
"Read a file and return the contents as a string"
(with-temp-buffer
(insert-file-contents filePath)
(buffer-string)))
(defun write-string-to-file (filePath contents)
(with-temp-buffer
(insert contents)
(write-file filePath)))
(defun sub-lists (list1 list2)
"Subtract list2 from list1"
(cl-reduce
(lambda (acc x) (remove x acc)) list2 :initial-value list1))
(defun sorted-helm-themes ()
"Sort and remove known bad themes from the lists"
(sub-lists
(cl-sort (mapcar 'symbol-name (helm-themes--candidates)) 'string-lessp :key 'downcase)
bad-themes))
(defun mark-and-htmlize-buffer ()
"Mark the entire current buffer and htmlize it"
(progn
(mark-whole-buffer)
(htmlize-region-for-paste (region-beginning) (region-end))))
;; HTML generation
(defun generate-index-html (tpl themes-html)
"Create the index.html file"
(s-format tpl 'elt (list spacemacs-version themes-html)))
(defun generate-theme-div (tpl theme)
"Generate the div for a single theme"
(helm-themes--load-theme theme)
(let* ((buffer-faces (htmlize-faces-in-buffer))
(face-map (htmlize-make-face-map (adjoin 'default buffer-faces)))
(style (mapconcat #'identity (htmlize-css-specs (gethash 'default face-map)) " ")))
(s-format tpl 'elt (list theme style (mark-and-htmlize-buffer)))))
(defun generate-and-join-all-theme-divs (tpl themes)
"Generate the divs containing the themed content that is embedded inside of the index.html layout"
(s-join "" (mapcar (lambda (theme) (generate-theme-div tpl theme)) themes)))
(defun generate-theme-gallery ()
"Generate all of the html needed for the theme gallery and place it's contents in a buffer called theme-gallery"
(interactive)
(rainbow-delimiters-mode-disable)
(let* ((layout-tpl (get-string-from-file "./templates/layout.html"))
(theme-tpl (get-string-from-file "./templates/theme.html"))
(themes-html (generate-and-join-all-theme-divs theme-tpl (sorted-helm-themes))))
(set-buffer (get-buffer-create "theme-gallery"))
(insert (generate-index-html layout-tpl themes-html)))
(rainbow-delimiters-mode-enable)
(switch-to-buffer (current-buffer)))