Skip to content

Commit

Permalink
Adding new ConsistentEmphasisStyleRule
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmook committed Apr 12, 2019
1 parent 623dd1c commit 02e3824
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.vladsch.flexmark.ast.BlockQuote
import com.vladsch.flexmark.ast.BulletList
import com.vladsch.flexmark.ast.BulletListItem
import com.vladsch.flexmark.ast.Code
import com.vladsch.flexmark.ast.Emphasis
import com.vladsch.flexmark.ast.FencedCodeBlock
import com.vladsch.flexmark.ast.Heading
import com.vladsch.flexmark.ast.HtmlBlock
Expand All @@ -22,6 +23,7 @@ import com.vladsch.flexmark.ast.OrderedList
import com.vladsch.flexmark.ast.OrderedListItem
import com.vladsch.flexmark.ast.Paragraph
import com.vladsch.flexmark.ast.Reference
import com.vladsch.flexmark.ast.StrongEmphasis
import com.vladsch.flexmark.ast.Text
import com.vladsch.flexmark.ast.ThematicBreak
import com.vladsch.flexmark.ext.gfm.tasklist.TaskListItem
Expand Down Expand Up @@ -64,6 +66,8 @@ class MarkdownDocument(val filename: String, val document: Document) {
val topLevelParagraphs: List<Paragraph> by lazy { document.find(Paragraph::class, visitChildren = false) }
val allText by lazy { document.find(Text::class) }

val allEmphasis by lazy { document.find(Emphasis::class, StrongEmphasis::class) }

val chars: BasedSequence by lazy { document.chars }
val lines by lazy { chars.splitIntoLines() }

Expand Down
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"
}
}
}
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
}
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) }
}
})
7 changes: 7 additions & 0 deletions markdown/src/test/resources/consistent_emphasis_asterisk.md
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 markdown/src/test/resources/consistent_emphasis_underscore.md
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__
4 changes: 2 additions & 2 deletions markdown/src/test/resources/emphasis_instead_of_headers.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
in culpa qui officia deserunt mollit anim id est laborum.

__Section 1.1: another section {NoEmphasisAsHeaderRule}__
__Section 1.1: {NoEmphasisAsHeaderRule} {ConsistentEmphasisStyleRule}__

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
Expand All @@ -25,7 +25,7 @@ Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
in culpa qui officia deserunt mollit anim id est laborum.

_Section 3: oh no more sections {NoEmphasisAsHeaderRule}_
_Section 3: {NoEmphasisAsHeaderRule} {ConsistentEmphasisStyleRule}_

This is a normal paragraph
**that just happens to have emphasized text in**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@
[test **test** test](www.test.com)
[test __test__ test](www.test.com)
[this should not raise](www.shouldnotraise.com)

{ConsistentEmphasisStyleRule:3} {ConsistentEmphasisStyleRule:4}
{ConsistentEmphasisStyleRule:4} {ConsistentEmphasisStyleRule:5}
{ConsistentEmphasisStyleRule:5} {ConsistentEmphasisStyleRule:5}
{ConsistentEmphasisStyleRule:6}
6 changes: 4 additions & 2 deletions markdown/src/test/resources/long_lines.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ _**[This long line is comprised of an emphasized and bolded link](https://exampl

{LineLengthRule:1} {LineLengthRule:11} {LineLengthRule:14}
{LineLengthRule:19} {LineLengthRule:21}
{ConsistentEmphasisStyleRule:35} {ConsistentEmphasisStyleRule:39}
{ConsistentEmphasisStyleRule:41} {ConsistentEmphasisStyleRule:43}

LineLengthRule:29 is invalid too but gets picked up as a link currently

Expand All @@ -58,13 +60,13 @@ This is a looooongish [line](https://example.com "This is the long link's title"
This is a looongish [line](https://example.com "This is the long link's title")A
This is a loooongish [line](https://example.com "This is the long link's title")B

{LineLengthRule:59}
{LineLengthRule:61}

This is a long long long long long long long long long long long long loongish [line](https://example.com "This is the long link's title")
This is a long long long long long long long long long long long long looongish [line](https://example.com "This is the long link's title")
This is a long long long long long long long long long long long long loooongish [line](https://example.com "This is the long link's title")

{LineLengthRule:64} {LineLengthRule:65}
{LineLengthRule:66} {LineLengthRule:67}

{NoReversedLinksRule:21} {NoReversedLinksRule:23} {NoReversedLinksRule:27}
{NoEmptyLinksRule:27}
2 changes: 1 addition & 1 deletion markdown/src/test/resources/proper-names.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Quoted "Markdownlint" {ProperNamesRule}

Emphasized *Markdownlint* {ProperNamesRule}

Emphasized _Markdownlint_ {ProperNamesRule}
Emphasized _Markdownlint_ {ProperNamesRule} {ConsistentEmphasisStyleRule}

JavaScript is a language

Expand Down
6 changes: 4 additions & 2 deletions markdown/src/test/resources/spaces_inside_emphasis_markers.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ Line with *Normal emphasis*

Line with **Normal strong**

Line with _Normal emphasis_
Line with _Normal emphasis_ {ConsistentEmphasisStyleRule}

Line with __Normal strong__
Line with __Normal strong__ {ConsistentEmphasisStyleRule}

Broken * emphasis * with spaces in {NoSpaceInEmphasisRule}

Expand All @@ -19,8 +19,10 @@ Mixed *ok emphasis* and * broken emphasis * {NoSpaceInEmphasisRule}
Mixed **ok strong** and ** broken strong ** {NoSpaceInEmphasisRule}

Mixed _ok emphasis_ and _ broken emphasis _ {NoSpaceInEmphasisRule}
{ConsistentEmphasisStyleRule:21}

Mixed __ok strong__ and __ broken strong __ {NoSpaceInEmphasisRule}
{ConsistentEmphasisStyleRule:24}

Mixed *ok emphasis* **ok strong** * broken emphasis * {NoSpaceInEmphasisRule}

Expand Down

0 comments on commit 02e3824

Please sign in to comment.