Skip to content

Instantly share code, notes, and snippets.

@chrislewis
Created September 13, 2009 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrislewis/186301 to your computer and use it in GitHub Desktop.
Save chrislewis/186301 to your computer and use it in GitHub Desktop.
trait XmlRenderer {
val handlers = Map(
"span" -> ((n: Node) =>
<handled>
<div class="boink">
{n.label}
</div>
</handled>
)
)
def render(xml: Node, depth: Int): NodeSeq = xml.child.map((n) => {
processNode(n) match {
case Some(node) => node
case None => Elem(n.prefix, n.label, n.attributes, n.scope, render(n, depth + 1):_*)
}
})
/**
* Process a node, resulting in a new node or not.
*/
def processNode(n: Node): Option[Node] = n match {
case Text(text) => Some(n)
case _ => handlers.get(n.label) match {
case Some(f) => Some(f(n))
case None => None
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment