我对Clojure和完整的HTML/Compojure Virgin相对较新。我正在尝试使用类似的函数来创建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 争论在这里是无关紧要的。只是在那里确保示例编译并在我的程序中运行。)

“编程clojure”表示呼吁 html 功能将产生格式化的HTML-带有压痕的多条线。我所获得的只是预期的DOC类型,其后是单行上的所有HTML。 HTML整洁 找不到输出文件内容的任何问题。如果我 println 它也是在卧底。

获得格式化输出需要其他东西吗?

有帮助吗?

解决方案

HTML输出的格式为CompoJure 出于表现和复杂性原因删除. 。要获取格式化的输出,您可能必须编写自己的打印机功能。

我通常会在Compojure认为合适的情况下输出HTML,并使用Firebug将其活在我的浏览器中。不管真的全部都在一行上,Firebug都会显示出很好的格式。大多数情况下,这足够好。如果您需要以可读的形式序列化此HTML,则可以将其保留为clojure矢量和性别,并以这种方式序列化。

其他提示

尽管布莱恩的回答使我指向了火药,使我想要的调试,但我只是要强迫强迫症才能让它独自一人。跟进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插件)中并导入它。配置函数告诉解析器输出格式化,缩进的HTML并跳过警告。现在,无论我用Firebug还是简单的文本编辑器打开它,从相当局部函数中的返回值都可以很好地格式化。

有很多 HTML漂亮的打印机可用于Java, , 尤其 Jtidy, ,一个Java港口 HTML整洁. 。您可以通过编程来轻松地通过此库来馈送Clojure的输出,并将其整齐地缩进并格式化HTML。

如果您愿意走这条路线,则HTML整理也可以作为UNIX的命令行程序提供 - 您只需像其他任何Shell程序一样将HTML插入它。

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

将此[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)))

如果有人仍在查看此查询,您需要 打ic 图书馆。如果格式化了html的格式,则完全来自所示的clojure数据结构。

所以


(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)))

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top