2013-10-29 23 views
0

我剛剛開始編程最近,我有這個問題,所以我有這個html代碼片段。我想分析img的src屬性並用urly路徑標準化對其進行標準化,並向src添加一些新路徑。更新和規範化attr src in enlive

<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"))) 
) 
+0

參見:https://groups.google.com/forum/#!topic/enlive-clj/Bz6TTu02Qv8。我相信它解決了你遇到的確切問題。 – dsm

回答

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)