1

これにどのようにアプローチすればよいかわかりません。何かにフィードして HTML を取得したい CSS ファイルのリストがあります。例えば、

(list "base.css" "index.css" "more_css.css") ;vector might be more appropriate?

次のように変換する必要があります。

<link href="css/base.css" rel="stylesheet" />
<link href="css/index.css" rel="stylesheet" />
<link href="css/more_css.css" rel="stylesheet" />

そこから に追加する必要があります<head>

defsnippetほとんど適切に見えますが、テンプレート ファイルとそのファイルのセクションのセレクターを取ります。ここで生成される HTML はテンプレートに依存せず、HTML のみを生成するものが適切と思われます。clone-for私が欲しいもののループ部分を行うかもしれませんが、それを使用する方法を理解するのに苦労しています。

4

2 に答える 2

5

defsnippet実際には、Enlive が理解できる任意のソースを使用します。ファイルである必要はありません。特に、linkタグ テンプレートのインライン Enlive スタイル表現にすることができます。バージョン 1.1.0 以降、Enlivenet.grand.enlive-html/htmlは Hiccup スタイル表記を解析するヘルパー ( ) も提供します。Hiccup スタイルは手書きの方が便利だと思うので、以下ではそれを使用します。(インライン HTML 文字列を : で囲んで使用することもできますStringReader(java.io.StringReader. "<div></div>"))

コードは次のとおりです。

(require '[net.cgrand.enlive-html :as enlive])

(enlive/defsnippet link-css
  ;; representation of the link tag template:
  (enlive/html [:link {:href "" :rel "stylesheet"}])
  ;; selector is required, :link works fine here:
  [:link]
  ;; this is the parameter vector of the fn defsnippet will generate:
  [hrefs]
  ;; clone-for will generate one link tag for each provided href:
  (enlive/clone-for [href hrefs]
    [:link]
    (enlive/set-attr :href href)))

次のように使用します。

(->> (link-css ["css/base.css" "css/index.css" "css/more_css.css"])
  (enlive/emit*)
  (apply str))
;= "<link href=\"css/base.css\" rel=\"stylesheet\" /><link href=\"css/index.css\" rel=\"stylesheet\" /><link href=\"css/more_css.css\" rel=\"stylesheet\" />"

printlnパイプラインの最後に追加すること->>は、これをテストする便利な方法です。これが出力です(わかりやすくするために2つの改行を手動で挿入しています):

<link href="css/base.css" rel="stylesheet" />
<link href="css/index.css" rel="stylesheet" />
<link href="css/more_css.css" rel="stylesheet" />
于 2013-12-28T16:43:34.473 に答える
0

または:

(require '[net.cgrand.enlive-html :as enlive])

(defn include-css [href]
      (first (enlive/html [:link {:href href :rel "stylesheet"}])))

(map include-css ["css/base.css" "css/index.css" "css/more_css.css"])
;; newlines added by hand for clarity
=> ({:tag :link, :attrs {:href "css/base.css", :rel "stylesheet"}, :content ()} 
    {:tag :link, :attrs {:href "css/index.css", :rel "stylesheet"}, :content ()} 
    {:tag :link, :attrs {:href "css/more_css.css", :rel "stylesheet"}, :content ()})

正しい HTML が生成されることを再確認します。

(print (apply str (html/emit* (map include-css ["css/base.css" "css/index.css" "css/more_css.css"]))))
;; newlines added by hand for clarity
=> <link href="css/base.css" rel="stylesheet" />
   <link href="css/index.css" rel="stylesheet" />
   <link href="css/more_css.css" rel="stylesheet" />
   nil
于 2014-01-30T04:28:43.560 に答える