From 257710fd3ed5dd1f37211ceca7737b21e548bb8b Mon Sep 17 00:00:00 2001 From: janos erdos Date: Sat, 19 Apr 2025 22:26:26 +0200 Subject: [PATCH 1/2] chore: simplify --- src/stencil/model/numbering.clj | 20 ++++++++++---------- src/stencil/model/relations.clj | 7 +++++++ src/stencil/model/style.clj | 14 +++++++------- src/stencil/postprocess/list_ref.clj | 6 +++--- 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/stencil/model/numbering.clj b/src/stencil/model/numbering.clj index 8d0dc193..facaf8c7 100644 --- a/src/stencil/model/numbering.clj +++ b/src/stencil/model/numbering.clj @@ -152,20 +152,20 @@ (prepare-numbering-xml tree)))) -(defn- main-numbering [dir main-document main-document-rels] - (when-let [main-numbering-path - (some #(when (= rel-type-numbering (:stencil.model/type %)) - (unix-path (io/file (fs/parent-file (io/file main-document)) - (:stencil.model/target %)))) - (vals (:parsed main-document-rels)))] - {:stencil.model/path main-numbering-path - :source-file (io/file dir main-numbering-path) - :parsed (parse (io/file dir main-numbering-path))})) +(defn- main-numbering [model dir] + (when-let [path (relations/path-by-type model rel-type-numbering)] + (let [main-document (:stencil.model/path model) + main-numbering-path (unix-path (io/file (fs/parent-file (io/file main-document)) path))] + {:stencil.model/path main-numbering-path + :source-file (io/file dir main-numbering-path) + :parsed (parse (io/file dir main-numbering-path))}))) + (defn assoc-numbering [model dir] - (->> (main-numbering dir (:stencil.model/path model) (:relations model)) + (->> (main-numbering model dir) (assoc-some model :stencil.model/numbering))) + (defn style-def-for [id lvl] (assert (string? id)) (assert (integer? lvl)) diff --git a/src/stencil/model/relations.clj b/src/stencil/model/relations.clj index b7a62b69..d45a31c4 100644 --- a/src/stencil/model/relations.clj +++ b/src/stencil/model/relations.clj @@ -176,3 +176,10 @@ {:stencil.model/type type :stencil.model/target target}) (update-in [:main :relations] dissoc :source-file))) + +(defn path-by-type [model rel-type] + (assert (:relations model)) + (assert (string? rel-type)) + (some->> model :relations :parsed vals + (find-first #(= rel-type (:stencil.model/type %))) + :stencil.model/target)) diff --git a/src/stencil/model/style.clj b/src/stencil/model/style.clj index dc5111d6..12f87a4f 100644 --- a/src/stencil/model/style.clj +++ b/src/stencil/model/style.clj @@ -4,7 +4,8 @@ [stencil.fs :as fs] [stencil.ooxml :as ooxml] [stencil.model.common :refer [->xml-writer]] - [stencil.util :refer [assoc-some find-first update-some]])) + [stencil.model.relations :as relations] + [stencil.util :refer [assoc-some update-some]])) (set! *warn-on-reflection* true) @@ -100,11 +101,10 @@ xml-tree)) -(defn- main-style-item [dir main-document main-document-rels] - (when-let [main-style (find-first #(= rel-type (:stencil.model/type %)) - (vals (:parsed main-document-rels)))] - (let [main-style-file (io/file (fs/parent-file (io/file main-document)) - (:stencil.model/target main-style)) +(defn- main-style-item [model dir] + (when-let [style-path (relations/path-by-type model rel-type)] + (let [main-document (:stencil.model/path model) + main-style-file (io/file (fs/parent-file (io/file main-document)) style-path) main-style-abs (io/file dir main-style-file)] {:stencil.model/path (fs/unix-path main-style-file) :source-file main-style-abs @@ -112,5 +112,5 @@ (defn assoc-style [model dir] - (->> (main-style-item dir (:stencil.model/path model) (:relations model)) + (->> (main-style-item model dir) (assoc-some model :style))) diff --git a/src/stencil/postprocess/list_ref.clj b/src/stencil/postprocess/list_ref.clj index cc2ff9b3..ed512100 100644 --- a/src/stencil/postprocess/list_ref.clj +++ b/src/stencil/postprocess/list_ref.clj @@ -12,7 +12,7 @@ ;; http://officeopenxml.com/WPnumbering-numFmt.php ;; http://www.datypic.com/sc/ooxml/t-w_ST_NumberFormat.html ;; TODO: cardinalText, ordinal, ordinalText, ... -(defmulti render-number (fn [style number] style)) +(defmulti render-number (fn [style _number] style)) (defmethod render-number :default [_ nr] (str nr)) (def ^:private roman-digits @@ -65,8 +65,8 @@ (clojure.string/join (repeat i c))) (dec number))) -(defmethod render-number "none" [_ number] "") -(defmethod render-number "bullet" [_ number] "") +(defmethod render-number "none" [_ _number] "") +(defmethod render-number "bullet" [_ _number] "") ;; reference: https://c-rex.net/projects/samples/ooxml/e1/Part4/OOXML_P4_DOCX_REFREF_topic_ID0ESRL1.html#topic_ID0ESRL1 From 3119a5b5a8c4d66eaf54e064c22d3bb00d06303c Mon Sep 17 00:00:00 2001 From: janos erdos Date: Sat, 19 Apr 2025 22:38:49 +0200 Subject: [PATCH 2/2] simplifications in content types handling --- src/stencil/model/content_types.clj | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/stencil/model/content_types.clj b/src/stencil/model/content_types.clj index a1afbcd1..df569e5e 100644 --- a/src/stencil/model/content_types.clj +++ b/src/stencil/model/content_types.clj @@ -8,6 +8,8 @@ (def xmlns "http://schemas.openxmlformats.org/package/2006/content-types") +(def content-types-file "[Content_Types].xml") + (def tag-types :xmlns.http%3A%2F%2Fschemas.openxmlformats.org%2Fpackage%2F2006%2Fcontent-types/Types) (def tag-override :xmlns.http%3A%2F%2Fschemas.openxmlformats.org%2Fpackage%2F2006%2Fcontent-types/Override) (def tag-default :xmlns.http%3A%2F%2Fschemas.openxmlformats.org%2Fpackage%2F2006%2Fcontent-types/Default) @@ -17,24 +19,23 @@ (def attr-content-type :ContentType) -(defn- parse-ct-file [content-types-file] - (with-open [reader (input-stream (file content-types-file))] +(defn- parse-ct-file [ct-file] + (with-open [reader (input-stream ct-file)] (let [parsed (xml/parse reader)] - (assert (= "Types" (name (:tag parsed)))) + (assert (= tag-types (:tag parsed))) (reduce (fn [m elem] - (case (name (:tag elem)) - "Default" (assoc-in m [::default (attr-extension (:attrs elem))] (attr-content-type (:attrs elem))) - "Override" (assoc-in m [::override (attr-part-name (:attrs elem))] (attr-content-type (:attrs elem))))) + (condp = (:tag elem) + tag-default (assoc-in m [::default (attr-extension (:attrs elem))] (attr-content-type (:attrs elem))) + tag-override (assoc-in m [::override (attr-part-name (:attrs elem))] (attr-content-type (:attrs elem))))) {} (remove string? (:content parsed)))))) ;; rm empty strings (defn parse-content-types [dir] (assert (fs/directory? dir)) - (let [cts (file dir "[Content_Types].xml")] + (let [cts (file dir content-types-file)] (assert (fs/exists? cts)) - (assert (.isFile cts)) {:parsed (parse-ct-file cts) - :stencil.model/path (.getName cts)})) + :stencil.model/path content-types-file})) (defn with-content-types [model]