Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow expression in fragment include directives #154

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ junit.xml
/*.docx
.DS_Store
.lsp/
*.jfr
*.jfr
.clj-kondo/
10 changes: 4 additions & 6 deletions src/stencil/cleanup.clj
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,7 @@
;; Itt nincsen blokk, amit normalizálni kellene
(defmethod control-ast-normalize :cmd/echo [echo-command] echo-command)

(defmethod control-ast-normalize :cmd/include [include-command]
(if-not (string? (:name include-command))
(throw (parsing-exception (pr-str (:name include-command))
"Parameter of include call must be a single string literal!"))
include-command))
(defmethod control-ast-normalize :cmd/include [include-command] include-command)

;; A feltételes elágazásoknál mindig generálunk egy javított THEN ágat
(defmethod control-ast-normalize :if [control-ast]
Expand Down Expand Up @@ -188,6 +184,7 @@
(collect-1 [mapping x]
(case (:cmd x)
:cmd/echo (expr mapping (:expression x))
:cmd/include (expr mapping (:name x))

:if (concat (expr mapping (:condition x))
(collect mapping (apply concat (::blocks x))))
Expand All @@ -205,7 +202,8 @@
;; returns a set of fragment names use in this document
(set (for [item (tree-seq map? (comp flatten ::blocks) {::blocks [control-ast]})
:when (map? item)
:when (= :cmd/include (:cmd item))]
:when (= :cmd/include (:cmd item))
:when (string? (:name item))]
(:name item))))

(defn process [raw-token-seq]
Expand Down
46 changes: 23 additions & 23 deletions src/stencil/model.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
(:require [clojure.data.xml :as xml]
[clojure.java.io :as io :refer [file]]
[stencil.eval :as eval]
[stencil.infix :refer [eval-rpn]]
[stencil.merger :as merger]
[stencil.types :refer [->FragmentInvoke]]
[stencil.util :refer [unlazy-tree eval-exception]]
Expand Down Expand Up @@ -193,32 +194,31 @@
; (xml-map-attrs {ooxml/r-embed id-rename ooxml/r-id id-rename} item))


(defmethod eval/eval-step :cmd/include [function local-data-map {frag-name :name}]
(defmethod eval/eval-step :cmd/include [function local-data-map step]
(assert (map? local-data-map))
(assert (string? frag-name))
(do
(if-let [fragment-model (get *all-fragments* frag-name)]
(let [;; merge style definitions from fragment
style-ids-rename (-> fragment-model :main :style :parsed (doto assert) (style/insert-styles!))
(let [frag-name (eval-rpn local-data-map function (:name step))]
(if-let [fragment-model (get *all-fragments* frag-name)]
(let [;; merge style definitions from fragment
style-ids-rename (-> fragment-model :main :style :parsed (doto assert) (style/insert-styles!))

relation-ids-rename (relations/ids-rename fragment-model frag-name)
relation-rename-map (into {} (map (juxt :old-id :new-id)) relation-ids-rename)
relation-ids-rename (relations/ids-rename fragment-model frag-name)
relation-rename-map (into {} (map (juxt :old-id :new-id)) relation-ids-rename)

;; evaluate
evaled (eval-template-model fragment-model local-data-map function {})
evaled (eval-template-model fragment-model local-data-map function {})

;; write back
get-xml (fn [x] (or (:xml x) @(:xml-delay x)))
evaled-parts (->> evaled :main :result
(get-xml)
(extract-body-parts)
(map (partial relations/xml-rename-relation-ids relation-rename-map))
(map (partial xml-map-attrs
{ooxml/attr-numId
(partial numbering/copy-numbering fragment-model (atom {}))}))
(map (partial style/xml-rename-style-ids style-ids-rename))
(doall))]
(swap! *inserted-fragments* conj frag-name)
(run! relations/add-extra-file! relation-ids-rename)
[{:text (->FragmentInvoke {:frag-evaled-parts evaled-parts})}])
(throw (eval-exception (str "No fragment for name: " frag-name) nil)))))
get-xml (fn [x] (or (:xml x) @(:xml-delay x)))
evaled-parts (->> evaled :main :result
(get-xml)
(extract-body-parts)
(map (partial relations/xml-rename-relation-ids relation-rename-map))
(map (partial xml-map-attrs
{ooxml/attr-numId
(partial numbering/copy-numbering fragment-model (atom {}))}))
(map (partial style/xml-rename-style-ids style-ids-rename))
(doall))]
(swap! *inserted-fragments* conj frag-name)
(run! relations/add-extra-file! relation-ids-rename)
[{:text (->FragmentInvoke {:frag-evaled-parts evaled-parts})}])
(throw (eval-exception (str "No fragment for name: " frag-name) nil)))))
6 changes: 0 additions & 6 deletions test/stencil/errors.clj
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@
(throw-ex-parsing? "<a>{%for x in xs%}a</a>")))


(deftest test-wrong-include
(testing "Unexpected value in inlude tag"
(throw-ex-parsing? "<a>{% include header %}</a>")
(throw-ex-parsing? "<a>{% include a+1 %}</a>")))


(deftest test-not-closed
(testing "Expressions are not closed properly"
(throw-ex-parsing? "<a>{%=</a>")
Expand Down
10 changes: 9 additions & 1 deletion test/stencil/process_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@
{:close :a}],
:fragments #{"elefant"}
:variables ()}
(test-prepare "<a><b>{%include \"elefant\"%}</b></a>")))))
(test-prepare "<a><b>{%include \"elefant\"%}</b></a>"))))
(testing "Fragment invocation is a dynamic expression"
(is (= {:dynamic? true,
:executable [{:open :a}
{:stencil.cleanup/blocks [], :cmd :cmd/include, :name 'valtozo :raw "{%include valtozo%}"}
{:close :a}],
:fragments #{}
:variables '("valtozo")}
(test-prepare "<a>{%include valtozo%}</a>")))))
Loading