Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to Clojure 1.4.0, reformatted #1

Merged
merged 7 commits into from Aug 7, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
@@ -1,6 +1,10 @@
.lein-deps-sum
.lein-failures
target/*
.DS_Store
pom.xml
lib
classes
.cake
build
build
*.jar
10 changes: 6 additions & 4 deletions project.clj
@@ -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"]])
Binary file removed slim-0.1.3.jar
Binary file not shown.
48 changes: 27 additions & 21 deletions src/slim/core.clj
@@ -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)

Expand All @@ -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"})
Expand Down Expand Up @@ -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 []) '()))
79 changes: 33 additions & 46 deletions test/slim/test/core.clj
Expand Up @@ -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>")))