Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Moves slim.clj off clojure.contrib, reformats for readability

  • Loading branch information...
commit a931356c71e10045a27be0cb299ae33327f7d372 1 parent 9652380
@devn devn authored
Showing with 27 additions and 21 deletions.
  1. +27 −21 src/slim/core.clj
View
48 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)
@@ -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 []) '()))
Please sign in to comment.
Something went wrong with that request. Please try again.