Skip to content

Commit

Permalink
Fixes for LineLengthRule to allow punctuation after links plus emphasis
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmook committed Mar 11, 2019
1 parent 9373794 commit 2ea6c86
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import com.appmattus.markdown.dsl.RuleSetup
import com.appmattus.markdown.errors.ErrorReporter
import com.appmattus.markdown.processing.MarkdownDocument
import com.appmattus.markdown.rules.extentions.splitIntoLines
import com.vladsch.flexmark.ast.Emphasis
import com.vladsch.flexmark.ast.StrongEmphasis
import com.vladsch.flexmark.util.ast.Node

/**
* # Line length
Expand All @@ -27,12 +30,17 @@ class LineLengthRule(
private val codeBlocks: Boolean = true,
private val tables: Boolean = true,
private val headings: Boolean = true,
punctuation: String = ".,;:!? ",
override val config: RuleSetup.Builder.() -> Unit = {}
) : Rule() {

private val terminatingPunctuationRegex = Regex("[${Regex.escape(punctuation)}]?\\s*")

override val description = "Line length"
override val tags = listOf("line_length")

data class LinkRef(val startOffset: Int, val endOffset: Int, val node: Node, val lineNumber: Int)

override fun visitDocument(document: MarkdownDocument, errorReporter: ErrorReporter) {
var result = document.chars.splitIntoLines().filter {
it.length > lineLength
Expand All @@ -41,10 +49,28 @@ class LineLengthRule(
}

// Remove links at the end of the file
val links = document.allLinks.map { IntRange(it.startOffset, it.endOffset) }
val links = document.allLinks.map {

var item: Node = it
while (item.parent is Emphasis || item.parent is StrongEmphasis) {
item = item.parent
}

LinkRef(item.startOffset, item.endOffset, item, document.getLineNumber(item.startOffset))
}
result = result.filter { range ->
links.find {
it.endInclusive == range.endInclusive && document.chars.getColumnAtIndex(it.start) <= lineLength
links.find { linkRef ->
val lineNumber = document.getLineNumber(range.start)

if (linkRef.lineNumber == lineNumber
&& document.getColumnNumber(linkRef.startOffset) + 1 <= lineLength
&& document.getColumnNumber(linkRef.endOffset - 1) + 2 >= lineLength
) {
val eol = document.chars.subSequence(linkRef.endOffset, document.chars.endOfLine(linkRef.endOffset))
eol.matches(terminatingPunctuationRegex)
} else {
false
}
} == null
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.appmattus.markdown.rules

import com.appmattus.markdown.errors.Error
import com.appmattus.markdown.processing.MarkdownDocument
import com.appmattus.markdown.loadDocument
import com.appmattus.markdown.processing.MarkdownDocument
import mockDocument
import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.style.gherkin.FeatureBody
Expand Down Expand Up @@ -32,7 +32,8 @@ fun FeatureBody.FileRuleScenario(
}

Then("we expect $expectedErrorCount errors") {
assertThat(ruleErrors.size).isEqualTo(expectedErrorCount).describedAs("Number of errors")
assertThat(ruleErrors.size).describedAs("Number of errors")
.withFailMessage(ruleErrors.joinToString("\n")).isEqualTo(expectedErrorCount)
}

if (expectedErrorCount > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import org.spekframework.spek2.style.gherkin.Feature

object LineLengthRuleTest : Spek({
Feature("LineLengthRule") {
FileRuleScenario(listOf("long_lines_close.md")) { LineLengthRule() }

FileRuleScenario(listOf("long_lines_100.md")) { LineLengthRule(lineLength = 100) }

FileRuleScenario(listOf("long_lines_code.md")) { LineLengthRule(codeBlocks = false, tables = false) }
Expand Down
71 changes: 69 additions & 2 deletions markdown/src/test/resources/long_lines.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,70 @@
This is a very very very very very very very very very very very very very very long line {LineLengthRule}
This is a very very very very very very very very very very very very very very long line

This line however, while very long, doesn't have whitespace after the 80th [columnwhichallowsforURLsandotherlongthings](#hi)
[This long line is comprised entirely of a link](https://example.com "This is the long link's title")

[This long line is comprised entirely of a link](https://example.com "This is the long link's title").

[This long line is comprised entirely of a link](https://example.com "This is the long link's title")

> [This long line is comprised entirely of a link](https://example.com "This is the long link's title")
[This long line is comprised entirely of a link](https://example.com "But is inside a code block")

```markdown
[This long line is comprised entirely of a link](https://example.com "But is inside a code block")
```

This [long line is comprised mostly of a link](https://example.com "This is the long link's title")

[This long line is comprised mostly of a link](https://example.com "This is the long link's title") text

This long line includes a simple [reference][label] link and is long enough to violate the rule.

[This long line is comprised entirely of a reference link and is long enough to violate the rule][label]

[label]: https://example.org "Title for a link reference that is itself long enough to violate the rule"

[Link to broken label][notlabel]

[notlabel\]: notlink "Invalid syntax for a link label because the right bracket is backslash-escaped"

[](https://example.com "This long line is comprised entirely of a link with empty text and a non-empty title")

*[This long line is comprised of an emphasized link](https://example.com "This is the long link's title")*

_[This long line is comprised of an emphasized link](https://example.com "This is the long link's title")_

**[This long line is comprised of a bolded link](https://example.com "This is the long link's title")**

__[This long line is comprised of a bolded link](https://example.com "This is the long link's title")__

_**[This long line is comprised of an emphasized and bolded link](https://example.com "This is the long link's title")**_

**_[This long line is comprised of an emphasized and bolded link](https://example.com "This is the long link's title")_**

*[](https://example.com "This long line is comprised of an emphasized link with empty text and a non-empty title")*

**[](https://example.com "This long line is comprised of a bolded link with empty text and a non-empty title")**

{LineLengthRule:1} {LineLengthRule:11} {LineLengthRule:14}
{LineLengthRule:19} {LineLengthRule:21}

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

This is a looongish [line](https://example.com "This is the long link's title")
This is a loooongish [line](https://example.com "This is the long link's title")
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}

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}

{NoReversedLinksRule:21} {NoReversedLinksRule:23} {NoReversedLinksRule:27}
{NoEmptyLinksRule:27}
7 changes: 7 additions & 0 deletions markdown/src/test/resources/long_lines_close.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This is a very very very very very very very very very very very very long line

This is a very very very very very very very very very very very very loong line

This is a very very very very very very very very very very very very looong line

{LineLengthRule:5}

0 comments on commit 2ea6c86

Please sign in to comment.