view doc/docchecker @ 28076:18c6b271579b

doc: translate from :hg:`help config.SECTION` to a valid link to hgrc.5.html Before this patch, ":hg:`help config.SECTION`" in online help text is translated to a link to "hg.1.html#config.SECTION" in HTML unintentionally. This patch translates from :hg:`help config.SECTION` in online help text to a valid link to "hgrc.5.html#SECTION" in HTML. This patch ignores element(s) under "SECTION" (e.g. "ITEM" of ":hg:`help config.SECTION.ITEM`"), because there is no way to refer directly to it in HTML, yet.
author FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
date Thu, 11 Feb 2016 23:15:34 +0900
parents c00f67c15c5a
children 9934362978e1
line wrap: on
line source

#!/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.
import sys
import re

leadingline = re.compile(r'(^\s*)(\S.*)$')
hg_backtick = re.compile(r""":hg:`[^`]*'[^`]*`""")
hg_cramped = re.compile(r'\w:hg:`')

def check(line):
    if hg_backtick.search(line):
        print(line)
        print("""warning: please avoid nesting ' in :hg:`...`""")
    if hg_cramped.search(line):
        print(line)
        print('warning: please have a space before :hg:')

def work(file):
    (llead, lline) = ('', '')

    for line in file:
        # this section unwraps lines
        match = leadingline.match(line)
        if not match:
            check(lline)
            (llead, lline) = ('', '')
            continue

        lead, line = match.group(1), match.group(2)
        if (lead == llead):
            if (lline != ''):
                lline += ' ' + line
            else:
                lline = line
        else:
            check(lline)
            (llead, lline) = (lead, line)
    check(lline)

def main():
    for f in sys.argv[1:]:
        try:
            with open(f) as file:
                work(file)
        except BaseException as e:
            print("failed to process %s: %s" % (f, e))

main()