Created
July 4, 2012 05:12
-
-
Save kaja47/3045478 to your computer and use it in GitHub Desktop.
anti-xml
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
import com.codecommit.antixml._ | |
class NodeOps[+A <: Node](node: A) { | |
def \@(attr: String)(implicit ev: A <:< Elem): String = node.attrs get attr getOrElse "" | |
def \@(attr: Symbol)(implicit ev: A <:< Elem): String = this \@ attr.name | |
def fulltext(implicit ev: A <:< Elem) = node \\ text mkString " " replaceAll ("\\s+", " ") trim | |
} | |
implicit def pimpNode[A <: Node](node: A): NodeOps[A] = new NodeOps(node) | |
class GroupOps[+A <: Node](group: Group[A]) { | |
def \?(f: A => Boolean): Group[A] = group.filter(f) | |
def \@(attr: String)(implicit ev: A <:< Elem): Seq[String] = group.flatMap(_.attrs get attr) | |
def \@(attr: Symbol)(implicit ev: A <:< Elem): Seq[String] = this \@ attr.name | |
def fulltext = group \\ text mkString " " replaceAll ("\\s+", " ") trim | |
} | |
implicit def pimpGroup[A <: Node](group: Group[A]): GroupOps[A] = new GroupOps(group) | |
// methods \, \\, \@ and \? has same precedence, you don't need to fuck around with parens | |
html \\ 'div \? (_ \@ 'class == "post") \\ 'span \? (_ \@ 'id startsWith "post_id_") | |
// //div[@class="post"]//div[starts-with(@id, "post_id_")] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment