forked from redhat-documentation/vale-at-red-hat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHardWrappedLines.tengo
62 lines (55 loc) · 1.98 KB
/
HardWrappedLines.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
/*
Tengo Language
Checks that lines are not hard-wrapped.
$ tengo HardWrappedLines.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"
sentence_regex := "^.*(\\.|\\?|\\!|:)$"
list_regex := "^(\\.|\\*|-).*$"
hard_wrap_80_regex := "^.{65,80}$"
hard_wrap_100_regex := "^.{75,100}$"
asciidoc_markup := "^(.*\\[.*\\]|\\/\\/.*|=+.*|<(\\.|\\d+)>.*|>|#+.*|\\..+|\\|.*|a\\|.*|:.+|.*::|`.*`|ifdef::|endif::|image::|include::|link::|video::).*$"
listingblock_delim_regex := "^-{4}$"
inside_listingblock := false
for line in text.split(scope, "\n") {
// trim trailing whitespace
line = text.trim_space(line)
//ignore content in codeblocks
if text.re_match(listingblock_delim_regex, line) && inside_listingblock == false {
inside_listingblock = true
} else if text.re_match(listingblock_delim_regex, line) && inside_listingblock == true {
inside_listingblock = false
}
//ignore sentences
if !text.re_match(sentence_regex, line) && inside_listingblock == false {
//ignore AsciiDoc elements
if !text.re_match(asciidoc_markup, line) {
//ignore lists
if !text.re_match(list_regex, line) {
//match hard wrapped lines at 80
if text.re_match(hard_wrap_80_regex, line) && inside_listingblock == false {
start := text.index(scope, line)
matches = append(matches, {begin: start, end: start + len(line)})
//match hard wrapped lines at 100
} else if text.re_match(hard_wrap_100_regex, line) && inside_listingblock == false {
start := text.index(scope, line)
matches = append(matches, {begin: start, end: start + len(line)})
}
}
}
}
}
if len(matches) == 0 {
fmt.println("No hard-wrapped lines detected")
} else {
fmt.println(matches)
}