Enlive - 提取原文 HTML
Enlive - extract original HTML
是否可以使用实时选择器检索原始 HTML(及其怪癖和格式)?
(def data "<div class=\"foo\"><p>some text <br> some more text</p></div>")
(apply str
(enlive/emit* (enlive/select (enlive/html-snippet data)
[:.foo :> enlive/any-node])))
=> "<p>some text <br /> some more text</p>"
在此示例中,enlive 已将 <br>
标签转换为自闭合标签,这与原始输入片段不同。
我怀疑 enlive 正在将其转换为类似打嗝的标签列表,以至于不幸丢失了原始信息。
您的怀疑是正确的,enlive 在努力提供对 HTML 的一致抽象的过程中使用了此信息。我认为这不是它旨在提供的功能。
虽然这可能只是切线相关,但如果您使用 "append" 您可以保留信息(例如评论),否则这些信息将被 net.cgrand.enlive-html/html-resource
https://github.com/cgrand/enlive/wiki/Table-and-Layout-Tutorial%2C-Part-3%3A-Simple-Transformations
<div id="wrapper">
<!--body-->
</div>
jcrit.server=> (pprint
(transform layout [:#wrapper]
(append page-content)))
({:tag :html,
{:tag :div,
:attrs {:id "wrapper"},
:content
("\n "
{:type :comment, :data "body"} ; <<== Still there.
"\n "
{:tag :p, :content ("Hi, mom!")})}
"\n")}
"\n\n")})
是否可以使用实时选择器检索原始 HTML(及其怪癖和格式)?
(def data "<div class=\"foo\"><p>some text <br> some more text</p></div>")
(apply str
(enlive/emit* (enlive/select (enlive/html-snippet data)
[:.foo :> enlive/any-node])))
=> "<p>some text <br /> some more text</p>"
在此示例中,enlive 已将 <br>
标签转换为自闭合标签,这与原始输入片段不同。
我怀疑 enlive 正在将其转换为类似打嗝的标签列表,以至于不幸丢失了原始信息。
您的怀疑是正确的,enlive 在努力提供对 HTML 的一致抽象的过程中使用了此信息。我认为这不是它旨在提供的功能。
虽然这可能只是切线相关,但如果您使用 "append" 您可以保留信息(例如评论),否则这些信息将被 net.cgrand.enlive-html/html-resource
https://github.com/cgrand/enlive/wiki/Table-and-Layout-Tutorial%2C-Part-3%3A-Simple-Transformations
<div id="wrapper">
<!--body-->
</div>
jcrit.server=> (pprint
(transform layout [:#wrapper]
(append page-content)))
({:tag :html,
{:tag :div,
:attrs {:id "wrapper"},
:content
("\n "
{:type :comment, :data "body"} ; <<== Still there.
"\n "
{:tag :p, :content ("Hi, mom!")})}
"\n")}
"\n\n")})