forked from redhat-documentation/vale-at-red-hat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidConditions.tengo
46 lines (40 loc) · 1.25 KB
/
ValidConditions.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
/*
Tengo Language
$ tengo ValidConditions.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"
if_regex := "^ifdef::.+\\[\\]"
ifn_regex := "^ifndef::.+\\[\\]"
ifeval_regex := "ifeval::\\[.+\\]"
endif_regex := "^endif::.*\\[\\]"
for line in text.split(scope, "\n") {
// trim trailing whitespace
line = text.trim_space(line)
if text.re_match(if_regex, line) || text.re_match(ifn_regex, line) || text.re_match(ifeval_regex, line) {
start := text.index(scope, line)
matches = append(matches, {begin: start, end: start + len(line)})
} else if text.re_match(endif_regex, line) {
if len(matches) > 0 {
//remove the most recently added open ifdef match
matches = matches[:len(matches)-1]
} else if len(matches) == 0 {
//add orphan endif::[] statements
start := text.index(scope, line)
matches = append(matches, {begin: start, end: start + len(line)})
}
}
}
if len(matches) == 0 {
fmt.println("if statements are balanced")
} else {
fmt.println(matches)
}