Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

change some names, add html->hiccup

  • Loading branch information...
commit 819675de0678a1bf1722df9de0a59a39578eafe6 1 parent 0413720
Hoàng Minh Thắng authored
Showing with 55 additions and 44 deletions.
  1. +37 −29 src/to_hiccup/core.clj
  2. +18 −15 test/to_hiccup/test/core.clj
66 src/to_hiccup/core.clj
View
@@ -1,4 +1,4 @@
-(ns leiningen.hicv
+(ns to-hiccup.core
"Convert html into hiccup and vice verse"
(:require [net.cgrand.enlive-html :as en]
[hiccup.core :as hic]
@@ -9,34 +9,34 @@
(def ^{:private true} hicv-dir-name "hicv")
-(defn- ensure-hicv-dir! []
- (let [f (io/file hicv-dir-name)]
- (if-not (.exists f)
- (.mkdir f))))
+(defn ensure-hicv-dir! []
+ (let [dir (io/file hicv-dir-name)]
+ (if-not (.exists dir)
+ (.mkdir dir))))
-(defn- remove-extension [file-path]
+(defn remove-extension [file-path]
(if-let [[_ pure-file-path] (re-matches #"(.*)\..*" file-path)]
pure-file-path
file-path))
-(defn- replace-extension [file-path extension]
- (-> file-path remove-extension (str ,,, extension)))
+(defn replace-extension [file-path extension]
+ (-> file-path remove-extension (str extension)))
-(defn- writeout-hiccup2html [file-path]
+(defn hiccup-file->html-file [file-path]
(spit (replace-extension file-path ".html")
(-> (slurp file-path :encoding (enc/detect file-path :default))
read-string
hic/html)))
-(defn- hiccups2htmls [file-paths]
+(defn hiccup-files->html-files [file-paths]
(ensure-hicv-dir!)
(let [file-paths (if (empty? file-paths)
(glob/glob (str hicv-dir-name "/**/*.clj") :s)
file-paths)]
(dorun
- (map writeout-hiccup2html file-paths))))
+ (map hiccup-file->html-file file-paths))))
-(defn- id&class-tag [tag {:keys [class id]}]
+(defn add-id&classes->tag [tag {:keys [class id]}]
(keyword
(str (name tag)
(when id
@@ -46,37 +46,45 @@
(interleave (repeat ".")
(re-seq #"[^ ]+" class)))))))
-(defn- enlive-node2hiccup [node]
+(defn enlive-node->hiccup [node]
(if (map? node)
;; for comment node {:type :comment, :data "[if IE]> ..."}
(if (= :comment (:type node))
(str "<!--" (:data node) "-->")
(let [{:keys [tag attrs content]} node
- tag (id&class-tag tag attrs)
+ tag (add-id&classes->tag tag attrs)
attrs (dissoc attrs :class :id)
hiccup-form (if (empty? attrs) [tag] [tag attrs])
cnts (filter #(not (and (string? %)
(re-matches #"\n\s*" %))) content)]
- (reduce conj hiccup-form (map enlive-node2hiccup cnts))))
+ (reduce conj hiccup-form (map enlive-node->hiccup cnts))))
node))
-(defn- url? [s]
+(defn url? [s]
(re-matches #"https?://.*" s))
-(defn- get-resource [resource-path]
+(defn get-resource [resource-path]
(if (url? resource-path)
(java.net.URL. resource-path)
(io/reader resource-path :encoding (enc/detect resource-path :default))))
-(defn- html2hiccup [resource-path]
+(defn html->hiccup [s]
+ (let [nodes (-> s
+ java.io.StringReader.
+ en/html-resource)]
+ (->> (map enlive-node->hiccup nodes)
+ (filter #(not (and (string? %)
+ (re-matches #"\n\s*" %)))))))
+
+(defn html-file->hiccup [resource-path]
(let [nodes (-> resource-path
get-resource
en/html-resource)]
- (->> (map enlive-node2hiccup nodes)
+ (->> (map enlive-node->hiccup nodes)
(filter #(not (and (string? %)
- (re-matches #"\n\s*" %))) ,,,))))
+ (re-matches #"\n\s*" %)))))))
-(defn- ensure-under-hicv-dir [^String resource-path]
+(defn ensure-under-hicv-dir [^String resource-path]
(if (url? resource-path)
(apply str hicv-dir-name "/" (replace {\/ \_} resource-path))
(if (.startsWith resource-path hicv-dir-name)
@@ -85,27 +93,27 @@
(or (re-find #"[^/]*$" resource-path) ;;"/ab/cd.html" => "cd.html"
"out.html")))))
-(defn- writeout-html2hiccup [resource-path]
+(defn html-file->hiccup-file [resource-path]
(spit (replace-extension (ensure-under-hicv-dir resource-path) ".clj")
(-> resource-path
- html2hiccup
+ html-file->hiccup
pp/pprint
with-out-str)))
-(defn- htmls2hiccups [resource-paths]
+(defn html-files->hiccup-files [resource-paths]
(ensure-hicv-dir!)
(let [resource-paths (if (empty? resource-paths)
- (glob/glob (str hicv-dir-name "/**/*.html") :s)
- resource-paths)]
+ (glob/glob (str hicv-dir-name "/**/*.html") :s)
+ resource-paths)]
(dorun
- (map writeout-html2hiccup resource-paths))))
+ (map html-file->hiccup-file resource-paths))))
(defn ^:no-project-needed hicv
"Convert html into hiccup and vice verse"
[project & [first-arg & rest-args]]
(condp = first-arg
- "2html" (hiccups2htmls rest-args)
- "2clj" (htmls2hiccups rest-args)
+ "2html" (hiccup-files->html-files rest-args)
+ "2clj" (html-files->hiccup-files rest-args)
(println "Usage:
lein hicv 2html
lein hicv 2clj\n")))
33 test/to_hiccup/test/core.clj
View
@@ -1,26 +1,29 @@
-(ns leiningen.hicv-test
- (:use clojure.test)
- (:require [leiningen.hicv :as hicv]))
+(ns to-hiccup.test.core
+ (:use clojure.test
+ to-hiccup.core))
(deftest replace-extension-test
(is (= "hicv/aaa.html"
- (#'hicv/replace-extension "hicv/aaa.clj" ".html")))
+ (replace-extension "hicv/aaa.clj" ".html")))
(is (= "hicv/aaa/bbb.clj"
- (#'hicv/replace-extension "hicv/aaa/bbb.html" ".clj"))))
+ (replace-extension "hicv/aaa/bbb.html" ".clj"))))
-(deftest id&class-tag-test
- (is (= :img (#'hicv/id&class-tag "img" {})))
- (is (= :img.myclass (#'hicv/id&class-tag "img" {:class "myclass"})))
- (is (= :img#myid (#'hicv/id&class-tag "img" {:id "myid"})))
- (is (= :img#myid.myclass (#'hicv/id&class-tag "img" {:id "myid" :class "myclass"})))
+(deftest add-id&classes->tag-test
+ (is (= :img (add-id&classes->tag "img" {})))
+ (is (= :img.myclass (add-id&classes->tag "img" {:class "myclass"})))
+ (is (= :img#myid (add-id&classes->tag "img" {:id "myid"})))
+ (is (= :img#myid.myclass
+ (add-id&classes->tag "img" {:id "myid" :class "myclass"})))
(is (= :img#myid.myclass1.myclass2
- (#'hicv/id&class-tag "img" {:id "myid" :class "myclass1 myclass2"})))
+ (add-id&classes->tag "img" {:id "myid" :class "myclass1 myclass2"})))
(is (= :img.foo.bar-foo
- (#'hicv/id&class-tag "img" {:class "foo bar-foo"}))))
+ (add-id&classes->tag "img" {:class "foo bar-foo"}))))
(deftest ensure-under-hicv-dir-test
- (is (= "hicv/aaa.html" (#'hicv/ensure-under-hicv-dir "hicv/aaa.html")))
- (is (= "hicv/bbb.html" (#'hicv/ensure-under-hicv-dir "/aaa/bbb.html")))
+ (is (= "hicv/aaa.html" (ensure-under-hicv-dir "hicv/aaa.html")))
+ (is (= "hicv/bbb.html" (ensure-under-hicv-dir "/aaa/bbb.html")))
(is (= "hicv/http:__google.com_aaa"
- (#'hicv/ensure-under-hicv-dir "http://google.com/aaa"))))
+ (ensure-under-hicv-dir "http://google.com/aaa"))))
+(deftest html->hiccup-test
+ (is (= '([:foo [:bar "bazz"]]) (html->hiccup "<foo><bar>bazz</bar></foo>"))))
Please sign in to comment.
Something went wrong with that request. Please try again.