Small util for transforming xhtml to html DSL

38 views
Skip to first unread message

Robin Brandt

unread,
Jun 27, 2009, 3:58:06 PM6/27/09
to Compojure
Hi all,

first of all thanks for your work on clojure. It's a lot of fun to
hack on it!

Maybe somebody has the same problem and can use the following code as
a start: I wanted to transform an XHTML-template to the HTML DSL
that's coming with clojure. Luckily everything is available in the
base or contrib libs so it wasn't that hard to do.

(ns de.evernet2000.util
(:use clojure.contrib.str-utils)
(:use clojure.contrib.duck-streams)
(:use clojure.contrib.pprint)
(:use [clojure.xml :only (parse)])
(:import (java.io File)))

(defn format-attrs
[m]
(when m
(format "%s" m)))

(defn empty-when-null
[x]
(if (nil? x)
""
x))

(declare format-full-node)

(defn format-node
[node]
(cond
(string? node) (format "\"%s\"" (.trim node))
(nil? node) nil
:else (format-full-node node)))

(defn format-full-node
[node]
(format "[%s %s %s]\n"
(:tag node)
(empty-when-null (format-attrs (:attrs node)))
(str-join " " (map format-node (:content node)))))

(defn transform-file
[filename]
(print (pprint (read-string (format-node (parse filename))))))

Cheers,
Robin

James Reeves

unread,
Jun 29, 2009, 3:39:06 PM6/29/09
to Compojure
On Jun 27, 8:58 pm, Robin Brandt <robin.bra...@gmail.com> wrote:
> Maybe somebody has the same problem and can use the following code as
> a start: I wanted to transform an XHTML-template to the HTML DSL
> that's coming with clojure.

You mean Compojure? Although I guess the same HTML syntax has been
adopted by clojure.contrib.prxml.

This looks pretty useful. Thanks for taking the time to write it.

- James
Reply all
Reply to author
Forward
0 new messages