| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# docchecker - look for problematic markup |
| 4 |
# |
| 5 |
# Copyright 2016 timeless <[email protected]> and others |
| 6 |
# |
| 7 |
# This software may be used and distributed according to the terms of the |
| 8 |
# GNU General Public License version 2 or any later version. |
| 9 |
|
| 10 |
|
| 11 |
import os |
| 12 |
import re |
| 13 |
import sys |
| 14 |
|
| 15 |
try: |
| 16 |
import msvcrt |
| 17 |
|
| 18 |
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) |
| 19 |
msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY) |
| 20 |
except ImportError: |
| 21 |
pass |
| 22 |
|
| 23 |
stdout = getattr(sys.stdout, 'buffer', sys.stdout) |
| 24 |
|
| 25 |
leadingline = re.compile(br'(^\s*)(\S.*)$') |
| 26 |
|
| 27 |
checks = [ |
| 28 |
( |
| 29 |
br""":hg:`[^`]*'[^`]*`""", |
| 30 |
b"""warning: please avoid nesting ' in :hg:`...`""", |
| 31 |
), |
| 32 |
(br'\w:hg:`', b'warning: please have a space before :hg:'), |
| 33 |
( |
| 34 |
br"""(?:[^a-z][^'.])hg ([^,;"`]*'(?!hg)){2}""", |
| 35 |
b'''warning: please use " instead of ' for hg ... "..."''', |
| 36 |
), |
| 37 |
] |
| 38 |
|
| 39 |
|
| 40 |
def check(line): |
| 41 |
messages = [] |
| 42 |
for match, msg in checks: |
| 43 |
if re.search(match, line): |
| 44 |
messages.append(msg) |
| 45 |
if messages: |
| 46 |
stdout.write(b'%s\n' % line) |
| 47 |
for msg in messages: |
| 48 |
stdout.write(b'%s\n' % msg) |
| 49 |
|
| 50 |
|
| 51 |
def work(file): |
| 52 |
(llead, lline) = (b'', b'') |
| 53 |
|
| 54 |
for line in file: |
| 55 |
# this section unwraps lines |
| 56 |
match = leadingline.match(line) |
| 57 |
if not match: |
| 58 |
check(lline) |
| 59 |
(llead, lline) = (b'', b'') |
| 60 |
continue |
| 61 |
|
| 62 |
lead, line = match.group(1), match.group(2) |
| 63 |
if lead == llead: |
| 64 |
if lline != b'': |
| 65 |
lline += b' ' + line |
| 66 |
else: |
| 67 |
lline = line |
| 68 |
else: |
| 69 |
check(lline) |
| 70 |
(llead, lline) = (lead, line) |
| 71 |
check(lline) |
| 72 |
|
| 73 |
|
| 74 |
def main(): |
| 75 |
for f in sys.argv[1:]: |
| 76 |
try: |
| 77 |
with open(f, 'rb') as file: |
| 78 |
work(file) |
| 79 |
except BaseException as e: |
| 80 |
sys.stdout.write(r"failed to process %s: %s\n" % (f, e)) |
| 81 |
|
| 82 |
|
| 83 |
main() |