docchecker
76 lines
| 1.9 KiB
| text/plain
|
TextLexer
/ doc / docchecker
|
r27730 | #!/usr/bin/env python | ||
# | ||||
# docchecker - look for problematic markup | ||||
# | ||||
# Copyright 2016 timeless <timeless@mozdev.org> and others | ||||
# | ||||
# This software may be used and distributed according to the terms of the | ||||
# GNU General Public License version 2 or any later version. | ||||
|
r29168 | |||
|
r29169 | from __future__ import absolute_import, print_function | ||
|
r29168 | |||
|
r41042 | import os | ||
|
r29168 | import re | ||
|
r27730 | import sys | ||
|
r41042 | try: | ||
import msvcrt | ||||
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) | ||||
msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY) | ||||
except ImportError: | ||||
pass | ||||
stdout = getattr(sys.stdout, 'buffer', sys.stdout) | ||||
leadingline = re.compile(br'(^\s*)(\S.*)$') | ||||
|
r28810 | |||
checks = [ | ||||
|
r41042 | (br""":hg:`[^`]*'[^`]*`""", | ||
b"""warning: please avoid nesting ' in :hg:`...`"""), | ||||
(br'\w:hg:`', | ||||
b'warning: please have a space before :hg:'), | ||||
(br"""(?:[^a-z][^'.])hg ([^,;"`]*'(?!hg)){2}""", | ||||
b'''warning: please use " instead of ' for hg ... "..."'''), | ||||
|
r28810 | ] | ||
|
r27730 | |||
def check(line): | ||||
|
r28810 | messages = [] | ||
for match, msg in checks: | ||||
if re.search(match, line): | ||||
messages.append(msg) | ||||
if messages: | ||||
|
r41042 | stdout.write(b'%s\n' % line) | ||
|
r28810 | for msg in messages: | ||
|
r41042 | stdout.write(b'%s\n' % msg) | ||
|
r27730 | |||
def work(file): | ||||
|
r41042 | (llead, lline) = (b'', b'') | ||
|
r27730 | |||
|
r28049 | for line in file: | ||
# this section unwraps lines | ||||
match = leadingline.match(line) | ||||
if not match: | ||||
check(lline) | ||||
|
r41042 | (llead, lline) = (b'', b'') | ||
|
r28049 | continue | ||
|
r27730 | |||
|
r28049 | lead, line = match.group(1), match.group(2) | ||
if (lead == llead): | ||||
|
r41042 | if (lline != b''): | ||
lline += b' ' + line | ||||
|
r28049 | else: | ||
lline = line | ||||
else: | ||||
check(lline) | ||||
(llead, lline) = (lead, line) | ||||
check(lline) | ||||
|
r27730 | |||
def main(): | ||||
|
r28049 | for f in sys.argv[1:]: | ||
try: | ||||
|
r41042 | with open(f, 'rb') as file: | ||
|
r28049 | work(file) | ||
except BaseException as e: | ||||
|
r41042 | sys.stdout.write(r"failed to process %s: %s\n" % (f, e)) | ||
|
r27730 | |||
main() | ||||