annotate Tools/scripts/texcheck.py @ 32527:0da660e05e69 legacy-trunk

"#if WITH_THREAD" is incorrect; must be #ifdef instead; WITH_THREAD isn't always set to an integer value when it's defined.
author Tim Peters <tim.peters@gmail.com>
date Wed, 07 Jul 2004 20:42:07 +0000
parents cf7fad9d6fcc
children 4fab4a5cce2f
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 sets
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
27 import sys
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
28 import getopt
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
29 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
30 import glob
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
31
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
32 cmdstr = r"""
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
33 \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
34 \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
35 \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
36 \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
37 \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
38 \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
39 \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
40 \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
41 \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
42 \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
43 \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
44 \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
45 \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
46 \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
47 \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
48 \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
49 \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
50 \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
51 \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
52 \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
53 \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
54 \platform \seeurl \leftmargin \labelwidth \localmoduletable
29091
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
55 \LaTeX \copyright \memberline \backslash \pi \centerline
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
56 \caption \vspace \textwidth \menuselection \textless
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
57 \makevar \csimplemacro \menuselection \bfcode \sub \release
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
58 \email \kwindex \refexmodindex \filenq \e \menuselection
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
59 \exindex \linev \newsgroup \verbatim \setshortversion
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
60 """
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 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
63 "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
64 try:
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
65 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
66 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
67 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
68 return
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
69 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
70 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
71 return
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
72
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
73 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
74 """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
75
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
76 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
77 -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
78 -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
79 -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
80 -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
81
29068
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
82 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
83 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
84 """
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 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
87 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
88
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
89 validcmds = sets.Set(cmdstr.split())
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
90 for cmd in morecmds:
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
91 validcmds.add('\\' + cmd)
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
92
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
93 if '-m' in opts:
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
94 pairmap = {']':'[(', ')':'(['} # Munged openers
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
95 else:
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
96 pairmap = {']':'[', ')':'('} # Normal opener for a given closer
29126
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
97 openpunct = sets.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
98
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
99 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
100 braces = re.compile(r'({)|(})')
30209
2db8c63e69cd Added doubled word warnings.
Raymond Hettinger <python@rcn.com>
parents: 29126
diff changeset
101 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
102 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
103
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
104 openers = [] # Stack of pending open delimiters
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
105 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
106
29068
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
107 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
108 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
109 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
110 tablelevel = ''
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
111 tablestartline = 0
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
112
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
113 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
114 lineno = 0
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
115
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
116 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
117 line = line.rstrip()
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
118
29126
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
119 # 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
120 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
121 if '-v' in opts:
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
122 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
123 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
124 openers.append((lineno, name))
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
125 elif punct in openpunct:
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
126 openers.append((lineno, punct))
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
127 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
128 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
129 elif punct in pairmap:
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
130 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
131 if '-v' in opts:
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
132 print ' --> ', openers
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
133
29091
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
134 # Balance opening and closing braces
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
135 for open, close in braces.findall(line):
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
136 if open == '{':
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
137 bracestack.append(lineno)
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
138 if close == '}':
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
139 try:
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
140 bracestack.pop()
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
141 except IndexError:
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
142 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
143
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
144 # 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
145 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
146 continue
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
147
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
148 # 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
149 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
150 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
151 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
152 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
153 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
154
30373
cf7fad9d6fcc Generalize the last change to check for other markup spacing errors.
Raymond Hettinger <python@rcn.com>
parents: 30369
diff changeset
155 # 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
156 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
157 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
158
29126
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
159 # Validate commands
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
160 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
161 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
162 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
163 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
164 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
165 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
166 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
167 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
168
29070
b10a7facc1e8 Minor fixups.
Raymond Hettinger <python@rcn.com>
parents: 29068
diff changeset
169 # 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
170 m = tablestart.search(line)
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
171 if m:
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
172 tablelevel = m.group(1)
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
173 tablestartline = lineno
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
174 m = tableline.search(line)
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
175 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
176 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
177 if tableend.search(line):
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
178 tablelevel = ''
1c848e9f6166 Add table level validation and made minor fix-ups.
Raymond Hettinger <python@rcn.com>
parents: 29061
diff changeset
179
29126
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
180 # 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
181 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
182 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
183
30209
2db8c63e69cd Added doubled word warnings.
Raymond Hettinger <python@rcn.com>
parents: 29126
diff changeset
184 for dw in doubledwords.findall(line):
2db8c63e69cd Added doubled word warnings.
Raymond Hettinger <python@rcn.com>
parents: 29126
diff changeset
185 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
186
29070
b10a7facc1e8 Minor fixups.
Raymond Hettinger <python@rcn.com>
parents: 29068
diff changeset
187 lastline = lineno
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
188 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
189 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
190 for lineno in bracestack:
22060004b94a * Added separate checks for matching braces.
Raymond Hettinger <python@rcn.com>
parents: 29070
diff changeset
191 print "Unmatched { on line %d" % (lineno,)
29070
b10a7facc1e8 Minor fixups.
Raymond Hettinger <python@rcn.com>
parents: 29068
diff changeset
192 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
193 return 0
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
194
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
195 def main(args=None):
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
196 if args is None:
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
197 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
198 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
199 opts = dict(optitems)
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
200 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
201 print __doc__
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
202 return 0
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
203
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
204 if len(arglist) < 1:
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
205 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
206 return 1
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
207
29126
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
208 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
209 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
210 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
211
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
212 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
213 err = []
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
214
29126
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
215 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
216 print '=' * 30
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
217 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
218 try:
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
219 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
220 except IOError:
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
221 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
222 return 2
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
223
29126
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
224 try:
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
225 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
226 finally:
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
227 f.close()
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
228
6d933307761c * Added file globbing to make it easier to check many LaTeX files.
Raymond Hettinger <python@rcn.com>
parents: 29116
diff changeset
229 return max(err)
29061
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
230
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
231 if __name__ == '__main__':
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
232 sys.exit(main())
1babbb4919a7 Added a tool for making a rough check of LaTeX documents.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
233