Created
December 1, 2013 20:31
-
-
Save seanedwards/7740349 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #!/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