Skip to content

Commit

Permalink
YAML ellipses rule
Browse files Browse the repository at this point in the history
  • Loading branch information
apinnick committed Sep 30, 2024
1 parent 1653816 commit f08a006
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 0 deletions.
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
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]
----
...
...
...
----
9 changes: 9 additions & 0 deletions .vale/fixtures/RedHat/YamlEllipses/testvalid.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[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") {
// 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)

0 comments on commit f08a006

Please sign in to comment.