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 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
|
(in-package #:cl-markdown)
(defstruct (html-markup
(:conc-name markup-)
(:print-object print-html-markup))
name outer inner tag encoding-method contentlessp (nestsp t))
(defmethod print-html-markup (markup stream)
(print-unreadable-object (markup stream :type nil :identity t)
(bind (((:struct markup- name inner outer encoding-method) markup))
(format stream "~a : ~a ~a ~a"
name outer inner encoding-method))))
(defparameter *markup->html*
(make-container
'simple-associative-container
:test #'equal
:initial-contents
(loop for datum in '((header1 nil nil "h1")
(header2 nil nil "h2")
(header3 nil nil "h3")
(header4 nil nil "h4")
(header5 nil nil "h5")
(header6 nil nil "h6")
(bullet ("ul") ("li") nil :nestsp t)
(code ("pre" "code") nil nil :new-method encode-pre)
(number ("ol") ("li") nil :nestsp nil)
(quote ("blockquote") nil nil)
(horizontal-rule nil nil "hr" :contentlessp t)) nconc
(bind (((tag outer inner markup &key
new-method
contentlessp (nestsp t))
datum))
(list (list tag)
(make-html-markup
:name tag
:outer outer
:inner inner
:tag markup
:encoding-method new-method
:contentlessp contentlessp
:nestsp nestsp))))))
(defvar *magic-space-p* nil)
(defvar *magic-line-p* 0)
(defparameter *magic-space* #\Space)
(defparameter *magic-line* nil)
#+(or)
(setf *magic-space* #\| *magic-line* #\~)
(defgeneric render-to-html (stuff encoding-method)
(:documentation ""))
(defmethod render ((document abstract-document) (style (eql :html)) stream)
(declare (ignore stream))
(setf *magic-space-p* nil
*magic-line-p* 0)
(render-to-html document nil))
(defun html-block-markup (chunk)
(aand+
(markup-class-for-html chunk)
(markup-outer it)))
(defun html-inner-block-markup (chunk)
(aand
(markup-class-for-html chunk)
(markup-inner it)))
(defmethod render-to-html ((chunk chunk) encoding-method)
(bind (((:struct markup- (markup tag)
(new-method encoding-method) contentlessp)
(markup-class-for-html chunk))
(paragraph? (paragraph? chunk)))
(cond (contentlessp
(format *output-stream* "<~a/>" markup))
(t
(encode-html chunk (or new-method encoding-method)
markup (when paragraph? "p"))))))
;;?? same code as below
(defmethod encode-html ((stuff chunk) encoding-method &rest codes)
(declare (dynamic-extent codes))
(setf *magic-line-p* 0)
(cond ((null codes)
(let ((code-p (member 'code (markup-class stuff))))
(iterate-elements
(lines stuff)
(lambda (line)
(render-to-html line encoding-method)
(when code-p (incf *magic-line-p*))))
;(fresh-line *output-stream*)
))
((null (first codes))
(apply #'encode-html stuff encoding-method (rest codes)))
(t (format *output-stream* "<~a>" (first codes))
(apply #'encode-html stuff encoding-method (rest codes))
(format *output-stream* "</~a>" (first codes)))))
;;?? same code as above
(defmethod encode-html ((stuff list) encoding-method &rest codes)
(declare (dynamic-extent codes))
(cond ((null codes)
(iterate-elements
stuff
(lambda (line)
(render-to-html line encoding-method))))
((null (first codes))
(apply #'encode-html stuff encoding-method (rest codes)))
(t (format *output-stream* "<~A>" (first codes))
(apply #'encode-html stuff encoding-method (rest codes))
(format *output-stream* "</~A>" (first codes)))))
(defmethod markup-class-for-html ((chunk chunk))
(if (markup-class chunk)
(let ((translation (item-at-1 *markup->html* (markup-class chunk))))
(unless translation
(markdown-warning "No translation for markup class '~A'"
(markup-class chunk)))
translation)
(load-time-value (make-html-markup))))
(defmethod render-to-html ((chunk list) encoding-method)
(render-span-to-html (first chunk) (rest chunk) encoding-method))
(defmethod render-to-html ((line string) encoding-method)
(when *magic-space-p*
(setf *magic-space-p* nil)
(princ *magic-space* *output-stream*))
(when (> *magic-line-p* 0)
(when *magic-line*
(princ *magic-line* *output-stream*))
(terpri *output-stream*))
(format *output-stream* "~a"
(funcall (or encoding-method 'encode-string-for-html) line))
(setf *magic-space-p* t))
(defun output-html (string &rest codes)
(declare (dynamic-extent codes))
(cond ((null codes) (princ (first string) *output-stream*))
(t (format *output-stream* "<~(~a~)>" (first codes))
(apply #'output-html string (rest codes))
(format *output-stream* "</~(~a~)>" (first codes)))))
(defmethod render-span-to-html ((code (eql 'strong)) body encoding-method)
(declare (ignore encoding-method))
(output-html body 'strong)
(setf *magic-space-p* nil))
(defmethod render-span-to-html ((code (eql 'mail)) body encoding-method)
(declare (ignore encoding-method))
(let ((address (first body)))
(output-link (format nil "mailto:~A" address) nil address)))
(defmethod render-span-to-html ((code (eql 'emphasis)) body encoding-method)
(declare (ignore encoding-method))
(output-html body 'em)
(setf *magic-space-p* nil))
(defmethod render-span-to-html ((code (eql 'strong-em)) body encoding-method)
(declare (ignore encoding-method))
(output-html body 'strong 'em)
(setf *magic-space-p* nil))
(defmethod render-span-to-html
((code (eql 'escaped-character)) body encoding-method)
(declare (ignore encoding-method))
(let ((char (aref (first body) 0)))
(cond ((char= #\< char) (princ "<" *output-stream*))
((char= #\> char) (princ ">" *output-stream*))
(t (output-html body))))
(setf *magic-space-p* nil))
(defmethod render-span-to-html ((code (eql 'code)) body encoding-method)
(declare (ignore encoding-method))
(format *output-stream* "<code>")
(setf *magic-space-p* nil)
(dolist (bit body)
(render-to-html bit 'encode-pre))
(format *output-stream* "</code>")
(setf *magic-space-p* nil))
(defmethod render-span-to-html ((code (eql 'entity)) body encoding-method)
(declare (ignore encoding-method))
(setf *magic-space-p* nil
*magic-line-p* -1)
(output-html body)
(setf *magic-space-p* nil
*magic-line-p* -1))
(defmethod render-span-to-html
((code (eql 'reference-link)) body encoding-method)
(declare (ignore encoding-method))
(bind (((:values text id nil)
(if (length-1-list-p body)
(values (first body) (first body) nil)
(values (butlast body 1) (first (last body)) t)))
(link-info (find-link id)))
(cond ((not (null link-info))
;; it _was_ a valid ID
(generate-link-output link-info text))
(t
(markdown-warning "No reference found for link ~s" id)
(format *output-stream* "~a" (if (consp text) (first text) text))
(setf *magic-space-p* nil)))))
(defmethod generate-link-output ((link-info link-info) text)
(output-link (url link-info) (title link-info) text (properties link-info)))
(defmethod generate-link-output ((link-info extended-link-info) text)
;; you didn't really want it to be fast did you...?
(generate-link-output-for-kind (kind link-info) link-info text))
(defmethod render-span-to-html
((code (eql 'inline-link)) body encoding-method)
(declare (ignore encoding-method))
(bind (((text &optional (url "") title) body))
(output-link url title (list text))))
(defmethod render-span-to-html ((code (eql 'link)) body encoding-method)
(declare (ignore encoding-method))
(let ((link (first body)))
(output-link link nil link)))
(defmethod render-span-to-html
((code (eql 'reference-image)) body encoding-method)
(declare (ignore encoding-method))
(bind (((:values text id nil)
(if (length-1-list-p body)
(values (first body) (first body) nil)
(values (butlast body 1) (first (last body)) t)))
(link-info (find-link id)))
(cond ((not (null link-info))
;; it _was_ a valid ID
(output-image (url link-info) (title link-info) text
(properties link-info)))
(t
;;?? hackish
(markdown-warning "No reference found for image link ~s" id)
(format *output-stream* "~a (image)"
(if (consp text) (first text) text))
(setf *magic-space-p* nil)))))
(defmethod render-span-to-html
((code (eql 'inline-image)) body encoding-method)
(declare (ignore encoding-method))
(bind (((text &optional (url "") title) body))
(output-image url title text)))
(defun output-link (url title text &optional properties)
(cond ((not (null url))
(format *output-stream* "<a href=\"~A\"~@[ title=\"~A\"~]"
url title)
(loop for (key . value) in properties do
(format *output-stream* " ~a=\"~a\"" key value))
(write-string ">" *output-stream*)
(setf *magic-space-p* nil)
(encode-html (ensure-list text) nil)
(format *output-stream* "</a>")
(setf *magic-space-p* nil))
(t
)))
(defun output-image (url title text &optional properties)
(cond ((not (null url))
(format *output-stream*
"<img src=\"~A\"~@[ title=\"~A\"~]~@[ alt=\"~A\"~]"
url title (first (ensure-list text)))
(loop for (key . value) in properties do
(format *output-stream* " ~a=\"~a\"" key value))
(write-string "></img>" *output-stream*)
(setf *magic-space-p* nil))
(t
)))
(defmethod render-span-to-html ((code (eql 'html)) body encoding-method)
;; hack!
(let ((output (first body)))
(etypecase output
(string
(output-html (list output #+(or) (encode-pre output))))
(list
(render-span-to-html (first output) (rest output) encoding-method)))))
;; Special cases R us.
(defmethod render-span-to-html ((code (eql 'break)) body encoding-method)
(encode-html body encoding-method)
(format *output-stream* "<br />~%"))
(defun stream-out-markup (markup reverse)
(dolist (marker (if reverse (reverse markup) markup))
(let ((cr? (member marker
*block-level-html-tags* :test 'string=)))
(when (and (not reverse) cr?)
(terpri *output-stream*))
(format *output-stream* "<~a~a>"
(if reverse "/" "") marker))))
;; FIXME - in case you're wondering, this is an ugly bit of code
(defmethod render-to-html ((document abstract-document) encoding-method)
(bind ((current-chunk nil)
(wrap-in-html (add-html-header-p document)))
(labels ((render-block (block level markup inner?)
; (print (list :rb level inner? (first block)))
(setf *magic-space-p* nil)
(let ((add-markup? (not (eq (first block) current-chunk)))
(real-markup
(if (and (not inner?) (length-1-list-p block))
(append
markup (html-inner-block-markup (first block)))
markup)))
(when add-markup?
(stream-out-markup real-markup nil))
(cond ((or (length-1-list-p block)
)
(render-to-html (first block) encoding-method))
((not add-markup?)
(render-to-html (first block) encoding-method)
(do-it (rest block) level))
(t
(setf current-chunk (and inner? (first block)))
(do-it block level)))
(when add-markup?
(stream-out-markup real-markup t))))
(do-it (chunks level)
; (print (list :di level (first chunks)))
(loop for rest = chunks then (rest rest)
for chunk = (first rest) then (first rest)
while chunk
for new-level = (level chunk)
when (= level new-level) do
(let ((index (inner-block rest))
(inner-markup (html-inner-block-markup chunk)))
(render-block (subseq rest 0 index)
level inner-markup t)
(setf rest (nthcdr (1- index) rest)))
when (< level new-level) do
(multiple-value-bind (block remaining method)
(next-block rest new-level)
(declare (ignore method))
(render-block
block new-level (html-block-markup chunk) nil)
(setf rest remaining)))))
(when wrap-in-html
(generate-html-header))
(do-it (collect-elements (chunks document)) (level document))
(when wrap-in-html
(format *output-stream* "~&</body>~&</html>~%")))))
(defmethod add-html-header-p ((document abstract-document))
(values nil))
(defmethod add-html-header-p ((document document))
(document-property :html))
(defun inner-block (chunks)
(bind ((level (level (first chunks)))
(markup-class (markup-class (first chunks)))
(nestsp (aand+ (markup-class-for-html (first chunks))
(markup-nestsp it))))
(or
;; if we go down a level immediately after, take whereever we go back up
;; or the end of the document.
;; FIXME - I think we're trying to find the _end_ of the _block_
;; and this would mean keeping track of nesting until we actually
;; come all the way "up" and out. Need a test case for this
(aand+ nestsp
(let ((next-chunk (first (rest chunks))))
(and next-chunk
(or (> (level next-chunk) level)
(and (= (level next-chunk) level)
(not (equal (markup-class next-chunk)
markup-class))))))
#+(or)
(position-if
(lambda (chunk)
(or (> (level chunk) level)
(and (= (level chunk) level)
(not (equal (markup-class chunk) markup-class)))))
(rest chunks))
(aif (position-if
(lambda (chunk)
(<= (level chunk) level))
(rest chunks) :start 1 #+(or) (1+ it))
(1+ it)
(length chunks)))
;; do we go up a level or change markup classes at the same level
(aand+ (position-if
(lambda (chunk)
(or (< (level chunk) level)
(and (= (level chunk) level)
(not (equal (markup-class chunk) markup-class)))))
(rest chunks))
(1+ it))
1)))
(defvar *html-meta*
'((name (author description copyright keywords date))
(http-equiv (refresh expires |Content-Type|))))
;; <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
(defun generate-html-header ()
(generate-doctype)
(format *output-stream* "~&<html xmlns='~a' xml:lang='~a' lang='~a'>"
(document-property :xmlns "http://www.w3.org/1999/xhtml")
(document-property :xmllang "en")
(document-property :lang "en"))
(format *output-stream* "~&<head>")
(when (document-property :header-comment)
(format *output-stream* "~&<!-- ~%~a~% -->~%"
(nth-value
1 (markdown (document-property :header-comment)
:properties '((:omit-initial-paragraph t)
(:omit-final-paragraph t)
(:html . nil))
:format :plain
:stream nil))))
(awhen (document-property "title")
(format *output-stream* "~&<title>~a</title>"
(process-child-markdown it :render :transfer-data nil)))
(let ((styles nil))
(flet ((output-style (it)
(bind (((name &optional media) (ensure-list it)))
(setf name (ensure-string name))
(unless (search ".css" name)
(setf name (concatenate 'string name ".css")))
(unless (member name styles :test 'string-equal)
(push name styles)
(if (document-property :make-style-sheet-inline)
(insert-style-sheet name media)
(format
*output-stream*
"~&<link type='text/css' href='~a' rel='stylesheet' ~@[media='~a' ~]/>"
name media))))))
(awhen (document-property "style-sheet")
(output-style it))
(loop for style in (document-property "style-sheets") do
(output-style style))
#+never ; This original loop form contained a free reference to `it'.
(loop for (kind properties) in *html-meta* do
(loop for property in properties do
(when (document-property (symbol-name property))
(format *output-stream* "~&<meta ~a=\"~a\" content=\"~a\"/>"
kind property it))))
; Rewritten smh 2008-05-`3
(loop for (kind properties) in *html-meta* do
(loop for property in properties
as val = (document-property (symbol-name property))
when val
do (format *output-stream* "~&<meta ~a=\"~a\" content=\"~a\"/>"
kind property val)))
(format *output-stream* "~&</head>~&<body~@[ id=\"~a\"~]>"
(document-property :markdown-body-id)))))
(defun generate-doctype ()
(format *output-stream*
"~&<!DOCTYPE HTML PUBLIC ~s ~s>"
"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"))
#|
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
|#
(defun insert-style-sheet (name media)
(let ((pathname (find-include-file name)))
(when pathname
(format *output-stream* "~&<style type='text/css'~@[ media='~a'~]>~%"
media)
(map-lines (pathname pathname)
(lambda (line)
(format *output-stream* "~& ~a~%" line)))
(format *output-stream* "~&</style>~%"))))
(defun output-anchor (name &optional (stream *output-stream*))
(let ((name (html-safe-name (ensure-string name))))
(format stream
"~&<a name=\"~a\" id=\"~a\"></a>~%"
name name)))
|