com.pinterest.ktlint.reporter.html.HtmlReporter.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ktlint-reporter-html Show documentation
Show all versions of ktlint-reporter-html Show documentation
An anti-bikeshedding Kotlin linter with built-in formatter.
/*
* MIT License
*
* Copyright (c) 2019 Matheus Candido
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.pinterest.ktlint.reporter.html
import com.pinterest.ktlint.core.LintError
import com.pinterest.ktlint.core.Reporter
import java.io.PrintStream
import java.util.concurrent.ConcurrentHashMap
class HtmlReporter(private val out: PrintStream) : Reporter {
private val acc = ConcurrentHashMap>()
private var issueCount = 0
private var correctedCount = 0
override fun onLintError(file: String, err: LintError, corrected: Boolean) {
if (!corrected) {
issueCount += 1
acc.getOrPut(file) { mutableListOf() }.add(err)
} else {
correctedCount += 1
}
}
override fun afterAll() {
html {
head {
cssLink("https://fonts.googleapis.com/css?family=Source+Code+Pro")
text("${System.lineSeparator()}")
}
body {
if (!acc.isEmpty()) {
h1 { text("Overview") }
paragraph {
text("Issues found: $issueCount")
}
paragraph {
text("Issues corrected: $correctedCount")
}
acc.forEach { file: String, errors: MutableList ->
h3 { text(file) }
ul {
errors.forEach { (line, col, ruleId, detail) ->
item("($line, $col): $detail ($ruleId)")
}
}
}
} else {
paragraph {
text("Congratulations, no issues found!")
}
}
}
}
}
private fun html(body: () -> Unit) {
out.println("")
body()
out.println("")
}
private fun head(body: () -> Unit) {
out.println("")
body()
out.println("")
}
private fun body(body: () -> Unit) {
out.println("")
body()
out.println("")
}
private fun h1(body: () -> Unit) {
out.print("")
body()
out.println("
")
}
private fun h3(body: () -> Unit) {
out.print("")
body()
out.println("
")
}
private fun text(value: String) {
out.print(value)
}
private fun ul(body: () -> Unit) {
out.println("")
body()
out.println("
")
}
private fun item(value: String) {
out.print("")
text(value.escapeHTMLAttrValue())
out.println(" ")
}
private fun cssLink(link: String) {
out.print("")
}
private fun paragraph(body: () -> Unit) {
out.print("")
body()
out.println("
")
}
private fun String.escapeHTMLAttrValue() =
this.replace("&", "&").replace("\"", """).replace("'", "'")
.replace("<", "<").replace(">", ">")
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy