5

Clojureのしゃっくりを逆転させる関数を探しています

それで

   <html></html>

になる

[:html]


@kotarak の回答をフォローアップすると、これでうまくいきます。

(use 'net.cgrand.enlive-html)
(import 'java.io.StringReader)

(defn enlive->hiccup
   [el]
   (if-not (string? el)
     (->> (map enlive->hiccup (:content el))
       (concat [(:tag el) (:attrs el)])
       (keep identity)
       vec)
     el))

(defn html->enlive 
  [html]
  (first (html-resource (StringReader. html))))

(defn html->hiccup [html]
  (-> html
      html->enlive
      enlive->hiccup))

=> (html->hiccup "<html><body id='foo'>hello</body></html>")
[:html [:body {:id "foo"} "hello"]]
4

3 に答える 3

8

enlivehtml-resourceから次のような構造を取得できます。

{:tag :html :attrs {} :content []}

次に、これをトラバースしてしゃっくり構造に変えます。

(defn html->hiccup
   [html]
   (if-not (string? html)
     (->> (map html->hiccup (:content html))
       (concat [(:tag html) (:attrs html)])
       (keep identity)
       vec)
     html))

使用例を次に示します。

user=>  (html->hiccup {:tag     :p
                       :content ["Hello" {:tag     :a
                                          :attrs   {:href "/foo"}
                                          :content ["World"]}
                                 "!"]})
[:p "Hello" [:a {:href "/foo"} "World"] "!"]
于 2012-06-19T05:42:50.257 に答える
3

これを行うヒッコリーがあります:https://github.com/davidsantiago/hickory

于 2014-03-07T10:46:12.083 に答える