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: hyperlinks #30

Merged
merged 5 commits into from
Sep 13, 2019
Merged
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
Binary file modified examples/Multipart Template/footer.docx
Binary file not shown.
Binary file modified examples/Multipart Template/template.docx
Binary file not shown.
22 changes: 16 additions & 6 deletions src/stencil/model.clj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
"Relationship type of image files in .rels files."
"http:https://schemas.openxmlformats.org/officeDocument/2006/relationships/image")

(def rel-type-hyperlink
"Relationship type of hyperlinks in .rels files."
"http:https://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink")

(def tag-relationships
:xmlns.http%3A%2F%2Fschemas.openxmlformats.org%2Fpackage%2F2006%2Frelationships/Relationships)

Expand Down Expand Up @@ -73,7 +77,7 @@
:when (= tag-relationship (:tag d))]
[(:Id (:attrs d)) {::type (doto (:Type (:attrs d)) assert)
::target (doto (:Target (:attrs d)) assert)
::mode (:TargetMode :attrs)}]))))
::mode (:TargetMode (:attrs d))}]))))


(defn- parse-style
Expand Down Expand Up @@ -273,6 +277,7 @@
(into result
(for [m (tree-seq coll? seq evaled-template-model)
:when (and (map? m) (not (sorted? m)) (:path m))
:when (not= "External" (::mode m))
:when (not (contains? result (:path m)))]
[(:path m) (or (:writer (:result m)) (resource-copier m))]))

Expand All @@ -284,6 +289,7 @@
(.getParentFile (file (:source-file m))))))
path-parent (some-> m :path file .getParentFile)]
relation (vals (:parsed (:relations m)))
:when (not= "External" (::mode relation))
:let [path (str (.normalize (.toPath (file path-parent (::target relation)))))]
:when (or (:writer relation) (not (contains? result path)))
:let [src (or (:source-file relation) (file @src-parent (::target relation)))]]
Expand Down Expand Up @@ -363,8 +369,11 @@
(assert (every? string? (keys id-rename)))
(assert (every? string? (vals id-rename)))
(doall (for [item executable]
;; Image relation ids are being renamed here.
(update-some item [:attrs ooxml/embed] id-rename))))
(-> item
;; Image relation ids are being renamed here.
(update-some [:attrs ooxml/r-embed] id-rename)
;; Hyperlink relation ids are being renamed here
(update-some [:attrs ooxml/r-id] id-rename)))))


;; generates a random relation id
Expand All @@ -374,10 +383,11 @@
(defn- relation-ids-rename [model fragment-name]
(doall
(for [[old-rel-id m] (-> model :main :relations :parsed (doto assert))
:when (= rel-type-image (::type m))
:when (#{rel-type-image rel-type-hyperlink} (::type m))
:let [new-id (->relation-id)
extension (last (.split (str (::target m)) "\\."))
new-path (str new-id "." extension)]]
new-path (if (= "External" (::mode m))
(::target m)
(str new-id "." (last (.split (str (::target m)) "\\."))))]]
{::type (::type m)
::mode (::mode m)
::target new-path
Expand Down
3 changes: 2 additions & 1 deletion src/stencil/ooxml.clj
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@
(def style-id :xmlns.http%3A%2F%2Fschemas.openxmlformats.org%2Fwordprocessingml%2F2006%2Fmain/styleId)

;; relation id used with images
(def embed :xmlns.http%3A%2F%2Fschemas.openxmlformats.org%2FofficeDocument%2F2006%2Frelationships/embed)
(def r-embed :xmlns.http%3A%2F%2Fschemas.openxmlformats.org%2FofficeDocument%2F2006%2Frelationships/embed)
(def r-id :xmlns.http%3A%2F%2Fschemas.openxmlformats.org%2FofficeDocument%2F2006%2Frelationships/id)