|
13 | 13 | import re
|
14 | 14 | import sys
|
15 | 15 | import getopt
|
| 16 | +from string import ascii_letters |
16 | 17 | from os.path import join, splitext, abspath, exists
|
17 | 18 | from collections import defaultdict
|
18 | 19 |
|
@@ -128,6 +129,81 @@ def check_leaked_markup(fn, lines):
|
128 | 129 | yield lno+1, 'possibly leaked markup: %r' % line
|
129 | 130 |
|
130 | 131 |
|
| 132 | +def hide_literal_blocks(lines): |
| 133 | + """Tool to remove literal blocks from given lines. |
| 134 | +
|
| 135 | + It yields empty lines in place of blocks, so line numbers are |
| 136 | + still meaningful. |
| 137 | + """ |
| 138 | + in_block = False |
| 139 | + for line in lines: |
| 140 | + if line.endswith("::\n"): |
| 141 | + in_block = True |
| 142 | + elif in_block: |
| 143 | + if line == "\n" or line.startswith(" "): |
| 144 | + line = "\n" |
| 145 | + else: |
| 146 | + in_block = False |
| 147 | + yield line |
| 148 | + |
| 149 | + |
| 150 | +def type_of_explicit_markup(line): |
| 151 | + if re.match(fr'\.\. {all_directives}::', line): |
| 152 | + return 'directive' |
| 153 | + if re.match(r'\.\. \[[0-9]+\] ', line): |
| 154 | + return 'footnote' |
| 155 | + if re.match(r'\.\. \[[^\]]+\] ', line): |
| 156 | + return 'citation' |
| 157 | + if re.match(r'\.\. _.*[^_]: ', line): |
| 158 | + return 'target' |
| 159 | + if re.match(r'\.\. \|[^\|]*\| ', line): |
| 160 | + return 'substitution_definition' |
| 161 | + return 'comment' |
| 162 | + |
| 163 | + |
| 164 | +def hide_comments(lines): |
| 165 | + """Tool to remove comments from given lines. |
| 166 | +
|
| 167 | + It yields empty lines in place of comments, so line numbers are |
| 168 | + still meaningfull. |
| 169 | + """ |
| 170 | + in_multiline_comment = False |
| 171 | + for line in lines: |
| 172 | + if line == "..\n": |
| 173 | + in_multiline_comment = True |
| 174 | + elif in_multiline_comment: |
| 175 | + if line == "\n" or line.startswith(" "): |
| 176 | + line = "\n" |
| 177 | + else: |
| 178 | + in_multiline_comment = False |
| 179 | + if line.startswith(".. ") and type_of_explicit_markup(line) == 'comment': |
| 180 | + line = "\n" |
| 181 | + yield line |
| 182 | + |
| 183 | + |
| 184 | + |
| 185 | +@checker(".rst", severity=2) |
| 186 | +def check_missing_surrogate_space_on_plural(fn, lines): |
| 187 | + r"""Check for missing 'backslash-space' between a code sample a letter. |
| 188 | +
|
| 189 | + Good: ``Point``\ s |
| 190 | + Bad: ``Point``s |
| 191 | + """ |
| 192 | + in_code_sample = False |
| 193 | + check_next_one = False |
| 194 | + for lno, line in enumerate(hide_comments(hide_literal_blocks(lines))): |
| 195 | + tokens = line.split("``") |
| 196 | + for token_no, token in enumerate(tokens): |
| 197 | + if check_next_one: |
| 198 | + if token[0] in ascii_letters: |
| 199 | + yield lno + 1, f"Missing backslash-space between code sample and {token!r}." |
| 200 | + check_next_one = False |
| 201 | + if token_no == len(tokens) - 1: |
| 202 | + continue |
| 203 | + if in_code_sample: |
| 204 | + check_next_one = True |
| 205 | + in_code_sample = not in_code_sample |
| 206 | + |
131 | 207 | def main(argv):
|
132 | 208 | usage = '''\
|
133 | 209 | Usage: %s [-v] [-f] [-s sev] [-i path]* [path]
|
|
0 commit comments