Skip to content

Commit

Permalink
version 0.2.1
Browse files Browse the repository at this point in the history
Updated to be used with leiningen
  • Loading branch information
antoniogarrote committed Dec 8, 2009
1 parent 46da963 commit ece6b50
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 410 deletions.
32 changes: 22 additions & 10 deletions README
Expand Up @@ -2,15 +2,26 @@ clj-haml is small HTML template library for Clojure inspired by the Ruby templat

== Installation

- Set up Clojure POM (http://github.com/technomancy/clojure-pom)
- Clone the project
- Compile, test, install with maven:
- clj-haml now uses Leiningen as a bulding tool and Clojars to manage the jar dependencies.
Take a look at http://clojars.org/ and http://github.com/technomancy/leiningen for more
information.
- clj-haml can be used with maven from the following repository clojars repository, take a look
at the included POM file.

mvn process-resources ; # <- now your are ready for emacs slime
mvn compile ; # <- see it compile your clojure code
mvn test ; # <- see it test your clojure code
mvn install ; # <- install a jar for other components
mvn assembly:assembly ; # <- build one big jar
<dependency>
<groupId>clj-haml</groupId>
<artifactId>clj-haml</artifactId>
<version>0.2.1-SNAPSHOT</version>
</dependency>

- The Leiningen dependecy can be included in you leiningen project with the following dep:

[clj-haml "0.2.1-SNAPSHOT"]

- if you have installed Leiningen you can build and test clj-haml with the following commands:

$lein compile
$lein test

== Use

Expand Down Expand Up @@ -66,5 +77,6 @@ Some tests are included in the source file. You can run them with the command 'm

== Changelog

- version 0.1 (02/28/2009): First version.
- version 0.2 (10/05/2009): Updated to last version of clojure. Clojure POM ready.
- version 0.1.0 (02/28/2009): First version.
- version 0.2.0 (10/05/2009): Updated to last version of clojure. Clojure POM ready.
- version 0.2.1 (10/05/2009): Updated to Leiningen and clojars infrastrcture.
Binary file removed clj-haml-0.2.0-SNAPSHOT.jar
Binary file not shown.
78 changes: 0 additions & 78 deletions pom.xml

This file was deleted.

1 change: 1 addition & 0 deletions project.clj
@@ -0,0 +1 @@
(defproject clj-haml "0.2.1-SNAPSHOT" :description "#haml style template library for Clojure" :dependencies [[org.clojure/clojure "1.1.0-alpha-SNAPSHOT"] [org.clojure/clojure-contrib "1.0-SNAPSHOT"]])
147 changes: 147 additions & 0 deletions src/clj_haml.clj
@@ -0,0 +1,147 @@
(comment
"HAML like templating library"
)


;; @author Antonio Garrote Hernandez
;;
;; This is free software available under the terms of the
;; GNU Lesser General Public License
;;

(ns clj-haml)

(defn keyword-to-string
"Transforms a keyword into a string minus ':'"
([kw] (. (str kw) (substring 1 (. (str kw) length)))))

(defmacro h=
"Main Haml function"
([& params]
(let [elems (if (keyword? (first params))
(let [kws (keyword-to-string (first params))]
(if (and (= -1 (. kws (indexOf "#")))
(= -1 (. kws (indexOf "."))))
{:tag kws :class nil :id nil}
(if (and (= -1 (. kws (indexOf "#")))
(not (= -1 (. kws (indexOf ".")))))
(let [parts (. kws (split "\\."))
tag (if (= (first parts) "") "div" (first parts))
class (loop [classes (rest parts)
class_txt ""]
(if (empty? classes)
class_txt
(recur (rest classes)
(str class_txt " " (first classes)))))]
{:tag tag :class class :id nil})
(if (and (not (= -1 (. kws (indexOf "#"))))
(= -1 (. kws (indexOf "."))))
(let [parts (. kws (split "#"))
tag (if (= (first parts) "") "div" (first parts))
id (second parts)]
{:tag tag :class nil :id id})
(let [idi (. kws (indexOf "#"))
idc (. kws (indexOf "."))]
(if (> idi idc)
(let [partsc (. kws (split "\\."))
tag (first partsc)]
(loop [other-classes (rest partsc)
the-classes ""
the-id ""]
(if (empty? other-classes)
{:tag tag :class the-classes :id the-id}
(let [this-class (first other-classes)
this-idi (. this-class (indexOf "#"))]
(if (= -1 this-idi)
(recur (rest other-classes)
(str the-classes " " this-class)
the-id)
(let [this-class-parts (. this-class (split "#"))]
(recur (rest other-classes)
(str the-classes " " (first this-class-parts))
(second this-class-parts))))))))
(let [partsc (. kws (split "#"))
tag (first partsc)
restc (. (second partsc) (split "\\."))
id (first restc)
class (loop [classes (rest restc)
class_txt ""]
(if (empty? classes)
class_txt
(recur (rest classes)
(str class_txt " " (first classes)))))]
{:tag tag :class class :id id}))))))))

tag (:tag elems)
id (:id elems)
class (:class elems)

attrs (loop [parts params]
(if (empty? parts)
nil
(let [this-part (first parts)]
(if (map? this-part)
this-part
(recur (rest parts))))))
;; `[~tag, ~id, ~class, ~attrs])))
self-closed? (let [last-param (last params)]
(if (symbol? last-param)
(if (= (str last-param) "/") true false)
false))
content (loop [looking params
acum ""]
(if (empty? looking)
acum
(let [maybe-content (first looking)]
(if (or (keyword? maybe-content)
(and (symbol? maybe-content)
(= \. (first (str maybe-content))))
(and (symbol? maybe-content)
(= \# (first (str maybe-content))))
(map? maybe-content))
(recur (rest looking)
acum)
(recur (rest looking)
(str acum (eval maybe-content)))))))
pre (str "<" tag
(when (not (nil? id))
(str " id='" id "'"))
(when (not (nil? class))
(str " class='" (. class (substring 1)) "'"))
(loop [attr-keys (keys attrs)
acum ""]
(if (not (empty? attr-keys))
(let [this-key (first attr-keys)
this-val (get attrs this-key)]
(recur (rest attr-keys)
(str acum " " (keyword-to-string this-key) "='" (eval this-val) "'")))
acum)))]
(if self-closed?
(str pre "/>")
(str pre ">" content "</" tag ">")))))


(defn h-file
"Loads and parses a Haml template"
([path]
(load-file path)))

(defmacro with-haml-template [template format & bindings]
`(binding [~@(first bindings)]
(apply h-file [(str ~template "." (keyword-to-string ~format) ".haml")])))

(defmacro !!! [& args]
(if (= :xml (first args))
(if (string? (second args))
`(str "<?xml version='1.0' encoding='" ~(second args) "' ?>"
~@(drop 2 args))
`(str "<?xml version='1.0' encoding='utf-8' ?>"
~@(rest args)))
(if (float? (first args))
`(str "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML " ~(first args) " Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"
~@(rest args))
(if (= "Strict" (str (first args)))
`(str "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"
~@(rest args))
`(str "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"
~@args)))))

0 comments on commit ece6b50

Please sign in to comment.