Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YAML ellipses rule #887

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .vale/fixtures/RedHat/YamlEllipses/.vale.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
; Vale configuration file to test the `YamlEllipses` rule
StylesPath = ../../../styles
MinAlertLevel = suggestion
[*.adoc]
RedHat.YamlEllipses = YES
apinnick marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 7 additions & 0 deletions .vale/fixtures/RedHat/YamlEllipses/testinvalid.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//vale-fixture
[source,yaml]
apinnick marked this conversation as resolved.
Show resolved Hide resolved
----
...
...
...
----
10 changes: 10 additions & 0 deletions .vale/fixtures/RedHat/YamlEllipses/testvalid.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//vale-fixture
[source,yaml]
----
# ...
# ...
# This is an ordinary comment
----

...

35 changes: 35 additions & 0 deletions .vale/styles/RedHat/YamlEllipses.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
extends: script
level: suggestion
message: "Use '# ...' instead of '...' in YAML code blocks."
link: https://yaml.org/spec/1.2.2/#22-structures
scope: raw
script: |
text := import("text")
matches := []

// clean out multi-line comments
scope = text.re_replace("(?s) *(\n////.*?////\n)", scope, "")
//add a newline, it might be missing
scope += "\n"

codeblock_delim_regex := "^-{4,}$"
comment_ellipsis := "^# \\.{3}$"
reg_ellipsis := "^\\s*\\.{3}$"
inside_codeblock := false

for line in text.split(scope, "\n") {
apinnick marked this conversation as resolved.
Show resolved Hide resolved
// trim trailing whitespace
line = text.trim_space(line)
//ignore content in codeblocks
if text.re_match(codeblock_delim_regex, line) && inside_codeblock == false {
inside_codeblock = true
} else if text.re_match(codeblock_delim_regex, line) && inside_codeblock == true {
inside_codeblock = false
}
if !text.re_match(comment_ellipsis, line) && text.re_match(reg_ellipsis, line) && inside_codeblock == true {
start := text.index(scope, line)
matches = append(matches, {begin: start, end: start + len(line)})
}
}

4 changes: 4 additions & 0 deletions modules/reference-guide/pages/ellipses.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@

Avoid ellipsis (...) except to indicate omitted words.

Use a commented out ellipsis (# ...) in YAML code blocks. YAML uses '...' to indicate the end of a document without starting a new one.

.Additional resources

* link:{ibmsg-url-print}[{ibmsg-print} - Ellipses, p. 49]
* link:{ibmsg-url}?topic=punctuation-ellipses[{ibmsg} - Ellipses]
* link:{repository-url}blob/main/.vale/styles/RedHat/Ellipses.yml[`Ellipses.yml` source code]
* link:https://yaml.org/spec/1.2.2/#22-structures[YAML 1.2: Structures]
* link:{repository-url}blob/main/.vale/styles/RedHat/YamlEllipses.yml[`YamlEllipses.yml` source code]
42 changes: 42 additions & 0 deletions tengo-rule-scripts/YamlEllipses.tengo
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Tengo Language
$ tengo ValidYamlCodeBlocks.tengo <asciidoc_file_to_validate>
*/

fmt := import("fmt")
os := import("os")
text := import("text")

input := os.args()
scope := os.read_file(input[2])
matches := []

// clean out multi-line comments
scope = text.re_replace("(?s) *(\n////.*?////\n)", scope, "")
//add a newline, it might be missing
scope += "\n"

codeblock_delim_regex := "^-{4,}$"
yaml_block_regex := "^\\[(source,yaml).*\\]"
ellipses := 0
comment_ellipsis := "^# \\.{3}$"
reg_ellipsis := "^\\s*\\.{3}$"
inside_codeblock := false

for line in text.split(scope, "\n") {
// trim trailing whitespace
line = text.trim_space(line)
// check only content in codeblocks
if text.re_match(codeblock_delim_regex, line) && inside_codeblock == false {
inside_codeblock = true
} else if text.re_match(codeblock_delim_regex, line) && inside_codeblock == true {
inside_codeblock = false
}
if !text.re_match(comment_ellipsis, line) && text.re_match(reg_ellipsis, line) && inside_codeblock == true {
start := text.index(scope, line)
matches = append(matches, {begin: start, end: start + len(line)})
}
}

fmt.println(matches)

Loading