Last active 1689843909

blahblah.clj Raw
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))))