markup.lisp 7.74 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
;;; Copyright 2011 Andrey Moskvitin <archimag@gmail.com>
;;; Copyright 2011, 2012 Vladimir Sedach <vsedach@common-lisp.net>
;;; Copyright 2012 Goheeca

;;; SPDX-License-Identifier: AGPL-3.0-or-later

;;; This program is free software: you can redistribute it and/or
;;; modify it under the terms of the GNU Affero 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
;;; Affero General Public License for more details.

;;; You should have received a copy of the GNU Affero General Public
;;; License along with this program. If not, see
;;; <http://www.gnu.org/licenses/>.

21
22
(in-package #:cliki2)
(in-readtable cliki2)
23

24
25
26
27
(sanitize:define-sanitize-mode +links-only+
    :elements ("a")
    :attributes (("a" . ("href" "class")))
    :protocols  (("a" . (("href" . (:ftp :http :https :mailto :relative))))))
Moskvitin Andrey's avatar
Moskvitin Andrey committed
28

29
(sanitize:define-sanitize-mode +cliki-tags+
30
31
32
33
    :elements ("a" "blockquote" "q" "dd" "dl" "dt" "h1" "h2" "h3" "h4" "h5"
               "h6" "hgroup" "pre" "code" "kbd" "samp" "cite" "var" "time"
               "figure" "figcaption" "img" "table" "caption" "tbody" "td"
               "tfoot" "th" "thead" "tr" "col" "colgroup" "ul" "ol" "li" "b"
34
35
               "em" "i" "small" "big" "tt" "strike" "strong" "dfn" "s" "sub"
               "sup" "u" "abbr" "ruby" "rp" "rt" "bdo" "mark" "br" "hr")
36
37
    :attributes ((:all         . ("dir" "lang" "title" "class"))
                 ("a"          . ("href"))
38
39
                 ("abbr"       . ("title"))
                 ("bdo"        . ("dir"))
40
                 ("blockquote" . ("cite"))
41
42
                 ("col"        . ("span" "width" "align" "valign"))
                 ("colgroup"   . ("span" "width" "align" "valign"))
43
44
45
46
47
48
49
50
51
52
53
54
55
                 ("img"        . ("align" "alt" "height" "src" "width"))
                 ("ol"         . ("start" "reversed" "type"))
                 ("ul"         . ("type"))
                 ("code"       . ("lang"))
                 ("q"          . ("cite"))
                 ("table"      . ("summary" "width"))
                 ("td"         . ("abbr" "axis" "colspan" "rowspan" "width"))
                 ("th"         . ("abbr" "axis" "colspan" "rowspan" "scope" "width")))

    :protocols (("a"           . (("href" . (:ftp :http :https :mailto :relative))))
                ("blockquote"  . (("cite" . (:http :https :relative))))
                ("img"         . (("src"  . (:http :https :relative))))
                ("q"           . (("cite" . (:http :https :relative))))))
56

57
(defun generate-html-from-markup (markup)
58
  #H[<div id="article">]
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
  (let ((start 0)
        tag-start
        close-tag)
      (labels ((find-tag (tag start)
                 (search tag markup :start2 start :test #'string-equal))
               (find-next-tag (start)
                 (let ((next-pre  (find-tag "<pre>" start))
                       (next-code (find-tag "<code" start))
                       min)
                   (when next-pre
                     (setf close-tag "</pre>"
                           min       next-pre))
                   (when next-code
                     (unless (and next-pre (< next-pre next-code))
                       (setf close-tag "</code>"
                             min       next-code)))
                   min)))
        (loop while (setf tag-start (find-next-tag start))
              do (write-string (parse-markup-fragment markup start tag-start)
                               *html-stream*)
                 (setf start (+ (length close-tag)
                                (or (find-tag close-tag tag-start)
                                    (return))))
                 (write-string (funcall (if (equal close-tag "</pre>")
                                            #'escape-pre-block
                                            #'markup-code)
                                        markup tag-start start)
                               *html-stream*)))
      (write-string (parse-markup-fragment markup start (length markup))
                    *html-stream*))
89
  #H[</div>])
90

91
(defun parse-markup-fragment (markup start end)
92
  (ppcre:regex-replace-all
93
94
   "\\n\\n"
   (sanitize:clean
95
    (ppcre:regex-replace-all
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
     "< "
     (parse-cliki-markup
      (escape-parens-in-href-links markup start end))
     "&lt; ")
    +cliki-tags+)
   "<p>"))

(defun escape-pre-block (markup start end)
  (ppcre:regex-replace
     "<(?:PRE|pre)>((?:.|\\n)*?)</(?:PRE|pre)>" markup
     (lambda (match preformatted)
       (declare (ignore match))
       #?[<pre>${(escape-for-html preformatted)}</pre>])
     :simple-calls t :start start :end end))

(defun escape-parens-in-href-links (markup start end)
  (ppcre:regex-replace-all
113
    "(?:href|HREF)=\"[^\"]*\""
114
    markup
115
116
117
118
119
120
121
122
    (lambda (match)
      (with-output-to-string (s)
        (loop for c across match do
          (princ (case c
                   (#\( "%28")
                   (#\) "%29")
                   (t   c))
                 s))))
123
    :simple-calls t :start start :end end))
124

125
(defun parse-cliki-markup (markup)
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
  (ppcre:regex-replace-all
   "(_|\\*|/|_H|_P)\\(((?:\\\\\\)|[^\\)])*)\\)"
   markup
   (lambda (match prefix title)
     (declare (ignore match))
     (with-output-to-string (*html-stream*)
       (funcall (cdr
                 (assoc prefix
                        '(("_"  . pprint-article-underscore-link)
                          ("*"  . pprint-topic-link)
                          ("/"  . format-topic-list)
                          ("_H" . format-hyperspec-link)
                          ("_P" . format-package-link))
                        :test #'string=))
                (ppcre:regex-replace-all "\\\\\\)" title ")"))))
   :simple-calls t))
142

143
(defun pprint-article-underscore-link (title)
144
145
146
147
  (destructuring-bind (&optional (real-title "") link-title)
      (ppcre:split "\\|" title)
    (%print-article-link real-title "internal"
                         (or link-title real-title))))
148

149
150
151
152
153
154
155
156
157
158
(defun article-description (article-title)
  (let ((c (cached-content article-title)))
    (subseq c 0 (ppcre:scan "\\.(?:\\s|$)|\\n|$" c))))

(defun pprint-article-summary-li (article-title separator)
  #H[<li>] (pprint-article-link article-title) #H[ ${separator}
  ${(sanitize:clean
     (with-output-to-string (*html-stream*)
       (generate-html-from-markup (article-description article-title)))
     +links-only+)}
159
  </li>])
160

161
162
(defun format-topic-list (topic) ;; /(
  #H[<ul>] (dolist (article (articles-by-topic topic))
163
             (pprint-article-summary-li article "-"))
164
  #H[</ul>])
Moskvitin Andrey's avatar
Moskvitin Andrey committed
165

166
167
(defun format-hyperspec-link (symbol) ;; _H(
  #H[<a href="${(clhs-lookup:spec-lookup symbol)}" class="hyperspec">${symbol}</a>])
168

169
(defun format-package-link (link) ;; _P(
170
  #H[<a href="${link}">ASDF-install package (obsolete) ${link}</a>])
171

172
173
174
175
(let ((supported-langs (sort (mapcar (lambda (x)
                                       (symbol-name (car x)))
                                     colorize::*coloring-types*)
                             #'> :key #'length)))
176
177
178
  (defun markup-code (markup start end)
    (ppcre:regex-replace
     "<(?:CODE|code)(.*?)?>((?:.|\\n)*?)</(?:CODE|code)>" markup
179
180
181
182
183
184
     (lambda (match maybe-lang code)
       (declare (ignore match))
       (let ((lang (loop for lang in supported-langs
                         when (search lang maybe-lang :test #'char-equal)
                         return (find-symbol lang :keyword))))
         (if lang
Goheeca's avatar
Goheeca committed
185
             (let ((colorize::*css-background-class* "nonparen"))
186
	       #?[<div class="code">${(colorize::html-colorization lang code)}</div>])
187
188
             #?[<code>${(escape-for-html code)}</code>])))
     :simple-calls t :start start :end end)))