Skip to content

Instantly share code, notes, and snippets.

@avantgardnerio
Created April 12, 2016 17:11
Show Gist options
  • Save avantgardnerio/62bc1678377e527545edd0f04972c1f3 to your computer and use it in GitHub Desktop.
Save avantgardnerio/62bc1678377e527545edd0f04972c1f3 to your computer and use it in GitHub Desktop.
HTML packer in a few lines of scala
// TODO: Replace with an off-the-shelf Java HTML packer
val doc = Jsoup.parse(html)
doc.select("link") // Replace CSS
.filter(link => "stylesheet".equalsIgnoreCase(link.attr("rel")))
.filter(link => new URI(link.attr("href")).getHost == null)
.foreach(link => {
val el = new Element(Tag.valueOf("style"), "")
val css = getResourceAsString(new URI(link.attr("href")).getPath)
val m = cssUrlPattern.matcher(css)
val s = new StringBuffer()
while (m.find()) {
val ext = FilenameUtils.getExtension(m.group(1))
val writer = new StringWriter()
val stream = getClass.getResourceAsStream(m.group(1))
IOUtils.copy(new Base64InputStream(stream, true, 0, Array[Byte]()), writer)
stream.close()
m.appendReplacement(s, s"url(data:image/$ext;base64,${writer.toString})")
}
m.appendTail(s)
el.appendChild(new DataNode(s.toString, ""))
link.replaceWith(el)
})
doc.select("script") // Replace JavaScript
.filter(script => "".equals(script.attr("type")) || "text/javascript".equalsIgnoreCase(script.attr("type")))
.filter(script => script.attr("src").length > 0)
.filter(script => new URI(script.attr("src")).getHost == null)
.foreach(script => {
val js = getResourceAsString(script.attr("src"))
script.removeAttr("src")
script.attr("type", "text/javascript")
script.appendChild(new DataNode(js, ""))
})
doc.select("script") // Replace JSON
.filter(script => "application/json".equalsIgnoreCase(script.attr("type")))
.filter(script => script.attr("src").length > 0)
.filter(script => new URI(script.attr("src")).getHost == null)
.foreach(script => {
script.removeAttr("src")
script.appendChild(new DataNode(data, ""))
})
html = doc.toString
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment