Add command line application to sanitize HTML files

This can be used to test what HTML messages will look like after K-9 Mail's sanitization process.
This commit is contained in:
cketti 2022-01-26 15:21:45 +01:00
parent 362ecae297
commit 3c9dd17ff7
5 changed files with 96 additions and 0 deletions

View file

@ -0,0 +1,17 @@
```text
Usage: html-cleaner [OPTIONS] INPUT [OUTPUT]
A tool that modifies HTML to only keep allowed elements and attributes the
same way that K-9 Mail does.
Options:
-h, --help Show this message and exit
Arguments:
INPUT HTML input file (needs to be UTF-8 encoded)
OUTPUT Output file
```
You can run this tool using the [html-cleaner](../../html-cleaner) script in the root directory of this repository.
It will compile the application and then run it using the given arguments. This allows you to make modifications to the
[HTML cleaning code](../../app/html-cleaner/src/main/java/app/k9mail/html/cleaner) and test the changes right away.

View file

@ -0,0 +1,20 @@
apply plugin: 'org.jetbrains.kotlin.jvm'
apply plugin: 'application'
version 'unspecified'
application {
mainClass = "app.k9mail.cli.html.cleaner.MainKt"
}
repositories {
mavenCentral()
}
dependencies {
implementation project(':app:html-cleaner')
implementation "org.jetbrains.kotlin:kotlin-stdlib"
implementation "com.github.ajalt.clikt:clikt:3.4.0"
implementation "com.squareup.okio:okio:${versions.okio}"
}

View file

@ -0,0 +1,55 @@
package app.k9mail.cli.html.cleaner
import app.k9mail.html.cleaner.HtmlHeadProvider
import app.k9mail.html.cleaner.HtmlProcessor
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.arguments.optional
import com.github.ajalt.clikt.parameters.types.file
import com.github.ajalt.clikt.parameters.types.inputStream
import java.io.File
import okio.buffer
import okio.sink
import okio.source
@Suppress("MemberVisibilityCanBePrivate")
class HtmlCleaner : CliktCommand(
help = "A tool that modifies HTML to only keep allowed elements and attributes the same way that K-9 Mail does."
) {
val input by argument(help = "HTML input file (needs to be UTF-8 encoded)")
.inputStream()
val output by argument(help = "Output file")
.file(mustExist = false, canBeDir = false)
.optional()
override fun run() {
val html = readInput()
val processedHtml = cleanHtml(html)
writeOutput(processedHtml)
}
private fun readInput(): String {
return input.source().buffer().use { it.readUtf8() }
}
private fun cleanHtml(html: String): String {
val htmlProcessor = HtmlProcessor(object : HtmlHeadProvider {
override val headHtml = """<meta name="viewport" content="width=device-width"/>"""
})
return htmlProcessor.processForDisplay(html)
}
private fun writeOutput(data: String) {
output?.writeOutput(data) ?: echo(data)
}
private fun File.writeOutput(data: String) {
sink().buffer().use {
it.writeUtf8(data)
}
}
}
fun main(args: Array<String>) = HtmlCleaner().main(args)

3
html-cleaner Executable file
View file

@ -0,0 +1,3 @@
#!/bin/sh
./gradlew --quiet ":cli:html-cleaner-cli:installDist" < /dev/null && ./cli/html-cleaner-cli/build/install/html-cleaner-cli/bin/html-cleaner-cli "$@"

View file

@ -25,3 +25,4 @@ include ':backend:webdav'
include ':backend:jmap'
include ':backend:demo'
include ':plugins:openpgp-api-lib:openpgp-api'
include ':cli:html-cleaner-cli'