Skip to content

Commit

Permalink
feat: move FragmentInvoke type out of types.clj
Browse files Browse the repository at this point in the history
  • Loading branch information
erdos committed Jul 25, 2024
1 parent ed46cf8 commit b80b388
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 27 deletions.
7 changes: 0 additions & 7 deletions src/stencil/functions.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
(ns stencil.functions
"Function definitions"
(:require [clojure.string]
[stencil.ooxml :as ooxml]
[stencil.types :refer [->FragmentInvoke]]
[stencil.util :refer [fail find-first]]))

(set! *warn-on-reflection* true)
Expand Down Expand Up @@ -102,8 +100,3 @@

(defmethod call-fn "replace" [_ text pattern replacement]
(clojure.string/replace (str text) (str pattern) (str replacement)))

;; inserts a page break at the current run.
(let [br {:tag ooxml/br :attrs {ooxml/type "page"}}
page-break (->FragmentInvoke {:frag-evaled-parts [br]})]
(defmethod call-fn "pageBreak" [_] page-break))
2 changes: 1 addition & 1 deletion src/stencil/grammar.clj
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
dotted (mapping (all (guarded #{:dot}) iden) (comp name second))
bracketed (mapping (all (guarded #{:open-bracket}) expression (guarded #{:close-bracket})) second)
args (mapping (optional (chained (all expression) (all (guarded #{:comma}) expression) into))
(fn [x] (take-nth 2 x)))
(partial take-nth 2))
args-suffix (parenthesed args)
iden-or-fncall (mapping (all iden (optional args-suffix))
(fn [[id xs]] (if xs (list* :fncall id xs) id)))
Expand Down
4 changes: 2 additions & 2 deletions src/stencil/log.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
(list 'to-array)
(list '. (list 'stencil.log/get-logger (str *ns*)) '~level msg#)))))

(declare trace debug info warn error)

(def-log-level trace)
(def-log-level debug)
(def-log-level info)
(def-log-level warn)
(def-log-level error)

(ns-unmap *ns* 'def-log-level)
3 changes: 1 addition & 2 deletions src/stencil/model.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
[stencil.eval :as eval]
[stencil.infix :refer [eval-rpn]]
[stencil.merger :as merger]
[stencil.types :refer [->FragmentInvoke]]
[stencil.util :refer [unlazy-tree]]
[stencil.model.common :refer [->xml-writer resource-copier]]
[stencil.ooxml :as ooxml]
Expand Down Expand Up @@ -206,4 +205,4 @@
(map (partial style/xml-rename-style-ids style-ids-rename))
(doall))]
(run! relations/add-extra-file! relation-ids-rename)
[{:text (->FragmentInvoke {:frag-evaled-parts evaled-parts})}]))
[{:text (fragments/->FragmentInvoke {:frag-evaled-parts evaled-parts})}]))
20 changes: 19 additions & 1 deletion src/stencil/model/fragments.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
(ns stencil.model.fragments
(:require [stencil.util :refer [eval-exception]]))
(:require [stencil.util :refer [eval-exception]]
[stencil.functions :refer [call-fn]]
[stencil.types :refer [ControlMarker]]
[stencil.ooxml :as ooxml]
[clojure.data.xml :as xml]))

;; all insertable fragments. map of id to frag def.
(def ^:dynamic *all-fragments* nil)
Expand All @@ -24,3 +28,17 @@
[~body @*inserted-fragments*])]
(swap! *inserted-fragments* into fragments#)
[result# fragments#]))

;; Invocation of a fragment by name
(defrecord FragmentInvoke [result] ControlMarker)

;; custom XML content
(defmethod call-fn "xml" [_ content]
(assert (string? content))
(let [content (:content (xml/parse-str (str "<a>" content "</a>")))]
(->FragmentInvoke {:frag-evaled-parts content})))

;; inserts a page break at the current run.
(let [br {:tag ooxml/br :attrs {ooxml/type "page"}}
page-break (->FragmentInvoke {:frag-evaled-parts [br]})]
(defmethod call-fn "pageBreak" [_] page-break))
14 changes: 3 additions & 11 deletions src/stencil/postprocess/fragments.clj
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
(ns stencil.postprocess.fragments
"Inserts contents of fragments."
(:import [stencil.types FragmentInvoke])
(:require [stencil.model.fragments])
(:import [stencil.model.fragments FragmentInvoke])
(:require [clojure.zip :as zip]
[clojure.data.xml :as xml]
[stencil.types :refer [->FragmentInvoke control?]]
[stencil.types :refer [control?]]
[stencil.ooxml :as ooxml]
[stencil.functions :refer [call-fn]]
[stencil.util :refer :all]))


(defn- remove+up
"Removes current node and moves pointer to parent node."
[loc]
Expand Down Expand Up @@ -167,9 +165,3 @@
"Walks the tree (Depth First) and evaluates FragmentInvoke objects."
[xml-tree]
(dfs-walk-xml-node xml-tree (partial instance? FragmentInvoke) unpack-fragment))

;; custom XML content
(defmethod call-fn "xml" [_ content]
(assert (string? content))
(let [content (:content (xml/parse-str (str "<a>" content "</a>")))]
(->FragmentInvoke {:frag-evaled-parts content})))
3 changes: 0 additions & 3 deletions src/stencil/types.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,4 @@

(defprotocol ControlMarker)

;; Invocation of a fragment by name
(defrecord FragmentInvoke [result] ControlMarker)

(defn control? [x] (satisfies? ControlMarker x))

0 comments on commit b80b388

Please sign in to comment.