Skip to content

Instantly share code, notes, and snippets.

@kaja47
Created July 4, 2012 05:12
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 kaja47/3045478 to your computer and use it in GitHub Desktop.
Save kaja47/3045478 to your computer and use it in GitHub Desktop.
anti-xml
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