forked from redhat-documentation/vale-at-red-hat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnsetAttributes.tengo
58 lines (52 loc) · 1.66 KB
/
UnsetAttributes.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
$ tengo UnsetAttributes.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"
// clean out multi-line comments
scope = text.re_replace("(?s) *(\n////.*?////\n)", scope, "")
attr_regex := "^:[\\w-_]+:.*$"
context_mod_docs_regex := "^:context|_content-type|_mod-docs-content-type:.*$"
attr_name_regex := ":[\\w-_]+:"
attr_name := ""
unset_attr_pref := ""
unset_attr_suff := ""
for line in text.split(scope, "\n") {
if text.re_match(attr_regex, line) {
if !text.re_match(context_mod_docs_regex, line) {
start := text.index(scope, line)
matches = append(matches, {begin: start, end: start + len(line)})
// re_find returns an array holding all matches
attr_name = ((text.re_find(attr_name_regex, line))[0][0])["text"]
unset_attr_pref = `^:!` + text.trim_prefix(attr_name, `:`)
unset_attr_suff = `^` + text.trim_suffix(attr_name, `:`) + `!:`
// loop through lines for every attr found
for line in text.split(scope, "\n") {
if text.re_match(unset_attr_pref, line) {
if len(matches) > 0 {
// remove the most recently added match
matches = matches[:len(matches)-1]
} else if text.re_match(unset_attr_suff, line) {
if len(matches) > 0 {
matches = matches[:len(matches)-1]
}
}
}
}
}
}
}
if len(matches) == 0 {
fmt.println("Attributes are unset properly")
} else {
fmt.println(matches)
}