forked from rohennes/vale-asciidoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatchingNumberedCallouts.tengo
56 lines (49 loc) · 1.35 KB
/
MatchingNumberedCallouts.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
/*
Tengo Language
Checks that numbered callouts (<\d+>) are matching
$ tengo MatchingNumberedCallouts.tengo <asciidoc_file_to_validate>
*/
fmt := import("fmt")
os := import("os")
text := import("text")
input := os.args()
scope := os.read_file(input[2])
matches := []
//trim extra whitespace
scope = text.trim_space(scope)
//add a newline, it might be missing
scope += "\n"
num_codeblock_callouts := 0
num_callouts := 0
codeblock_callout_regex := ".(<\\d+>)+"
callout_regex := "^<(\\d+)>"
for line in text.split(scope, "\n") {
if text.re_match(codeblock_callout_regex, line) {
//restart for new listingblock
num_callouts = 0
fmt.println(line)
//account for lines with multiple callouts
for i := 1; i < len(line); i++ {
//text.count must be str, not regex
str := "<" + i + ">"
num_callouts_in_line := text.count(line, str)
num_codeblock_callouts += num_callouts_in_line
}
}
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("Numbered callouts are balanced")
} else {
fmt.println(matches)
}