forked from redhat-documentation/vale-at-red-hat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatchingDotCallouts.tengo
58 lines (50 loc) · 1.4 KB
/
MatchingDotCallouts.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
/*
Tengo Language
Checks that dot callouts (<.>) are matching
$ tengo MatchingDotCallouts.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"
num_codeblock_callouts := 0
num_callouts := 0
codeblock_callout_regex := ".+<(\\.)>"
callout_regex := "^<(\\.)>"
for line in text.split(scope, "\n") {
// trim trailing whitespace
line = text.trim_space(line)
if text.re_match(codeblock_callout_regex, line) {
//restart for new listingblock
num_callouts = 0
//account for lines with multiple callouts
num_callouts_in_line := text.count(line, "<.>")
if num_callouts_in_line > 1 {
num_codeblock_callouts = num_codeblock_callouts + num_callouts_in_line
} else {
num_codeblock_callouts++
}
}
if text.re_match(callout_regex, line) {
num_callouts++
if num_callouts > num_codeblock_callouts {
start := text.index(scope, line)
matches = append(matches, {begin: start, end: start + len(line)})
}
if num_callouts == num_codeblock_callouts {
num_callouts = 0
num_codeblock_callouts = 0
}
}
}
if len(matches) == 0 {
fmt.println("Dot callouts are balanced")
} else {
fmt.println(matches)
}