Show More
@@ -1,164 +1,168 b'' | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | # |
|
3 | 3 | # checkseclevel - checking section title levels in each online help document |
|
4 | 4 | |
|
5 | 5 | import sys, os |
|
6 | 6 | import optparse |
|
7 | 7 | |
|
8 | 8 | # import from the live mercurial repo |
|
9 | 9 | os.environ['HGMODULEPOLICY'] = 'py' |
|
10 | 10 | sys.path.insert(0, "..") |
|
11 | 11 | from mercurial import demandimport; demandimport.enable() |
|
12 | 12 | from mercurial.commands import table |
|
13 | 13 | from mercurial.help import helptable |
|
14 | 14 | from mercurial import extensions |
|
15 | 15 | from mercurial import minirst |
|
16 | 16 | from mercurial import ui as uimod |
|
17 | 17 | |
|
18 | 18 | level2mark = ['"', '=', '-', '.', '#'] |
|
19 | 19 | reservedmarks = ['"'] |
|
20 | 20 | |
|
21 | 21 | mark2level = {} |
|
22 | 22 | for m, l in zip(level2mark, xrange(len(level2mark))): |
|
23 | 23 | if m not in reservedmarks: |
|
24 | 24 | mark2level[m] = l |
|
25 | 25 | |
|
26 | 26 | initlevel_topic = 0 |
|
27 | 27 | initlevel_cmd = 1 |
|
28 | 28 | initlevel_ext = 1 |
|
29 | 29 | initlevel_ext_cmd = 3 |
|
30 | 30 | |
|
31 | 31 | def showavailables(ui, initlevel): |
|
32 | 32 | ui.warn((' available marks and order of them in this help: %s\n') % |
|
33 | 33 | (', '.join(['%r' % (m * 4) for m in level2mark[initlevel + 1:]]))) |
|
34 | 34 | |
|
35 | 35 | def checkseclevel(ui, doc, name, initlevel): |
|
36 | 36 | ui.note(('checking "%s"\n') % name) |
|
37 | 37 | blocks, pruned = minirst.parse(doc, 0, ['verbose']) |
|
38 | 38 | errorcnt = 0 |
|
39 | 39 | curlevel = initlevel |
|
40 | 40 | for block in blocks: |
|
41 | 41 | if block['type'] != 'section': |
|
42 | 42 | continue |
|
43 | 43 | mark = block['underline'] |
|
44 | 44 | title = block['lines'][0] |
|
45 | 45 | if (mark not in mark2level) or (mark2level[mark] <= initlevel): |
|
46 | 46 | ui.warn(('invalid section mark %r for "%s" of %s\n') % |
|
47 | 47 | (mark * 4, title, name)) |
|
48 | 48 | showavailables(ui, initlevel) |
|
49 | 49 | errorcnt += 1 |
|
50 | 50 | continue |
|
51 | 51 | nextlevel = mark2level[mark] |
|
52 | 52 | if curlevel < nextlevel and curlevel + 1 != nextlevel: |
|
53 | 53 | ui.warn(('gap of section level at "%s" of %s\n') % |
|
54 | 54 | (title, name)) |
|
55 | 55 | showavailables(ui, initlevel) |
|
56 | 56 | errorcnt += 1 |
|
57 | 57 | continue |
|
58 | 58 | ui.note(('appropriate section level for "%s %s"\n') % |
|
59 | 59 | (mark * (nextlevel * 2), title)) |
|
60 | 60 | curlevel = nextlevel |
|
61 | 61 | |
|
62 | 62 | return errorcnt |
|
63 | 63 | |
|
64 | 64 | def checkcmdtable(ui, cmdtable, namefmt, initlevel): |
|
65 | 65 | errorcnt = 0 |
|
66 | 66 | for k, entry in cmdtable.items(): |
|
67 | 67 | name = k.split("|")[0].lstrip("^") |
|
68 | 68 | if not entry[0].__doc__: |
|
69 | 69 | ui.note(('skip checking %s: no help document\n') % |
|
70 | 70 | (namefmt % name)) |
|
71 | 71 | continue |
|
72 | 72 | errorcnt += checkseclevel(ui, entry[0].__doc__, |
|
73 | 73 | namefmt % name, |
|
74 | 74 | initlevel) |
|
75 | 75 | return errorcnt |
|
76 | 76 | |
|
77 | 77 | def checkhghelps(ui): |
|
78 | 78 | errorcnt = 0 |
|
79 | 79 | for names, sec, doc in helptable: |
|
80 | 80 | if callable(doc): |
|
81 | 81 | doc = doc(ui) |
|
82 | 82 | errorcnt += checkseclevel(ui, doc, |
|
83 | 83 | '%s help topic' % names[0], |
|
84 | 84 | initlevel_topic) |
|
85 | 85 | |
|
86 | 86 | errorcnt += checkcmdtable(ui, table, '%s command', initlevel_cmd) |
|
87 | 87 | |
|
88 | 88 | for name in sorted(extensions.enabled().keys() + |
|
89 | 89 | extensions.disabled().keys()): |
|
90 | 90 | mod = extensions.load(None, name, None) |
|
91 | 91 | if not mod.__doc__: |
|
92 | 92 | ui.note(('skip checking %s extension: no help document\n') % name) |
|
93 | 93 | continue |
|
94 | 94 | errorcnt += checkseclevel(ui, mod.__doc__, |
|
95 | 95 | '%s extension' % name, |
|
96 | 96 | initlevel_ext) |
|
97 | 97 | |
|
98 | 98 | cmdtable = getattr(mod, 'cmdtable', None) |
|
99 | 99 | if cmdtable: |
|
100 | 100 | errorcnt += checkcmdtable(ui, cmdtable, |
|
101 | 101 | '%s command of ' + name + ' extension', |
|
102 | 102 | initlevel_ext_cmd) |
|
103 | 103 | return errorcnt |
|
104 | 104 | |
|
105 | 105 | def checkfile(ui, filename, initlevel): |
|
106 | 106 | if filename == '-': |
|
107 | 107 | filename = 'stdin' |
|
108 | 108 | doc = sys.stdin.read() |
|
109 | 109 | else: |
|
110 | 110 | fp = open(filename) |
|
111 | 111 | try: |
|
112 | 112 | doc = fp.read() |
|
113 | 113 | finally: |
|
114 | 114 | fp.close() |
|
115 | 115 | |
|
116 | 116 | ui.note(('checking input from %s with initlevel %d\n') % |
|
117 | 117 | (filename, initlevel)) |
|
118 | 118 | return checkseclevel(ui, doc, 'input from %s' % filename, initlevel) |
|
119 | 119 | |
|
120 | 120 | def main(): |
|
121 | 121 | optparser = optparse.OptionParser("""%prog [options] |
|
122 | 122 | |
|
123 | 123 | This checks all help documents of Mercurial (topics, commands, |
|
124 | 124 | extensions and commands of them), if no file is specified by --file |
|
125 | 125 | option. |
|
126 | 126 | """) |
|
127 | 127 | optparser.add_option("-v", "--verbose", |
|
128 | 128 | help="enable additional output", |
|
129 | 129 | action="store_true") |
|
130 | optparser.add_option("-d", "--debug", | |
|
131 | help="debug mode", | |
|
132 | action="store_true") | |
|
130 | 133 | optparser.add_option("-f", "--file", |
|
131 | 134 | help="filename to read in (or '-' for stdin)", |
|
132 | 135 | action="store", default="") |
|
133 | 136 | |
|
134 | 137 | optparser.add_option("-t", "--topic", |
|
135 | 138 | help="parse file as help topic", |
|
136 | 139 | action="store_const", dest="initlevel", const=0) |
|
137 | 140 | optparser.add_option("-c", "--command", |
|
138 | 141 | help="parse file as help of core command", |
|
139 | 142 | action="store_const", dest="initlevel", const=1) |
|
140 | 143 | optparser.add_option("-e", "--extension", |
|
141 | 144 | help="parse file as help of extension", |
|
142 | 145 | action="store_const", dest="initlevel", const=1) |
|
143 | 146 | optparser.add_option("-C", "--extension-command", |
|
144 | 147 | help="parse file as help of extension command", |
|
145 | 148 | action="store_const", dest="initlevel", const=3) |
|
146 | 149 | |
|
147 | 150 | optparser.add_option("-l", "--initlevel", |
|
148 | 151 | help="set initial section level manually", |
|
149 | 152 | action="store", type="int", default=0) |
|
150 | 153 | |
|
151 | 154 | (options, args) = optparser.parse_args() |
|
152 | 155 | |
|
153 | 156 | ui = uimod.ui() |
|
154 | 157 | ui.setconfig('ui', 'verbose', options.verbose, '--verbose') |
|
158 | ui.setconfig('ui', 'debug', options.debug, '--debug') | |
|
155 | 159 | |
|
156 | 160 | if options.file: |
|
157 | 161 | if checkfile(ui, options.file, options.initlevel): |
|
158 | 162 | sys.exit(1) |
|
159 | 163 | else: |
|
160 | 164 | if checkhghelps(ui): |
|
161 | 165 | sys.exit(1) |
|
162 | 166 | |
|
163 | 167 | if __name__ == "__main__": |
|
164 | 168 | main() |
General Comments 0
You need to be logged in to leave comments.
Login now