Permalink
Browse files

Merge pull request #1 from devn/master

Updated to Clojure 1.4.0, reformatted
  • Loading branch information...
2 parents 7180ee9 + 354f0b7 commit fa65f9b96eaadb927815c3bcb2b6b2223074951f @chaslemley committed Aug 7, 2012
Showing with 71 additions and 72 deletions.
  1. +5 −1 .gitignore
  2. +6 −4 project.clj
  3. BIN slim-0.1.3.jar
  4. +27 −21 src/slim/core.clj
  5. +33 −46 test/slim/test/core.clj
View
@@ -1,6 +1,10 @@
+.lein-deps-sum
+.lein-failures
+target/*
.DS_Store
pom.xml
lib
classes
.cake
-build
+build
+*.jar
View
@@ -1,5 +1,7 @@
-(defproject slim "0.1.3"
+(defproject devn/slim "0.1.5"
:description "Simple HTML Templating for Clojure"
- :dependencies [[org.clojure/clojure "1.2.0"]
- [org.clojure/clojure-contrib "1.2.0"]
- [hiccup "0.3.0"]])
+ :url "http://github.com/devn/slim.clj"
+ :license {:name "Eclipse Public License"
+ :url "http://www.eclipse.org/legal/epl-v10.html"}
+ :dependencies [[org.clojure/clojure "1.4.0"]
+ [hiccup "1.0.0"]])
View
Binary file not shown.
View
@@ -1,5 +1,6 @@
(ns slim.core
- (:use [clojure.contrib.duck-streams :only (read-lines)]))
+ (:require [clojure.java.io :as io]
+ [clojure.string :as s]))
(defstruct html-node :depth :interpreted_type :interpreted :selector :attributes :content)
@@ -14,16 +15,21 @@
content] (re-find #"^(!!!)?([\s]*)([-=;|]?)([\w]*)([(](.*?)[)])?(.*)$" line)]
(if (or (= (.trim full-text) "") (= interpreted ";"))
acc
- (conj acc (struct-map html-node :depth (count leading_whitespace),
- :interpreted_type interpreted,
- :interpreted (or (= interpreted "=") (= interpreted "-")),
- :directive? (= directive "!!!"),
- :selector selector,
- :attributes attributes,
- :content (.trim content))))))
+ (conj acc (struct-map html-node
+ :depth (count leading_whitespace),
+ :interpreted_type interpreted,
+ :interpreted (or (= interpreted "=") (= interpreted "-")),
+ :directive? (= directive "!!!"),
+ :selector selector,
+ :attributes attributes,
+ :content (.trim content))))))
+
+(defn- read-lines [f]
+ (with-open [f (io/reader (io/file f))]
+ (doall (line-seq f))))
(defn- process-file [file-name line-accum]
- (reduce process-line line-accum (clojure.contrib.duck-streams/read-lines file-name)))
+ (reduce process-line line-accum (read-lines file-name)))
(def self-closing-tags
#{"meta" "br" "link"})
@@ -56,20 +62,20 @@
(if (empty? data)
(reduce #(str %1 (closing-tag %2)) "" stack)
(let [current (first data)]
- (if (<= (get current :depth -1) (get (first stack) :depth -2))
- (str (closing-tag (first stack)) (write-html data (rest stack)))
- (if (current :directive?)
- (str (directive current) (write-html (rest data) stack))
- (if (current :interpreted)
- (if (= "=" (current :interpreted_type))
- (str (interpreted-clojure current) (write-html (rest data) stack))
- (do (interpreted-clojure current) (str (write-html (rest data) stack))))
- (if (= (current :interpreted_type) "|")
- (str (content current) " " (write-html (rest data) stack))
- (str (opening-tag current) (content current) (write-html (rest data) (conj stack current))))))))))
+ (if (<= (get current :depth -1) (get (first stack) :depth -2))
+ (str (closing-tag (first stack)) (write-html data (rest stack)))
+ (if (current :directive?)
+ (str (directive current) (write-html (rest data) stack))
+ (if (current :interpreted)
+ (if (= "=" (current :interpreted_type))
+ (str (interpreted-clojure current) (write-html (rest data) stack))
+ (do (interpreted-clojure current) (str (write-html (rest data) stack))))
+ (if (= (current :interpreted_type) "|")
+ (str (content current) " " (write-html (rest data) stack))
+ (str (opening-tag current) (content current) (write-html (rest data) (conj stack current))))))))))
(defn render-template [template & data]
(use 'hiccup.core)
(intern *ns* 'locals (fn [s] (get (first data) s)))
-
+
(write-html (process-file template []) '()))
View
@@ -2,65 +2,52 @@
(:use [slim.core] :reload)
(:use [clojure.test]))
-(deftest slim
- (is (=
- (render-template "test/samples/basic.slim.html")
- "<h1>Hello Chas</h1>"))
+(defn- path-to [filename]
+ (str "test/samples/" filename))
- (is (=
- (render-template "test/samples/basic_with_attr.slim.html")
- "<h1 class=\"my_class\" id=\"my_id\">Hello Chas</h1>"))
+(deftest slim
+ (is (= (render-template (path-to "basic.slim.html"))
+ "<h1>Hello Chas</h1>"))
- (is (=
- (render-template "test/samples/basic_with_delimeter.slim.html")
- "<p>Lorem Ipsum Dolor Sit </p>"))
+ (is (= (render-template (path-to "basic_with_attr.slim.html"))
+ "<h1 class=\"my_class\" id=\"my_id\">Hello Chas</h1>"))
- (is (=
- (render-template "test/samples/nested_tags.slim.html")
- "<div class='chas'><p>Lorem Ipsum</p></div>"))
+ (is (= (render-template (path-to "basic_with_delimeter.slim.html"))
+ "<p>Lorem Ipsum Dolor Sit </p>"))
- (is (=
- (render-template "test/samples/self_closing_tags.slim.html")
- "<br /><meta name=\"keyword\" content=\"clojure, templating, slim\" />"))
+ (is (= (render-template (path-to "nested_tags.slim.html"))
+ "<div class='chas'><p>Lorem Ipsum</p></div>"))
- (is (=
- (render-template "test/samples/comments_and_empty_lines.slim.html")
- "<p>My Paragraph</p>"))
+ (is (= (render-template (path-to "self_closing_tags.slim.html"))
+ "<br /><meta name=\"keyword\" content=\"clojure, templating, slim\" />"))
- (is (=
- (render-template "test/samples/multiple_depths.slim.html")
- "<html><head><title>The Title</title></head><body><div><p>Welcome to Helloworldian</p></div></body></html>"))
+ (is (= (render-template (path-to "comments_and_empty_lines.slim.html"))
+ "<p>My Paragraph</p>"))
- (is (=
- (render-template "test/samples/embedded_clojure.slim.html")
- "<h1>Hello, Chas</h1>"))
-)
+ (is (= (render-template (path-to "multiple_depths.slim.html"))
+ "<html><head><title>The Title</title></head><body><div><p>Welcome to Helloworldian</p></div></body></html>"))
+ (is (= (render-template (path-to "embedded_clojure.slim.html"))
+ "<h1>Hello, Chas</h1>")))
(deftest with_hiccup
- (is (=
- (render-template "test/samples/hiccup_integration.slim.html")
- "<div><h1 class=\"heading cats\" id=\"garfield\">Hello World</h1></div>"))
-)
+ (is (= (render-template (path-to "hiccup_integration.slim.html"))
+ "<div><h1 class=\"heading cats\" id=\"garfield\">Hello World</h1></div>")))
(deftest with_directive
- (is (=
- (render-template "test/samples/directive.slim.html")
- "<!DOCTYPE HTML><html><head></head><body></body></html>"))
-)
+ (is (= (render-template (path-to "directive.slim.html"))
+ "<!DOCTYPE HTML><html><head></head><body></body></html>")))
(deftest with-xml_directive
- (is (=
- (render-template "test/samples/basic.slim.xml")
- "<?xml version='1.0' encoding='utf-8' ?><response><content>Hello World</content></response>"))
-)
+ (is (= (render-template (path-to "basic.slim.xml"))
+ "<?xml version='1.0' encoding='utf-8' ?><response><content>Hello World</content></response>")))
(deftest with-passed-args
- (is (=
- (render-template "test/samples/passed-args.slim.html" {:paragraph "Hello Passed Args"})
- "<div><p>Hello Passed Args</p></div>"))
-
- (is (=
- (render-template "test/samples/advanced_pass-args.slim.html" {:title "My Blog Title" :content "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."})
- "<!DOCTYPE HTML><html><head><title>My Blog Title</title></head><body><h1>My Blog Title</h1><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p></body></html>"))
-)
+ (is (= (render-template (path-to "passed-args.slim.html")
+ {:paragraph "Hello Passed Args"})
+ "<div><p>Hello Passed Args</p></div>"))
+
+ (is (= (render-template (path-to "advanced_pass-args.slim.html")
+ {:title "My Blog Title"
+ :content "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."})
+ "<!DOCTYPE HTML><html><head><title>My Blog Title</title></head><body><h1>My Blog Title</h1><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p></body></html>")))

0 comments on commit fa65f9b

Please sign in to comment.