Skip to content

Instantly share code, notes, and snippets.

@seanedwards
Created December 1, 2013 20:31
Show Gist options
  • Select an option

  • Save seanedwards/7740349 to your computer and use it in GitHub Desktop.

Select an option

Save seanedwards/7740349 to your computer and use it in GitHub Desktop.
#!/usr/bin/env scala
import scala.xml._
val pattern = args(0);
val data =
if (args.size == 2)
XML.load(args(1));
else
XML.load(System.in);
def highlight(e : NodeSeq) : NodeSeq = {
e.flatMap(_ match {
case text : Text =>
val highlighted = <span style="background-color: blue; color: white">{pattern}</span>
text.data.split(pattern).map(Text(_)).flatMap(List(highlighted, _)).tail
case elem : Elem =>
elem.copy(child=highlight(elem.child))
case other => other
})
}
val newdata = data.copy(child=
data.child.filter({
case elem : Elem => elem.label != "body"
case other => true
}) ++
highlight(data \\ "body")
);
println(newdata)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment