2009-12-17 69 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)))