Skip to content

Instantly share code, notes, and snippets.

@dector
Created December 11, 2020 12:56
Show Gist options
  • Save dector/3bc43d4d5c5b51529a43b66c389ad19a to your computer and use it in GitHub Desktop.
Save dector/3bc43d4d5c5b51529a43b66c389ad19a to your computer and use it in GitHub Desktop.
Pretty-print HTML
// Dependency: https://github.com/jtidy/jtidy
import org.w3c.tidy.Tidy
import space.dector.glow.engine.HtmlWebPageContent
import java.io.StringWriter
fun HtmlWebPageContent.prettyPrint(): HtmlWebPageContent {
val tidy = Tidy().apply {
quiet()
dropProprietaryTags = false
fixComments = false
tidyMark = false
wraplen = 120
smartIndent = true
}
val outputString = StringWriter()
tidy.parse(value.reader(), outputString)
return HtmlWebPageContent(outputString.toString())
}
private fun Tidy.quiet() = apply {
quiet = true
showWarnings = false
showErrors = 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment