Skip to content

Commit

Permalink
fix indent
Browse files Browse the repository at this point in the history
  • Loading branch information
Takahiro Hozumi committed Jul 6, 2012
1 parent ab7b34e commit 85b4954
Showing 1 changed file with 138 additions and 138 deletions.
276 changes: 138 additions & 138 deletions src/leiningen/hicv.clj
Original file line number Original file line Diff line number Diff line change
@@ -1,16 +1,16 @@
(ns leiningen.hicv (ns leiningen.hicv
(:refer-clojure :exclude [pop!]) (:refer-clojure :exclude [pop!])
(:require [net.cgrand.enlive-html :as en] (:require [net.cgrand.enlive-html :as en]
[hiccup.core :as hic] [hiccup.core :as hic]
[clojure.contrib.def :as cdef :only [defvar-]] [clojure.contrib.def :as cdef :only [defvar-]]
[clojure.java.io :as io] [clojure.java.io :as io]
[clojure.pprint :as pp] [clojure.pprint :as pp]
[hozumi.det-enc :as enc] [hozumi.det-enc :as enc]
[pattern-match :as pat] [pattern-match :as pat]
[org.satta.glob :as glob]) [org.satta.glob :as glob])
(:import [java.util.regex Pattern] (:import [java.util.regex Pattern]
[java.io StringReader PushbackReader [java.io StringReader PushbackReader
FileInputStream InputStreamReader LineNumberReader])) FileInputStream InputStreamReader LineNumberReader]))


(cdef/defvar- *clj-tag* :c--) (cdef/defvar- *clj-tag* :c--)
(cdef/defvar- *clj-attr-key* :clj) (cdef/defvar- *clj-attr-key* :clj)
Expand All @@ -19,22 +19,22 @@
(defn- mk-tag [tag {:keys [class id]}] (defn- mk-tag [tag {:keys [class id]}]
(keyword (keyword
(str (name tag) (str (name tag)
(when id (when id
(str "#" id)) (str "#" id))
(when class (when class
(apply str (apply str
(interleave (repeat ".") (interleave (repeat ".")
(re-seq #"\w+" class))))))) (re-seq #"\w+" class)))))))


(defn- replace-when [pred coll replacements] (defn- replace-when [pred coll replacements]
(lazy-seq (lazy-seq
(when (seq coll) (when (seq coll)
(if (seq replacements) (if (seq replacements)
(if (pred (first coll)) (if (pred (first coll))
(cons (first replacements) (cons (first replacements)
(replace-when pred (rest coll) (rest replacements))) (replace-when pred (rest coll) (rest replacements)))
(cons (first coll) (cons (first coll)
(replace-when pred (rest coll) replacements))) (replace-when pred (rest coll) replacements)))
coll)))) coll))))


(defn- read-from-str [s-str] (defn- read-from-str [s-str]
Expand All @@ -43,77 +43,77 @@


(defn- attr-solve [attrs] (defn- attr-solve [attrs]
(reduce conj {} (reduce conj {}
(map (fn [[k v]] (map (fn [[k v]]
[(if-let [[_ c] (re-matches [(if-let [[_ c] (re-matches
(Pattern/compile (str *attr-code-prefix* "(.*)")) (Pattern/compile (str *attr-code-prefix* "(.*)"))
(name k))] (name k))]
(read-from-str c) k) (read-from-str c) k)
(if-let [[_ c] (re-matches (if-let [[_ c] (re-matches
(Pattern/compile (str *attr-code-prefix* "(.*)")) (Pattern/compile (str *attr-code-prefix* "(.*)"))
v)] v)]
(read-from-str c) v)]) attrs))) (read-from-str c) v)]) attrs)))


(defn- html2hic* [node] (defn- html2hic* [node]
(letfn [(into-it [s cnts] (letfn [(into-it [s cnts]
(replace-when #(and (symbol? %) (replace-when #(and (symbol? %)
(= \$ (first (str %)))) (= \$ (first (str %))))
s (map html2hic* cnts)))] s (map html2hic* cnts)))]
(if (map? node) (if (map? node)
(let [{:keys [tag attrs content]} node (let [{:keys [tag attrs content]} node
tag (mk-tag tag attrs) tag (mk-tag tag attrs)
attrs (dissoc attrs :class :id) attrs (dissoc attrs :class :id)
attrs (attr-solve attrs) attrs (attr-solve attrs)
v (if (not (empty? attrs)) v (if (not (empty? attrs))
[tag attrs] [tag]) [tag attrs] [tag])
cnts (filter #(not (and (string? %) cnts (filter #(not (and (string? %)
(re-matches #"\n\s*" %))) content)] (re-matches #"\n\s*" %))) content)]
(if (and (= tag *clj-tag*) (if (and (= tag *clj-tag*)
(*clj-attr-key* attrs)) (*clj-attr-key* attrs))
(with-open [pbr (-> attrs *clj-attr-key* StringReader. PushbackReader.)] (with-open [pbr (-> attrs *clj-attr-key* StringReader. PushbackReader.)]
(let [s (read pbr)] (let [s (read pbr)]
(cond (cond
(seq? s) (into-it s cnts) (seq? s) (into-it s cnts)
(vector? s) (vec (into-it s cnts));;(reduce conj s (map html2hic* cnts)) (vector? s) (vec (into-it s cnts));;(reduce conj s (map html2hic* cnts))
(coll? s) (reduce conj s (map html2hic* cnts)) (coll? s) (reduce conj s (map html2hic* cnts))
:else s))) :else s)))
(reduce conj v (map html2hic* cnts)))) (reduce conj v (map html2hic* cnts))))
node))) node)))


(defn- source2s (defn- source2s
[x] [x]
(when-let [v (resolve x)] (when-let [v (resolve x)]
(when-let [filepath (:file (meta v))] (when-let [filepath (:file (meta v))]
(with-open [rdr (-> filepath (with-open [rdr (-> filepath
FileInputStream. FileInputStream.
InputStreamReader. InputStreamReader.
LineNumberReader.)] LineNumberReader.)]
(dotimes [_ (-> v meta :line dec)] (.readLine rdr)) (dotimes [_ (-> v meta :line dec)] (.readLine rdr))
(with-open [pbr (PushbackReader. rdr)] (with-open [pbr (PushbackReader. rdr)]
(read pbr)))))) (read pbr))))))


(defn- html-node? [s] (defn- html-node? [s]
(and (vector? s) (and (vector? s)
(keyword? (first s)))) (keyword? (first s))))


(defn- tree-search [pred node] (defn- tree-search [pred node]
(letfn [(inner [s q] (letfn [(inner [s q]
(if-let [v (pred s)] (if-let [v (pred s)]
v v
(cond (cond
(map? s) (map? s)
(let [[fs & res] (reduce conj q (let [[fs & res] (reduce conj q
(concat (keys s) (concat (keys s)
(vals s)))] (vals s)))]
(recur fs (vec res))) (recur fs (vec res)))

(coll? s)
(let [[fs & res] (reduce conj q s)]
(recur fs (vec res)))


:else (coll? s)
(if (empty? q) (let [[fs & res] (reduce conj q s)]
nil (recur fs (vec res)))
(recur (first q) (vec (rest q)))))))]
:else
(if (empty? q)
nil
(recur (first q) (vec (rest q)))))))]
(inner node []))) (inner node [])))


(defn- should-be-child? [node] (defn- should-be-child? [node]
Expand All @@ -123,10 +123,10 @@
(defn- clj-attr [node] (defn- clj-attr [node]
(with-out-str (with-out-str
(pr (let [ans (replace-when should-be-child? (pr (let [ans (replace-when should-be-child?
node node
(map #(symbol (str "$" %)) (iterate inc 1)))] (map #(symbol (str "$" %)) (iterate inc 1)))]
(if (vector? node) (if (vector? node)
(vec ans) ans))))) (vec ans) ans)))))


(defn- attr-code [code] (defn- attr-code [code]
(with-out-str (with-out-str
Expand All @@ -135,19 +135,18 @@


(defn- hic2vec* [node] (defn- hic2vec* [node]
(condp #(%1 %2) node (condp #(%1 %2) node
seq? seq? (reduce conj
(reduce conj [*clj-tag* {*clj-attr-key* (clj-attr node)}]
[*clj-tag* {*clj-attr-key* (clj-attr node)}] (map hic2vec* (filter should-be-child? node)))
(map hic2vec* (filter should-be-child? node)))
symbol? [*clj-tag* {*clj-attr-key* (str node)}] symbol? [*clj-tag* {*clj-attr-key* (str node)}]

html-node? (vec (map hic2vec* node)) html-node? (vec (map hic2vec* node))
vector? (reduce conj [*clj-tag* {*clj-attr-key* (clj-attr node)}] vector? (reduce conj [*clj-tag* {*clj-attr-key* (clj-attr node)}]
(map hic2vec* (filter should-be-child? node))) (map hic2vec* (filter should-be-child? node)))
map? (reduce conj {} map? (reduce conj {}
(map (fn [[k v]] (map (fn [[k v]]
[(if (keyword? k) k (keyword (attr-code k))) [(if (keyword? k) k (keyword (attr-code k)))
(if (string? v) v (attr-code v))]) node)) (if (string? v) v (attr-code v))]) node))
node)) node))


(defn- hic2vec [fn-sym-or-s] (defn- hic2vec [fn-sym-or-s]
Expand All @@ -167,46 +166,46 @@
(defn- list-s [path] (defn- list-s [path]
(let [encoding (enc/detect path :default)] (let [encoding (enc/detect path :default)]
(with-open [pbr (-> path (with-open [pbr (-> path
FileInputStream. FileInputStream.
(InputStreamReader. encoding) (InputStreamReader. encoding)
PushbackReader.)] PushbackReader.)]
(doall (take-while identity (doall (take-while identity
(repeatedly (repeatedly
#(try (read pbr) #(try (read pbr)
(catch java.lang.Exception _ (catch java.lang.Exception _
nil)))))))) nil))))))))


(defn- ns2filename [ns-str] (defn- ns2filename [ns-str]
(let [replaced (.replaceAll ns-str "/" ".")] (let [replaced (.replaceAll ns-str "/" ".")]
(str *hicv-dir-name* (str *hicv-dir-name*
replaced replaced
".html"))) ".html")))


(defn- get-name [exp] (defn- get-name [exp]
(let [expanded (macroexpand exp)] (let [expanded (macroexpand exp)]
(pat/match expanded (pat/match expanded
v :when (not (coll? v)) nil v :when (not (coll? v)) nil
[fs x & _] :when (= fs 'def) x [fs x & _] :when (= fs 'def) x
_ nil))) _ nil)))


(defn- path2ns [path src-path] (defn- path2ns [path src-path]
(let [src-path (if (= \/ (last src-path)) (let [src-path (if (= \/ (last src-path))
src-path src-path
(str src-path \/)) (str src-path \/))
p (Pattern/compile (str src-path "(.*)\\.clj")) p (Pattern/compile (str src-path "(.*)\\.clj"))
[_ n] (re-matches p path)] [_ n] (re-matches p path)]
(-> n (-> n
(.replaceAll ,,, "_" "-") (.replaceAll ,,, "_" "-")
(.replaceAll ,,, "/" ".")))) (.replaceAll ,,, "/" "."))))


(defn- search-hic [src-path] (defn- search-hic [src-path]
(filter (fn [[_ hics]] (not (empty? hics))) (filter (fn [[_ hics]] (not (empty? hics)))
(for [file-path (glob/glob (str src-path "/**/*.clj") :s)] (for [file-path (glob/glob (str src-path "/**/*.clj") :s)]
[(path2ns file-path src-path) [(path2ns file-path src-path)
(filter identity (filter identity
(for [exp (list-s file-path)] (for [exp (list-s file-path)]
(if-let [n (and (should-be-child? exp) (get-name exp))] (if-let [n (and (should-be-child? exp) (get-name exp))]
[n exp])))]))) [n exp])))])))


(defn- mk-syms [nspace hic-names] (defn- mk-syms [nspace hic-names]
(map #(symbol (str nspace "/" %)) hic-names)) (map #(symbol (str nspace "/" %)) hic-names))
Expand All @@ -215,53 +214,54 @@
(prepare-hicv-dir!) (prepare-hicv-dir!)
(doseq [[nspace name&exps] (search-hic src-path)] (doseq [[nspace name&exps] (search-hic src-path)]
(do (with-open [f (-> nspace ns2filename io/writer)] (do (with-open [f (-> nspace ns2filename io/writer)]
(doto f (doto f
(.write "<hicv />") (.write "<hicv />")
(.newLine) (.newLine)
(.newLine))) (.newLine)))
(with-open [f (-> nspace ns2filename (io/writer ,,, :append true))] (with-open [f (-> nspace ns2filename (io/writer ,,, :append true))]
(doseq [[_ exp] name&exps] (doseq [[_ exp] name&exps]
(doto f (doto f
(.write (-> exp hic2vec hic/html)) (.write (-> exp hic2vec hic/html))
(.newLine) (.newLine)
(.newLine))))))) (.newLine)))))))


(defn- hic2htmls [src-path targets] (defn- hic2htmls [src-path targets]
(prepare-hicv-dir!) (prepare-hicv-dir!)
(doseq [[nspace name&exps] (search-hic src-path) (doseq [[nspace name&exps] (search-hic src-path)
[nam exp] name&exps] [nam exp] name&exps]
(with-open [f (-> (str nspace "." (name nam)) ns2filename io/writer)] (with-open [f (-> (str nspace "." (name nam)) ns2filename io/writer)]
(doto f (doto f
(.write "<hicv />") (.write "<hicv />")
(.newLine) (.newLine)
(.newLine) (.newLine)
(.write (hic/html (hic2vec exp))) (.write (hic/html (hic2vec exp)))
(.newLine) (.newLine)
(.newLine))))) (.newLine)))))


(defn- html2hic [resource] (defn- html2hic [resource]
(let [encoding (enc/detect resource :default) (let [encoding (enc/detect resource :default)
nodes (-> resource nodes (-> resource
FileInputStream. FileInputStream.
(InputStreamReader. encoding) (InputStreamReader. encoding)
en/html-resource first :content)] en/html-resource first :content)]
(map html2hic* nodes))) (map html2hic* nodes)))


(defn- html2hic-front [file-names] (defn- html2hic-front [file-names]
(doall (map pp/pprint (doall
(filter #(not (and (string? %) (map pp/pprint
(re-matches #"\n\s*" %))) (filter #(not (and (string? %)
(mapcat html2hic (if (empty? file-names) (re-matches #"\n\s*" %)))
(-> *hicv-dir-name* io/file .listFiles) (mapcat html2hic (if (empty? file-names)
file-names)))))) (-> *hicv-dir-name* io/file .listFiles)
file-names))))))


(defn hicv (defn hicv
[project & [first-arg & rest-args]] [project & [first-arg & rest-args]]
(condp = first-arg (condp = first-arg
"2html" (hic2html (:source-path project) (:target-hiccup project)) "2html" (hic2html (:source-path project) (:target-hiccup project))
"2htmls" (hic2htmls (:source-path project) (:target-hiccup project)) "2htmls" (hic2htmls (:source-path project) (:target-hiccup project))
"2hic" (html2hic-front rest-args) "2hic" (html2hic-front rest-args)
(println "Usage: (println "Usage:
lein hicv 2html lein hicv 2html
lein hicv 2htmls lein hicv 2htmls
lein hicv 2hic\n"))) lein hicv 2hic\n")))

0 comments on commit 85b4954

Please sign in to comment.