blahblah.clj
· 966 B · Clojure
Raw
(defn ^String modify-link
"Takes a DOM tree `document` and a Ring request map `request` as arguments.
This function will modify the links in <link> tags of <item> elements to point
to our download proxy. Returns the modified document as a String."
[document request]
(let [dom (parse-xml document)
tfn (doto (make-transformer)
(.setOutputProperty OutputKeys/INDENT "yes")
(.setOutputProperty "{http://xml.apache.org/xslt}indent-amount" "2"))
src (DOMSource. dom)
dst (StreamResult. (StringWriter.))]
;; Modify all <link> tag whose child is <item>
(->> dom
(get-tags "link")
(filter in-<item>?)
(run! (fn [tag]
(let [id (get-id (tag-text tag))
resource (absolute-url request id)]
(set-tag-text! tag resource)))))
;; Return the transformed feed
(.transform tfn src dst)
(.toString (.getWriter dst))))
1 | (defn ^String modify-link |
2 | "Takes a DOM tree `document` and a Ring request map `request` as arguments. |
3 | This function will modify the links in <link> tags of <item> elements to point |
4 | to our download proxy. Returns the modified document as a String." |
5 | [document request] |
6 | (let [dom (parse-xml document) |
7 | tfn (doto (make-transformer) |
8 | (.setOutputProperty OutputKeys/INDENT "yes") |
9 | (.setOutputProperty "{http://xml.apache.org/xslt}indent-amount" "2")) |
10 | src (DOMSource. dom) |
11 | dst (StreamResult. (StringWriter.))] |
12 | ;; Modify all <link> tag whose child is <item> |
13 | (->> dom |
14 | (get-tags "link") |
15 | (filter in-<item>?) |
16 | (run! (fn [tag] |
17 | (let [id (get-id (tag-text tag)) |
18 | resource (absolute-url request id)] |
19 | (set-tag-text! tag resource))))) |
20 | ;; Return the transformed feed |
21 | (.transform tfn src dst) |
22 | (.toString (.getWriter dst)))) |