Created
September 13, 2009 19:42
-
-
Save chrislewis/186301 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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