| 1 |
;;;; mustache.lisp --- Mustache Template Renderer |
| 2 |
|
| 3 |
;;; Copyright (C) 2012, 2013 Kan-Ru Chen |
| 4 |
|
| 5 |
;;; Author: Kan-Ru Chen <[email protected]> |
| 6 |
|
| 7 |
;;; Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 8 |
;;; this software and associated documentation files (the "Software"), to deal in |
| 9 |
;;; the Software without restriction, including without limitation the rights to |
| 10 |
;;; use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 11 |
;;; of the Software, and to permit persons to whom the Software is furnished to do |
| 12 |
;;; so, subject to the following conditions: |
| 13 |
|
| 14 |
;;; The above copyright notice and this permission notice shall be included in all |
| 15 |
;;; copies or substantial portions of the Software. |
| 16 |
|
| 17 |
;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 |
;;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 |
;;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 |
;;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 |
;;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 |
;;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 |
;;; SOFTWARE. |
| 24 |
|
| 25 |
;;;; Commentary: |
| 26 |
|
| 27 |
;;; Mustache is a kind of logic-less template formats. |
| 28 |
;;; See also: http://mustache.github.com/ |
| 29 |
|
| 30 |
;;;; TODO: |
| 31 |
;;; |
| 32 |
;;; * Optimize lambda sections |
| 33 |
;;; * Optimize compiled renderer |
| 34 |
;;; * Better error reporting |
| 35 |
|
| 36 |
;;;; Code: |
| 37 |
|
| 38 |
(in-package :mustache) |
| 39 |
|
| 40 |
;;; Types |
| 41 |
|
| 42 |
(deftype offset () |
| 43 |
"File or string offset." |
| 44 |
'(unsigned-byte 32)) |
| 45 |
|
| 46 |
(deftype space-char () |
| 47 |
'(member #\Space #\Tab)) |
| 48 |
|
| 49 |
(deftype newline-char () |
| 50 |
'(member #\Linefeed #\Return)) |
| 51 |
|
| 52 |
(deftype text-char () |
| 53 |
'(and character (not (or space-char newline-char)))) |
| 54 |
|
| 55 |
(defun space-char-p (char) |
| 56 |
(declare (inline)) |
| 57 |
(typep char 'space-char)) |
| 58 |
|
| 59 |
(defun newline-char-p (char) |
| 60 |
(declare (inline)) |
| 61 |
(typep char 'newline-char)) |
| 62 |
|
| 63 |
(defun text-char-p (char) |
| 64 |
(declare (inline)) |
| 65 |
(typep char 'text-char)) |
| 66 |
|
| 67 |
(defclass token () ()) |
| 68 |
|
| 69 |
(defclass beginning-of-line (token) ()) |
| 70 |
|
| 71 |
(defclass text (token) |
| 72 |
((%text :type string |
| 73 |
:initarg :text |
| 74 |
:accessor text))) |
| 75 |
|
| 76 |
(defclass whitespace (text) ()) |
| 77 |
|
| 78 |
(defclass newline (text) |
| 79 |
((%text :initform #.(coerce '(#\Linefeed) 'string)))) |
| 80 |
|
| 81 |
(defvar crlf (coerce '(#\Return #\Linefeed) 'string)) |
| 82 |
|
| 83 |
(defclass crlf-newline (newline) |
| 84 |
((%text :initform crlf))) |
| 85 |
|
| 86 |
(defclass tag (token) |
| 87 |
((%text :type string |
| 88 |
:initarg :text |
| 89 |
:accessor text) |
| 90 |
(%escapep :type boolean |
| 91 |
:initarg :escape |
| 92 |
:initform t |
| 93 |
:reader escapep) |
| 94 |
(%indent :type list |
| 95 |
:initarg :indent |
| 96 |
:initform () |
| 97 |
:accessor indent))) |
| 98 |
(defclass can-standalone-tag (tag) ()) |
| 99 |
(defclass non-standalone-tag (tag) ()) |
| 100 |
|
| 101 |
(defclass normal-tag (non-standalone-tag) ()) |
| 102 |
|
| 103 |
(defclass ampersand-tag (non-standalone-tag) |
| 104 |
((%escapep :initform nil))) |
| 105 |
|
| 106 |
(defclass delimiter-tag (can-standalone-tag) ()) |
| 107 |
|
| 108 |
(defclass comment-tag (can-standalone-tag) ()) |
| 109 |
|
| 110 |
(defclass partial-tag (can-standalone-tag) ()) |
| 111 |
|
| 112 |
(defclass section-start-tag (can-standalone-tag) |
| 113 |
((%falsey :type boolean |
| 114 |
:initarg :falsey |
| 115 |
:initform nil |
| 116 |
:accessor falsey) |
| 117 |
(%end :type offset |
| 118 |
:initarg :end |
| 119 |
:initform 0 |
| 120 |
:accessor end) |
| 121 |
(%open-delimiter :type string |
| 122 |
:initarg :open-delimiter |
| 123 |
:initform "" |
| 124 |
:accessor open-delimiter) |
| 125 |
(%close-delimiter :type string |
| 126 |
:initarg :close-delimiter |
| 127 |
:initform "" |
| 128 |
:accessor close-delimiter))) |
| 129 |
|
| 130 |
(defclass section-end-tag (can-standalone-tag) |
| 131 |
((%start :type offset |
| 132 |
:initarg :start |
| 133 |
:initform 0 |
| 134 |
:accessor start))) |
| 135 |
|
| 136 |
(defclass section-tag (section-start-tag section-end-tag) |
| 137 |
((%tokens :type list |
| 138 |
:initarg :tokens |
| 139 |
:accessor tokens))) |
| 140 |
|
| 141 |
;;; Delimiter |
| 142 |
|
| 143 |
(defparameter *default-open-delimiter* "{{") |
| 144 |
(defparameter *default-close-delimiter* "}}") |
| 145 |
(defparameter *default-triple-open-delimiter* "{{{") |
| 146 |
(defparameter *default-triple-close-delimiter* "}}}") |
| 147 |
|
| 148 |
(defvar *open-delimiter* *default-open-delimiter*) |
| 149 |
(defvar *close-delimiter* *default-close-delimiter*) |
| 150 |
(defvar *triple-open-delimiter* *default-triple-open-delimiter*) |
| 151 |
(defvar *triple-close-delimiter* *default-triple-close-delimiter*) |
| 152 |
|
| 153 |
(defun change-delimiter (text) |
| 154 |
"Change the mustache tag delimiter according to TEXT. |
| 155 |
The syntax grammar is: |
| 156 |
delimiter-tag = left-d 1*space right-d |
| 157 |
left-d = *ALPHANUM |
| 158 |
right-d = *ALPHANUM |
| 159 |
space = #\\Space #\\Tab" |
| 160 |
(declare (type string text)) |
| 161 |
(let* ((left-edge (position #\Space text)) |
| 162 |
(right-edge (position #\Space text :from-end t))) |
| 163 |
(unless (and left-edge right-edge |
| 164 |
(every #'space-char-p |
| 165 |
(subseq text left-edge right-edge))) |
| 166 |
(error "Invalid delimiter tag ~a" text)) |
| 167 |
(setf *open-delimiter* (subseq text 0 left-edge) |
| 168 |
*close-delimiter* (subseq text (1+ right-edge))))) |
| 169 |
|
| 170 |
;;; Parser |
| 171 |
|
| 172 |
(defvar *mustache-tag-table* (make-hash-table)) |
| 173 |
|
| 174 |
(defun set-mustache-character (char new-function) |
| 175 |
(setf (gethash char *mustache-tag-table*) new-function)) |
| 176 |
|
| 177 |
(defun get-mustache-character (char) |
| 178 |
(declare (inline)) |
| 179 |
(gethash char *mustache-tag-table*)) |
| 180 |
|
| 181 |
(defun make-tag (&key str escapep start end) |
| 182 |
(declare (type simple-string str) |
| 183 |
(type boolean escapep) |
| 184 |
(type offset start end)) |
| 185 |
(let* ((tag-fun (get-mustache-character (char str 0))) |
| 186 |
(tag-text (string-trim '(#\Space #\Tab) str)) |
| 187 |
(arg-text (string-trim '(#\Space #\Tab) (subseq tag-text 1)))) |
| 188 |
(if tag-fun |
| 189 |
(funcall tag-fun str arg-text escapep start end) |
| 190 |
(make-instance 'normal-tag :text tag-text :escape escapep)))) |
| 191 |
|
| 192 |
(defmacro define-mustache-character (char &body body) |
| 193 |
`(set-mustache-character |
| 194 |
,char (lambda (raw-text arg-text escapep start end) |
| 195 |
(declare (ignorable raw-text arg-text escapep start end) |
| 196 |
(type simple-string raw-text arg-text) |
| 197 |
(boolean escapep) |
| 198 |
(offset start end)) |
| 199 |
,@body))) |
| 200 |
|
| 201 |
(define-mustache-character #\& |
| 202 |
(make-instance 'ampersand-tag :text arg-text)) |
| 203 |
|
| 204 |
(define-mustache-character #\# |
| 205 |
(make-instance 'section-start-tag :text arg-text :end end |
| 206 |
:open-delimiter *open-delimiter* |
| 207 |
:close-delimiter *close-delimiter*)) |
| 208 |
|
| 209 |
(define-mustache-character #\^ |
| 210 |
(make-instance 'section-start-tag :text arg-text :end end :falsey t)) |
| 211 |
|
| 212 |
(define-mustache-character #\/ |
| 213 |
(make-instance 'section-end-tag :text arg-text :start start)) |
| 214 |
|
| 215 |
(define-mustache-character #\! |
| 216 |
(make-instance 'comment-tag :text "")) |
| 217 |
|
| 218 |
(define-mustache-character #\= |
| 219 |
(let ((arg-text (string-trim '(#\Space #\Tab #\=) arg-text))) |
| 220 |
(prog1 |
| 221 |
(make-instance 'delimiter-tag :text arg-text) |
| 222 |
(change-delimiter arg-text)))) |
| 223 |
|
| 224 |
(define-mustache-character #\> |
| 225 |
(make-instance 'partial-tag :text arg-text)) |
| 226 |
|
| 227 |
(defmethod print-object ((object tag) stream) |
| 228 |
(print-unreadable-object (object stream :type t :identity t) |
| 229 |
(princ (text object) stream))) |
| 230 |
|
| 231 |
;;; Lexer |
| 232 |
|
| 233 |
;; Invariant token |
| 234 |
(defvar beginning-of-line (make-instance 'beginning-of-line)) |
| 235 |
(defvar newline (make-instance 'newline)) |
| 236 |
(defvar crlf-newline (make-instance 'crlf-newline)) |
| 237 |
|
| 238 |
(defun string-starts-with-p (pattern string start) |
| 239 |
(declare (type string pattern string) |
| 240 |
(type offset start) |
| 241 |
(inline)) |
| 242 |
(eql (string<= pattern string :start2 start) |
| 243 |
(length pattern))) |
| 244 |
|
| 245 |
(defun read-text (type string start end) |
| 246 |
(declare (type symbol type) |
| 247 |
(type string string) |
| 248 |
(offset start end)) |
| 249 |
(loop :for idx :from start :below end |
| 250 |
:while (case type |
| 251 |
(text (text-char-p (char string idx))) |
| 252 |
(whitespace (space-char-p (char string idx)))) |
| 253 |
:until (string-starts-with-p *open-delimiter* string idx) |
| 254 |
:finally (return (values (make-instance type |
| 255 |
:text (subseq string start idx)) |
| 256 |
idx)))) |
| 257 |
|
| 258 |
(defun read-newline (string start) |
| 259 |
(declare (type string string) |
| 260 |
(type offset start)) |
| 261 |
(if (string-starts-with-p crlf string start) |
| 262 |
(values crlf-newline (+ 2 start)) |
| 263 |
(values newline (1+ start)))) |
| 264 |
|
| 265 |
(defun read-tag (string triplep start end) |
| 266 |
(declare (type string string) |
| 267 |
(type boolean triplep) |
| 268 |
(type offset start end)) |
| 269 |
(let ((before-tag start) |
| 270 |
(tag-open (if triplep *triple-open-delimiter* *open-delimiter*)) |
| 271 |
(tag-close (if triplep *triple-close-delimiter* *close-delimiter*))) |
| 272 |
(when (string-starts-with-p tag-open string start) |
| 273 |
(incf start (length tag-open)) |
| 274 |
(loop :for idx :from start :below end |
| 275 |
:until (string-starts-with-p tag-close string idx) |
| 276 |
:finally (let ((endpos (+ idx (length tag-close)))) |
| 277 |
(return (values (make-tag :str (subseq string |
| 278 |
start idx) |
| 279 |
:escapep (not triplep) |
| 280 |
:start before-tag |
| 281 |
:end endpos) |
| 282 |
endpos))))))) |
| 283 |
|
| 284 |
(defun read-token (string start end) |
| 285 |
(declare (type string string) |
| 286 |
(type offset start end)) |
| 287 |
(let ((char (char string start))) |
| 288 |
(cond |
| 289 |
((space-char-p char) |
| 290 |
(read-text 'whitespace string start end)) |
| 291 |
((newline-char-p char) |
| 292 |
(read-newline string start)) |
| 293 |
((string-starts-with-p *triple-open-delimiter* string start) |
| 294 |
(read-tag string t start end)) |
| 295 |
((string-starts-with-p *open-delimiter* string start) |
| 296 |
(read-tag string nil start end)) |
| 297 |
(t |
| 298 |
(read-text 'text string start end))))) |
| 299 |
|
| 300 |
(defun scan (string &optional (start 0) (end (length string))) |
| 301 |
(declare (type string string) |
| 302 |
(type offset start end)) |
| 303 |
(let ((*open-delimiter* *default-open-delimiter*) |
| 304 |
(*close-delimiter* *default-close-delimiter*)) |
| 305 |
(loop :with idx :of-type offset := start |
| 306 |
:with token |
| 307 |
:while (> end idx) |
| 308 |
:when (zerop idx) |
| 309 |
:collect beginning-of-line |
| 310 |
:do (multiple-value-setq (token idx) |
| 311 |
(read-token string idx end)) |
| 312 |
:collect token |
| 313 |
:when (and (< idx end) |
| 314 |
(typep token 'newline)) |
| 315 |
:collect beginning-of-line))) |
| 316 |
|
| 317 |
;;; Parser |
| 318 |
|
| 319 |
(deftype text-token () |
| 320 |
'(and token |
| 321 |
(not (or beginning-of-line can-standalone-tag newline whitespace)))) |
| 322 |
|
| 323 |
(defun collect-line (tokens) |
| 324 |
(declare (type list tokens)) |
| 325 |
(flet ((newlinep (token) |
| 326 |
(typep token 'newline))) |
| 327 |
(loop :for start := 0 :then (1+ finish) |
| 328 |
:for finish := (position-if #'newlinep tokens :start start) |
| 329 |
:when (subseq tokens start (and finish (1+ finish))) |
| 330 |
:collect it |
| 331 |
:until (null finish)))) |
| 332 |
|
| 333 |
(defun tokens-standalone-p (tokens) |
| 334 |
(declare (type list tokens)) |
| 335 |
(when (eq (car tokens) beginning-of-line) |
| 336 |
(loop :for token :in tokens |
| 337 |
:count (typep token 'can-standalone-tag) :into tags |
| 338 |
:count (typep token 'text-token) :into texts |
| 339 |
:finally (return (and (= 1 tags) |
| 340 |
(= 0 texts)))))) |
| 341 |
|
| 342 |
(defun find-standalone-tag (tokens) |
| 343 |
(declare (type list tokens)) |
| 344 |
(flet ((tagp (token) |
| 345 |
(typep token 'tag))) |
| 346 |
(let* ((pos (position-if #'tagp tokens)) |
| 347 |
(tag (elt tokens pos))) |
| 348 |
(setf (indent tag) (remove beginning-of-line |
| 349 |
(subseq tokens 0 pos))) |
| 350 |
tag))) |
| 351 |
|
| 352 |
(defun trim-standalone (tokens) |
| 353 |
(declare (type list tokens)) |
| 354 |
(loop :for line :in (collect-line tokens) |
| 355 |
:append (if (tokens-standalone-p line) |
| 356 |
(list (find-standalone-tag line)) |
| 357 |
line))) |
| 358 |
|
| 359 |
(defun make-section-tag (start-tag end-tag tokens) |
| 360 |
(declare (type tag start-tag end-tag) |
| 361 |
(type list tokens)) |
| 362 |
(make-instance 'section-tag |
| 363 |
:tokens tokens |
| 364 |
:text (text start-tag) |
| 365 |
:falsey (falsey start-tag) |
| 366 |
:start (end start-tag) |
| 367 |
:end (start end-tag) |
| 368 |
:open-delimiter (open-delimiter start-tag) |
| 369 |
:close-delimiter (close-delimiter start-tag))) |
| 370 |
|
| 371 |
(defun group-sections (tokens &optional sections acc) |
| 372 |
(declare (type list tokens sections acc)) |
| 373 |
(labels ((push-group (acc) |
| 374 |
(cons nil acc)) |
| 375 |
(push-token (token acc) |
| 376 |
(cons (cons token (car acc)) (cdr acc))) |
| 377 |
(pop-group (acc) |
| 378 |
(cdr acc)) |
| 379 |
(top-group (acc) |
| 380 |
(reverse (car acc))) |
| 381 |
(push-section-tag (start-tag end-tag acc) |
| 382 |
(push-token (make-section-tag start-tag end-tag (top-group acc)) |
| 383 |
(pop-group acc))) |
| 384 |
(tag-match (tag1 tag2) |
| 385 |
(string-equal (text tag1) (text tag2)))) |
| 386 |
(if (not tokens) |
| 387 |
(top-group acc) |
| 388 |
(let ((token (car tokens)) |
| 389 |
(rest (cdr tokens)) |
| 390 |
(start-tag (car sections))) |
| 391 |
(typecase token |
| 392 |
(section-start-tag |
| 393 |
(group-sections rest (cons token sections) (push-group acc))) |
| 394 |
(section-end-tag |
| 395 |
(when (tag-match token start-tag) |
| 396 |
(group-sections rest (cdr sections) |
| 397 |
(push-section-tag start-tag token acc)))) |
| 398 |
(otherwise |
| 399 |
(group-sections rest sections (push-token token acc)))))))) |
| 400 |
|
| 401 |
(defun fold-text (tokens) |
| 402 |
(declare (type list tokens)) |
| 403 |
(flet ((textp (token) |
| 404 |
(typep token 'text))) |
| 405 |
(loop :for start := 0 :then next |
| 406 |
:for finish := (position-if-not #'textp tokens :start start) |
| 407 |
:for next := (and finish (position-if #'textp tokens :start finish)) |
| 408 |
:for texts := (subseq tokens start finish) |
| 409 |
:when texts |
| 410 |
:collect (make-instance 'text :text |
| 411 |
(format nil "~{~a~}" (mapcar #'text texts))) |
| 412 |
:when (and finish |
| 413 |
(subseq tokens finish next)) |
| 414 |
:append it |
| 415 |
:while next))) |
| 416 |
|
| 417 |
(defun parse (template) |
| 418 |
(declare (inline)) |
| 419 |
(group-sections (fold-text (trim-standalone (scan template))))) |
| 420 |
|
| 421 |
;;; Context |
| 422 |
|
| 423 |
(defvar *context* nil "Current context for lambda section") |
| 424 |
|
| 425 |
(defclass context () |
| 426 |
((%data :initarg :data |
| 427 |
:initform nil |
| 428 |
:accessor data) |
| 429 |
(%indent :type list |
| 430 |
:initarg :indent |
| 431 |
:initform nil |
| 432 |
:accessor indent) |
| 433 |
(%partials :initarg :partials |
| 434 |
:initform nil |
| 435 |
:accessor partials) |
| 436 |
(%next :type (or null context) |
| 437 |
:initarg :next |
| 438 |
:initform nil |
| 439 |
:accessor next))) |
| 440 |
|
| 441 |
(defun parse-key (string) |
| 442 |
(declare (type string string)) |
| 443 |
(cond |
| 444 |
((string= string ".") 'implicit-iterator) |
| 445 |
(t (loop :for start := 0 :then (1+ finish) |
| 446 |
:for finish := (position #\. string :start start) |
| 447 |
:collect (string-upcase (subseq string start finish)) |
| 448 |
:until (null finish))))) |
| 449 |
|
| 450 |
(defun key (token) |
| 451 |
(check-type token token) |
| 452 |
(parse-key (text token))) |
| 453 |
|
| 454 |
(deftype alist () |
| 455 |
'(cons (cons atom) (or cons null))) |
| 456 |
|
| 457 |
(defun save-hash-table (source) |
| 458 |
(typecase source |
| 459 |
(null) |
| 460 |
(string source) |
| 461 |
(alist |
| 462 |
(loop :with table := (make-hash-table :test 'equal) |
| 463 |
:for (key . value) :in (reverse source) |
| 464 |
:do (setf (gethash (string-upcase key) table) |
| 465 |
(save-hash-table value)) |
| 466 |
:finally (return table))) |
| 467 |
(sequence |
| 468 |
(let ((result (map 'vector #'save-hash-table source))) |
| 469 |
(when (plusp (length result)) |
| 470 |
result))) |
| 471 |
(otherwise source))) |
| 472 |
|
| 473 |
(defun make-context-chain (data context) |
| 474 |
(declare (type context context)) |
| 475 |
(let ((ctx (make-instance 'context))) |
| 476 |
(setf (data ctx) data |
| 477 |
(indent ctx) (indent context) |
| 478 |
(partials ctx) (partials context) |
| 479 |
(next ctx) context) |
| 480 |
ctx)) |
| 481 |
|
| 482 |
(defun ensure-context (maybe-context) |
| 483 |
"Ensure MAYBE-CONTEXT is a valid context. If not then make one." |
| 484 |
(ctypecase maybe-context |
| 485 |
(list (make-instance 'context :data (save-hash-table maybe-context))) |
| 486 |
(hash-table (make-instance 'context :data maybe-context)) |
| 487 |
(context maybe-context))) |
| 488 |
|
| 489 |
(defgeneric context-get (key context) |
| 490 |
(:documentation "Get data from CONTEXT by KEY.")) |
| 491 |
|
| 492 |
(defmethod context-get ((key string) (context null)) |
| 493 |
(declare (ignore key)) |
| 494 |
(values)) |
| 495 |
|
| 496 |
(defmethod context-get ((key string) (context hash-table)) |
| 497 |
(gethash (string-upcase key) context)) |
| 498 |
|
| 499 |
(defmethod context-get ((key string) context) |
| 500 |
(multiple-value-bind (data find) |
| 501 |
(context-get key (data context)) |
| 502 |
(if find |
| 503 |
(values data find) |
| 504 |
(when (next context) |
| 505 |
(context-get key (next context)))))) |
| 506 |
|
| 507 |
(defmethod context-get ((key (eql 'implicit-iterator)) context) |
| 508 |
(values (data context) t)) |
| 509 |
|
| 510 |
(defmethod context-get ((key list) context) |
| 511 |
(multiple-value-bind (data find) |
| 512 |
(context-get (car key) context) |
| 513 |
(if (cdr key) |
| 514 |
(context-get (cdr key) data) |
| 515 |
(values data find)))) |
| 516 |
|
| 517 |
;;; Partials |
| 518 |
|
| 519 |
(defvar *load-path* (list *default-pathname-defaults*) |
| 520 |
"A list. The search pathes for partials.") |
| 521 |
(defvar *default-pathname-type* "mustache" |
| 522 |
"The default file extension for partials.") |
| 523 |
|
| 524 |
(defun filename (filename) |
| 525 |
(declare (type (or string pathname) filename)) |
| 526 |
(or (uiop:file-exists-p filename) |
| 527 |
(uiop:file-exists-p (make-pathname :type *default-pathname-type* |
| 528 |
:defaults filename)))) |
| 529 |
|
| 530 |
(defun locate-file (filename) |
| 531 |
(declare (type (or string pathname) filename)) |
| 532 |
(uiop:ensure-pathname filename :want-file t) |
| 533 |
(labels ((filename (path filename) |
| 534 |
(merge-pathnames |
| 535 |
path (make-pathname |
| 536 |
:type *default-pathname-type* |
| 537 |
:defaults filename))) |
| 538 |
(dir-file-exists-p (path) |
| 539 |
(uiop:file-exists-p (filename path filename)))) |
| 540 |
(some #'dir-file-exists-p *load-path*))) |
| 541 |
|
| 542 |
(defun read-partial (filename context) |
| 543 |
(declare (type (or string pathname) filename) |
| 544 |
(type context context)) |
| 545 |
(let ((from-context (context-get filename (partials context)))) |
| 546 |
(if from-context |
| 547 |
from-context |
| 548 |
(let ((pathname (locate-file filename))) |
| 549 |
(when pathname |
| 550 |
(uiop:read-file-string pathname)))))) |
| 551 |
|
| 552 |
;;; Rendering Utils |
| 553 |
|
| 554 |
(defun escape (string) |
| 555 |
"HTML escape STRING." |
| 556 |
(declare (type string string)) |
| 557 |
(flet ((needs-escape-p (char) |
| 558 |
(member char '(#\< #\> #\& #\\ #\" #\'))) |
| 559 |
(escape-char (char) |
| 560 |
(case char |
| 561 |
(#\& "&") |
| 562 |
(#\< "<") |
| 563 |
(#\> ">") |
| 564 |
(#\" """) |
| 565 |
(t (format nil "&#~d;" (char-code char)))))) |
| 566 |
(with-output-to-string (datum) |
| 567 |
(loop :for start := 0 :then (1+ pos) |
| 568 |
:for pos := (position-if #'needs-escape-p string :start start) |
| 569 |
:do (write-string string datum :start start :end pos) |
| 570 |
:when pos |
| 571 |
:do (write-string (escape-char (char string pos)) datum) |
| 572 |
:while pos)))) |
| 573 |
|
| 574 |
(defvar *real-standard-output* (make-synonym-stream 'cl:*standard-output*)) |
| 575 |
(defvar *output-stream* (make-synonym-stream 'cl:*standard-output*) |
| 576 |
"The default output stream for mustache rendering. Bind this |
| 577 |
variable before calling mustache-rendering and friends. Default is |
| 578 |
*standard-output*.") |
| 579 |
|
| 580 |
(defun %output () |
| 581 |
(if (eq *mustache-output* *real-standard-output*) |
| 582 |
*output-stream* |
| 583 |
*mustache-output*)) |
| 584 |
|
| 585 |
(defgeneric print-data (data escapep context)) |
| 586 |
|
| 587 |
(defmethod print-data ((data string) escapep context) |
| 588 |
(declare (ignore context)) |
| 589 |
(write-string (if escapep (escape data) data) (%output))) |
| 590 |
|
| 591 |
(defmethod print-data ((data symbol) escapep context) |
| 592 |
(print-data (string data) escapep context)) |
| 593 |
|
| 594 |
(defmethod print-data ((data function) escapep context) |
| 595 |
(let ((*context* context)) |
| 596 |
(let* ((value (format nil "~a" (funcall data))) |
| 597 |
(fun (compile-template value)) |
| 598 |
(output (with-output-to-string (*output-stream*) |
| 599 |
(funcall fun context)))) |
| 600 |
(write-string (if escapep (escape output) output) (%output))))) |
| 601 |
|
| 602 |
(defmethod print-data (token escapep context) |
| 603 |
(print-data (princ-to-string token) escapep context)) |
| 604 |
|
| 605 |
(defun call-lambda (lambda text context) |
| 606 |
(declare (type function lambda) |
| 607 |
(type string text) |
| 608 |
(type context context)) |
| 609 |
(let ((*context* context)) |
| 610 |
(let* ((value (format nil "~a" (funcall lambda text))) |
| 611 |
(fun (compile-template value)) |
| 612 |
(output (with-output-to-string (*output-stream*) |
| 613 |
(funcall fun context)))) |
| 614 |
(write-string output (%output))))) |
| 615 |
|
| 616 |
;;; Renderer |
| 617 |
|
| 618 |
(defgeneric render-token (token context template)) |
| 619 |
|
| 620 |
(defmethod render-token ((token text) context (template string)) |
| 621 |
(declare (ignore template)) |
| 622 |
(print-data (text token) nil context)) |
| 623 |
|
| 624 |
(defmethod render-token ((token tag) context (template string)) |
| 625 |
(declare (ignore template)) |
| 626 |
(multiple-value-bind (dat find) |
| 627 |
(context-get (key token) context) |
| 628 |
(when find |
| 629 |
(print-data dat (escapep token) context)))) |
| 630 |
|
| 631 |
(defmethod render-token ((token partial-tag) context (template string)) |
| 632 |
(let ((fun (compile-template |
| 633 |
(or (read-partial (text token) context) "")))) |
| 634 |
(push (indent token) (indent context)) |
| 635 |
(funcall fun context) |
| 636 |
(pop (indent context)))) |
| 637 |
|
| 638 |
(defmethod render-token ((token section-tag) context (template string)) |
| 639 |
(multiple-value-bind (ctx find) |
| 640 |
(context-get (key token) context) |
| 641 |
(when (or find (falsey token)) |
| 642 |
(flet ((render (context template) |
| 643 |
(render-tokens (tokens token) context template))) |
| 644 |
(if (falsey token) |
| 645 |
(when (null ctx) |
| 646 |
(render (make-context-chain () context) template)) |
| 647 |
(typecase ctx |
| 648 |
(hash-table |
| 649 |
(render (make-context-chain ctx context) template)) |
| 650 |
(function |
| 651 |
(let ((*default-open-delimiter* (open-delimiter token)) |
| 652 |
(*default-close-delimiter* (close-delimiter token))) |
| 653 |
(call-lambda ctx (subseq template (start token) (end token)) context))) |
| 654 |
((and (not string) sequence) |
| 655 |
(map nil (lambda (ctx) |
| 656 |
(render (make-context-chain ctx context) template)) |
| 657 |
ctx)) |
| 658 |
(null) |
| 659 |
(t |
| 660 |
(render context template)))))))) |
| 661 |
|
| 662 |
(defmethod render-token ((token beginning-of-line) context (template string)) |
| 663 |
(declare (ignore token)) |
| 664 |
(render-tokens (car (indent context)) context template)) |
| 665 |
|
| 666 |
(defun render-tokens (tokens context template) |
| 667 |
(declare (type list tokens) |
| 668 |
(type context context) |
| 669 |
(type string template)) |
| 670 |
(loop :for token :in tokens |
| 671 |
:do (render-token token context template))) |
| 672 |
|
| 673 |
(defun render-body (tokens context template) |
| 674 |
(let ((context (ensure-context context))) |
| 675 |
(with-standard-io-syntax |
| 676 |
(render-tokens tokens context template)))) |
| 677 |
|
| 678 |
;;; Interfaces |
| 679 |
|
| 680 |
(defun version () |
| 681 |
"Return the CL-MUSTACHE version." |
| 682 |
#.(format nil "CL-MUSTACHE ~A (Mustache spec ~A)" |
| 683 |
(with-open-file (f (merge-pathnames "version.lisp-expr" |
| 684 |
(or *compile-file-pathname* |
| 685 |
*load-truename*))) |
| 686 |
(read f)) |
| 687 |
(with-open-file (f (merge-pathnames "spec-version.lisp-expr" |
| 688 |
(or *compile-file-pathname* |
| 689 |
*load-truename*))) |
| 690 |
(read f)))) |
| 691 |
|
| 692 |
(defun make-context (&key data partials) |
| 693 |
"Create mustache context from alist DATA." |
| 694 |
(make-instance 'context :data (save-hash-table data) |
| 695 |
:partials (save-hash-table partials))) |
| 696 |
|
| 697 |
(defgeneric compile-template (template) |
| 698 |
(:documentation "Return a compiled rendering function.")) |
| 699 |
|
| 700 |
(defmethod compile-template ((template string)) |
| 701 |
(let ((tokens (parse template))) |
| 702 |
(lambda (&optional context output-stream) |
| 703 |
(let ((*output-stream* (or output-stream *output-stream*))) |
| 704 |
(render-body tokens context template))))) |
| 705 |
|
| 706 |
(defmethod compile-template ((template pathname)) |
| 707 |
(let ((buffer (uiop:read-file-string (filename template)))) |
| 708 |
(compile-template buffer))) |
| 709 |
|
| 710 |
(defgeneric render (template &optional context output-stream) |
| 711 |
(:documentation |
| 712 |
"Render TEMPLATE with optional CONTEXT to *OUTPUT-STREAM* or OUTPUT-STREAM")) |
| 713 |
|
| 714 |
(defmethod render ((template string) &optional context output-stream) |
| 715 |
(let ((*output-stream* (or output-stream *output-stream*))) |
| 716 |
(render-body (parse template) context template))) |
| 717 |
|
| 718 |
(defmethod render ((template pathname) &optional context output-stream) |
| 719 |
(let ((buffer (uiop:read-file-string (filename template)))) |
| 720 |
(render buffer context output-stream))) |
| 721 |
|
| 722 |
(defun render* (template &optional context) |
| 723 |
"Render TEMPLATE with optional CONTEXT to string." |
| 724 |
(with-output-to-string (out) |
| 725 |
(render template context out))) |
| 726 |
|
| 727 |
(defmacro define (name template) |
| 728 |
"Define a named renderer of string TEMPLATE." |
| 729 |
`(setf (symbol-function ',name) |
| 730 |
(compile-template ,template))) |
| 731 |
|
| 732 |
;;; mustache.lisp ends here |
| 733 |
|
| 734 |
;;; Local Variables: |
| 735 |
;;; mode: lisp |
| 736 |
;;; End: |