Skip to content

Instantly share code, notes, and snippets.

@ababup1192
Created August 19, 2021 07:23
Show Gist options
  • Save ababup1192/56dc15d12a7b5db3e43d664f5e3a13f0 to your computer and use it in GitHub Desktop.
Save ababup1192/56dc15d12a7b5db3e43d664f5e3a13f0 to your computer and use it in GitHub Desktop.
import Dependencies._
ThisBuild / scalaVersion := "2.13.6"
ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / organization := "com.example"
ThisBuild / organizationName := "example"
lazy val root = (project in file("."))
.settings(
name := "Scala Seed Project",
libraryDependencies ++= Seq(
scalaTest % Test,
"org.scala-lang.modules" %% "scala-xml" % "2.0.1"
)
)
// See https://www.scala-sbt.org/1.x/docs/Using-Sonatype.html for instructions on how to publish to Sonatype.
package example
import scala.xml._
import scala.xml.transform._
object Hello extends App {
val xml = """
|<root>
|<p><a href="http://foo@gmail.com">abc</a></p>
|<p><a href="http://foo.com">abc</a></p>
|<h1>
| <p><a href="http://foo@gmail.com">abc</a></p>
|</h1>
|</root>
""".stripMargin
val rule = new RuleTransformer(new RewriteRule {
override def transform(n: Node): NodeSeq = n match {
case e: Elem if (e \\ "p" \\ "a" \@ "href").contains("@") =>
<p>{(e \\ "p" \ "a").text}</p>
case _ => n
}
})
println(rule.transform(XML.loadString(xml)))
//<root>
//<p>abc</p>
//<p><a href="http://foo.com">abc</a></p>
//<h1>
// <p>abc</p>
//</h1>
//</root>
}
@ababup1192
Copy link
Author

abcefghi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment