Commit 76f458b3 authored by David Santiago's avatar David Santiago

Initial commit, with quoin.text and tests.

# quoin
I'm an app. Or maybe I'm a library? I haven't decided yet.
The choice is up to you!
## Usage
## License
Copyright © 2012 FIXME
Distributed under the Eclipse Public License, the same as Clojure.
(defproject quoin "0.1.0-SNAPSHOT"
:description "Template engine support and utilities"
:url ""
:license {:name "Eclipse Public License"
:url ""}
:dependencies [[org.clojure/clojure "1.3.0"]])
(ns quoin.core)
(defn -main
"I don't do a whole lot."
[& args]
(println "Hello, World!"))
(ns quoin.text
"Functions that process text.")
(defn html-escape
"HTML-escapes the given string."
[^String s]
;; This method is "Java in Clojure" for serious speedups.
(let [sb (StringBuilder.)
slength (long (count s))]
(loop [idx (long 0)]
(if (>= idx slength)
(.toString sb)
(let [c (char (.charAt s idx))]
(case c
\& (.append sb "&")
\< (.append sb "&lt;")
\> (.append sb "&gt;")
\" (.append sb "&quot;")
(.append sb c))
(recur (inc idx)))))))
(defn indent-string
"Given a String s, indents each line by inserting the string indentation
at the beginning."
[^String s ^String indentation]
(let [str+padding (StringBuilder.)]
(loop [start-idx 0
next-idx (.indexOf s "\n")] ;; \n handles both \r\n & \n linebreaks.
(if (= -1 next-idx)
;; We've reached the end. If the start and end are the same, don't
;; indent before an empty string. Either way, return the string.
(do (when (not= start-idx (count s))
(.append str+padding indentation)
(.append str+padding s start-idx (count s)))
(.toString str+padding))
(let [next-idx (inc next-idx)]
(.append str+padding indentation)
(.append str+padding s start-idx next-idx)
(recur next-idx (.indexOf s "\n" next-idx)))))))
(ns quoin.test.text
(:use clojure.test
(deftest test-html-escape
(is (= "&lt;script&gt;"
(html-escape "<script>")))
(is (= "&amp;lt;script&amp;gt;"
(html-escape (html-escape "<script>"))))
(is (= "&lt;script src=&quot;blah.js&quot;&gt;"
(html-escape "<script src=\"blah.js\">"))))
(deftest test-indent-string
(is (= " blah\n blah")
(indent-string "blah\nblah" " "))
(is (= " blah\r\n blah"
(indent-string "blah\r\nblah" " ")))
(is (= " blah"
(indent-string "blah" " ")))
;; Shouldn't indent a non-existing last line when string ends on \n.
(is (= " blah\n"
(indent-string "blah\n" " "))))
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment