在enlive中更新并规范化attr src

时间:2013-10-29 12:00:54

标签: clojure enlive

我刚刚开始编程,我遇到了这个问题,所以我有这个html片段。我想解析img的src属性并使用urly path normalization对其进行规范化,并为src添加一些新路径。

<html>
<body>
    <div class="content">lorem ipsum
        <img style="margin-top: -5px;" src="/img/car.png" />
    </div>
    <img style="margin-top: -5px;" src="/img/chair.png" />
</body>
</html>

成为这个

<html>
<body>
    <div class="content">lorem ipsum
        <img style="margin-top: -5px;" src="/path1/img/car.png" />
    </div>
    <img style="margin-top: -5px;" src="/path1/img/chair.png" />
</body>
</html>

我想到了这个方法,但我找不到获取src值的方法

(html/deftemplate template-about "../resources/public/build/about/index.html"
[]
[:img] (html/set-attr :src (str "path1" (urly/path-of ("the src value")))
)

1 个答案:

答案 0 :(得分:1)

您正在寻找update-attr函数,discussed before

如:

(html/deftemplate template-about "../resources/public/build/about/index.html"
[]
[:img] (fn [node]
          (let [href (-> node :attrs :href)]
             (assoc-in node [:attrs :href] (urly/path-of href))))

或采用通用路径

(defn update-attr [attr f & args]
    (fn [node]
      (apply update-in node [:attrs attr] f args))))

然后

(update-attr :href urly/path-of)