doc/docchecker
author Pulkit Goyal <pulkit@yandex-team.ru>
Sat, 02 Mar 2019 05:12:45 +0530
changeset 41833 bce2356ece68
parent 41016 9bfbb9fc5871
child 43731 47ef023d0165
permissions -rwxr-xr-x
py3: port things from chgserver.py I have installed mercurial on my personal laptop using Python 3.7. I also have hg aliased to chg. Before this commit, `hg version` didn't work. After this patch, things are better and now chg works. Differential Revision: https://phab.mercurial-scm.org/D6040
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27730
ed86fb2a4187 docchecker: introduce a way to check for poor markup
timeless <timeless@mozdev.org>
parents:
diff changeset
     1
#!/usr/bin/env python
ed86fb2a4187 docchecker: introduce a way to check for poor markup
timeless <timeless@mozdev.org>
parents:
diff changeset
     2
#
ed86fb2a4187 docchecker: introduce a way to check for poor markup
timeless <timeless@mozdev.org>
parents:
diff changeset
     3
# docchecker - look for problematic markup
ed86fb2a4187 docchecker: introduce a way to check for poor markup
timeless <timeless@mozdev.org>
parents:
diff changeset
     4
#
ed86fb2a4187 docchecker: introduce a way to check for poor markup
timeless <timeless@mozdev.org>
parents:
diff changeset
     5
# Copyright 2016 timeless <timeless@mozdev.org> and others
ed86fb2a4187 docchecker: introduce a way to check for poor markup
timeless <timeless@mozdev.org>
parents:
diff changeset
     6
#
ed86fb2a4187 docchecker: introduce a way to check for poor markup
timeless <timeless@mozdev.org>
parents:
diff changeset
     7
# This software may be used and distributed according to the terms of the
ed86fb2a4187 docchecker: introduce a way to check for poor markup
timeless <timeless@mozdev.org>
parents:
diff changeset
     8
# GNU General Public License version 2 or any later version.
29168
8f2805ce93d9 py3: make doc/docchecker use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28811
diff changeset
     9
29169
c9ab5a0bc7c5 py3: make doc/docchecker use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29168
diff changeset
    10
from __future__ import absolute_import, print_function
29168
8f2805ce93d9 py3: make doc/docchecker use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28811
diff changeset
    11
41016
9bfbb9fc5871 py3: byteify docchecker
Matt Harbison <matt_harbison@yahoo.com>
parents: 29169
diff changeset
    12
import os
29168
8f2805ce93d9 py3: make doc/docchecker use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28811
diff changeset
    13
import re
27730
ed86fb2a4187 docchecker: introduce a way to check for poor markup
timeless <timeless@mozdev.org>
parents:
diff changeset
    14
import sys
ed86fb2a4187 docchecker: introduce a way to check for poor markup
timeless <timeless@mozdev.org>
parents:
diff changeset
    15
41016
9bfbb9fc5871 py3: byteify docchecker
Matt Harbison <matt_harbison@yahoo.com>
parents: 29169
diff changeset
    16
try:
9bfbb9fc5871 py3: byteify docchecker
Matt Harbison <matt_harbison@yahoo.com>
parents: 29169
diff changeset
    17
    import msvcrt
9bfbb9fc5871 py3: byteify docchecker
Matt Harbison <matt_harbison@yahoo.com>
parents: 29169
diff changeset
    18
    msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
9bfbb9fc5871 py3: byteify docchecker
Matt Harbison <matt_harbison@yahoo.com>
parents: 29169
diff changeset
    19
    msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
9bfbb9fc5871 py3: byteify docchecker
Matt Harbison <matt_harbison@yahoo.com>
parents: 29169
diff changeset
    20
except ImportError:
9bfbb9fc5871 py3: byteify docchecker
Matt Harbison <matt_harbison@yahoo.com>
parents: 29169
diff changeset
    21
    pass
9bfbb9fc5871 py3: byteify docchecker
Matt Harbison <matt_harbison@yahoo.com>
parents: 29169
diff changeset
    22
9bfbb9fc5871 py3: byteify docchecker
Matt Harbison <matt_harbison@yahoo.com>
parents: 29169
diff changeset
    23
stdout = getattr(sys.stdout, 'buffer', sys.stdout)
9bfbb9fc5871 py3: byteify docchecker
Matt Harbison <matt_harbison@yahoo.com>
parents: 29169
diff changeset
    24
9bfbb9fc5871 py3: byteify docchecker
Matt Harbison <matt_harbison@yahoo.com>
parents: 29169
diff changeset
    25
leadingline = re.compile(br'(^\s*)(\S.*)$')
28810
9934362978e1 docchecker: report context line at most once
timeless <timeless@mozdev.org>
parents: 28049
diff changeset
    26
9934362978e1 docchecker: report context line at most once
timeless <timeless@mozdev.org>
parents: 28049
diff changeset
    27
checks = [
41016
9bfbb9fc5871 py3: byteify docchecker
Matt Harbison <matt_harbison@yahoo.com>
parents: 29169
diff changeset
    28
  (br""":hg:`[^`]*'[^`]*`""",
9bfbb9fc5871 py3: byteify docchecker
Matt Harbison <matt_harbison@yahoo.com>
parents: 29169
diff changeset
    29
   b"""warning: please avoid nesting ' in :hg:`...`"""),
9bfbb9fc5871 py3: byteify docchecker
Matt Harbison <matt_harbison@yahoo.com>
parents: 29169
diff changeset
    30
  (br'\w:hg:`',
9bfbb9fc5871 py3: byteify docchecker
Matt Harbison <matt_harbison@yahoo.com>
parents: 29169
diff changeset
    31
   b'warning: please have a space before :hg:'),
9bfbb9fc5871 py3: byteify docchecker
Matt Harbison <matt_harbison@yahoo.com>
parents: 29169
diff changeset
    32
  (br"""(?:[^a-z][^'.])hg ([^,;"`]*'(?!hg)){2}""",
9bfbb9fc5871 py3: byteify docchecker
Matt Harbison <matt_harbison@yahoo.com>
parents: 29169
diff changeset
    33
   b'''warning: please use " instead of ' for hg ... "..."'''),
28810
9934362978e1 docchecker: report context line at most once
timeless <timeless@mozdev.org>
parents: 28049
diff changeset
    34
]
27730
ed86fb2a4187 docchecker: introduce a way to check for poor markup
timeless <timeless@mozdev.org>
parents:
diff changeset
    35
ed86fb2a4187 docchecker: introduce a way to check for poor markup
timeless <timeless@mozdev.org>
parents:
diff changeset
    36
def check(line):
28810
9934362978e1 docchecker: report context line at most once
timeless <timeless@mozdev.org>
parents: 28049
diff changeset
    37
    messages = []
9934362978e1 docchecker: report context line at most once
timeless <timeless@mozdev.org>
parents: 28049
diff changeset
    38
    for match, msg in checks:
9934362978e1 docchecker: report context line at most once
timeless <timeless@mozdev.org>
parents: 28049
diff changeset
    39
        if re.search(match, line):
9934362978e1 docchecker: report context line at most once
timeless <timeless@mozdev.org>
parents: 28049
diff changeset
    40
            messages.append(msg)
9934362978e1 docchecker: report context line at most once
timeless <timeless@mozdev.org>
parents: 28049
diff changeset
    41
    if messages:
41016
9bfbb9fc5871 py3: byteify docchecker
Matt Harbison <matt_harbison@yahoo.com>
parents: 29169
diff changeset
    42
        stdout.write(b'%s\n' % line)
28810
9934362978e1 docchecker: report context line at most once
timeless <timeless@mozdev.org>
parents: 28049
diff changeset
    43
        for msg in messages:
41016
9bfbb9fc5871 py3: byteify docchecker
Matt Harbison <matt_harbison@yahoo.com>
parents: 29169
diff changeset
    44
            stdout.write(b'%s\n' % msg)
27730
ed86fb2a4187 docchecker: introduce a way to check for poor markup
timeless <timeless@mozdev.org>
parents:
diff changeset
    45
ed86fb2a4187 docchecker: introduce a way to check for poor markup
timeless <timeless@mozdev.org>
parents:
diff changeset
    46
def work(file):
41016
9bfbb9fc5871 py3: byteify docchecker
Matt Harbison <matt_harbison@yahoo.com>
parents: 29169
diff changeset
    47
    (llead, lline) = (b'', b'')
27730
ed86fb2a4187 docchecker: introduce a way to check for poor markup
timeless <timeless@mozdev.org>
parents:
diff changeset
    48
28049
c00f67c15c5a docchecker: use indentation of 4 spaces
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28048
diff changeset
    49
    for line in file:
c00f67c15c5a docchecker: use indentation of 4 spaces
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28048
diff changeset
    50
        # this section unwraps lines
c00f67c15c5a docchecker: use indentation of 4 spaces
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28048
diff changeset
    51
        match = leadingline.match(line)
c00f67c15c5a docchecker: use indentation of 4 spaces
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28048
diff changeset
    52
        if not match:
c00f67c15c5a docchecker: use indentation of 4 spaces
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28048
diff changeset
    53
            check(lline)
41016
9bfbb9fc5871 py3: byteify docchecker
Matt Harbison <matt_harbison@yahoo.com>
parents: 29169
diff changeset
    54
            (llead, lline) = (b'', b'')
28049
c00f67c15c5a docchecker: use indentation of 4 spaces
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28048
diff changeset
    55
            continue
27730
ed86fb2a4187 docchecker: introduce a way to check for poor markup
timeless <timeless@mozdev.org>
parents:
diff changeset
    56
28049
c00f67c15c5a docchecker: use indentation of 4 spaces
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28048
diff changeset
    57
        lead, line = match.group(1), match.group(2)
c00f67c15c5a docchecker: use indentation of 4 spaces
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28048
diff changeset
    58
        if (lead == llead):
41016
9bfbb9fc5871 py3: byteify docchecker
Matt Harbison <matt_harbison@yahoo.com>
parents: 29169
diff changeset
    59
            if (lline != b''):
9bfbb9fc5871 py3: byteify docchecker
Matt Harbison <matt_harbison@yahoo.com>
parents: 29169
diff changeset
    60
                lline += b' ' + line
28049
c00f67c15c5a docchecker: use indentation of 4 spaces
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28048
diff changeset
    61
            else:
c00f67c15c5a docchecker: use indentation of 4 spaces
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28048
diff changeset
    62
                lline = line
c00f67c15c5a docchecker: use indentation of 4 spaces
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28048
diff changeset
    63
        else:
c00f67c15c5a docchecker: use indentation of 4 spaces
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28048
diff changeset
    64
            check(lline)
c00f67c15c5a docchecker: use indentation of 4 spaces
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28048
diff changeset
    65
            (llead, lline) = (lead, line)
c00f67c15c5a docchecker: use indentation of 4 spaces
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28048
diff changeset
    66
    check(lline)
27730
ed86fb2a4187 docchecker: introduce a way to check for poor markup
timeless <timeless@mozdev.org>
parents:
diff changeset
    67
ed86fb2a4187 docchecker: introduce a way to check for poor markup
timeless <timeless@mozdev.org>
parents:
diff changeset
    68
def main():
28049
c00f67c15c5a docchecker: use indentation of 4 spaces
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28048
diff changeset
    69
    for f in sys.argv[1:]:
c00f67c15c5a docchecker: use indentation of 4 spaces
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28048
diff changeset
    70
        try:
41016
9bfbb9fc5871 py3: byteify docchecker
Matt Harbison <matt_harbison@yahoo.com>
parents: 29169
diff changeset
    71
            with open(f, 'rb') as file:
28049
c00f67c15c5a docchecker: use indentation of 4 spaces
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28048
diff changeset
    72
                work(file)
c00f67c15c5a docchecker: use indentation of 4 spaces
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28048
diff changeset
    73
        except BaseException as e:
41016
9bfbb9fc5871 py3: byteify docchecker
Matt Harbison <matt_harbison@yahoo.com>
parents: 29169
diff changeset
    74
            sys.stdout.write(r"failed to process %s: %s\n" % (f, e))
27730
ed86fb2a4187 docchecker: introduce a way to check for poor markup
timeless <timeless@mozdev.org>
parents:
diff changeset
    75
ed86fb2a4187 docchecker: introduce a way to check for poor markup
timeless <timeless@mozdev.org>
parents:
diff changeset
    76
main()