From d036fade731faf57f45acb2fe0129ef0e8207342 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Sat, 19 Mar 2011 13:02:59 +0100 Subject: Add class TEMPLATE for improved encapsulation. --- json-template.lisp | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'json-template.lisp') diff --git a/json-template.lisp b/json-template.lisp index ab7b04d..0abd91b 100644 --- a/json-template.lisp +++ b/json-template.lisp @@ -24,6 +24,10 @@ (in-package #:json-template) +(defclass template () + ((things :initarg :things :accessor template-things))) + + (defun tokenize-template-string (string) (loop with in-command-p = nil with skip-newline-p = nil @@ -48,7 +52,8 @@ until (null terminator))) (defun parse-template-string (string) - (parse-raw-tokens (tokenize-template-string string))) + (make-instance 'template + :things (parse-raw-tokens (tokenize-template-string string)))) (defun parse-directive (string) (let* ((space1 (position #\Space string)) @@ -165,7 +170,7 @@ (lookup-in-stack contexts))) -(defun expand-template-to-stream (template contexts stream) +(defun expand-things-to-stream (template contexts stream) (dolist (thing template) (ecase (first thing) (:text @@ -182,9 +187,9 @@ (:section (destructuring-bind (section branch alternative) (cdr thing) (let ((value (lookup-context contexts section))) - (expand-template-to-stream (if value branch alternative) - (cons value contexts) - stream)))) + (expand-things-to-stream (if value branch alternative) + (cons value contexts) + stream)))) (:repeated-section (destructuring-bind (section branch alternative separator) (cdr thing) (let ((value (lookup-context contexts section))) @@ -193,9 +198,13 @@ (map nil (lambda (ctx) (unless first-loop - (expand-template-to-stream separator contexts stream)) - (expand-template-to-stream branch (cons ctx contexts) stream) + (expand-things-to-stream separator contexts stream)) + (expand-things-to-stream branch (cons ctx contexts) stream) (setq first-loop nil)) value)) - (expand-template-to-stream alternative contexts stream)))))))) + (expand-things-to-stream alternative contexts stream)))))))) + + +(defun expand-template-to-stream (template contexts stream) + (expand-things-to-stream (template-things template) contexts stream)) -- cgit v1.2.3