Skip to content
Draft
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
19 changes: 10 additions & 9 deletions src/stencil/model/content_types.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]
Expand Down
20 changes: 10 additions & 10 deletions src/stencil/model/numbering.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
7 changes: 7 additions & 0 deletions src/stencil/model/relations.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))
14 changes: 7 additions & 7 deletions src/stencil/model/style.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -100,17 +101,16 @@
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
:parsed (parse main-style-abs)})))


(defn assoc-style [model dir]
(->> (main-style-item dir (:stencil.model/path model) (:relations model))
(->> (main-style-item model dir)
(assoc-some model :style)))
6 changes: 3 additions & 3 deletions src/stencil/postprocess/list_ref.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down