|
| 1 | +--- |
| 2 | +extends: script |
| 3 | +message: "Numbered callout does not follow sequentially." |
| 4 | +level: warning |
| 5 | +link: https://docs.asciidoctor.org/asciidoc/latest/verbatim/callouts/ |
| 6 | +scope: raw |
| 7 | +script: | |
| 8 | + text := import("text") |
| 9 | + matches := [] |
| 10 | +
|
| 11 | + // clean out multi-line comments |
| 12 | + scope = text.re_replace("(?s) *(\n////.*?////\n)", scope, "") |
| 13 | + //add a newline, it might be missing |
| 14 | + scope += "\n" |
| 15 | +
|
| 16 | + prev_num := 0 |
| 17 | + callout_regex := "^<(\\d+)>" |
| 18 | + listingblock_delim_regex := "^-{4,}$" |
| 19 | + if_regex := "^ifdef::|ifndef::" |
| 20 | + endif_regex := "^endif::\\[\\]" |
| 21 | + inside_if := false |
| 22 | +
|
| 23 | + for line in text.split(scope, "\n") { |
| 24 | + // trim trailing whitespace |
| 25 | + line = text.trim_space(line) |
| 26 | +
|
| 27 | + // check if we're entering a conditional block |
| 28 | + if text.re_match(if_regex, line) { |
| 29 | + inside_if = true |
| 30 | + } else if text.re_match(endif_regex, line) { |
| 31 | + inside_if = false |
| 32 | + } |
| 33 | +
|
| 34 | + //reset count if we hit a listing block delimiter |
| 35 | + if text.re_match(listingblock_delim_regex, line) { |
| 36 | + prev_num = 0 |
| 37 | + } |
| 38 | +
|
| 39 | + //only count callouts where there are no ifdefs |
| 40 | + if !inside_if { |
| 41 | + if text.re_match(callout_regex, line) { |
| 42 | + callout := text.re_find("<(\\d+)>", line) |
| 43 | + for key, value in callout { |
| 44 | + //trim angle brackets from string |
| 45 | + trimmed := callout[key][0]["text"] |
| 46 | + trimmed = text.trim_prefix(trimmed, "<") |
| 47 | + trimmed = text.trim_suffix(trimmed, ">") |
| 48 | + //cast string > int |
| 49 | + num := text.atoi(trimmed) |
| 50 | + //start counting |
| 51 | + if num != prev_num+1 { |
| 52 | + start := text.index(scope, line) |
| 53 | + matches = append(matches, {begin: start, end: start + len(line)}) |
| 54 | + } |
| 55 | + prev_num = num |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
0 commit comments