Skip to content

Commit

Permalink
finds all uncommented ellipses
Browse files Browse the repository at this point in the history
  • Loading branch information
apinnick committed Sep 30, 2024
1 parent 7dd6722 commit ae8d251
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .vale/fixtures/RedHat/YamlEllipses/testinvalid.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
----
...
...
----
...
----
6 changes: 3 additions & 3 deletions .vale/fixtures/RedHat/YamlEllipses/testvalid.adoc
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[source,yaml]
----
# ...
# ...
# This is an ordinary comment
----

[source,terminal]
----
...
----

20 changes: 12 additions & 8 deletions .vale/styles/RedHat/YamlEllipses.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
extends: existence
extends: script
level: suggestion
message: "Use '# ...' rather than '...' as ellipsis in YAML code blocks."
message: "Use '# ...' instead of '...' in YAML code blocks."
link: https://yaml.org/spec/1.2.2/#22-structures
scope: raw
script: |
Expand All @@ -14,18 +14,22 @@ script: |
scope += "\n"
codeblock_delim_regex := "^-{4,}$"
source_block_regex := "^\\[(source,yaml).*\\]"
ellipses := 0
reg_ellipsis := "^\\s*\\.\\.\\."
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)
if text.re_match(source_block_regex, 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)})
} else if text.re_match(reg_ellipsis, line){
ellipses ++
}
}
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]
18 changes: 12 additions & 6 deletions tengo-rule-scripts/YamlEllipses.tengo
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,26 @@ scope = text.re_replace("(?s) *(\n////.*?////\n)", scope, "")
scope += "\n"

codeblock_delim_regex := "^-{4,}$"
source_block_regex := "^\\[(yml|yaml).*\\]"
yaml_block_regex := "^\\[(source,yaml).*\\]"
ellipses := 0
reg_ellipsis := "^\\s*\\.\\.\\."
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)
if text.re_match(source_block_regex, 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)})
} else if text.re_match(reg_ellipsis, line){
ellipses ++
}
}

fmt.println(ellipses)
fmt.println(matches)

0 comments on commit ae8d251

Please sign in to comment.