使用 enlive 在 html 页面上进行多次转换

Multiple transforms on an html page using enlive

Clojure 和 enlive 很棒。在试图理解 Enlive 的力量时,我试图将两个转换应用到 html 页面。

HTML 页面有 2 个区域 (divs) 我要转换。有问题的第一个 div 被克隆了 ~16 次。有问题的第二个 div 被克隆了 5 次。原始 divs(来自 html 文件)应该被覆盖或根本不出现。

Enlive 有惯用的方法

(apply str (enlive-html/emit* ze-contant-transferm))

这对于一次转换非常有效。

但是,我想对页面应用两个转换,所以我尝试了类似的方法:

(str
  (apply str (enlive-html/emit* ze-first-wan))
  (apply str (enlive-html/emit* ze-secand-wan)))

单独完成的转换完全按照我的意愿进行:它们吃掉了原始的 HTML 并显示了我用来填充信息的克隆。

然而,以这种方式一起完成,原始的 html 页 divs 被保留,所以我最终得到了原始的 html 文件 divs和我的克隆人一起,这种行为是不明智的。

请帮忙。

非常感谢。

Enlive-html 为此提供了 do-> 功能。

(defn do->
 "Chains (composes) several transformations. Applies functions from left to right."
 [& fns]
  #(reduce (fn [nodes f] (flatmap f nodes)) (as-nodes %) fns))

你可以使用这样的东西:

 (apply str (enlive-html/emit* (enlive-html/do-> ze-first-wan ze-second-wan)))