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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
|
(in-package #:cl-markdown)
(defun next-block (chunks level)
(let* (;; the first chunk after the current one with a lower level
(pos-level (position-if
(lambda (other)
(< (level other) level))
(rest chunks)))
;; the first chunk after the current one with different markup
;; and level less than or equal to
(pos-markup (position-if
(lambda (other)
(and (<= (level other) level)
(markup-class other) ;paragraphs don't count
(not (samep (markup-class other)
(markup-class (first chunks))))))
(rest chunks)))
pos-style
deeper)
#+Ignore
(format t "~%~d ~2,D:POS: ~A, ~A - ~A"
level (length chunks) pos-level pos-markup (first chunks))
;; remember that there will be another call to rest
(cond #+(or) ((null pos-level)
;; go all the way to the end
(setf deeper (subseq chunks 0)
chunks nil
pos-style :rest))
((or (and pos-level pos-markup (> pos-level pos-markup))
(and pos-level (not pos-markup)))
;(format t " -- level")
(setf deeper (subseq chunks 0 (1+ pos-level))
chunks (nthcdr pos-level chunks)
pos-style :level))
((or (and pos-level pos-markup (> pos-markup pos-level))
(and (not pos-level) pos-markup))
;(format t " -- markup")
(setf deeper (subseq chunks 0 (+ pos-markup 1))
chunks (nthcdr pos-markup chunks)
pos-style :markup))
((and pos-level pos-markup (= pos-level pos-markup))
;(format t " -- level")
(setf deeper (subseq chunks 0 (1+ pos-level))
chunks (nthcdr pos-level chunks)
pos-style :level))
(t
;; nothing found, take the rest
(setf deeper chunks
chunks nil
pos-style :none)))
;(format t "~{~% ~a~}" (collect-elements deeper))
(values deeper chunks pos-style)))
(defmethod render-to-stream (document style stream-specifier)
(with-stream-from-specifier (stream stream-specifier :output
:if-exists :supersede)
(let ((*current-document* document)
(*current-format* style)
(*output-stream* stream))
(setf (level document) 0
(markup document) nil)
(render document style stream))))
(defun ensure-string (it)
(typecase it
(string it)
(symbol (symbol-name it))
(t (format nil "~a" it))))
;; FIXME - bad name
;; this is the parent that is a child-document or document, not, e.g.,
;; an included-document
(defmethod main-parent ((document included-document))
(main-parent (parent document)))
(defmethod main-parent ((document abstract-document))
document)
(defun root-parent (document)
(or (and (parent document)
(root-parent (parent document)))
document))
(defun collect-links (document)
(collect-key-value (link-info document)
:transform (lambda (name link)
(cons name (url link)))
:filter (lambda (k v)
(declare (ignore k))
(typep v 'link-info))))
(defun starts-with (string prefix)
(let ((mismatch (mismatch prefix string)))
(or (not mismatch) (= mismatch (length prefix)))))
(defun markdown-warning (msg &rest args)
(let* ((*print-readably* nil)
(warning (apply #'format nil msg args)))
(when *current-document*
(push (cons (main-parent *current-document*) warning)
(warnings (root-parent *current-document*))))
(fresh-line *debug-io*)
(write-string warning *debug-io*)
(terpri *debug-io*)))
(eval-always
(defun _mark-range (array start end)
(loop for a from (char-code start) to (char-code end) do
(setf (sbit array a) 1)))
(defun _mark-one (array ch)
(setf (sbit array (char-code ch)) 1)))
(defparameter +first-name-characters+
(let ((array (make-array 255 :element-type 'bit :initial-element 0)))
(_mark-range array #\a #\z)
(_mark-range array #\A #\Z)
array))
(defparameter +name-characters+
(let ((array (copy-seq +first-name-characters+)))
(_mark-range array #\0 #\9)
(_mark-one array #\_)
(_mark-one array #\-)
(_mark-one array #\.)
(_mark-one array #\:)
array))
(defun html-safe-name (name)
;; Copied from HTML-Encode
;;?? this is very consy
;;?? crappy name
(declare (type simple-string name))
(let ((output (make-array (truncate (length name) 2/3)
:element-type 'character
:adjustable t
:fill-pointer 0))
(first? t))
(with-output-to-string (out output)
(loop for char across name
for code = (char-code char)
for valid = +first-name-characters+ then +name-characters+
do (cond ((and (< code 255)
(= (sbit valid code) 1))
(write-char char out))
(t
;; See http://www.w3.org/TR/html4/types.html#h-6.2
;; ID and NAME tokens must begin with a letter ([A-Za-z])
;; and may be followed by any number of letters,
;; digits ([0-9]), hyphens ("-"), underscores ("_"),
;; colons (":"), and periods (".").
(when first?
(write-char #\x out))
(format out ":~:@(~16,r~)" code)))
(setf first? nil)))
(coerce output 'simple-string)))
(defun string->list (string &key (stop-word-p (constantly nil))
(ignore-character-p (constantly nil)))
(let ((word-array (make-array (length string) :element-type 'character))
(results nil)
(index 0)
(in-quote-p nil))
(labels ((grab-char (ch)
(setf (aref word-array index) ch)
(incf index))
(add-word (word)
(push word results))
(maybe-add-word ()
(let ((word (coerce (subseq word-array 0 index) 'string)))
(unless (funcall stop-word-p word)
(add-word (strip-whitespace word))))
(setf index 0)))
(loop for i below (length string)
for ch = (aref string i) do
(cond ((char= ch #\\)
(grab-char ch)
(incf i)
(grab-char (aref string i)))
((char= ch #\")
(setf in-quote-p (not in-quote-p))
#+(or)
(grab-char #\"))
((and (not in-quote-p) (> index 0) (whitespacep ch))
;; have a word
(maybe-add-word))
((and (not in-quote-p) (funcall ignore-character-p ch))
)
(t
(grab-char ch))))
(when (> index 0)
(maybe-add-word))
(nreverse results))))
(defun process-brackets (document current-line line-iterator)
(bind ((output (make-array 4096
:element-type 'character
:adjustable t
:fill-pointer 0))
(depth 0)
(location 0)
(buffers (bracket-references document))
(buffer-count (size buffers))
(current-buffer nil))
(with-output-to-string (out output)
(block
iteration-block
(flet ((write-buffer-count (count)
(format out " ~d" count)
(setf current-buffer nil))
(add-char (ch)
(if current-buffer
(vector-push-extend ch current-buffer)
(write-char ch out)))
(start-buffer ()
(setf current-buffer
(make-array 4096
:element-type 'character
:adjustable t
:fill-pointer 0))))
(flet ((process-brackets-in-line (line)
;;(print (list :line line))
(with-iterator (iterator line
:treat-contents-as :characters
:skip-empty-chunks? nil)
(iterate-elements
iterator
(lambda (ch)
;;(print (list ch (not (null current-buffer))))
(incf location)
(cond ((char= ch #\\)
(unless (move-forward-p iterator)
(error "Invalid escape at char ~d" location))
(add-char ch)
(add-char (next-element iterator)))
((and (= depth 1) (not current-buffer)
(whitespacep ch))
;; finished reading the command name
;; (don't write the ws)
(start-buffer))
((char= ch #\{)
(incf depth)
(add-char ch))
((char= ch #\})
(decf depth)
(cond ((= depth 0)
(insert-item
buffers
(coerce current-buffer 'simple-string))
(write-buffer-count buffer-count))
((< depth 0)
;; FIXME -- an error
(setf depth 0)))
(add-char ch))
(t
(add-char ch))))))
;; if no brackets to process at the end of a line, bail
(when (= depth 0)
(return-from iteration-block nil))
(if (and (= depth 1) (not current-buffer))
(start-buffer)
(add-char #\Newline))))
(process-brackets-in-line current-line)
(move-forward line-iterator)
(iterate-elements
line-iterator
(lambda (line)
(process-brackets-in-line line))))))
(coerce output 'simple-string))))
#+ignore
(let ((li (make-iterator "a b
c d e
f" :treat-contents-as :lines)))
(iterate-elements
li
(lambda (x)
(iterate-elements li
(lambda (y)
(let ((ci (make-iterator y :treat-contents-as :characters :skip-empty-chunks? nil)))
(iterate-elements ci #'print)))))))
(defun short-source (source)
(typecase source
(pathname source)
(string
(format nil "~a~@[...~]"
(substitute-if #\Space
(lambda (ch)
(or (char= ch #\newline)
(char= ch #\linefeed)))
(subseq source 0 (min 50 (length source))))
(> (length source) 50)))
(t (format nil "Something of type ~s"
(type-of source)))))
(defun could-be-html-tag-p (string start)
;; assumes that start == the index _after_ the #\<
(let ((state :start))
(loop for index from start below (length string)
for ch = (schar string index) do
(ecase state
(:start (when (whitespacep ch) (return nil))
(setf state :running))
(:running
(case ch
(#\' (setf state :single-quote))
(#\" (setf state :double-quote))
(#\> (return index))))
(:single-quote
(case ch
(#\' (setf state :running))))
(:double-quote
(case ch
(#\" (setf state :running))))))))
;; <a title='>'>
;; ^
(defun stream-string-for-html (string stream)
(declare (simple-string string))
(let ((next-index 1)
(last-index nil))
(with-output (out stream)
(loop for char across string
do (cond
((char= char #\&) (write-string "&" out))
((char= char #\<)
(setf last-index (could-be-html-tag-p string next-index))
(if last-index
(write-char char out)
(write-string "<" out)))
((and (null last-index) (char= char #\>))
(write-string ">" out))
(t (write-char char out)))
(when (and last-index (> next-index last-index))
(setf last-index nil))
(incf next-index)))))
#+(or)
(stream-string-for-html "hello a > b and b < a <a title='>'>" nil)
(defun encode-string-for-html (string)
(stream-string-for-html string nil))
;; Copied from HTML-Encode
;;?? this is very consy
;;?? crappy name
(defun encode-pre (string)
(declare (simple-string string))
(let ((output (make-array (truncate (length string) 2/3)
:element-type 'character
:adjustable t
:fill-pointer 0)))
(with-output-to-string (out output)
(loop for char across string
do (case char
((#\&) (write-string "&" out))
((#\<) (write-string "<" out))
((#\>) (write-string ">" out))
(t (write-char char out)))))
(coerce output 'simple-string)))
;; Copied from HTML-Encode
;;?? this is very consy
;;?? crappy name
;;?? this is really bugging me -- what gross code and it's repeated FOUR times
;;
(defun encode-string-for-title (string)
(declare (simple-string string))
(if (find #\' string :test #'char=)
(let ((output (make-array (truncate (length string) 2/3)
:element-type 'character
:adjustable t
:fill-pointer 0)))
(with-output-to-string (out output)
(loop for char across string
do (case char
;; (code-char 39) ==> #\'
((#\') (write-string "'" out))
(t (write-char char out)))))
(coerce output 'simple-string))
string))
(defun find-include-file (pathname)
(bind ((pathname (ensure-string pathname))
(search-locations (ensure-list (document-property :search-locations)))
(result
(or (probe-file (merge-pathnames pathname))
;; look in search-locations
(some (lambda (location)
(probe-file (merge-pathnames pathname location)))
search-locations))))
(unless result
(markdown-warning
"Unable to find ~a in any of the search-locations ~{~a~^, ~}"
pathname search-locations))
result))
(defun process-child-markdown (text phase &key (transfer-data nil))
(bind (((:values child output)
(markdown text
:parent *current-document*
:format (if (eq phase :parse) :none *current-format*)
:properties '((:omit-initial-paragraph t)
(:omit-final-paragraph t))
:stream nil
:document-class 'included-document)))
(push child (children *current-document*))
(when transfer-data
(transfer-link-info *current-document* child "")
(transfer-document-metadata *current-document* child)
(transfer-selected-properties
*current-document* child
(set-difference (collect-keys (properties child))
(list :omit-initial-paragraph :omit-final-paragraph))))
(ecase phase
(:parse child)
(:render (strip-whitespace output)))))
(defun asdf-system-source-file (system-name)
(let ((system (asdf:find-system system-name)))
(make-pathname
:type "asd"
:name (asdf:component-name system)
:defaults (asdf:component-relative-pathname system))))
(defun asdf-system-source-directory (system-name)
(make-pathname :name nil
:type nil
:defaults (asdf-system-source-file system-name)))
(defun system-relative-pathname (system pathname &key name type)
(relative-pathname (asdf-system-source-directory system)
pathname :name name :type type))
|