-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding new ConsistentEmphasisStyleRule
- Loading branch information
Showing
11 changed files
with
139 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
markdown/src/main/kotlin/com/appmattus/markdown/rules/ConsistentEmphasisStyleRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.appmattus.markdown.rules | ||
|
||
import com.appmattus.markdown.dsl.RuleSetup | ||
import com.appmattus.markdown.errors.ErrorReporter | ||
import com.appmattus.markdown.processing.MarkdownDocument | ||
import com.appmattus.markdown.rules.config.EmphasisStyle | ||
import com.vladsch.flexmark.ast.DelimitedNode | ||
import com.vladsch.flexmark.ast.Emphasis | ||
|
||
/** | ||
* # Emphasis marker style | ||
* | ||
* This rule is triggered when the symbols used in the document for italic and bold text do not match the configured | ||
* emphasis marker style: | ||
* | ||
* *Italic* | ||
* _Also italic_ | ||
* | ||
* To fix this issue, use the configured style for emphasis markers throughout the document: | ||
* | ||
* *Italic* | ||
* *Also italic* | ||
* | ||
* Note: the configured emphasis marker style can be a specific symbol ([EmphasisStyle.Asterisk], | ||
* [EmphasisStyle.Underscore]), or simply require that the usage be [EmphasisStyle.Consistent] within the document. | ||
*/ | ||
class ConsistentEmphasisStyleRule( | ||
private val style: EmphasisStyle = EmphasisStyle.Asterisk, | ||
override val config: RuleSetup.Builder.() -> Unit = {} | ||
) : Rule() { | ||
|
||
override fun visitDocument(document: MarkdownDocument, errorReporter: ErrorReporter) { | ||
|
||
val allEmphasis = document.allEmphasis | ||
|
||
if (allEmphasis.isEmpty()) { | ||
return | ||
} | ||
|
||
val docStyle = if (style == EmphasisStyle.Consistent) allEmphasis.first().style() else style | ||
|
||
allEmphasis.forEach { | ||
if (it.style() != docStyle) { | ||
val type = if (it is Emphasis) "Italic" else "Bold" | ||
|
||
val symbolCount = if (it is Emphasis) 1 else 2 | ||
val expected = docStyle.symbol().repeat(symbolCount) | ||
val actual = it.style().symbol().repeat(symbolCount) | ||
|
||
val description = "$type text expected to use '$expected' emphasis markers but is '$actual', for " + | ||
"example $expected${it.text}$expected. Configuration: style=${style.description()}." | ||
|
||
errorReporter.reportError(it.startOffset, it.endOffset, description) | ||
} | ||
} | ||
} | ||
|
||
private fun DelimitedNode.style(): EmphasisStyle { | ||
return when { | ||
openingMarker.contains('*') -> EmphasisStyle.Asterisk | ||
openingMarker.contains('_') -> EmphasisStyle.Underscore | ||
else -> throw IllegalStateException() | ||
} | ||
} | ||
|
||
private fun EmphasisStyle.symbol(): String { | ||
return when (this) { | ||
EmphasisStyle.Asterisk -> "*" | ||
EmphasisStyle.Underscore -> "_" | ||
else -> throw IllegalStateException() | ||
} | ||
} | ||
|
||
private fun EmphasisStyle.description(): String { | ||
return when (this) { | ||
EmphasisStyle.Asterisk -> "Asterisk '*'" | ||
EmphasisStyle.Underscore -> "Underscore '_'" | ||
EmphasisStyle.Consistent -> "Consistent" | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
markdown/src/main/kotlin/com/appmattus/markdown/rules/config/EmphasisStyle.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.appmattus.markdown.rules.config | ||
|
||
enum class EmphasisStyle { | ||
Consistent, Asterisk, Underscore | ||
} |
19 changes: 19 additions & 0 deletions
19
markdown/src/test/kotlin/com/appmattus/markdown/rules/ConsistentEmphasisStyleRuleTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.appmattus.markdown.rules | ||
|
||
import com.appmattus.markdown.rules.config.EmphasisStyle | ||
import org.spekframework.spek2.Spek | ||
import org.spekframework.spek2.style.gherkin.Feature | ||
|
||
object ConsistentEmphasisStyleRuleTest : Spek({ | ||
Feature("ConsistentEmphasisStyleRule") { | ||
FileRuleScenario(files = listOf("consistent_emphasis_asterisk.md")) { | ||
ConsistentEmphasisStyleRule(EmphasisStyle.Asterisk) | ||
} | ||
|
||
FileRuleScenario(files = listOf("consistent_emphasis_underscore.md")) { | ||
ConsistentEmphasisStyleRule(EmphasisStyle.Underscore) | ||
} | ||
|
||
FileRuleScenario { ConsistentEmphasisStyleRule(EmphasisStyle.Consistent) } | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
_This should violate {ConsistentEmphasisStyleRule}_ | ||
|
||
*This will not* | ||
|
||
__This will also violate {ConsistentEmphasisStyleRule}__ | ||
|
||
**But this will be fine** |
7 changes: 7 additions & 0 deletions
7
markdown/src/test/resources/consistent_emphasis_underscore.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
*This should violate {ConsistentEmphasisStyleRule}* | ||
|
||
_This will not_ | ||
|
||
**This will also violate {ConsistentEmphasisStyleRule}** | ||
|
||
__But this will be fine__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters