forked from orthecreedence/wookie-doc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.lisp
278 lines (253 loc) · 12 KB
/
template.lisp
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
(in-package :wookie-doc)
(define-condition view-not-found (error)
((view :initarg :view :reader view-name :initform nil))
(:report (lambda (c s) (format s "View not found: ~a" (view-name c))))
(:documentation "Describes the condition when a view doesn't exist."))
(defparameter *scanner-md-header*
(cl-ppcre:create-scanner "^\s*---(.*?)---"
:case-insensitive-mode t
:single-line-mode t)
"Defines a scanner used to pull the header section from a markdown file.")
(defvar *views* nil
"Defines a container for all parsed/compiled views to be cached.")
(defmacro deflayout (name (data-var &key (stream-var 's) top-level) &body body)
"Define a wookie layout function. Can be used in conjunction with a
'layout: ...' header in a markdown file."
(let ((view-name (intern (string-upcase (format nil "layout-~a" name)) :wookie-doc)))
`(progn
(defun ,view-name (,data-var)
(cl-who:with-html-output-to-string (,stream-var nil :prologue ,top-level :indent t)
,@body))
',view-name)))
(defmacro parent-layout (name data-var &body body)
"Allows a layout to be wrapped by another (parent) layout."
`(str
(layout ,name (append (list :content (cl-who:with-html-output-to-string (s)
,@body))
,data-var))))
(defun generate-view-name (view-dir file)
"Generates a symbol that can be used to identify a view."
(let* ((view-dir (namestring view-dir))
(file (namestring file))
(file (subseq file (1+ (mismatch file view-dir))))
(file (subseq file 0 (position #\. file :from-end t))))
(intern (string-upcase file) :keyword)))
(defun parse-markdown-header (header)
"Parses the key-value section in a markdown header."
(let ((kv-pairs nil))
(when header
(dolist (line (cl-ppcre:split "[\\r\\n]+" header))
(let ((colon-pos (position #\: line)))
(when colon-pos
(let* ((key (intern (string-upcase (subseq line 0 colon-pos)) :keyword))
(value (subseq line (+ colon-pos 1)))
(value (string-trim #(#\space #\return #\newline) value)))
(case key
(:layout (setf value (intern (string-upcase value) :wookie-doc))))
(setf (getf kv-pairs key) value))))))
kv-pairs))
(defun format-code-blocks (str)
"Turn
```lisp
(defun () ...)
```
into
<pre><code class=\"lisp\">(defun () ...)</code></pre>
"
(cl-ppcre:regex-replace-all
(cl-ppcre:create-scanner
"```(.*?)\\n(.*?)(\\n)```"
:case-insensitive-mode t
:single-line-mode t)
str
"\\3<pre><code class=\"\\1\">\\2</code></pre>\\3"))
(defun convert-to-html-id (str)
"Convert 'Omg Lol Wtf' to 'omg-lol-wtf'"
(let* ((str (cl-ppcre:regex-replace-all "[^\\w]" str "-"))
(str (cl-ppcre:regex-replace-all "-+" str "-"))
(str (cl-ppcre:regex-replace-all "(^-+|-+$)" str "")))
(string-downcase str)))
(defun generate-table-of-contents (str)
"Generate a table of contents for documentation. Injects itself into {{toc}}
tags."
(unless (search "{{toc}}" str)
(return-from generate-table-of-contents str))
(let* ((headers nil)
(str (cl-ppcre:regex-replace-all
(cl-ppcre:create-scanner "^((#{1,6})\\s*(.*?)(\\s*#+)?)$" :multi-line-mode t)
str
(lambda (match &rest regs)
(declare (ignore match))
(let* ((regs (cddddr regs))
(rs (car regs))
(re (cadr regs))
(tag (subseq str (aref rs 0) (aref re 0)))
(level (length (subseq str (aref rs 1) (aref re 1))))
(title (subseq str (aref rs 2) (aref re 2)))
(type-pos (position (code-char 40) title))
(type (if type-pos (subseq title (1+ type-pos) (position (code-char 41) title :start type-pos)) ""))
(title (if type-pos (subseq title 0 type-pos) title))
(id (convert-to-html-id title)))
(push (list :title title
;; NOTE: disabling "type" since it's mainly used for code docs
;:type type
:type ""
:id id
:level (- level 1)) headers)
(concatenate 'string "<a class=\"toc-anchor\" id=\"" id "\"></a>" markdown.cl::*nl* tag)))))
(headers (reverse headers))
;; finds top-level headers (<h1>)
(header-search-fn (lambda (header) (zerop (getf header :level)))))
;; if we only have one top-level header, assume that we don't want to TOC it
(when (= (length (remove-if-not header-search-fn headers)) 1)
(setf headers (remove-if header-search-fn headers)))
(cl-ppcre:regex-replace "{{toc}}" str
(lambda (&rest _)
(declare (ignore _))
(with-output-to-string (s)
(format s "{{{div.toc}}}~%")
(dolist (header headers)
(dotimes (i (getf header :level))
(format s " "))
(let* ((title (getf header :title))
(title (cl-ppcre:regex-replace-all "[\\[\\]]" title ""))
(id (getf header :id))
(type (getf header :type)))
(format s "- [~a](#~a)" title id)
(unless (string= type "")
(format s " _~a_" type))
(format s "~a" markdown.cl::*nl*)))
(format s "{{{/div}}}~%"))))))
(defun fix-anchors (html)
"Removes <p>...</p> around <a id=...> tags"
(let* ((scanner (cl-ppcre:create-scanner "(<p>\\n*)?(<a id=\"(.*?)\"></a>)(\\n*</p>)?" :single-line-mode t))
(html (cl-ppcre:regex-replace-all scanner html "\\2")))
html))
(defun parse-special-tags (markdown)
"Replaces things like {{{div.my-class}}} with {{tpl|tag|div.myclass}}. Also
removes whitespace preceding the replacment so we don't confuse the markdown
parser, and adds whitespace around the tag for easy <p> removal."
(cl-ppcre:regex-replace-all
(cl-ppcre:create-scanner "^\\s*{{{(/?[a-z0-9]+((\.[a-z0-9 -]+)+)?)}}}" :multi-line-mode t)
markdown
(format nil "~c~c{{tpl|tag|\\1}}~c~c" #\newline #\newline #\newline #\newline)))
(defun finish-special-tags (html)
"Takes the output of parse-special-tags and returns wonderful HTML."
(let* ((p-scanner (cl-ppcre:create-scanner "(<p>\\n*)?({{tpl\|tag\|[a-z0-9\. -]+}}})" :single-line-mode t))
(html (cl-ppcre:regex-replace-all p-scanner html "\\2"))
;; AL - hack to get the closing </p> to GO AWAY. couldn't do it in regex above.
(p-scanner (cl-ppcre:create-scanner "}}\\n</p>" :single-line-mode t))
(html (cl-ppcre:regex-replace-all p-scanner html "}}"))
(html (cl-ppcre:regex-replace-all
"{{tpl\\|tag\\|(.*?)}}"
html
(lambda (match &rest regs)
(let* ((regs (cddddr regs))
(rs (car regs))
(re (cadr regs))
(tag (subseq match (aref rs 0) (aref re 0))))
(let ((dot (position #\. tag)))
(if (eq (aref tag 0) #\/)
;; closing tag? ignore the class (if we have one)
(format nil "</~a>" (subseq tag 1 dot))
;; opening tag. JOY!!! parse out the class if it was given
(if dot
(format nil "<~a class=\"~a\">"
(subseq tag 0 dot)
(substitute #\space #\. (subseq tag (1+ dot))))
(format nil "<~a>" tag)))))))))
html))
(defun process-markdown-view (markdown-str)
"Given a markdown string, runs all app-based processing on it to turn it into
HTML. This includes parsing the header block, generating table of contents,
and parsing special HTML tags."
(let* ((markdown-header (nth-value 1 (cl-ppcre:scan-to-strings *scanner-md-header* markdown-str)))
(markdown-header (when markdown-header (aref markdown-header 0)))
(parsed-headers (parse-markdown-header markdown-header))
(markdown-str (cl-ppcre:regex-replace *scanner-md-header* markdown-str ""))
(raw (if (string= (getf parsed-headers :markdown) "off")
markdown-str
nil))
(markdown-str (unless raw (format-code-blocks markdown-str)))
(markdown-str (unless raw (generate-table-of-contents markdown-str)))
(markdown-str (unless raw (parse-special-tags markdown-str)))
(html (if raw
raw
(markdown.cl:parse markdown-str)))
(html (fix-anchors html))
(html (finish-special-tags html)))
(values html parsed-headers)))
(defun save-view (view-name file ext)
"Load a file's contents from the given `file` var, parse it, and save the
contents into the view of the given name."
(cond ((string= ext ".lisp")
(load file))
((string= ext ".md")
(multiple-value-bind (html parsed-headers)
(process-markdown-view (file-contents file))
(setf (gethash view-name *views*)
(list :meta parsed-headers
:html html))))))
(defun load-views (&key subdir (clear t) (view-directory (format nil "~a/views" *root*)))
"Load and cache all view files."
(when clear
(setf *layouts* nil
*views* (make-hash-table :test 'eq)))
(dolist (file (cl-fad:list-directory (if subdir subdir view-directory)))
(cond ((cl-fad:directory-exists-p file)
(load-views :subdir file :clear nil))
((cl-fad:file-exists-p file)
(let* ((file-str (namestring file))
(ext (subseq file-str (or (position #\. file-str :from-end t) (length file-str))))
(view-name (generate-view-name view-directory file-str)))
(save-view view-name file ext)))))
;; process modules (only after ALL views are loaded)
(loop for k being the hash-keys of *views*
for v being the hash-values of *views* do
(let* ((html (getf v :html))
(html (inject-modules html)))
(setf (getf v :html) html
(gethash k *views*) v)))
*views*)
(defun find-module (name)
"Find a module by name (without the leading 'module:')"
(loop for k being the hash-keys of *views* do
(let ((module-str (string k)))
(when (zerop (or (search "MODULES/" module-str) -1))
(let ((modname (string-downcase (subseq module-str 8))))
(when (string= (string-downcase name) modname)
(return-from find-module (getf (gethash k *views*) :html)))))))
"")
(defun inject-modules (html)
"Add in any modules to this HTML string."
(unless (search "{{module:" html)
(return-from inject-modules html))
(cl-ppcre:regex-replace-all
(cl-ppcre:create-scanner "{{module:([a-z0-9_-]+)}}" :multi-line-mode t)
html
(lambda (match &rest regs)
(declare (ignore match))
(let* ((regs (cddddr regs))
(rs (car regs))
(re (cadr regs))
(name (subseq html (aref rs 0) (aref re 0)))
(module (find-module name)))
module))))
(defun load-view (name &key data)
"Load a view from the view cache."
(let ((view (gethash name *views*)))
(if view
(let ((html (getf view :html))
(meta (getf view :meta)))
(let ((data (append data
(list :title (getf meta :title)
:content html)))
(layout (getf meta :layout)))
(if layout
(layout layout data)
html)))
(error 'view-not-found :view name))))
(defun layout (name data)
"Sends data to a layout and returns the content for that layout."
(let ((layout-fn (intern (string-upcase (format nil "layout-~a" name)) :wookie-doc)))
(funcall layout-fn data)))