Compojure HTML 형식
-
20-09-2019 - |
문제
저는 Clojure와 완전한 HTML/Compojure Virgin을 처음 접했습니다. 나는 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을 생성합니다. 내가 얻는 것은 예상대로 DOC 유형과 한 줄의 모든 HTML이 뒤 따릅니다. HTML 깔끔한 출력 파일의 내용에 문제가 없습니다. 내가 만약 한 줄로 나옵니다 println
그것도 대답에도.
서식 출력을 얻는 데 필요한 다른 것이 있습니까?
해결책
Compojure에서 HTML 출력의 형식은 성능과 복잡성 이유를 위해 제거되었습니다. 포맷 된 출력을 얻으려면 자신의 프린터 기능을 작성해야 할 것입니다.
나는 보통 compojure가 적합한 것을보고 Firebug를 사용하여 내 브라우저에서 라이브를 볼 때 일반적으로 HTML을 출력합니다. Firebug는 실제로 한 줄에 있든 없든 관계없이 잘 형식화됩니다. 이것은 대부분의 시간에 충분히 잘 작동합니다. 이 HTML을 읽을 수있는 형태로 직렬화 해야하는 경우 Clojure 벡터 및 SexP로 유지하여 직렬화 할 수 있습니다.
다른 팁
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을 출력하고 경고를 건너 뛰도록 지시합니다. Pretty-Printer 함수의 리턴 값은 이제 Firebug 또는 간단한 텍스트 편집기로 열든 여부에 관계없이 멋지게 형식화됩니다.
톤이 있습니다 HTML Java에 사용할 수있는 Pretty Printer, 특히 JTIDY, 자바 포트 HTML 깔끔한. 이 라이브러리를 통해 Clojure의 출력을 쉽게 공급하고 깔끔하게 들여 쓰기 및 포맷 된 HTML을 다시 얻을 수 있습니다.
HTML Tidy는 해당 경로를 가고 싶다면 UNIX의 명령 줄 프로그램으로도 제공됩니다. 다른 쉘 프로그램과 마찬가지로 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)))
누군가가 여전히이 쿼리를보고 있다면 딸꾹질 도서관. 정확히 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)))
원래 포스터가 의도 한대로 정확하게 작동합니다. 권장.