Skip to content

Commit

Permalink
Merge pull request #22 from reedho/html-support
Browse files Browse the repository at this point in the history
html-support (with hickory)
  • Loading branch information
markus-wa authored Oct 18, 2023
2 parents a852403 + efd9774 commit 28889c3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
4 changes: 3 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
com.cognitect/transit-clj {:mvn/version "1.0.324"}
medley/medley {:mvn/version "1.3.0"}
mvxcvi/puget {:mvn/version "1.3.1"}
tolitius/xml-in {:mvn/version "0.1.1"}}
tolitius/xml-in {:mvn/version "0.1.1"}
org.clj-commons/hickory {:mvn/version "0.7.3"}
org.jsoup/jsoup {:mvn/version "1.14.3"}}
:aliases
{:run
{:main-opts ["-m" "cq.main"]}
Expand Down
18 changes: 17 additions & 1 deletion src/cq/formats.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
(:require [clojure.data.csv :as csv]
[clojure.data.json :as json]
[clojure.data.xml :as xml]
[hickory.core :as html]
[hickory.render :refer [hickory-to-html]]
[clojure.edn :as edn]
[clojure.pprint :as ppt]
[clojure.java.io :as io]
Expand Down Expand Up @@ -167,6 +169,18 @@
(with-open [w (io/writer out)]
(emit x w)))))

(defn ->html-reader
[_]
(fn [in]
(html/as-hickory (html/parse (slurp (io/reader in))))))

(defn ->html-writer
[_]
(fn [x out]
(binding [*out* (io/writer out)]
(print (hickory-to-html x))
(flush))))

(def formats
{"json" {:->reader ->json-reader
:->writer ->json-writer}
Expand All @@ -185,7 +199,9 @@
"transit" {:->reader ->transit-reader
:->writer ->transit-writer}
"xml" {:->reader ->xml-reader
:->writer ->xml-writer}})
:->writer ->xml-writer}
"html" {:->reader ->html-reader
:->writer ->html-writer}})

(defn format->reader
[format in opts]
Expand Down
33 changes: 32 additions & 1 deletion test/cq/formats_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
(:require [cq.formats :as sut]
[clojure.test :refer :all]
[clojure.java.io :as io]
[clojure.string :as str])
[clojure.string :as str]
[hickory.core :as html])
(:import [java.io ByteArrayInputStream BufferedInputStream PrintStream ByteArrayOutputStream]))

(defn- to-out-stream
Expand Down Expand Up @@ -152,3 +153,33 @@
<a:article>Hello</a:article>
</a:html>\n"
(test-writer-str sut/->xml-writer {:pretty true} test-xml-data))))))

(def test-html-str
"<p>hello</p>")

(def test-html-data
{:type :document
:content
[{:type :element
:attrs nil
:tag :html
:content
[{:type :element
:attrs nil
:tag :head :content nil}
{:type :element
:attrs nil
:tag :body :content
[{:type :element
:attrs nil
:tag :p
:content ["hello"]}]}]}]})

(deftest html
(testing "reader"
(is (= test-html-data
(test-reader-str sut/->html-reader nil test-html-str))))

(testing "writer"
(is (= "<html><head></head><body><p>hello</p></body></html>"
(test-writer-str sut/->html-writer nil test-html-data)))))

0 comments on commit 28889c3

Please sign in to comment.