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: numbering from fragments #148

Merged
merged 38 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
3382ca5
fix: keep style of numberings in fragments
erdos Apr 23, 2023
4a6812c
styling
erdos Apr 23, 2023
c20f430
wip
erdos Apr 23, 2023
1f03340
wip
erdos Apr 23, 2023
3677cf8
wip
erdos Apr 23, 2023
e60004d
wip
erdos Apr 23, 2023
afbf9c4
wip
erdos Apr 23, 2023
00c2810
wip
erdos Apr 23, 2023
151f971
wip
erdos Apr 23, 2023
d969fd0
fixes
erdos Apr 23, 2023
f70b504
Merge remote-tracking branch 'origin/master' into fix-numbering-fragm…
erdos Apr 24, 2023
9670705
wip
erdos Apr 24, 2023
b0730b8
wip
erdos Apr 24, 2023
f5084b4
wip
erdos Apr 24, 2023
730d2a5
wip
erdos Apr 25, 2023
a5be2d3
wip
erdos Apr 25, 2023
ebcf428
wip
erdos Apr 25, 2023
1407f3a
Merge remote-tracking branch 'origin/master' into fix-numbering-fragm…
erdos Apr 25, 2023
a4c4870
typos
erdos Apr 25, 2023
52ba186
fix: expectations
erdos Apr 25, 2023
f3aa038
cleanup
erdos Apr 25, 2023
d15bb34
Merge remote-tracking branch 'origin/master' into fix-numbering-fragm…
erdos Apr 25, 2023
0db3fb7
tests
erdos Apr 26, 2023
5f94214
wip
erdos Apr 26, 2023
3fb7343
wip
erdos Apr 26, 2023
a1d3be8
test: rm blur
erdos Apr 27, 2023
72f8108
reset font
erdos Apr 27, 2023
1ba76db
fixes
erdos Apr 27, 2023
05a0837
:wq
erdos Jul 12, 2023
fcc2188
fixes
erdos Jul 12, 2023
1ce08c9
Merge remote-tracking branch 'origin/master' into fix-numbering-fragm…
erdos Jul 12, 2023
6969c25
wip: visual flow
erdos Jul 12, 2023
d7051a0
Merge remote-tracking branch 'origin/master' into fix-numbering-fragm…
erdos Jul 12, 2023
2964510
wip
erdos Jul 12, 2023
62c3fc2
Merge remote-tracking branch 'origin/master' into fix-numbering-fragm…
erdos Jul 12, 2023
950ea60
Merge remote-tracking branch 'origin/master' into fix-numbering-fragm…
erdos Jul 12, 2023
c0ea535
wip: script
erdos Jul 13, 2023
7b0b23e
small comment
erdos Jul 13, 2023
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
Prev Previous commit
Next Next commit
wip
  • Loading branch information
erdos committed Apr 23, 2023
commit e60004d437b8afe3bae9c3935c788d66884709df
6 changes: 3 additions & 3 deletions src/stencil/model.clj
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,9 @@
(get-xml)
(extract-body-parts)
(map (partial relations/xml-rename-relation-ids relation-rename-map))
;; TODO: also rename numbering ids.
(map (partial xml-map-attrs {ooxml/attr-numId
(partial numbering/copy-numbering fragment-model (atom {}))}))
(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)
Expand Down
53 changes: 34 additions & 19 deletions src/stencil/model/numbering.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,62 @@
(def ^:private rel-type-numbering
"http:https://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering")


;; children of numbering xml definition as a vector in an atom.
(def ^:dynamic *numbering* nil)

(defn -initial-numbering-context [template-model]
(let [xml-tree (-> template-model :main :stencil.model/numbering :parsed)]
{:target (atom []) ;; extra elems to be added when exiting context
:id->numbering (into {} (for [e (:content xml-tree)
:when (= ooxml/tag-num (:tag e))]
[(get-in e [:attrs ooxml/attr-numId]) e]))
:id->abstract-numbering (into {} (for [e (:content xml-tree)
:when (= ooxml/tag-abstract-num (:tag e))]
[(get-in e [:attrs ooxml/xml-abstract-num-id]) e]))}))
{:extra-elems (atom []) ;; elems added during evaluation in context
:parsed xml-tree}))

;; defines target context. changes to numberngs will be moved here.
(defmacro with-numbering-context [template-model body]
`(binding [*numbering* (-initial-numbering-context ~template-model)]
;; TODO: update body by writing new entries from current numbering context
(-> ~body
(assoc-in [:main :stencil.model/numbering :parsed]
updated-numbering ;; TODO
))))
(let [body# ~body]
(if-let [extra-elems# (:extra-elems (not-empty @*numbering*))]
(-> body#
(update-in [:main :stencil.model/numbering] dissoc :source-file)
(update-in [:main :stencil.model/numbering :parsed :content] conj extra-elems#))
body#))))

(defn- add-numbering-entry! [xml-element]
(swap! (:target *numbering*) conj xml-element) nil)
(swap! (:extra-elems *numbering*) conj xml-element) nil)

;; cache atom is a map with {:num-id-rename {} :abstract-num-id-rename {}}
(defn copy-numbering [source-model cache-atom numbering-id]
(let [numbering-root (-> source-model :main :stencil.model/numbering :parsed)
(let [numbering-root (-> source-model :main :stencil.model/numbering :parsed)
id->numbering (into {} (for [e (:content numbering-root)
:when (= ooxml/tag-num (:tag e))]
[(get-in e [:attrs ooxml/attr-numId]) e]))
id->abstract-numbering (into {} (for [e (:content numbering-root)
:when (= ooxml/tag-abstract-num (:tag e))]
[(get-in e [:attrs ooxml/xml-abstract-num-id]) e]))
copy-abstract-nring (fn [cache abstract-nr-id]
(if (contains? (:abstract-num-id-rename cache) abstract-nr-id)
cache
(let [elem (get-in *numbering* [:id->abstract-numbering abstract-nr-id])
(let [elem (id->abstract-numbering abstract-nr-id)
new-id (gensym "abstract-numbering")]
(add-numbering-entry! (assoc-in elem [:attrs ooxml/xml-abstract-num-id] new-id))
(assoc-in cache [:abstract-num-id-rename abstract-nr-id] new-id))))
copy-nring (fn [cache numbering-id]
(if (contains? (:num-id-rename cache) numbering-id)
cache
(let [elem (get-in *numbering* [:id->numbering numbering-id])
new-id (gensym "numbering")]
(add-numbering-entry! (assoc-in elem [:attrs ooxml/attr-numId] new-id))
;; TODO: also update abstracts when necessary!!!!
(let [elem (id->numbering numbering-id)
new-id (gensym "numbering")

;; if numbering definition has an abstractNumId child then we need to also map that
abstract-id? (-> elem :content
(find-first (fn [e] (= ooxml/xml-abstract-num-id (:tag e))))
:attrs ooxml/val)
cache (if abstract-id? (copy-abstract-nring cache abstract-id?) cache)
abstract-rename? (get-in cache [:abstract-num-id-rename abstract-id?])]
(-> elem
(assoc-in [:attrs ooxml/attr-numId] new-id)
(update :content (partial mapv (fn [c] (if (= ooxml/xml-abstract-num-id (:tag c))
(assoc-in c [:attrs ooxml/val] abstract-rename?)
c))))
(add-numbering-entry!))
(assoc-in cache [:num-id-rename numbering-id] new-id))))]
(assert numbering-root)
(-> cache-atom
Expand Down
4 changes: 3 additions & 1 deletion test/stencil/model/numbering_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
(if (map? (second body)) body (list* (first body) {} (next body)))]
{:tag tag, :attrs attrs, :content (mapv hiccup children)}))

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

#_
(deftest test-style-for-def-with-abstract
(binding [*numbering*
{:parsed
Expand All @@ -31,7 +33,7 @@
(is (= {:lvl-text "", :num-fmt "none", :start 1}
(style-def-for "id-1" 2)))))


#_
(deftest test-style-for-def
(binding [*numbering*
{:parsed
Expand Down