Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

fix indent

  • Loading branch information...
commit 85b4954f39179fc932c63f4e5980ef2a640c83a6 1 parent ab7b34e
Takahiro Hozumi authored

Showing 1 changed file with 138 additions and 138 deletions. Show diff stats Hide diff stats

  1. +138 138 src/leiningen/hicv.clj
276 src/leiningen/hicv.clj
... ... @@ -1,16 +1,16 @@
1 1 (ns leiningen.hicv
2 2 (:refer-clojure :exclude [pop!])
3 3 (:require [net.cgrand.enlive-html :as en]
4   - [hiccup.core :as hic]
5   - [clojure.contrib.def :as cdef :only [defvar-]]
6   - [clojure.java.io :as io]
7   - [clojure.pprint :as pp]
8   - [hozumi.det-enc :as enc]
9   - [pattern-match :as pat]
10   - [org.satta.glob :as glob])
  4 + [hiccup.core :as hic]
  5 + [clojure.contrib.def :as cdef :only [defvar-]]
  6 + [clojure.java.io :as io]
  7 + [clojure.pprint :as pp]
  8 + [hozumi.det-enc :as enc]
  9 + [pattern-match :as pat]
  10 + [org.satta.glob :as glob])
11 11 (:import [java.util.regex Pattern]
12   - [java.io StringReader PushbackReader
13   - FileInputStream InputStreamReader LineNumberReader]))
  12 + [java.io StringReader PushbackReader
  13 + FileInputStream InputStreamReader LineNumberReader]))
14 14
15 15 (cdef/defvar- *clj-tag* :c--)
16 16 (cdef/defvar- *clj-attr-key* :clj)
@@ -19,22 +19,22 @@
19 19 (defn- mk-tag [tag {:keys [class id]}]
20 20 (keyword
21 21 (str (name tag)
22   - (when id
23   - (str "#" id))
24   - (when class
25   - (apply str
26   - (interleave (repeat ".")
27   - (re-seq #"\w+" class)))))))
  22 + (when id
  23 + (str "#" id))
  24 + (when class
  25 + (apply str
  26 + (interleave (repeat ".")
  27 + (re-seq #"\w+" class)))))))
28 28
29 29 (defn- replace-when [pred coll replacements]
30 30 (lazy-seq
31 31 (when (seq coll)
32 32 (if (seq replacements)
33 33 (if (pred (first coll))
34   - (cons (first replacements)
35   - (replace-when pred (rest coll) (rest replacements)))
36   - (cons (first coll)
37   - (replace-when pred (rest coll) replacements)))
  34 + (cons (first replacements)
  35 + (replace-when pred (rest coll) (rest replacements)))
  36 + (cons (first coll)
  37 + (replace-when pred (rest coll) replacements)))
38 38 coll))))
39 39
40 40 (defn- read-from-str [s-str]
@@ -43,40 +43,40 @@
43 43
44 44 (defn- attr-solve [attrs]
45 45 (reduce conj {}
46   - (map (fn [[k v]]
47   - [(if-let [[_ c] (re-matches
48   - (Pattern/compile (str *attr-code-prefix* "(.*)"))
49   - (name k))]
50   - (read-from-str c) k)
51   - (if-let [[_ c] (re-matches
52   - (Pattern/compile (str *attr-code-prefix* "(.*)"))
53   - v)]
54   - (read-from-str c) v)]) attrs)))
  46 + (map (fn [[k v]]
  47 + [(if-let [[_ c] (re-matches
  48 + (Pattern/compile (str *attr-code-prefix* "(.*)"))
  49 + (name k))]
  50 + (read-from-str c) k)
  51 + (if-let [[_ c] (re-matches
  52 + (Pattern/compile (str *attr-code-prefix* "(.*)"))
  53 + v)]
  54 + (read-from-str c) v)]) attrs)))
55 55
56 56 (defn- html2hic* [node]
57 57 (letfn [(into-it [s cnts]
58   - (replace-when #(and (symbol? %)
59   - (= \$ (first (str %))))
60   - s (map html2hic* cnts)))]
  58 + (replace-when #(and (symbol? %)
  59 + (= \$ (first (str %))))
  60 + s (map html2hic* cnts)))]
61 61 (if (map? node)
62 62 (let [{:keys [tag attrs content]} node
63   - tag (mk-tag tag attrs)
64   - attrs (dissoc attrs :class :id)
65   - attrs (attr-solve attrs)
66   - v (if (not (empty? attrs))
67   - [tag attrs] [tag])
68   - cnts (filter #(not (and (string? %)
69   - (re-matches #"\n\s*" %))) content)]
70   - (if (and (= tag *clj-tag*)
71   - (*clj-attr-key* attrs))
72   - (with-open [pbr (-> attrs *clj-attr-key* StringReader. PushbackReader.)]
73   - (let [s (read pbr)]
74   - (cond
75   - (seq? s) (into-it s cnts)
76   - (vector? s) (vec (into-it s cnts));;(reduce conj s (map html2hic* cnts))
77   - (coll? s) (reduce conj s (map html2hic* cnts))
78   - :else s)))
79   - (reduce conj v (map html2hic* cnts))))
  63 + tag (mk-tag tag attrs)
  64 + attrs (dissoc attrs :class :id)
  65 + attrs (attr-solve attrs)
  66 + v (if (not (empty? attrs))
  67 + [tag attrs] [tag])
  68 + cnts (filter #(not (and (string? %)
  69 + (re-matches #"\n\s*" %))) content)]
  70 + (if (and (= tag *clj-tag*)
  71 + (*clj-attr-key* attrs))
  72 + (with-open [pbr (-> attrs *clj-attr-key* StringReader. PushbackReader.)]
  73 + (let [s (read pbr)]
  74 + (cond
  75 + (seq? s) (into-it s cnts)
  76 + (vector? s) (vec (into-it s cnts));;(reduce conj s (map html2hic* cnts))
  77 + (coll? s) (reduce conj s (map html2hic* cnts))
  78 + :else s)))
  79 + (reduce conj v (map html2hic* cnts))))
80 80 node)))
81 81
82 82 (defn- source2s
@@ -84,12 +84,12 @@
84 84 (when-let [v (resolve x)]
85 85 (when-let [filepath (:file (meta v))]
86 86 (with-open [rdr (-> filepath
87   - FileInputStream.
88   - InputStreamReader.
89   - LineNumberReader.)]
90   - (dotimes [_ (-> v meta :line dec)] (.readLine rdr))
91   - (with-open [pbr (PushbackReader. rdr)]
92   - (read pbr))))))
  87 + FileInputStream.
  88 + InputStreamReader.
  89 + LineNumberReader.)]
  90 + (dotimes [_ (-> v meta :line dec)] (.readLine rdr))
  91 + (with-open [pbr (PushbackReader. rdr)]
  92 + (read pbr))))))
93 93
94 94 (defn- html-node? [s]
95 95 (and (vector? s)
@@ -97,23 +97,23 @@
97 97
98 98 (defn- tree-search [pred node]
99 99 (letfn [(inner [s q]
100   - (if-let [v (pred s)]
101   - v
102   - (cond
103   - (map? s)
104   - (let [[fs & res] (reduce conj q
105   - (concat (keys s)
106   - (vals s)))]
107   - (recur fs (vec res)))
108   -
109   - (coll? s)
110   - (let [[fs & res] (reduce conj q s)]
111   - (recur fs (vec res)))
  100 + (if-let [v (pred s)]
  101 + v
  102 + (cond
  103 + (map? s)
  104 + (let [[fs & res] (reduce conj q
  105 + (concat (keys s)
  106 + (vals s)))]
  107 + (recur fs (vec res)))
112 108
113   - :else
114   - (if (empty? q)
115   - nil
116   - (recur (first q) (vec (rest q)))))))]
  109 + (coll? s)
  110 + (let [[fs & res] (reduce conj q s)]
  111 + (recur fs (vec res)))
  112 +
  113 + :else
  114 + (if (empty? q)
  115 + nil
  116 + (recur (first q) (vec (rest q)))))))]
117 117 (inner node [])))
118 118
119 119 (defn- should-be-child? [node]
@@ -123,10 +123,10 @@
123 123 (defn- clj-attr [node]
124 124 (with-out-str
125 125 (pr (let [ans (replace-when should-be-child?
126   - node
127   - (map #(symbol (str "$" %)) (iterate inc 1)))]
128   - (if (vector? node)
129   - (vec ans) ans)))))
  126 + node
  127 + (map #(symbol (str "$" %)) (iterate inc 1)))]
  128 + (if (vector? node)
  129 + (vec ans) ans)))))
130 130
131 131 (defn- attr-code [code]
132 132 (with-out-str
@@ -135,19 +135,18 @@
135 135
136 136 (defn- hic2vec* [node]
137 137 (condp #(%1 %2) node
138   - seq?
139   - (reduce conj
140   - [*clj-tag* {*clj-attr-key* (clj-attr node)}]
141   - (map hic2vec* (filter should-be-child? node)))
  138 + seq? (reduce conj
  139 + [*clj-tag* {*clj-attr-key* (clj-attr node)}]
  140 + (map hic2vec* (filter should-be-child? node)))
142 141 symbol? [*clj-tag* {*clj-attr-key* (str node)}]
143   -
  142 +
144 143 html-node? (vec (map hic2vec* node))
145 144 vector? (reduce conj [*clj-tag* {*clj-attr-key* (clj-attr node)}]
146   - (map hic2vec* (filter should-be-child? node)))
  145 + (map hic2vec* (filter should-be-child? node)))
147 146 map? (reduce conj {}
148   - (map (fn [[k v]]
149   - [(if (keyword? k) k (keyword (attr-code k)))
150   - (if (string? v) v (attr-code v))]) node))
  147 + (map (fn [[k v]]
  148 + [(if (keyword? k) k (keyword (attr-code k)))
  149 + (if (string? v) v (attr-code v))]) node))
151 150 node))
152 151
153 152 (defn- hic2vec [fn-sym-or-s]
@@ -167,46 +166,46 @@
167 166 (defn- list-s [path]
168 167 (let [encoding (enc/detect path :default)]
169 168 (with-open [pbr (-> path
170   - FileInputStream.
171   - (InputStreamReader. encoding)
172   - PushbackReader.)]
  169 + FileInputStream.
  170 + (InputStreamReader. encoding)
  171 + PushbackReader.)]
173 172 (doall (take-while identity
174   - (repeatedly
175   - #(try (read pbr)
176   - (catch java.lang.Exception _
177   - nil))))))))
  173 + (repeatedly
  174 + #(try (read pbr)
  175 + (catch java.lang.Exception _
  176 + nil))))))))
178 177
179 178 (defn- ns2filename [ns-str]
180 179 (let [replaced (.replaceAll ns-str "/" ".")]
181 180 (str *hicv-dir-name*
182   - replaced
183   - ".html")))
  181 + replaced
  182 + ".html")))
184 183
185 184 (defn- get-name [exp]
186 185 (let [expanded (macroexpand exp)]
187 186 (pat/match expanded
188   - v :when (not (coll? v)) nil
189   - [fs x & _] :when (= fs 'def) x
190   - _ nil)))
  187 + v :when (not (coll? v)) nil
  188 + [fs x & _] :when (= fs 'def) x
  189 + _ nil)))
191 190
192 191 (defn- path2ns [path src-path]
193 192 (let [src-path (if (= \/ (last src-path))
194   - src-path
195   - (str src-path \/))
196   - p (Pattern/compile (str src-path "(.*)\\.clj"))
197   - [_ n] (re-matches p path)]
  193 + src-path
  194 + (str src-path \/))
  195 + p (Pattern/compile (str src-path "(.*)\\.clj"))
  196 + [_ n] (re-matches p path)]
198 197 (-> n
199   - (.replaceAll ,,, "_" "-")
200   - (.replaceAll ,,, "/" "."))))
  198 + (.replaceAll ,,, "_" "-")
  199 + (.replaceAll ,,, "/" "."))))
201 200
202 201 (defn- search-hic [src-path]
203 202 (filter (fn [[_ hics]] (not (empty? hics)))
204   - (for [file-path (glob/glob (str src-path "/**/*.clj") :s)]
205   - [(path2ns file-path src-path)
206   - (filter identity
207   - (for [exp (list-s file-path)]
208   - (if-let [n (and (should-be-child? exp) (get-name exp))]
209   - [n exp])))])))
  203 + (for [file-path (glob/glob (str src-path "/**/*.clj") :s)]
  204 + [(path2ns file-path src-path)
  205 + (filter identity
  206 + (for [exp (list-s file-path)]
  207 + (if-let [n (and (should-be-child? exp) (get-name exp))]
  208 + [n exp])))])))
210 209
211 210 (defn- mk-syms [nspace hic-names]
212 211 (map #(symbol (str nspace "/" %)) hic-names))
@@ -215,53 +214,54 @@
215 214 (prepare-hicv-dir!)
216 215 (doseq [[nspace name&exps] (search-hic src-path)]
217 216 (do (with-open [f (-> nspace ns2filename io/writer)]
218   - (doto f
219   - (.write "<hicv />")
220   - (.newLine)
221   - (.newLine)))
222   - (with-open [f (-> nspace ns2filename (io/writer ,,, :append true))]
223   - (doseq [[_ exp] name&exps]
224   - (doto f
225   - (.write (-> exp hic2vec hic/html))
226   - (.newLine)
227   - (.newLine)))))))
  217 + (doto f
  218 + (.write "<hicv />")
  219 + (.newLine)
  220 + (.newLine)))
  221 + (with-open [f (-> nspace ns2filename (io/writer ,,, :append true))]
  222 + (doseq [[_ exp] name&exps]
  223 + (doto f
  224 + (.write (-> exp hic2vec hic/html))
  225 + (.newLine)
  226 + (.newLine)))))))
228 227
229 228 (defn- hic2htmls [src-path targets]
230 229 (prepare-hicv-dir!)
231 230 (doseq [[nspace name&exps] (search-hic src-path)
232   - [nam exp] name&exps]
  231 + [nam exp] name&exps]
233 232 (with-open [f (-> (str nspace "." (name nam)) ns2filename io/writer)]
234 233 (doto f
235   - (.write "<hicv />")
236   - (.newLine)
237   - (.newLine)
238   - (.write (hic/html (hic2vec exp)))
239   - (.newLine)
240   - (.newLine)))))
  234 + (.write "<hicv />")
  235 + (.newLine)
  236 + (.newLine)
  237 + (.write (hic/html (hic2vec exp)))
  238 + (.newLine)
  239 + (.newLine)))))
241 240
242 241 (defn- html2hic [resource]
243 242 (let [encoding (enc/detect resource :default)
244   - nodes (-> resource
245   - FileInputStream.
246   - (InputStreamReader. encoding)
247   - en/html-resource first :content)]
  243 + nodes (-> resource
  244 + FileInputStream.
  245 + (InputStreamReader. encoding)
  246 + en/html-resource first :content)]
248 247 (map html2hic* nodes)))
249 248
250 249 (defn- html2hic-front [file-names]
251   - (doall (map pp/pprint
252   - (filter #(not (and (string? %)
253   - (re-matches #"\n\s*" %)))
254   - (mapcat html2hic (if (empty? file-names)
255   - (-> *hicv-dir-name* io/file .listFiles)
256   - file-names))))))
  250 + (doall
  251 + (map pp/pprint
  252 + (filter #(not (and (string? %)
  253 + (re-matches #"\n\s*" %)))
  254 + (mapcat html2hic (if (empty? file-names)
  255 + (-> *hicv-dir-name* io/file .listFiles)
  256 + file-names))))))
257 257
258 258 (defn hicv
259 259 [project & [first-arg & rest-args]]
260 260 (condp = first-arg
261   - "2html" (hic2html (:source-path project) (:target-hiccup project))
262   - "2htmls" (hic2htmls (:source-path project) (:target-hiccup project))
263   - "2hic" (html2hic-front rest-args)
264   - (println "Usage:
  261 + "2html" (hic2html (:source-path project) (:target-hiccup project))
  262 + "2htmls" (hic2htmls (:source-path project) (:target-hiccup project))
  263 + "2hic" (html2hic-front rest-args)
  264 + (println "Usage:
265 265 lein hicv 2html
266 266 lein hicv 2htmls
267 267 lein hicv 2hic\n")))

0 comments on commit 85b4954

Please sign in to comment.
Something went wrong with that request. Please try again.