/
RulePrinter.kt
75 lines (62 loc) · 2.41 KB
/
RulePrinter.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package io.gitlab.arturbosch.detekt.generator.printer
import io.github.detekt.utils.MarkdownContent
import io.github.detekt.utils.bold
import io.github.detekt.utils.codeBlock
import io.github.detekt.utils.crossOut
import io.github.detekt.utils.h3
import io.github.detekt.utils.h4
import io.github.detekt.utils.markdown
import io.github.detekt.utils.paragraph
import io.gitlab.arturbosch.detekt.generator.collection.Active
import io.gitlab.arturbosch.detekt.generator.collection.Rule
internal object RulePrinter : DocumentationPrinter<Rule> {
override fun print(item: Rule): String {
return markdown {
if (item.isDeprecated()) {
h3 { crossOut { item.name } }
paragraph { escapeHtml(item.deprecationMessage.orEmpty()) }
} else {
h3 { item.name }
}
if (item.description.isNotEmpty()) {
paragraph { escapeHtml(item.description) }
} else {
paragraph { "TODO: Specify description" }
}
paragraph {
"${bold { "Active by default" }}: ${if (item.defaultActivationStatus.active) "Yes" else "No"}" +
((item.defaultActivationStatus as? Active)?.let { " - Since v${it.since}" }.orEmpty())
}
if (item.requiresTypeResolution) {
paragraph {
bold { "Requires Type Resolution" }
}
}
if (item.debt.isNotEmpty()) {
paragraph {
"${bold { "Debt" }}: ${item.debt}"
}
}
if (!item.aliases.isNullOrEmpty()) {
paragraph {
"${bold { "Aliases" }}: ${item.aliases}"
}
}
markdown { RuleConfigurationPrinter.print(item.configuration) }
printRuleCodeExamples(item)
}
}
private fun MarkdownContent.printRuleCodeExamples(rule: Rule) {
if (rule.nonCompliantCodeExample.isNotEmpty()) {
h4 { "Noncompliant Code:" }
paragraph { codeBlock { rule.nonCompliantCodeExample } }
}
if (rule.compliantCodeExample.isNotEmpty()) {
h4 { "Compliant Code:" }
paragraph { codeBlock { rule.compliantCodeExample } }
}
}
internal fun escapeHtml(input: String) = input
.replace("<", "<")
.replace(">", ">")
}