6

我对 Clojure 和一个完整的 HTML/Compojure 处女比较陌生。我正在尝试使用 Compojure 使用类似于以下的函数创建 HTML 的静态页面:

(defn fake-write-html
  [dir args]
  (let [file (str dir *file-separator* *index-file*)
        my-html (html
                  (doctype :html4)
                  [:html
                   [:head
                    [:title "Docs and Dirs:"]]
                   [:body
                    [:div
                     [:h2 "A nice title"]]
                    [:div
                     [:ul
                      [:li "One"]
                      [:li "Two"]]]]])]
    (clojure.contrib.duck-streams/spit file my-html)))

该函数只是将 HTML 写入文件。(这里的args论点无关紧要。只是为了确保示例在我的程序中编译和运行。)

“Programming Clojure”表明对该html函数的调用将产生格式化的 HTML——多行带有缩进。我得到的只是预期的文档类型,然后是一行中的所有 HTML。HTML Tidy没有发现输出文件的内容有任何问题。println如果我也在 REPL 上,它会以单行形式出现。

是否需要其他东西来获得格式化输出?

4

5 回答 5

10

出于性能和复杂性的原因,Compojure 中的 HTML 输出格式已被删除。要获得格式化输出,您可能必须编写自己的打印机函数。

我通常输出 Compojure 认为合适的 HTML,并使用 Firebug 在我的浏览器中实时查看它。无论它是否真的都在一行上,Firebug 都会以很好的格式显示它。这在大多数情况下都足够好用。如果您需要以可读的形式序列化此 HTML,您可以将其保存为 Clojure 向量和 sexps 并以这种方式序列化它。

于 2009-12-17T01:58:58.417 回答
9

尽管布赖恩的回答将我指向 Firebug,启用了我想要的调试,但我只是强迫症不要管它。根据 kwertii 指向 JTidy 的指针,我在我的程序中包含了以下代码。

编辑:稍微简化了代码

(ns net.dneclark.someprogram
  (:gen-class)
  ...
  (:import (org.w3c.tidy Tidy))
      )

  ...

(defn configure-pretty-printer
  "Configure the pretty-printer (an instance of a JTidy Tidy class) to
generate output the way we want -- formatted and without sending warnings.
Return the configured pretty-printer."
  []
  (doto (new Tidy)
    (.setSmartIndent true)
    (.setTrimEmptyElements true)
    (.setShowWarnings false)
    (.setQuiet true)))

(defn pretty-print-html
  "Pretty-print the html and return it as a string."
  [html]
  (let [swrtr (new StringWriter)]
    (.parse (configure-pretty-printer) (new StringReader (str html)) swrtr)
    (str swrtr)))

我将 jtidy-r938.jar 添加到我的项目(使用 enclojure 插件的 NetBeans)并导入它。配置函数告诉解析器输出格式化、缩进的 HTML 并跳过警告。现在,无论我是用 Firebug 还是简单的文本编辑器打开它,漂亮打印机函数的返回值都被很好地格式化了。

于 2010-01-01T17:32:04.890 回答
4

有大量的HTML 漂亮打印机可用于 Java,尤其是JTidy ,它是HTML Tidy的 Java 端口。您可以轻松地以编程方式通过该库提供 Clojure 的输出,并获得整齐的缩进和格式化的 HTML。

HTML Tidy 也可用作 Unix 的命令行程序,如果您愿意走这条路——您可以像任何其他 shell 程序一样通过管道传输您的 HTML。

于 2009-12-27T22:07:48.030 回答
1

以上对我不起作用。我改变了一点。

将此 [jtidy "4aug2000r7-dev"] 添加到 project.clj

(:use clojure.core)
(:import (org.w3c.tidy Tidy))
(:import (java.io ByteArrayInputStream ByteArrayOutputStream)))



(defn configure-pretty-printer
 "Configure the pretty-printer (an instance of a JTidy Tidy class) to
generate output the way we want -- formatted and without sending warnings.
 Return the configured pretty-printer."
[]
(doto (new Tidy)
(.setSmartIndent true)
;(.setTrimEmptyElements true)
(.setShowWarnings false)
(.setQuiet true)))

(defn pretty-print-html
  "Pretty-print the html and return it as a string."
 [html]
(let [swrtr ( ByteArrayOutputStream.)]
  (.parse (configure-pretty-printer) (ByteArrayInputStream. (.getBytes (str html)))  swrtr)
(str swrtr)))
于 2013-05-20T18:26:20.037 回答
0

如果有人仍在查看此查询,则您需要打嗝库。如果完全按照所示的 Clojure 数据结构格式化 HTML。

所以


(require '[hiccup.core :refer [html]])

(defn fake-write-html
  [dir args]
  (let [file (str dir *file-separator* *index-file*)
        my-html (html
                  [:html
                   [:head
                    [:title "Docs and Dirs:"]]
                   [:body
                    [:div
                     [:h2 "A nice title"]]
                    [:div
                     [:ul
                      [:li "One"]
                      [:li "Two"]]]]])]
    (clojure.contrib.duck-streams/spit file my-html)))

将完全按照原始海报的预期工作。强力推荐。

于 2019-03-10T09:43:18.230 回答