Skip to content

Commit

Permalink
Remove all usages of contrib.
Browse files Browse the repository at this point in the history
  • Loading branch information
technomancy authored and paraseba committed Oct 21, 2011
1 parent 56d51eb commit c58a753
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 29 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
:description "Generate CSS from clojure code. Like an embedded sass."
:url "http://github.com/paraseba/cssgen"
:dependencies [[org.clojure/clojure "1.2.0"]
[org.clojure/clojure-contrib "1.2.0"]]
[org.clojure/algo.generic "0.1.0-SNAPSHOT"]]
:dev-dependencies [[vimclojure/server "2.2.0"]])
32 changes: 16 additions & 16 deletions src/cssgen.clj
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
(ns cssgen
(:require (clojure.contrib [string :as s]
[strint :as strint]
[io :as io]
[def :as ccdef]))
(:require [clojure.string :as s]
[clojure.java.io :as io])
(:use [cssgen.types :only (repr)]))


(defprotocol Container
(nest [child parent])
(add-properties [this new-props])
Expand All @@ -30,7 +27,7 @@
(defn- properties [x] (:properties x))
(defn- rules [x] (:rules x))
(defn- selector [x] (:selector x))
(ccdef/defvar- empty-mixin (Mixin. [] []))
(def ^{:private true} empty-mixin (Mixin. [] []))
(defn- empty-rule [selector] (Rule. selector [] []))

(defn- container? [x]
Expand All @@ -52,21 +49,22 @@

(defn- rule-css [rule]
(letfn [(format-prop [prop]
(let [vals (s/join " " (map repr (next prop)))]
(strint/<< " ~(repr (first prop)): ~{vals};")))
(let [key (repr (first prop))
vals (s/join " " (map repr (next prop)))]
(format " %s: %s;" key vals)))

(format-props [props]
(let [lines (map format-prop props)]
(s/join "\n" lines)))

(nest-single-selector [parent child]
(if (s/substring? "&" child)
(s/replace-str "&" parent child)
(if (.contains child "&")
(.replace child "&" parent)
(str parent (if-not (s/blank? parent) " ") child)))

(nest-selector [parent child]
(let [parents (s/split #"," (or parent ""))
children (s/split #"," (or child ""))]
(let [parents (s/split (or parent "") #",")
children (s/split (or child "") #",")]
(s/join ", " (for [p parents c children]
(nest-single-selector (s/trim p) (s/trim c))))))

Expand All @@ -75,15 +73,17 @@
properties (properties rule)
children (rules rule)
nested-selector (nest-selector parent-selector selector)
parent-css (strint/<< "~{nested-selector} {\n~(format-props properties)\n}\n")
children-css (s/join "\n" (map #(child-rule-css % nested-selector) children))]
parent-css (format "%s {\n%s\n}\n"
nested-selector (format-props properties))
children-css (s/join "\n" (for [c children]
(child-rule-css c nested-selector)))]
(str parent-css children-css)))]

(child-rule-css rule nil)))

(defn css [& rules]
(s/map-str rule-css rules))
(apply str (map rule-css rules)))

(defn css-file [path & rules]
(io/spit path (apply css rules)))
(spit path (apply css rules)))

26 changes: 15 additions & 11 deletions src/cssgen/types.clj
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
(ns cssgen.types
(:require
(clojure.contrib [string :as s]
[def :as ccdef])
[clojure.contrib.generic.arithmetic :as generic]))
(:require [clojure.string :as s]
[clojure.algo.generic.arithmetic :as generic]))

;; copied from contrib
(defn- as-str [x]
(if (instance? clojure.lang.Named x)
(name x)
(str x)))

(defprotocol Value
(repr [x]))

(defrecord Length [mag unit]
Value
(repr [_] (str (s/as-str mag) (s/as-str unit))))
(repr [_] (str (as-str mag) (as-str unit))))

(defrecord Color [r g b]
Value
Expand Down Expand Up @@ -37,19 +41,19 @@
([r g b] (letfn [(limit [x] (max 0 (min x 255)))]
(Color. (limit r) (limit g) (limit b))))

([string] (letfn [(remove-number-sign [s] (s/replace-first-re #"#" "" s))
([string] (letfn [(remove-number-sign [s] (s/replace-first s #"#" ""))
(duplicate [s] (if (= (.length s) 3) (apply str (interleave s s)) s))]
(let [components (->> string s/as-str remove-number-sign duplicate (re-seq #".."))
(let [components (->> string as-str remove-number-sign duplicate (re-seq #".."))
[r g b] (map #(Integer/parseInt % 16) components)]

(make-color r g b)))))


(defn- make-length [mag unit]
{:pre [(number? mag)]}
(Length. mag (s/as-str unit)))
(Length. mag (as-str unit)))

(ccdef/defmacro- def-length-constr [name]
(defmacro ^{:private true} def-length-constr [name]
`(defn ~name [x#] (make-length x# ~(keyword name))))

(def-length-constr em)
Expand Down Expand Up @@ -95,7 +99,7 @@
(make-length ((generic/qsym generic /) ma num) ua))


(ccdef/defmacro- compwise-col-col-op [sym f]
(defmacro ^{:private true} compwise-col-col-op [sym f]
(let [f f]
`(defmethod ~sym [Color Color]
[{ra# :r ga# :g ba# :b} {rb# :r gb# :g bb# :b}]
Expand All @@ -111,7 +115,7 @@
((generic/qsym generic /) ba bb)))


(ccdef/defmacro- compwise-col-num-op [sym f]
(defmacro ^{:private true} compwise-col-num-op [sym f]
(let [f f]
`(do
(defmethod ~sym [Color Number]
Expand Down
2 changes: 1 addition & 1 deletion test/operations.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(:use
cssgen cssgen.types
clojure.test
[clojure.contrib.generic.arithmetic :only (+ - * /)]))
[clojure.algo.generic.arithmetic :only (+ - * /)]))

(def all-lengths [em ex px in cm mm pt pc % deg])

Expand Down

0 comments on commit c58a753

Please sign in to comment.