|
| 1 | +# Report unsupported block titles. |
| 2 | +--- |
| 3 | +extends: script |
| 4 | +message: "Block titles can only be assigned to examples, figures, and tables in DITA." |
| 5 | +level: warning |
| 6 | +link: https://github.com/jhradilek/asciidoctor-dita-vale/blob/main/README.md#warnings |
| 7 | +scope: raw |
| 8 | +script: | |
| 9 | + text := import("text") |
| 10 | + matches := [] |
| 11 | +
|
| 12 | + r_add_resources := text.re_compile("^\\.{1,2}Additional resources[ \\t]*$") |
| 13 | + r_attribute_list := text.re_compile("^\\[(?:|[\\w.#%{,\"'].*)\\][ \\t]*$") |
| 14 | + r_attribute := text.re_compile("^:!?\\S[^:]*:") |
| 15 | + r_block_title := text.re_compile("^\\.{1,2}[^ \\t.].*$") |
| 16 | + r_code_block := text.re_compile("^(?:\\.{4,}|-{4,})[ \\t]*$") |
| 17 | + r_comment_block := text.re_compile("^/{4,}\\s*$") |
| 18 | + r_comment_line := text.re_compile("^(//|//[^/].*)$") |
| 19 | + r_conditional := text.re_compile("^(?:ifn?def|ifeval|endif)::\\S*\\[.*\\][ \\t]*$") |
| 20 | + r_content_type := text.re_compile("^:_(?:mod-docs-content|content|module)-type:[ \\t]+(?i:procedure)") |
| 21 | + r_empty_line := text.re_compile("^[ \\t]*$") |
| 22 | + r_example_block := text.re_compile("^\\[example\\][ \\t]*$") |
| 23 | + r_example_delim := text.re_compile("^={4,}[ \\t]*$") |
| 24 | + r_image := text.re_compile("^image::(?:\\S|\\S.*\\S)\\[.*\\][ \\t]*$") |
| 25 | + r_supported_title := text.re_compile("^\\.{1,2}(?:Prerequisites?|Procedure|Verification|Results?|Troubleshooting|Troubleshooting steps?|Next steps?)[ \\t]*$") |
| 26 | + r_table_cell := text.re_compile("^\\.[^ \\t|]+\\|") |
| 27 | + r_table := text.re_compile("^\\|={3,}[ \\t]*$") |
| 28 | +
|
| 29 | + document := text.split(text.trim_suffix(scope, "\n"), "\n") |
| 30 | +
|
| 31 | + in_code_block := false |
| 32 | + in_comment_block := false |
| 33 | + is_procedure := false |
| 34 | + expect_block := false |
| 35 | + start := 0 |
| 36 | + end := 0 |
| 37 | +
|
| 38 | + for line in document { |
| 39 | + start += end |
| 40 | + end = len(line) + 1 |
| 41 | +
|
| 42 | + if r_comment_block.match(line) { |
| 43 | + delimiter := text.trim_space(line) |
| 44 | + if ! in_comment_block { |
| 45 | + in_comment_block = delimiter |
| 46 | + } else if in_comment_block == delimiter { |
| 47 | + in_comment_block = false |
| 48 | + } |
| 49 | + continue |
| 50 | + } |
| 51 | + if in_comment_block { continue } |
| 52 | + if r_comment_line.match(line) { continue } |
| 53 | +
|
| 54 | + if r_code_block.match(line) { |
| 55 | + delimiter := text.trim_space(line) |
| 56 | + if ! in_code_block { |
| 57 | + in_code_block = delimiter |
| 58 | + } else if in_code_block == delimiter { |
| 59 | + in_code_block = false |
| 60 | + } |
| 61 | + continue |
| 62 | + } |
| 63 | + if in_code_block { continue } |
| 64 | +
|
| 65 | + if r_content_type.match(line) { |
| 66 | + is_procedure = true |
| 67 | + continue |
| 68 | + } |
| 69 | +
|
| 70 | + if r_attribute_list.match(line) && ! r_example_block.match(line) { continue } |
| 71 | + if r_attribute.match(line) { continue } |
| 72 | + if r_conditional.match(line) { continue } |
| 73 | + if r_empty_line.match(line) { continue } |
| 74 | +
|
| 75 | + if r_block_title.match(line) { |
| 76 | + if is_procedure && r_supported_title.match(line) { continue } |
| 77 | + if r_add_resources.match(line) { continue } |
| 78 | + if r_table_cell.match(line) { continue } |
| 79 | +
|
| 80 | + expect_block = {begin: start, end: start + end -1} |
| 81 | + continue |
| 82 | + } |
| 83 | +
|
| 84 | + if r_table.match(line) || r_image.match(line) || |
| 85 | + r_example_block.match(line) || r_example_delim.match(line) { |
| 86 | + expect_block = false |
| 87 | + continue |
| 88 | + } |
| 89 | +
|
| 90 | + if expect_block { |
| 91 | + matches = append(matches, expect_block) |
| 92 | + expect_block = false |
| 93 | + } |
| 94 | + } |
| 95 | +
|
| 96 | + if expect_block { |
| 97 | + matches = append(matches, expect_block) |
| 98 | + } |
0 commit comments