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

Fix loop numbering #145

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Next Next commit
wip
  • Loading branch information
erdos committed Dec 25, 2022
commit 2fc2473a262a16cd03f2ccb67c57f5df444382c6
2 changes: 1 addition & 1 deletion src/stencil/cleanup.clj
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@
identity identity
(fn [e]
(if (= ooxml/attr-numId (:open+close e))
(assoc e ::depth (numid->depth (-> e :attrs ooxml/val)))
(assoc-in e [:attrs ::depth] (numid->depth (-> e :attrs ooxml/val)))
e)))
ast)))

Expand Down
3 changes: 1 addition & 2 deletions src/stencil/eval.clj
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@
(:branch/body-run-none item))))

(defmethod eval-step ooxml/attr-numId [_ _ trace item]
(println :!!! item)
[item])
[(assoc-in item [:attrs ::trace] trace)])

(defn eval-executable [part data functions]
(->> (:executable part)
Expand Down
5 changes: 3 additions & 2 deletions src/stencil/model.clj
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
(assert (:main template-model) "Should be a result of load-template-model call!")
(assert (some? fragments))
(binding [*current-styles* (atom (:parsed (:style (:main template-model))))
numbering/*numbering* (::numbering (:main template-model))
numbering/*numbering* (atom (::numbering (:main template-model)))
*inserted-fragments* (atom #{})
*extra-files* (atom #{})
*all-fragments* (into {} fragments)]
Expand Down Expand Up @@ -175,6 +175,7 @@
:finally (assoc :result result))))]
(-> template-model
(update :main evaluate)
(assoc-in [:main ::numbering] @numbering/*numbering*)
(update-in [:main :headers+footers] (partial mapv evaluate))

(cond-> (-> template-model :main :style)
Expand Down Expand Up @@ -240,7 +241,7 @@
elem)))


(defmethod eval/eval-step :cmd/include [function local-data-map {frag-name :name}]
(defmethod eval/eval-step :cmd/include [function local-data-map _ {frag-name :name}]
(assert (map? local-data-map))
(assert (string? frag-name))
(expect-fragment-context!
Expand Down
20 changes: 16 additions & 4 deletions src/stencil/model/numbering.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
(:require [clojure.data.xml :as xml]
[clojure.java.io :as io]
[stencil.ooxml :as ooxml]
[stencil.util :refer [unlazy-tree ->int]]
[stencil.util :refer [unlazy-tree ->int find-first]]
[stencil.model.common :refer [unix-path]]))


(def ^:private rel-type-numbering
"http:https://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering")


;; swap an atom here!
(def ^:dynamic *numbering* nil)


Expand Down Expand Up @@ -66,7 +66,7 @@
(let [tree (xml/parse r)]
(prepare-numbering-xml tree))))


;; finds the
(defn main-numbering [dir main-document main-document-rels]
(when-let [main-numbering-path
(some #(when (= rel-type-numbering (:stencil.model/type %))
Expand All @@ -81,6 +81,18 @@
(defn style-def-for [id lvl]
(assert (string? id))
(assert (integer? lvl))
(some-> (:parsed *numbering*)
(some-> (:parsed @*numbering*)
(get-id-style-xml id lvl)
(xml-lvl-parse)))

;; returns new id for the numbering copied from old-id
(defn copy-numbering! [old-id]
(let [old-elem (find-first (fn [e] (-> e :attrs ooxml/attr-numId (= old-id))) (:content (:parsed @*numbering*)))
new-id #_(name (gensym "xx")) (str (int (* 1000 (Math/random))))
new-elem (assoc-in old-elem [:attrs ooxml/attr-numId] new-id)]
(assert old-elem)
(swap! *numbering* update :parsed update :content concat [new-elem])
(swap! *numbering* dissoc :source-file)
(swap! *numbering* (fn [numbering]
(assoc numbering :result {:writer (stencil.model.common/->xml-writer (:parsed numbering))})))
new-id))
18 changes: 0 additions & 18 deletions src/stencil/postprocess/list_ref.clj
Original file line number Diff line number Diff line change
Expand Up @@ -307,21 +307,3 @@
(let [xml-tree (enrich-dirty-refs-meta xml-tree)
bookmark-meta (get-bookmark-meta xml-tree)]
(rerender-refs xml-tree bookmark-meta)))

;; finds nodes with unique id and if these are not unique anymore, replace the numbering id with a duplicate numbering definition
(defn fix-numbering-reset [xml-tree]
(let [found (atom #{})
last-val (atom nil)
edit-attrs (fn [attrs]
(let [old-id (ooxml/val attrs)
unq-id (:stencil.model/unique attrs)
new-id old-id] ;; TODO
;; ha az unique id benne van a found set-ben
;; akkor uj value-t szamolunk es beallitjuk a last-val-t.
;; egyebkent ha a last-val nem ugyanaz mint a mostani, akkor beallitjuk a last val-t (?)
(-> attrs
(dissoc :stencil.model/unique)
(assoc ooxml/val new-id))))]
;; find nodes with unique id in attrs
(dfs-walk-xml-node xml-tree (fn [n] (and (map? n) (:stencil.model/unique (:attrs n))))
(fn [n] (zip/edit n update :attrs edit-attrs)))))
30 changes: 30 additions & 0 deletions src/stencil/postprocess/numberings.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
(ns stencil.postprocess.numberings
(:require [stencil.util :refer :all]
[stencil.ooxml :as ooxml]
[stencil.model.numbering :as numbering]
[stencil.log :as log]
[clojure.zip :as zip]))

(defn- get-new-id [numbering-id trace]
(log/debug "Getting new list for old {} trace {}" numbering-id trace)
(if (empty? trace)
numbering-id
(numbering/copy-numbering! numbering-id)))

(defn- lookup [get-new-id element]
(get-new-id (ooxml/val (:attrs element))
(take-last (:stencil.cleanup/depth (:attrs element))
(:stencil.eval/trace (:attrs element)))))

(defn- fix-one [numbering lookup]
(-> numbering
(update :attrs dissoc :stencil.cleanup/depth :stencil.eval/trace)
(update :attrs assoc ooxml/val (lookup numbering))))

(defn fix-numberings [xml-tree]
(let [lookup (partial lookup (memoize get-new-id))]
(dfs-walk-xml-node
xml-tree
(fn [e] (= ooxml/attr-numId (:tag e)))
(fn [e] (zip/edit e fix-one lookup)))))

3 changes: 2 additions & 1 deletion src/stencil/tree_postprocess.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[stencil.postprocess.images :refer :all]
[stencil.postprocess.list-ref :refer :all]
[stencil.postprocess.fragments :refer :all]
[stencil.postprocess.numberings :refer [fix-numberings]]
[stencil.postprocess.html :refer :all]))

;; calls postprocess
Expand All @@ -26,7 +27,7 @@

#'fix-list-dirty-refs

#'fix-numbering-reset
#'fix-numberings

#'replace-images

Expand Down
8 changes: 5 additions & 3 deletions test/stencil/model/numbering_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
{:tag tag, :attrs attrs, :content (mapv hiccup children)}))

(deftest test-style-for-def-empty
(binding [*numbering* {:parsed (prepare-numbering-xml {:tag :numbering :content []})}]
(binding [*numbering* (atom {:parsed (prepare-numbering-xml {:tag :numbering :content []})})]
(is (= nil (style-def-for "id-1" 2)))))

(deftest test-style-for-def-with-abstract
(binding [*numbering*
(atom
{:parsed
(prepare-numbering-xml
(hiccup
Expand All @@ -27,13 +28,14 @@
[:lvlText {ooxml/val ""}]
[:lvlJc {ooxml/val "start"}]]]
[ooxml/tag-num {ooxml/attr-numId "id-1"}
[ooxml/xml-abstract-num-id {ooxml/val "a1"}]]]))}]
[ooxml/xml-abstract-num-id {ooxml/val "a1"}]]]))})]
(is (= {:lvl-text "", :num-fmt "none", :start 1}
(style-def-for "id-1" 2)))))


(deftest test-style-for-def
(binding [*numbering*
(atom
{:parsed
(prepare-numbering-xml
(hiccup
Expand All @@ -44,6 +46,6 @@
[:numFmt {ooxml/val "none"}]
[:suff {ooxml/val "nothing"}]
[:lvlText {ooxml/val ""}]
[:lvlJc {ooxml/val "start"}]]]]))}]
[:lvlJc {ooxml/val "start"}]]]]))})]
(is (= {:lvl-text "", :num-fmt "none", :start 1}
(style-def-for "id-1" 2)))))