Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 162 lines (144 sloc) 6.985 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
(in-package #:cliki2)
(in-readtable cliki2)

(sanitize:define-sanitize-mode +links-only+
    :elements ("a")
    :attributes (("a" . ("href" "class")))
    :protocols (("a" . (("href" . (:ftp :http :https :mailto :relative))))))

(sanitize:define-sanitize-mode +cliki-tags+
    :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"
               "em" "i" "small" "big" "tt" "strike" "strong" "dfn" "s" "sub"
               "sup" "u" "abbr" "ruby" "rp" "rt" "bdo" "mark" "br" "hr")
    :attributes ((:all . ("dir" "lang" "title" "class"))
                 ("a" . ("href"))
                 ("abbr" . ("title"))
                 ("bdo" . ("dir"))
                 ("blockquote" . ("cite"))
                 ("col" . ("span" "width" "align" "valign"))
                 ("colgroup" . ("span" "width" "align" "valign"))
                 ("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))))))

(defun generate-html-from-markup (markup)
  #H[<div id="article">]
  (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*))
  #H[</div>])

(defun parse-markup-fragment (markup start end)
  (ppcre:regex-replace-all
   "\\n\\n"
   (sanitize:clean
    (cl-ppcre:regex-replace-all
     "< "
     (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
    #?/(?:href|HREF)="(.*?)"/
markup
(lambda (match url)
(declare (ignore match))
(format nil "href=\"~A\""
(cl-ppcre:regex-replace-all "\\(|\\)" url #'uri-encode :simple-calls t)))
:simple-calls t :start start :end end))

(defun parse-cliki-markup (markup)
(loop for prefix in '("_" "_H" "\\*" "\\/" "_P")
for formatter in '(pprint-article-underscore-link format-hyperspec-link pprint-topic-link format-topic-list format-package-link)
do (setf markup (process-cliki-rule markup prefix formatter)))
markup)

(defun pprint-article-underscore-link (title)
(destructuring-bind (real-title &optional link-title)
(cl-ppcre:split "\\|" title)
(%print-article-link real-title "internal" (or link-title real-title))))

(defun process-cliki-rule (markup prefix formatter)
(ppcre:regex-replace-all #?/${prefix}\((.*?)\)/
markup
(lambda (match r1)
(declare (ignore match))
(with-output-to-string (*html-stream*)
(funcall formatter r1)))
:simple-calls t))

(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+)}
</li>])

(defun format-topic-list (topic) ;; /(
#H[<ul>] (dolist (article (articles-by-topic topic))
(pprint-article-summary-li article "-"))
#H[</ul>])

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

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

(let ((supported-langs (sort (mapcar (lambda (x)
(symbol-name (car x)))
colorize::*coloring-types*)
#'> :key #'length)))
(defun markup-code (markup start end)
(ppcre:regex-replace
"<(?:CODE|code)(.*?)?>((?:.|\\n)*?)</(?:CODE|code)>" markup
(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
(let ((colorize::*css-background-class* "nonparen"))
#?[<div class="code">${(colorize::html-colorization lang code)}</div>])
             #?[<code>${(escape-for-html code)}</code>])))
     :simple-calls t :start start :end end)))

Something went wrong with that request. Please try again.