2009-12-17 44 views
5

我对Clojure和一个完整的HTML/Compojure处女相对较新。我试图用Compojure使用类似下面的函数来创建HTML的静态页面: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说法是不相关的。只需在有保证的例子可以编译和运行在我的计划。)

“编程Clojure的”表示,调用html功能将产生HTML格式 - 多行缩进。我所得到的就是预期的文档类型,然后是单行上的所有HTML。 HTML Tidy没有发现输出文件内容的任何问题。如果我在REPL也是println,它会作为一条线出现。

还有什么需要获得格式化输出?

回答

9

Compojure中的HTML输出格式为removed for performance and complexity reasons。要获得格式化的输出,您可能必须编写自己的打印机功能。

我通常会输出HTML,因为Compojure认为合适,并使用Firebug在我的浏览器中查看它。无论它是否全部在一行上,Firebug都会很好地显示它的格式。这在大多数情况下工作得很好。如果你需要以可读的形式对这个HTML进行序列化,你可以把它保存为Clojure矢量和sexp,并以这种方式对它进行序列化。

+0

感谢另一个答案布赖恩。我对Firebug并不熟悉,但在玩了几分钟后,它似乎给了我一直在寻找的调试能力。 我也在http://www.erik-rasmussen.com/blog/2009/09/08/xml-renderer-in-clojure/上找到了另一个有趣的方法。还没有尝试过,但看起来比开发我的打印机功能更容易(更快)。或者我可以只抓取格式化的早期版本的Compojure。 – clartaq 2009-12-17 20:06:12

4

有大量HTML pretty printers available for Java,特别是JTidy,一个Java端口HTML Tidy。您可以通过该库以编程方式轻松地提供Clojure的输出,并将整齐缩进和格式化的HTML返回。

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

+0

感谢您的指针,kwertii。我没有意识到这一点,并会试一试。 – clartaq 2009-12-27 22:27:35

8

虽然Brian的回答指出我是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还是简单的文本编辑器打开它,漂亮打印机功能的返回值现在都可以很好地格式化。

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