Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 182 lines (145 sloc) 7.133 kb
d3e78ff @archimag added registration and authentication
archimag authored
1 ;;;; markup.lisp
2
57013f5 @archimag added cliki2 markup extensions: article-link, person-link, hyperspec-lin...
archimag authored
3 (in-package #:cliki2.markup)
4
5a7e779 @archimag refactoring
archimag authored
5 (defvar *cliki2-rules* (alexandria:copy-hash-table *rules*))
6
7 (defvar *cliki2-compiled-grammar* nil)
57013f5 @archimag added cliki2 markup extensions: article-link, person-link, hyperspec-lin...
archimag authored
8
9 (defmacro with-cliki2-rules (&body body)
7b531a5 @archimag Revert "sync with 3bmd/extensible branch"
archimag authored
10 `(let ((*rules* *cliki2-rules*))
57013f5 @archimag added cliki2 markup extensions: article-link, person-link, hyperspec-lin...
archimag authored
11 ,@body))
12
5a7e779 @archimag refactoring
archimag authored
13 (defun recompile-cliki2-grammar ()
14 (setf *cliki2-compiled-grammar*
15 (esrap:compile-grammar '3bmd-grammar::block)))
16
57013f5 @archimag added cliki2 markup extensions: article-link, person-link, hyperspec-lin...
archimag authored
17 (defmacro define-rule (symbol expression &body options)
18 `(with-cliki2-rules
19 (defrule ,symbol ,expression ,@options)))
20
21 (defun parse-cliki2-doc (markup &aux (curpos 0))
5a7e779 @archimag refactoring
archimag authored
22 (with-cliki2-rules
23 (unless *cliki2-compiled-grammar*
24 (recompile-cliki2-grammar))
25 (iter (multiple-value-bind (block pos)
26 (parse *cliki2-compiled-grammar* markup :start curpos :junk-allowed t)
27 (while block)
28 (collect block)
29 (while pos)
30 (setf curpos pos)))))
d3e78ff @archimag added registration and authentication
archimag authored
31
32 (defun generate-html-from-markup (markup)
57013f5 @archimag added cliki2 markup extensions: article-link, person-link, hyperspec-lin...
archimag authored
33 (let ((input (3bmd::expand-tabs markup :add-newlines t)))
1b6cc32 @archimag sanitize cliki2 pages
archimag authored
34 (sanitize:clean (with-output-to-string (s)
35 (3bmd:print-doc-to-stream (parse-cliki2-doc input) s))
36 sanitize:+relaxed+)))
57013f5 @archimag added cliki2 markup extensions: article-link, person-link, hyperspec-lin...
archimag authored
37
38 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
39 ;;; cliki2 markup extensions
40 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
41
b4b73a6 @archimag added "package-link" markup extension
archimag authored
42 ;;;; article-link
43
57013f5 @archimag added cliki2 markup extensions: article-link, person-link, hyperspec-lin...
archimag authored
44 (define-rule article-link (and (and (? #\\) "_(") (+ (and (! #\)) character)) #\))
45 (:destructure (start article end)
46 (declare (ignore start end))
7b531a5 @archimag Revert "sync with 3bmd/extensible branch"
archimag authored
47 (cons :article-link (cliki2:normalize-name (concat article)))))
57013f5 @archimag added cliki2 markup extensions: article-link, person-link, hyperspec-lin...
archimag authored
48
7b531a5 @archimag Revert "sync with 3bmd/extensible branch"
archimag authored
49 (defmethod 3bmd:print-tagged-element ((tag (eql :article-link)) stream title)
57013f5 @archimag added cliki2 markup extensions: article-link, person-link, hyperspec-lin...
archimag authored
50 (write-string (cliki2.view:article-link
51 (list :title title
e9228f3 @archimag improved internal links
archimag authored
52 :new (null (cliki2::article-with-downcase-title (string-downcase title)))
57013f5 @archimag added cliki2 markup extensions: article-link, person-link, hyperspec-lin...
archimag authored
53 :href (restas:genurl 'cliki2:view-article :title title)))
54 stream))
55
b4b73a6 @archimag added "package-link" markup extension
archimag authored
56 ;;;; person-link
57
57013f5 @archimag added cliki2 markup extensions: article-link, person-link, hyperspec-lin...
archimag authored
58 (define-rule person-link (and "_P(" (+ (and (! #\)) character)) #\))
59 (:destructure (start name end)
60 (declare (ignore start end))
7b531a5 @archimag Revert "sync with 3bmd/extensible branch"
archimag authored
61 (cons :person-link (cliki2:normalize-name (concat name)))))
57013f5 @archimag added cliki2 markup extensions: article-link, person-link, hyperspec-lin...
archimag authored
62
7b531a5 @archimag Revert "sync with 3bmd/extensible branch"
archimag authored
63 (defmethod 3bmd:print-tagged-element ((tag (eql :person-link)) stream name)
57013f5 @archimag added cliki2 markup extensions: article-link, person-link, hyperspec-lin...
archimag authored
64 (write-string (cliki2.view:person-link
65 (list :name name
66 :href (restas:genurl 'cliki2:view-person :name name)))
67 stream))
68
b4b73a6 @archimag added "package-link" markup extension
archimag authored
69 ;;;; hyperspec-link
70
57013f5 @archimag added cliki2 markup extensions: article-link, person-link, hyperspec-lin...
archimag authored
71 (define-rule hyperspec-link (and "_H(" (+ (and (! #\)) character)) #\))
72 (:destructure (start symbol end)
73 (declare (ignore start end))
7b531a5 @archimag Revert "sync with 3bmd/extensible branch"
archimag authored
74 (cons :hyperspec-link (concat symbol))))
5606e43 @archimag added to markup: hyperspec-ref and code-block
archimag authored
75
7b531a5 @archimag Revert "sync with 3bmd/extensible branch"
archimag authored
76 (defmethod 3bmd:print-tagged-element ((tag (eql :hyperspec-link)) stream symbol)
57013f5 @archimag added cliki2 markup extensions: article-link, person-link, hyperspec-lin...
archimag authored
77 (write-string (cliki2.view:hyperspec-link
78 (list :symbol symbol
79 :href (clhs-lookup:spec-lookup symbol)))
80 stream))
5606e43 @archimag added to markup: hyperspec-ref and code-block
archimag authored
81
b4b73a6 @archimag added "package-link" markup extension
archimag authored
82 ;;;; category-link
83
b2d06bf @archimag added cliki2 markup extensions: category-link and category-list
archimag authored
84 (define-rule category-link (and (and (? #\\) "*(") (+ (and (! #\)) character)) #\))
85 (:destructure (start category end)
86 (declare (ignore start end))
7b531a5 @archimag Revert "sync with 3bmd/extensible branch"
archimag authored
87 (cons :article-link (cliki2:normalize-name (concat category)))))
b2d06bf @archimag added cliki2 markup extensions: category-link and category-list
archimag authored
88
b4b73a6 @archimag added "package-link" markup extension
archimag authored
89 ;;;; code-block
90
57013f5 @archimag added cliki2 markup extensions: article-link, person-link, hyperspec-lin...
archimag authored
91 (define-rule empty-lines
92 (* (and (* (or #\Space #\Tab)) (? #\Return) #\Newline)))
5606e43 @archimag added to markup: hyperspec-ref and code-block
archimag authored
93
57013f5 @archimag added cliki2 markup extensions: article-link, person-link, hyperspec-lin...
archimag authored
94 (define-rule code-block (and "<code>"
95 empty-lines
96 (+ (and (! (and empty-lines "</code>")) character))
97 empty-lines
98 "</code>")
99 (:destructure (start w1 code w2 end)
100 (declare (ignore start w1 w2 end))
7b531a5 @archimag Revert "sync with 3bmd/extensible branch"
archimag authored
101 (cons :lisp-code-block (concat code))))
17416b2 @archimag added markup: article-ref and person-ref
archimag authored
102
7b531a5 @archimag Revert "sync with 3bmd/extensible branch"
archimag authored
103 (defmethod 3bmd:print-tagged-element ((tag (eql :lisp-code-block)) stream code)
57013f5 @archimag added cliki2 markup extensions: article-link, person-link, hyperspec-lin...
archimag authored
104 (write-string (cliki2.view:code-block
105 (list :code (colorize::html-colorization :common-lisp code)))
106 stream))
b4b73a6 @archimag added "package-link" markup extension
archimag authored
107
108 ;;;; category-list
57013f5 @archimag added cliki2 markup extensions: article-link, person-link, hyperspec-lin...
archimag authored
109
b2d06bf @archimag added cliki2 markup extensions: category-link and category-list
archimag authored
110 (defun category-char-p (character)
111 (not (member character '(#\: #\" #\)))))
112
113 (define-rule category-name (and (? #\") (+ (category-char-p character)) (? #\"))
114 (:lambda (list)
7b531a5 @archimag Revert "sync with 3bmd/extensible branch"
archimag authored
115 (concat (second list))))
b2d06bf @archimag added cliki2 markup extensions: category-link and category-list
archimag authored
116
117 (define-rule category-list (and (and (? #\\) "_/(")
118 category-name
119 (* (and (! #\)) character))
120 ")")
121 (:lambda (list)
122 (cons :cliki2-category-list (cliki2:category-keyword (second list)))))
123
e666c10 @archimag "category list" markup extension - add short article description (as in ...
archimag authored
124 (sanitize:define-sanitize-mode +simple+
125 :elements ("a")
e9228f3 @archimag improved internal links
archimag authored
126 :attributes (("a" . ("href" "class")))
e666c10 @archimag "category list" markup extension - add short article description (as in ...
archimag authored
127 :protocols (("a" . (("href" . (:ftp :http :https :mailto :relative))))))
128
129 (defun format-article-description (article)
130 (sanitize:with-clean-fragment (fragment
131 (with-output-to-string (s)
132 (let ((3bmd::*references* (make-hash-table)))
7b531a5 @archimag Revert "sync with 3bmd/extensible branch"
archimag authored
133 (3bmd::print-element (parse '3bmd-grammar::block
e666c10 @archimag "category list" markup extension - add short article description (as in ...
archimag authored
134 (cliki2::article-content-head article)
135 :junk-allowed t)
136 s)))
137 +simple+)
138 (with-output-to-string (out)
139 (iter (for item in-child-nodes fragment)
140 (for text = (html:serialize-html item :to-string))
141 (for len initially 0 then (+ len (length text)))
142 (let ((dot-pos (if (xtree:text-p item) (position #\. text))))
143 (cond
144 (dot-pos (write-string (subseq text 0 (1+ dot-pos)) out)
145 (finish))
146 (t (write-string text out))))))))
b2d06bf @archimag added cliki2 markup extensions: category-link and category-list
archimag authored
147
7b531a5 @archimag Revert "sync with 3bmd/extensible branch"
archimag authored
148 (defmethod 3bmd:print-tagged-element ((tag (eql :cliki2-category-list)) stream category)
b2d06bf @archimag added cliki2 markup extensions: category-link and category-list
archimag authored
149 (write-string (cliki2.view:category-content
150 (list :items
e666c10 @archimag "category list" markup extension - add short article description (as in ...
archimag authored
151 (iter (for article in (sort (copy-list (cliki2::articles-with-category category))
152 #'string<
247ecc7 @archimag improved "category list" markup extension - sort articles by downcase ti...
archimag authored
153 :key 'cliki2::article-downcase-title))
b2d06bf @archimag added cliki2 markup extensions: category-link and category-list
archimag authored
154 (collect
155 (list :title (cliki2::article-title article)
e666c10 @archimag "category list" markup extension - add short article description (as in ...
archimag authored
156 :head (format-article-description article)
b2d06bf @archimag added cliki2 markup extensions: category-link and category-list
archimag authored
157 :href (restas:genurl 'cliki2:view-article
158 :title (cliki2::article-title article)))))))
159 stream))
9873a6f @archimag added categories
archimag authored
160
b4b73a6 @archimag added "package-link" markup extension
archimag authored
161 ;;;; package-link
162
163 (define-rule package-link (and ":(package" (+ (or #\Tab #\Space #\Newline #\Return)) "\"" (+ (and (! #\") character)) "\")")
164 (:destructure (start w1 quote link end)
165 (declare (ignore start w1 quote end))
7b531a5 @archimag Revert "sync with 3bmd/extensible branch"
archimag authored
166 (cons :package-link (concat link))))
b4b73a6 @archimag added "package-link" markup extension
archimag authored
167
7b531a5 @archimag Revert "sync with 3bmd/extensible branch"
archimag authored
168 (defmethod 3bmd:print-tagged-element ((tag (eql :package-link)) stream link)
b4b73a6 @archimag added "package-link" markup extension
archimag authored
169 (write-string (cliki2.view:package-link (list :href link))
170 stream))
171
172 ;;;; cliki2 markup extensions
e666c10 @archimag "category list" markup extension - add short article description (as in ...
archimag authored
173
7b531a5 @archimag Revert "sync with 3bmd/extensible branch"
archimag authored
174 (define-rule 3bmd-grammar:inline-extensions
175 (or article-link
176 person-link
177 hyperspec-link
178 category-link
179 code-block
180 category-list
181 package-link))
Something went wrong with that request. Please try again.