2

enlive セレクターを使用して元の 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>"

この例で<br>は、元の入力スニペットとは異なり、enlive によってタグが自己終了タグに変換されています。

私は enlive がそれをタグのしゃっくりのようなリストに変換しているのではないかと考えています。そのため、残念ながら元の情報は失われています。

4

2 に答える 2

1

これはおそらく接線上の関係にすぎませんが、「追加」を使用すると、そうでなければ 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")})
于 2015-02-06T00:09:36.907 に答える