Permalink
Browse files

attribute syntax

  • Loading branch information...
1 parent 843f4f5 commit e314a7bceadc3c1f01f212388262d483a11f2a75 @mmikulicic committed Mar 29, 2010
Showing with 40 additions and 3 deletions.
  1. +7 −0 README.md
  2. +31 −3 src/haml_macro/core.clj
  3. +2 −0 views/layouts/application.haml
View
7 README.md
@@ -1,3 +1,10 @@
+INTRODUCTION:
+
+This is a Haml implementation for clojure. Currently the main target is the compojure framework.
+
+Wherever haml expects ruby expressions, here you can use clojure expressions. In some cases ruby compatibility is maintained as much as possible,
+especially to enable cut&paste of existing Haml templates from/to the ruby world.
+
USAGE:
(ns example)
View
34 src/haml_macro/core.clj
@@ -33,6 +33,8 @@
(def newlinep (one-of "\n"))
(def sspace (one-of " "))
+; TODO: fix this: accept colons
+(def xmlTagName baseIdentifier)
;; parser
@@ -61,10 +63,35 @@
(let [autoTag (if (not= \% prefix) "div")]
(result (keyword (apply str autoTag (if (not= \% prefix) prefix) rest))))))
-(defn make-compojure-tag [t inline body]
- (apply vector (filter not-nil? (apply vector t inline body))))
+(defn make-compojure-tag [t attrs inline body]
+ (apply vector (filter not-nil? (apply vector t attrs inline body))))
+
+(defn quotedString [ch]
+ (between (is-char ch) (is-char ch) (many (not-char ch))))
+
+(def hamlStringLiteral
+ (stringify (lexeme (either (quotedString \') (quotedString \")))))
+
+(def rubyAttrPair (let-bind [name (lexeme (either hamlStringLiteral (>> (string ":") baseIdentifier)))
+ _ (lexeme (string "=>"))
+ value hamlStringLiteral]
+ (result {(keyword name) value})))
+
+(def rubyAttrList (>>== (braces (sepBy rubyAttrPair comma))
+ #(apply merge %)))
+
+(def htmlAttrPair (let-bind [name (lexeme xmlTagName)
+ _ (lexeme (string "="))
+ value hamlStringLiteral]
+ (result {(keyword name) value})))
+
+(def htmlAttrList (>>== (parens (sepBy htmlAttrPair sspace))
+ #(apply merge %)))
+
+(def attrList (either rubyAttrList htmlAttrList))
+
(def inlineTag (let-bind [p (not-one-of " \n")
rest (text 0)]
(result (apply str p rest))))
@@ -75,10 +102,11 @@
(defn tag [l]
(let-bind [t tagName
+ attrs (optional attrList)
_ (many sspace)
inline (optional inlineTag)
rest (optional (tagBody l))]
- (result (make-compojure-tag t inline rest))))
+ (result (make-compojure-tag t attrs inline rest))))
(defn statements [l] (followedBy (sepBy1 (statement l) newlinep) (optional newlinep)))
View
2 views/layouts/application.haml
@@ -1,3 +1,5 @@
%html
+ %head
+ %link{:href=>"/static/stylesheets/application.css", :media=>"all", :rel=>"stylesheet", :type=>"text/css"}
%body
= yield

0 comments on commit e314a7b

Please sign in to comment.