Skip to content

Commit

Permalink
with-output-whitespace
Browse files Browse the repository at this point in the history
Introduce tag for local control of whitespace output.
Have a look at tests.
See Github issue #4
  • Loading branch information
mmontone committed Jan 14, 2024
1 parent 9815c31 commit 98d863a
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 12 deletions.
24 changes: 13 additions & 11 deletions compiler.lisp
Expand Up @@ -77,26 +77,28 @@
(coerce body 'list))
for output := (emit elem)
when output
collect output))))
collect output))))
(let ((exprs (read-template-expressions (code tag))))
(case (first exprs)
(ten/template::section ;; sections are a special case
(ten/template:with-output-whitespace ;; control whitespace output
(let ((*output-whitespace* (cadr exprs)))
`(,@exprs ,@(emit-body (body tag)))))
(ten/template::section ;; sections are a special case
;; push the section to the list of sections
;; to generate render-section methods later
(push (list (second exprs)
(emit-body (body tag)))
*sections*)
`(ten/compiler::render-section ',(second exprs) ten/template::%ten-template %ten-stream))
(cl:if ;; check there's an else tag in body and emit
(when (not (find-if (lambda (x) (typep x 'ten/parser::<else-tag>))
(body tag)))
(error "Missing {% else %} in {% if %} expression: ~a"
tag))
`(,@exprs ,@(emit-body (body tag))))
(t ;; otherwise, just emit
(cl:if ;; check there's an else tag in body and emit
(when (not (find-if (lambda (x) (typep x 'ten/parser::<else-tag>))
(body tag)))
(error "Missing {% else %} in {% if %} expression: ~a"
tag))
`(,@exprs ,@(emit-body (body tag))))
(t ;; otherwise, just emit
`(,@exprs ,@(emit-body (body tag))))))))


(defun control-element-p (element)
(typep element '<control-tag>))

Expand Down Expand Up @@ -134,7 +136,7 @@
(ten/template::%ten-template ,(getf *compiling-template* :name))
%ten-stream)
(declare (ignore section))
(declare (ignorable %ten-stream))
(declare (ignorable %ten-stream))
(with-slots ,arg-names ten/template::%ten-template
,@body))))))))

Expand Down
22 changes: 22 additions & 0 deletions examples/with-output-whitespace.html
@@ -0,0 +1,22 @@
{% template with-output-whitespace-test-1 (:output-whitespace nil) (data) %}
{% loop for item in data do %}
{{item.car}} {{item.cdr}}
{% end %}
{% end %}

{% template with-output-whitespace-test-2 (:output-whitespace nil) (data) %}
{% loop for item in data do %}
{% with-output-whitespace T %}{{item.car}} {{item.cdr}}
{% end %}
{% end %}
{% end %}

{% template with-output-whitespace-test-3 (:output-whitespace nil) (data) %}
{% loop for item in data do %}
{% with-output-whitespace T %}{{item.car}} {{item.cdr}}
{% end %}
{% end %}
{% loop for item in data do %}
{{item.car}} {{item.cdr}}
{% end %}
{% end %}
3 changes: 3 additions & 0 deletions package.lisp
Expand Up @@ -22,6 +22,7 @@
:super
:section
:comment
:with-output-whitespace
:_
:%ten-stream
:*template-output*
Expand Down Expand Up @@ -49,6 +50,7 @@
:verbatim
:comment
:super
:with-output-whitespace
:section
:begin-raw
:begin-verb
Expand All @@ -64,6 +66,7 @@
:comment
:super
:section
:with-output-whitespace
:begin-raw
:begin-verb
:begin-verbatim
Expand Down
4 changes: 4 additions & 0 deletions template.lisp
Expand Up @@ -153,3 +153,7 @@ IMPORTANT: some of this macro arguments are processed by CALL-WITH-TEMPLATE-HEAD
(progn
(call-next-method)
""))

(defmacro with-output-whitespace (value &body body)
`(let ((*output-whitespace* ,value))
,@body))
1 change: 1 addition & 0 deletions ten.examples.asd
Expand Up @@ -21,4 +21,5 @@
(:ten-template "filters" :file-extension "html" :package :ten/examples)
(:ten-template "escaping" :file-extension "html" :package :ten/examples)
(:ten-template "control" :file-extension "html" :package :ten/examples)
(:ten-template "with-output-whitespace" :file-extension "html" :package :ten/examples)
))))
15 changes: 14 additions & 1 deletion tests.lisp
Expand Up @@ -31,7 +31,7 @@

(def-test if-expression-test ()
;; {% if %} without {% else %} should fail to compile
(signals error
(signals error
(ten:compile-template "{% template if-test-1 () (locale) %}
<html {% if locale %}lang=\"{{ locale }}\"{% end %}>
{% end %}"))
Expand All @@ -45,3 +45,16 @@
(ten:compile-template "{% template if-test-3 () (locale) %}
<html {% when locale %}lang=\"{{ locale }}\"{% end %}>
{% end %}")))

(def-test with-output-whitespace-test ()
(is (string= (ten/examples:with-output-whitespace-test-1 '((:x . 1) (:y . 2)))
"X1Y2"))
(is (string= (ten/examples:with-output-whitespace-test-2 '((:x . 1) (:y . 2)))
"X 1
Y 2
")
(is (string= (ten/examples:with-output-whitespace-test-3 '((:x . 1) (:y . 2)))
"X 1
Y 2
X1Y2"))
))

0 comments on commit 98d863a

Please sign in to comment.