forked from redhat-documentation/vale-at-red-hat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSequentialNumberedCallouts.tengo
69 lines (60 loc) · 1.74 KB
/
SequentialNumberedCallouts.tengo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
Tengo Language
Checks that callouts outside the listing block are sequential
$ tengo SequentialNumberedCallouts.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"
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
}
//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
}
}
}
}
if len(matches) == 0 {
fmt.println("Callouts are sequential")
} else {
fmt.println(matches)
}