Skip to content

Commit 510635e

Browse files
committed
fix: replaceImage works with imagedata tags
1 parent 8306f4f commit 510635e

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/stencil/ooxml.clj

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373

7474
(def attr-ilvl :xmlns.http%3A%2F%2Fschemas.openxmlformats.org%2Fwordprocessingml%2F2006%2Fmain/ilvl)
7575

76+
(def tag-imagedata :xmlns.urn%3Aschemas-microsoft-com%3Avml/imagedata)
77+
7678
(def default-aliases
7779
{;default namespace aliases from a LibreOffice 6.4 OOXML Text document
7880
"http://schemas.openxmlformats.org/markup-compatibility/2006" "mc"

src/stencil/postprocess/images.clj

+11-4
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,26 @@
3535
{:mime-type (.toLowerCase (.substring data-uri-str 5 end-of-mimetype))
3636
:bytes (.decode (java.util.Base64/getDecoder) (.getBytes (.substring data-uri-str start-of-data)))}))
3737

38+
39+
;; mapping of tag name to attribute for nodes that represent an image and have an attribute for relation id.
40+
(def ^:private img-tag-attr
41+
{ooxml/blip ooxml/r-embed
42+
ooxml/tag-imagedata ooxml/r-id})
43+
3844
(defn- update-image [img-node, ^ReplaceImage data]
39-
(assert (= ooxml/blip (:tag img-node)))
45+
(assert (img-tag-attr (:tag img-node)))
4046
(assert (instance? ReplaceImage data))
41-
(let [current-rel (-> img-node :attrs ooxml/r-embed)
47+
(let [attr-key (img-tag-attr (:tag img-node))
48+
current-rel (-> img-node :attrs attr-key)
4249
new-val (-> data .relation)]
4350
(assert new-val)
4451
(log/debug "Replacing image relation {} by {}" current-rel new-val)
45-
(assoc-in img-node [:attrs ooxml/r-embed] new-val)))
52+
(assoc-in img-node [:attrs attr-key] new-val)))
4653

4754
(defn- replace-image [marker-loc]
4855
(if-let [img-loc (->> (zip/remove marker-loc)
4956
(iterations zip/prev)
50-
(find-first (comp #{ooxml/blip} :tag zip/node)))]
57+
(find-first (comp img-tag-attr :tag zip/node)))]
5158
(zip/edit img-loc update-image (zip/node marker-loc))
5259
(fail "Did not find image to replace. The location of target image must precede the replaceImage() function call location." {})))
5360

0 commit comments

Comments
 (0)