Skip to content

Commit

Permalink
Merge pull request #883 from aireilly/SequentialNumberedCallouts
Browse files Browse the repository at this point in the history
Adding numbered sequential callout rule fix
  • Loading branch information
aireilly authored Sep 13, 2024
2 parents 4d5c627 + 82dee6e commit cf45d52
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 35 deletions.
27 changes: 24 additions & 3 deletions .vale/fixtures/AsciiDoc/SequentialNumberedCallouts/testvalid.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@
----
require 'sinatra' <1>
get '/hi' do <1>
"Goodbye cruel world!"
end
key: value <2>
key: another value <3>
----
<1> Library import
<2> URL mapping
// Rule skips any block containing ifdefs
ifndef::test1[]
<3> Some text
endif::[]
ifdef::test2[]
<3> Some other text
endif::[]

//vale-fixture
[source,ruby]
----
require 'sinatra' <1>
get '/hi' do <2> <3>
"Hello World!"
end
Expand All @@ -16,10 +37,10 @@ end
----
require 'sinatra' <1>
get '/hi' do <1>
"Hello World!"
get '/hi' do <2> <3>
"Hello World again!"
end
key: value <2>
----
<1> Library import
<2> URL mapping
<3> Response block
44 changes: 29 additions & 15 deletions .vale/styles/AsciiDoc/SequentialNumberedCallouts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,44 @@ script: |
prev_num := 0
callout_regex := "^<(\\d+)>"
listingblock_delim_regex := "^-{4,}$"
if_regex := "^ifdef::|ifndef::"
endif_regex := "^endif::\\[\\]"
inside_if := false
for line in text.split(scope, "\n") {
// trim trailing whitespace
line = text.trim_space(line)
// check if we're entering a conditional block
if text.re_match(if_regex, line) {
inside_if = true
} else if text.re_match(endif_regex, line) {
inside_if = false
}
//reset count if we hit a listing block delimiter
if text.re_match(listingblock_delim_regex, line) {
prev_num = 0
}
if text.re_match(callout_regex, line) {
callout := text.re_find("<(\\d+)>", line)
for key, value in callout {
//trim angle brackets from string
trimmed := callout[key][0]["text"]
trimmed = text.trim_prefix(trimmed, "<")
trimmed = text.trim_suffix(trimmed, ">")
//cast string > int
num := text.atoi(trimmed)
//start counting
if num != prev_num+1 {
start := text.index(scope, line)
matches = append(matches, {begin: start, end: start + len(line)})
//only count callouts where there are no ifdefs
if !inside_if {
if text.re_match(callout_regex, line) {
callout := text.re_find("<(\\d+)>", line)
for key, value in callout {
//trim angle brackets from string
trimmed := callout[key][0]["text"]
trimmed = text.trim_prefix(trimmed, "<")
trimmed = text.trim_suffix(trimmed, ">")
//cast string > int
num := text.atoi(trimmed)
//start counting
if num != prev_num+1 {
start := text.index(scope, line)
matches = append(matches, {begin: start, end: start + len(line)})
}
prev_num = num
}
prev_num = num
}
}
}
}
48 changes: 31 additions & 17 deletions tengo-rule-scripts/SequentialNumberedCallouts.tengo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
Tengo Language
Checks that callouts outside the listing block are sequential
Checks that callouts outside the listing block are sequential
$ tengo SequentialNumberedCallouts.tengo <asciidoc_file_to_validate>
*/

Expand All @@ -20,36 +20,50 @@ scope += "\n"
prev_num := 0
callout_regex := "^<(\\d+)>"
listingblock_delim_regex := "^-{4,}$"
if_regex := "^ifdef::|ifndef::"
endif_regex := "^endif::\\[\\]"
inside_if := false

for line in text.split(scope, "\n") {
// trim trailing whitespace
line = text.trim_space(line)

// check if we're entering a conditional block
if text.re_match(if_regex, line) {
inside_if = true
} else if text.re_match(endif_regex, line) {
inside_if = false
}

//reset count if we hit a listing block delimiter
if text.re_match(listingblock_delim_regex, line) {
prev_num = 0
}

if text.re_match(callout_regex, line) {
callout := text.re_find("<(\\d+)>", line)
for key, value in callout {
//trim angle brackets from string
trimmed := callout[key][0]["text"]
trimmed = text.trim_prefix(trimmed, "<")
trimmed = text.trim_suffix(trimmed, ">")
//cast string > int
num := text.atoi(trimmed)
//start counting
if num != prev_num+1 {
start := text.index(scope, line)
matches = append(matches, {begin: start, end: start + len(line)})
//only count callouts where there are no ifdefs
if !inside_if {
if text.re_match(callout_regex, line) {
callout := text.re_find("<(\\d+)>", line)
for key, value in callout {
//trim angle brackets from string
trimmed := callout[key][0]["text"]
trimmed = text.trim_prefix(trimmed, "<")
trimmed = text.trim_suffix(trimmed, ">")
//cast string > int
num := text.atoi(trimmed)
//start counting
if num != prev_num+1 {
start := text.index(scope, line)
matches = append(matches, {begin: start, end: start + len(line)})
}
prev_num = num
}
prev_num = num
}
}
}

if len(matches) == 0 {
fmt.println("Callouts are sequential")
} else {
fmt.println(matches)
}
fmt.println(matches)
}

0 comments on commit cf45d52

Please sign in to comment.