annotate Tools/scripts/texcheck.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 5ee46bbed1f9
children 46625e6122e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1 """ TeXcheck.py -- rough syntax checking on Python style LaTeX documents.
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
3 Written by Raymond D. Hettinger <python at rcn.com>
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
4 Copyright (c) 2003 Python Software Foundation. All rights reserved.
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
5
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
6 Designed to catch common markup errors including:
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
7 * Unbalanced or mismatched parenthesis, brackets, and braces.
29126
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
8 * Unbalanced or mismatched \\begin and \\end blocks.
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
9 * Misspelled or invalid LaTeX commands.
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
10 * Use of forward slashes instead of backslashes for commands.
29126
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
11 * Table line size mismatches.
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
12
29126
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
13 Sample command line usage:
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
14 python texcheck.py -k chapterheading -m lib/librandomtex *.tex
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
15
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
16 Options:
29091
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
17 -m Munge parenthesis and brackets. [0,n) would normally mismatch.
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
18 -k keyword: Keyword is a valid LaTeX command. Do not include the backslash.
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
19 -d: Delimiter check only (useful for non-LaTeX files).
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
20 -h: Help
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
21 -s lineno: Start at lineno (useful for skipping complex sections).
29126
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
22 -v: Verbose. Trace the matching of //begin and //end blocks.
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
23 """
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
24
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
25 import re
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
26 import sys
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
27 import getopt
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
28 from itertools import izip, count, islice
29126
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
29 import glob
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
30
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
31 cmdstr = r"""
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
32 \section \module \declaremodule \modulesynopsis \moduleauthor
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
33 \sectionauthor \versionadded \code \class \method \begin
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
34 \optional \var \ref \end \subsection \lineiii \hline \label
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
35 \indexii \textrm \ldots \keyword \stindex \index \item \note
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
36 \withsubitem \ttindex \footnote \citetitle \samp \opindex
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
37 \noindent \exception \strong \dfn \ctype \obindex \character
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
38 \indexiii \function \bifuncindex \refmodule \refbimodindex
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
39 \subsubsection \nodename \member \chapter \emph \ASCII \UNIX
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
40 \regexp \program \production \token \productioncont \term
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
41 \grammartoken \lineii \seemodule \file \EOF \documentclass
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
42 \usepackage \title \input \maketitle \ifhtml \fi \url \Cpp
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
43 \tableofcontents \kbd \programopt \envvar \refstmodindex
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
44 \cfunction \constant \NULL \moreargs \cfuncline \cdata
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
45 \textasciicircum \n \ABC \setindexsubitem \versionchanged
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
46 \deprecated \seetext \newcommand \POSIX \pep \warning \rfc
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
47 \verbatiminput \methodline \textgreater \seetitle \lineiv
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
48 \funclineni \ulink \manpage \funcline \dataline \unspecified
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
49 \textbackslash \mimetype \mailheader \seepep \textunderscore
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
50 \longprogramopt \infinity \plusminus \shortversion \version
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
51 \refmodindex \seerfc \makeindex \makemodindex \renewcommand
29068
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
52 \indexname \appendix \protect \indexiv \mbox \textasciitilde
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
53 \platform \seeurl \leftmargin \labelwidth \localmoduletable
29091
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
54 \LaTeX \copyright \memberline \backslash \pi \centerline
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
55 \caption \vspace \textwidth \menuselection \textless
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
56 \makevar \csimplemacro \menuselection \bfcode \sub \release
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
57 \email \kwindex \refexmodindex \filenq \e \menuselection
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
58 \exindex \linev \newsgroup \verbatim \setshortversion
32634
ab18ef83f5a6 Add more known macros.
Raymond Hettinger <python@rcn.com>
parents: 32538
diff changeset
59 \author \authoraddress \paragraph \subparagraph \cmemberline
36387
5ee46bbed1f9 Add two missing markup tags.
Georg Brandl <georg@python.org>
parents: 35183
diff changeset
60 \textbar \C \seelink
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
61 """
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
62
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
63 def matchclose(c_lineno, c_symbol, openers, pairmap):
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
64 "Verify that closing delimiter matches most recent opening delimiter"
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
65 try:
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
66 o_lineno, o_symbol = openers.pop()
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
67 except IndexError:
29126
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
68 print "\nDelimiter mismatch. On line %d, encountered closing '%s' without corresponding open" % (c_lineno, c_symbol)
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
69 return
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
70 if o_symbol in pairmap.get(c_symbol, [c_symbol]): return
29126
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
71 print "\nOpener '%s' on line %d was not closed before encountering '%s' on line %d" % (o_symbol, o_lineno, c_symbol, c_lineno)
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
72 return
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
73
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
74 def checkit(source, opts, morecmds=[]):
29068
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
75 """Check the LaTeX formatting in a sequence of lines.
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
76
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
77 Opts is a mapping of options to option values if any:
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
78 -m munge parenthesis and brackets
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
79 -d delimiters only checking
29126
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
80 -v verbose trace of delimiter matching
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
81 -s lineno: linenumber to start scan (default is 1).
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
82
29068
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
83 Morecmds is a sequence of LaTeX commands (without backslashes) that
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
84 are to be considered valid in the scan.
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
85 """
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
86
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
87 texcmd = re.compile(r'\\[A-Za-z]+')
29116
24786c3130ca Forward slash warnings now only occur for potentially valid LaTeX commands.
Raymond Hettinger <python@rcn.com>
parents: 29091
diff changeset
88 falsetexcmd = re.compile(r'\/([A-Za-z]+)') # Mismarked with forward slash
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
89
32636
6f36e02b1f69 Use set() instead of sets.Set()
Raymond Hettinger <python@rcn.com>
parents: 32634
diff changeset
90 validcmds = set(cmdstr.split())
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
91 for cmd in morecmds:
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
92 validcmds.add('\\' + cmd)
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
93
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
94 if '-m' in opts:
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
95 pairmap = {']':'[(', ')':'(['} # Munged openers
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
96 else:
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
97 pairmap = {']':'[', ')':'('} # Normal opener for a given closer
32636
6f36e02b1f69 Use set() instead of sets.Set()
Raymond Hettinger <python@rcn.com>
parents: 32634
diff changeset
98 openpunct = set('([') # Set of valid openers
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
99
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
100 delimiters = re.compile(r'\\(begin|end){([_a-zA-Z]+)}|([()\[\]])')
29091
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
101 braces = re.compile(r'({)|(})')
30209
2db8c63e69cd Added doubled word warnings.
Raymond Hettinger <python@rcn.com>
parents: 29126
diff changeset
102 doubledwords = re.compile(r'(\b[A-za-z]+\b) \b\1\b')
30373
cf7fad9d6fcc Generalize the last change to check for other markup spacing errors.
Raymond Hettinger <python@rcn.com>
parents: 30369
diff changeset
103 spacingmarkup = re.compile(r'\\(ABC|ASCII|C|Cpp|EOF|infinity|NULL|plusminus|POSIX|UNIX)\s')
29091
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
104
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
105 openers = [] # Stack of pending open delimiters
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
106 bracestack = [] # Stack of pending open braces
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
107
29068
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
108 tablestart = re.compile(r'\\begin{(?:long)?table([iv]+)}')
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
109 tableline = re.compile(r'\\line([iv]+){')
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
110 tableend = re.compile(r'\\end{(?:long)?table([iv]+)}')
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
111 tablelevel = ''
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
112 tablestartline = 0
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
113
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
114 startline = int(opts.get('-s', '1'))
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
115 lineno = 0
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
116
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
117 for lineno, line in izip(count(startline), islice(source, startline-1, None)):
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
118 line = line.rstrip()
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
119
29126
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
120 # Check balancing of open/close parenthesis, brackets, and begin/end blocks
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
121 for begend, name, punct in delimiters.findall(line):
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
122 if '-v' in opts:
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
123 print lineno, '|', begend, name, punct,
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
124 if begend == 'begin' and '-d' not in opts:
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
125 openers.append((lineno, name))
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
126 elif punct in openpunct:
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
127 openers.append((lineno, punct))
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
128 elif begend == 'end' and '-d' not in opts:
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
129 matchclose(lineno, name, openers, pairmap)
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
130 elif punct in pairmap:
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
131 matchclose(lineno, punct, openers, pairmap)
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
132 if '-v' in opts:
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
133 print ' --> ', openers
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
134
29091
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
135 # Balance opening and closing braces
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
136 for open, close in braces.findall(line):
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
137 if open == '{':
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
138 bracestack.append(lineno)
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
139 if close == '}':
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
140 try:
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
141 bracestack.pop()
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
142 except IndexError:
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
143 print r'Warning, unmatched } on line %s.' % (lineno,)
29126
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
144
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
145 # Optionally, skip LaTeX specific checks
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
146 if '-d' in opts:
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
147 continue
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
148
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
149 # Warn whenever forward slashes encountered with a LaTeX command
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
150 for cmd in falsetexcmd.findall(line):
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
151 if '822' in line or '.html' in line:
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
152 continue # Ignore false positives for urls and for /rfc822
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
153 if '\\' + cmd in validcmds:
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
154 print 'Warning, forward slash used on line %d with cmd: /%s' % (lineno, cmd)
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
155
30373
cf7fad9d6fcc Generalize the last change to check for other markup spacing errors.
Raymond Hettinger <python@rcn.com>
parents: 30369
diff changeset
156 # Check for markup requiring {} for correct spacing
cf7fad9d6fcc Generalize the last change to check for other markup spacing errors.
Raymond Hettinger <python@rcn.com>
parents: 30369
diff changeset
157 for cmd in spacingmarkup.findall(line):
cf7fad9d6fcc Generalize the last change to check for other markup spacing errors.
Raymond Hettinger <python@rcn.com>
parents: 30369
diff changeset
158 print r'Warning, \%s should be written as \%s{} on line %d' % (cmd, cmd, lineno)
30369
e4cc232ed583 Check for \NULL markup errors.
Raymond Hettinger <python@rcn.com>
parents: 30209
diff changeset
159
29126
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
160 # Validate commands
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
161 nc = line.find(r'\newcommand')
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
162 if nc != -1:
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
163 start = line.find('{', nc)
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
164 end = line.find('}', start)
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
165 validcmds.add(line[start+1:end])
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
166 for cmd in texcmd.findall(line):
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
167 if cmd not in validcmds:
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
168 print r'Warning, unknown tex cmd on line %d: \%s' % (lineno, cmd)
29091
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
169
29070
b10a7facc1e8 Minor fixups.
Raymond Hettinger <python@rcn.com>
parents: 29068
diff changeset
170 # Check table levels (make sure lineii only inside tableii)
29068
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
171 m = tablestart.search(line)
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
172 if m:
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
173 tablelevel = m.group(1)
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
174 tablestartline = lineno
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
175 m = tableline.search(line)
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
176 if m and m.group(1) != tablelevel:
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
177 print r'Warning, \line%s on line %d does not match \table%s on line %d' % (m.group(1), lineno, tablelevel, tablestartline)
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
178 if tableend.search(line):
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
179 tablelevel = ''
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
180
29126
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
181 # Style guide warnings
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
182 if 'e.g.' in line or 'i.e.' in line:
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
183 print r'Style warning, avoid use of i.e or e.g. on line %d' % (lineno,)
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
184
30209
2db8c63e69cd Added doubled word warnings.
Raymond Hettinger <python@rcn.com>
parents: 29126
diff changeset
185 for dw in doubledwords.findall(line):
2db8c63e69cd Added doubled word warnings.
Raymond Hettinger <python@rcn.com>
parents: 29126
diff changeset
186 print r'Doubled word warning. "%s" on line %d' % (dw, lineno)
29126
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
187
29070
b10a7facc1e8 Minor fixups.
Raymond Hettinger <python@rcn.com>
parents: 29068
diff changeset
188 lastline = lineno
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
189 for lineno, symbol in openers:
29068
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
190 print "Unmatched open delimiter '%s' on line %d" % (symbol, lineno)
29091
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
191 for lineno in bracestack:
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
192 print "Unmatched { on line %d" % (lineno,)
29070
b10a7facc1e8 Minor fixups.
Raymond Hettinger <python@rcn.com>
parents: 29068
diff changeset
193 print 'Done checking %d lines.' % (lastline,)
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
194 return 0
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
195
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
196 def main(args=None):
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
197 if args is None:
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
198 args = sys.argv[1:]
29116
24786c3130ca Forward slash warnings now only occur for potentially valid LaTeX commands.
Raymond Hettinger <python@rcn.com>
parents: 29091
diff changeset
199 optitems, arglist = getopt.getopt(args, "k:mdhs:v")
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
200 opts = dict(optitems)
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
201 if '-h' in opts or args==[]:
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
202 print __doc__
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
203 return 0
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
204
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
205 if len(arglist) < 1:
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
206 print 'Please specify a file to be checked'
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
207 return 1
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
208
29126
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
209 for i, filespec in enumerate(arglist):
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
210 if '*' in filespec or '?' in filespec:
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
211 arglist[i:i+1] = glob.glob(filespec)
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
212
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
213 morecmds = [v for k,v in optitems if k=='-k']
29126
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
214 err = []
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
215
29126
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
216 for filename in arglist:
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
217 print '=' * 30
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
218 print "Checking", filename
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
219 try:
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
220 f = open(filename)
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
221 except IOError:
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
222 print 'Cannot open file %s.' % arglist[0]
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
223 return 2
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
224
29126
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
225 try:
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
226 err.append(checkit(f, opts, morecmds))
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
227 finally:
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
228 f.close()
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
229
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
230 return max(err)
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
231
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
232 if __name__ == '__main__':
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
233 sys.exit(main())