diff options
| author | test@dangofactory.cos <you@example.com> | 2026-08-20 16:57:18 +0000 |
|---|---|---|
| committer | test@dangofactory.cos <you@example.com> | 2026-08-20 16:57:18 +0000 |
| commit | 50ce5d5e6cd0d720ff437d35eace545816848428 (patch) | |
| tree | b59af206a36265cf081945e434900b32b0c43edd /cli/html-cleaner-cli | |
| download | monocles_mail-50ce5d5e6cd0d720ff437d35eace545816848428.tar.gz monocles_mail-50ce5d5e6cd0d720ff437d35eace545816848428.tar.bz2 monocles_mail-50ce5d5e6cd0d720ff437d35eace545816848428.zip | |
Inital
Diffstat (limited to 'cli/html-cleaner-cli')
| -rw-r--r-- | cli/html-cleaner-cli/README.md | 17 | ||||
| -rw-r--r-- | cli/html-cleaner-cli/build.gradle.kts | 23 | ||||
| -rw-r--r-- | cli/html-cleaner-cli/src/main/kotlin/Main.kt | 55 |
3 files changed, 95 insertions, 0 deletions
diff --git a/cli/html-cleaner-cli/README.md b/cli/html-cleaner-cli/README.md new file mode 100644 index 0000000..2b10cb3 --- /dev/null +++ b/cli/html-cleaner-cli/README.md @@ -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. diff --git a/cli/html-cleaner-cli/build.gradle.kts b/cli/html-cleaner-cli/build.gradle.kts new file mode 100644 index 0000000..77ae675 --- /dev/null +++ b/cli/html-cleaner-cli/build.gradle.kts @@ -0,0 +1,23 @@ +@Suppress("DSL_SCOPE_VIOLATION") +plugins { + alias(libs.plugins.kotlin.jvm) + id("application") +} + +version = "unspecified" + +application { + mainClass.set("app.k9mail.cli.html.cleaner.MainKt") +} + +dependencies { + implementation(projects.app.htmlCleaner) + + implementation(libs.clikt) + implementation(libs.okio) +} + +java { + sourceCompatibility = ThunderbirdProjectConfig.javaVersion + targetCompatibility = ThunderbirdProjectConfig.javaVersion +} diff --git a/cli/html-cleaner-cli/src/main/kotlin/Main.kt b/cli/html-cleaner-cli/src/main/kotlin/Main.kt new file mode 100644 index 0000000..9fc7830 --- /dev/null +++ b/cli/html-cleaner-cli/src/main/kotlin/Main.kt @@ -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) |
