blob: ebd1b959752f0bb9fe60292c43c5c784a2664042 [file] [log] [blame]
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001#
2# partparse.py: parse a by-Guido-written-and-by-Jan-Hein-edited LaTeX file,
3# and generate texinfo source.
4#
5# This is *not* a good example of good programming practices. In fact, this
6# file could use a complete rewrite, in order to become faster, more
Guido van Rossum36f219d1996-09-11 21:30:40 +00007# easily extensible and maintainable.
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00008#
9# However, I added some comments on a few places for the pityful person who
10# would ever need to take a look into this file.
11#
12# Have I been clear enough??
13#
14# -jh
Guido van Rossum36f219d1996-09-11 21:30:40 +000015#
16# Yup. I made some performance improvements and hope this lasts a while;
17# I don't want to be the schmuck who ends up re-writting it!
18#
19# -fld
Fred Drakee8b46131998-02-17 05:54:46 +000020#
21# (sometime later...)
22#
23# Ok, I've re-worked substantial chunks of this. It's only getting worse.
24# It just might be gone before the next source release. (Yeah!)
25#
26# -fld
Guido van Rossum95cd2ef1992-12-08 14:37:55 +000027
Guido van Rossum7a2dba21993-11-05 14:45:11 +000028import sys, string, regex, getopt, os
Guido van Rossum95cd2ef1992-12-08 14:37:55 +000029
Guido van Rossum49604d31996-09-10 22:19:51 +000030from types import IntType, ListType, StringType, TupleType
31
Fred Drakee8b46131998-02-17 05:54:46 +000032release_version = sys.version[:3]
33
Guido van Rossum95cd2ef1992-12-08 14:37:55 +000034# Different parse modes for phase 1
35MODE_REGULAR = 0
36MODE_VERBATIM = 1
37MODE_CS_SCAN = 2
38MODE_COMMENT = 3
39MODE_MATH = 4
40MODE_DMATH = 5
41MODE_GOBBLEWHITE = 6
42
Guido van Rossum5f18d6c1996-09-10 22:34:20 +000043the_modes = (MODE_REGULAR, MODE_VERBATIM, MODE_CS_SCAN, MODE_COMMENT,
Guido van Rossum36f219d1996-09-11 21:30:40 +000044 MODE_MATH, MODE_DMATH, MODE_GOBBLEWHITE)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +000045
46# Show the neighbourhood of the scanned buffer
47def epsilon(buf, where):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +000048 wmt, wpt = where - 10, where + 10
49 if wmt < 0:
50 wmt = 0
51 if wpt > len(buf):
52 wpt = len(buf)
53 return ' Context ' + `buf[wmt:where]` + '.' + `buf[where:wpt]` + '.'
Guido van Rossum95cd2ef1992-12-08 14:37:55 +000054
55# Should return the line number. never worked
56def lin():
Guido van Rossum5f18d6c1996-09-10 22:34:20 +000057 global lineno
58 return ' Line ' + `lineno` + '.'
Guido van Rossum95cd2ef1992-12-08 14:37:55 +000059
60# Displays the recursion level.
61def lv(lvl):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +000062 return ' Level ' + `lvl` + '.'
Guido van Rossum95cd2ef1992-12-08 14:37:55 +000063
64# Combine the three previous functions. Used often.
65def lle(lvl, buf, where):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +000066 return lv(lvl) + lin() + epsilon(buf, where)
67
68
Guido van Rossum95cd2ef1992-12-08 14:37:55 +000069# This class is only needed for _symbolic_ representation of the parse mode.
70class Mode:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +000071 def __init__(self, arg):
72 if arg not in the_modes:
73 raise ValueError, 'mode not in the_modes'
74 self.mode = arg
Guido van Rossum95cd2ef1992-12-08 14:37:55 +000075
Guido van Rossum5f18d6c1996-09-10 22:34:20 +000076 def __cmp__(self, other):
77 if type(self) != type(other):
Guido van Rossum36f219d1996-09-11 21:30:40 +000078 other = mode[other]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +000079 return cmp(self.mode, other.mode)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +000080
Guido van Rossum5f18d6c1996-09-10 22:34:20 +000081 def __repr__(self):
82 if self.mode == MODE_REGULAR:
83 return 'MODE_REGULAR'
84 elif self.mode == MODE_VERBATIM:
85 return 'MODE_VERBATIM'
86 elif self.mode == MODE_CS_SCAN:
87 return 'MODE_CS_SCAN'
88 elif self.mode == MODE_COMMENT:
89 return 'MODE_COMMENT'
90 elif self.mode == MODE_MATH:
91 return 'MODE_MATH'
92 elif self.mode == MODE_DMATH:
93 return 'MODE_DMATH'
94 elif self.mode == MODE_GOBBLEWHITE:
95 return 'MODE_GOBBLEWHITE'
96 else:
97 raise ValueError, 'mode not in the_modes'
Guido van Rossum95cd2ef1992-12-08 14:37:55 +000098
99# just a wrapper around a class initialisation
Guido van Rossum36f219d1996-09-11 21:30:40 +0000100mode = {}
101for t in the_modes:
102 mode[t] = Mode(t)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000103
104
105# After phase 1, the text consists of chunks, with a certain type
106# this type will be assigned to the chtype member of the chunk
107# the where-field contains the file position where this is found
108# and the data field contains (1): a tuple describing start- end end
109# positions of the substring (can be used as slice for the buf-variable),
110# (2) just a string, mostly generated by the changeit routine,
111# or (3) a list, describing a (recursive) subgroup of chunks
112PLAIN = 0 # ASSUME PLAINTEXT, data = the text
113GROUP = 1 # GROUP ({}), data = [chunk, chunk,..]
114CSNAME = 2 # CONTROL SEQ TOKEN, data = the command
115COMMENT = 3 # data is the actual comment
116DMATH = 4 # DISPLAYMATH, data = [chunk, chunk,..]
117MATH = 5 # MATH, see DISPLAYMATH
118OTHER = 6 # CHAR WITH CATCODE OTHER, data = char
119ACTIVE = 7 # ACTIVE CHAR
120GOBBLEDWHITE = 8 # Gobbled LWSP, after CSNAME
121ENDLINE = 9 # END-OF-LINE, data = '\n'
122DENDLINE = 10 # DOUBLE EOL, data='\n', indicates \par
123ENV = 11 # LaTeX-environment
Guido van Rossum36f219d1996-09-11 21:30:40 +0000124 # data =(envname,[ch,ch,ch,.])
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000125CSLINE = 12 # for texi: next chunk will be one group
Guido van Rossum36f219d1996-09-11 21:30:40 +0000126 # of args. Will be set all on 1 line
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000127IGNORE = 13 # IGNORE this data
128ENDENV = 14 # TEMP END OF GROUP INDICATOR
129IF = 15 # IF-directive
Guido van Rossum36f219d1996-09-11 21:30:40 +0000130 # data = (flag,negate,[ch, ch, ch,...])
131
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000132the_types = (PLAIN, GROUP, CSNAME, COMMENT, DMATH, MATH, OTHER, ACTIVE,
Guido van Rossum36f219d1996-09-11 21:30:40 +0000133 GOBBLEDWHITE, ENDLINE, DENDLINE, ENV, CSLINE, IGNORE, ENDENV, IF)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000134
135# class, just to display symbolic name
136class ChunkType:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000137 def __init__(self, chunk_type):
138 if chunk_type not in the_types:
139 raise ValueError, 'chunk_type not in the_types'
140 self.chunk_type = chunk_type
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000141
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000142 def __cmp__(self, other):
143 if type(self) != type(other):
Guido van Rossum36f219d1996-09-11 21:30:40 +0000144 other = chunk_type[other]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000145 return cmp(self.chunk_type, other.chunk_type)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000146
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000147 def __repr__(self):
148 if self.chunk_type == PLAIN:
149 return 'PLAIN'
150 elif self.chunk_type == GROUP:
151 return 'GROUP'
152 elif self.chunk_type == CSNAME:
153 return 'CSNAME'
154 elif self.chunk_type == COMMENT:
155 return 'COMMENT'
156 elif self.chunk_type == DMATH:
157 return 'DMATH'
158 elif self.chunk_type == MATH:
159 return 'MATH'
160 elif self.chunk_type == OTHER:
161 return 'OTHER'
162 elif self.chunk_type == ACTIVE:
163 return 'ACTIVE'
164 elif self.chunk_type == GOBBLEDWHITE:
165 return 'GOBBLEDWHITE'
166 elif self.chunk_type == DENDLINE:
167 return 'DENDLINE'
168 elif self.chunk_type == ENDLINE:
169 return 'ENDLINE'
170 elif self.chunk_type == ENV:
171 return 'ENV'
172 elif self.chunk_type == CSLINE:
173 return 'CSLINE'
174 elif self.chunk_type == IGNORE:
175 return 'IGNORE'
176 elif self.chunk_type == ENDENV:
177 return 'ENDENV'
178 elif self.chunk_type == IF:
179 return 'IF'
180 else:
181 raise ValueError, 'chunk_type not in the_types'
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000182
183# ...and the wrapper
Guido van Rossum36f219d1996-09-11 21:30:40 +0000184chunk_type = {}
Guido van Rossum49604d31996-09-10 22:19:51 +0000185for t in the_types:
Guido van Rossum36f219d1996-09-11 21:30:40 +0000186 chunk_type[t] = ChunkType(t)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000187
188# store a type object of the ChunkType-class-instance...
Guido van Rossum36f219d1996-09-11 21:30:40 +0000189chunk_type_type = type(chunk_type[PLAIN])
Guido van Rossum49604d31996-09-10 22:19:51 +0000190
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000191# this class contains a part of the parsed buffer
192class Chunk:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000193 def __init__(self, chtype, where, data):
194 if type(chtype) != chunk_type_type:
Guido van Rossum36f219d1996-09-11 21:30:40 +0000195 chtype = chunk_type[chtype]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000196 self.chtype = chtype
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000197 self.where = where
198 self.data = data
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000199
Fred Drakeb98cd391998-03-04 06:33:43 +0000200 __datatypes = [chunk_type[CSNAME], chunk_type[PLAIN], chunk_type[CSLINE]]
201
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000202 def __repr__(self):
Fred Drakeb98cd391998-03-04 06:33:43 +0000203 if self.chtype in self.__datatypes:
204 data = s(self.buf, self.data)
205 else:
206 data = self.data
207 return 'chunk' + `self.chtype, self.where, data`
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000208
209# and the wrapper
Guido van Rossum49604d31996-09-10 22:19:51 +0000210chunk = Chunk
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000211
212
213error = 'partparse.error'
214
215#
216# TeX's catcodes...
217#
218CC_ESCAPE = 0
219CC_LBRACE = 1
220CC_RBRACE = 2
221CC_MATHSHIFT = 3
222CC_ALIGNMENT = 4
223CC_ENDLINE = 5
224CC_PARAMETER = 6
225CC_SUPERSCRIPT = 7
226CC_SUBSCRIPT = 8
227CC_IGNORE = 9
228CC_WHITE = 10
229CC_LETTER = 11
230CC_OTHER = 12
231CC_ACTIVE = 13
232CC_COMMENT = 14
233CC_INVALID = 15
234
235# and the names
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000236cc_names = [
237 'CC_ESCAPE',
238 'CC_LBRACE',
239 'CC_RBRACE',
240 'CC_MATHSHIFT',
241 'CC_ALIGNMENT',
242 'CC_ENDLINE',
243 'CC_PARAMETER',
244 'CC_SUPERSCRIPT',
245 'CC_SUBSCRIPT',
246 'CC_IGNORE',
247 'CC_WHITE',
248 'CC_LETTER',
249 'CC_OTHER',
250 'CC_ACTIVE',
251 'CC_COMMENT',
252 'CC_INVALID',
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000253 ]
254
255# Show a list of catcode-name-symbols
256def pcl(codelist):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000257 result = ''
258 for i in codelist:
259 result = result + cc_names[i] + ', '
260 return '[' + result[:-2] + ']'
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000261
262# the name of the catcode (ACTIVE, OTHER, etc.)
263def pc(code):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000264 return cc_names[code]
265
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000266
267# Which catcodes make the parser stop parsing regular plaintext
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000268regular_stopcodes = [CC_ESCAPE, CC_LBRACE, CC_RBRACE, CC_MATHSHIFT,
269 CC_ALIGNMENT, CC_PARAMETER, CC_SUPERSCRIPT, CC_SUBSCRIPT,
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000270 CC_IGNORE, CC_ACTIVE, CC_COMMENT, CC_INVALID, CC_ENDLINE]
271
272# same for scanning a control sequence name
273csname_scancodes = [CC_LETTER]
274
275# same for gobbling LWSP
276white_scancodes = [CC_WHITE]
277##white_scancodes = [CC_WHITE, CC_ENDLINE]
278
279# make a list of all catcode id's, except for catcode ``other''
280all_but_other_codes = range(16)
281del all_but_other_codes[CC_OTHER]
282##print all_but_other_codes
283
284# when does a comment end
285comment_stopcodes = [CC_ENDLINE]
286
287# gather all characters together, specified by a list of catcodes
288def code2string(cc, codelist):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000289 ##print 'code2string: codelist = ' + pcl(codelist),
290 result = ''
291 for category in codelist:
292 if cc[category]:
293 result = result + cc[category]
294 ##print 'result = ' + `result`
295 return result
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000296
297# automatically generate all characters of catcode other, being the
298# complement set in the ASCII range (128 characters)
299def make_other_codes(cc):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000300 otherchars = range(256) # could be made 256, no problem
301 for category in all_but_other_codes:
302 if cc[category]:
303 for c in cc[category]:
304 otherchars[ord(c)] = None
305 result = ''
306 for i in otherchars:
307 if i != None:
308 result = result + chr(i)
309 return result
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000310
311# catcode dump (which characters have which catcodes).
312def dump_cc(name, cc):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000313 ##print '\t' + name
314 ##print '=' * (8+len(name))
315 if len(cc) != 16:
316 raise TypeError, 'cc not good cat class'
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000317## for i in range(16):
318## print pc(i) + '\t' + `cc[i]`
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000319
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000320
321# In the beginning,....
322epoch_cc = [None] * 16
323##dump_cc('epoch_cc', epoch_cc)
324
325
326# INITEX
327initex_cc = epoch_cc[:]
328initex_cc[CC_ESCAPE] = '\\'
329initex_cc[CC_ENDLINE], initex_cc[CC_IGNORE], initex_cc[CC_WHITE] = \
330 '\n', '\0', ' '
331initex_cc[CC_LETTER] = string.uppercase + string.lowercase
332initex_cc[CC_COMMENT], initex_cc[CC_INVALID] = '%', '\x7F'
333#initex_cc[CC_OTHER] = make_other_codes(initex_cc) I don't need them, anyway
334##dump_cc('initex_cc', initex_cc)
335
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000336
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000337# LPLAIN: LaTeX catcode setting (see lplain.tex)
338lplain_cc = initex_cc[:]
339lplain_cc[CC_LBRACE], lplain_cc[CC_RBRACE] = '{', '}'
340lplain_cc[CC_MATHSHIFT] = '$'
341lplain_cc[CC_ALIGNMENT] = '&'
342lplain_cc[CC_PARAMETER] = '#'
343lplain_cc[CC_SUPERSCRIPT] = '^\x0B' # '^' and C-k
344lplain_cc[CC_SUBSCRIPT] = '_\x01' # '_' and C-a
345lplain_cc[CC_WHITE] = lplain_cc[CC_WHITE] + '\t'
346lplain_cc[CC_ACTIVE] = '~\x0C' # '~' and C-l
347lplain_cc[CC_OTHER] = make_other_codes(lplain_cc)
348##dump_cc('lplain_cc', lplain_cc)
349
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000350
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000351# Guido's LaTeX environment catcoded '_' as ``other''
352# my own purpose catlist
353my_cc = lplain_cc[:]
354my_cc[CC_SUBSCRIPT] = my_cc[CC_SUBSCRIPT][1:] # remove '_' here
355my_cc[CC_OTHER] = my_cc[CC_OTHER] + '_' # add it to OTHER list
356dump_cc('my_cc', my_cc)
357
358
359
360# needed for un_re, my equivalent for regexp-quote in Emacs
361re_meaning = '\\[]^$'
362
363def un_re(str):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000364 result = ''
365 for i in str:
366 if i in re_meaning:
367 result = result + '\\'
368 result = result + i
369 return result
370
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000371# NOTE the negate ('^') operator in *some* of the regexps below
372def make_rc_regular(cc):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000373 # problems here if '[]' are included!!
374 return regex.compile('[' + code2string(cc, regular_stopcodes) + ']')
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000375
376def make_rc_cs_scan(cc):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000377 return regex.compile('[^' + code2string(cc, csname_scancodes) + ']')
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000378
379def make_rc_comment(cc):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000380 return regex.compile('[' + code2string(cc, comment_stopcodes) + ']')
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000381
382def make_rc_endwhite(cc):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000383 return regex.compile('[^' + code2string(cc, white_scancodes) + ']')
384
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000385
386
387# regular: normal mode:
388rc_regular = make_rc_regular(my_cc)
389
390# scan: scan a command sequence e.g. `newlength' or `mbox' or `;', `,' or `$'
391rc_cs_scan = make_rc_cs_scan(my_cc)
392rc_comment = make_rc_comment(my_cc)
393rc_endwhite = make_rc_endwhite(my_cc)
394
395
Guido van Rossum36f219d1996-09-11 21:30:40 +0000396# parseit (BUF, PARSEMODE=mode[MODE_REGULAR], START=0, RECURSION-LEVEL=0)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000397# RECURSION-LEVEL will is incremented on entry.
398# result contains the list of chunks returned
399# together with this list, the buffer position is returned
400
401# RECURSION-LEVEL will be set to zero *again*, when recursively a
402# {,D}MATH-mode scan has been enetered.
403# This has been done in order to better check for environment-mismatches
404
Guido van Rossum36f219d1996-09-11 21:30:40 +0000405def parseit(buf, parsemode=mode[MODE_REGULAR], start=0, lvl=0):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000406 global lineno
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000407
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000408 result = []
409 end = len(buf)
Guido van Rossum36f219d1996-09-11 21:30:40 +0000410 if lvl == 0 and parsemode == mode[MODE_REGULAR]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000411 lineno = 1
412 lvl = lvl + 1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000413
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000414 ##print 'parseit(' + epsilon(buf, start) + ', ' + `parsemode` + ', ' + `start` + ', ' + `lvl` + ')'
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000415
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000416 #
417 # some of the more regular modes...
418 #
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000419
Guido van Rossum36f219d1996-09-11 21:30:40 +0000420 if parsemode in (mode[MODE_REGULAR], mode[MODE_DMATH], mode[MODE_MATH]):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000421 cstate = []
422 newpos = start
423 curpmode = parsemode
424 while 1:
425 where = newpos
426 #print '\tnew round: ' + epsilon(buf, where)
427 if where == end:
Guido van Rossum36f219d1996-09-11 21:30:40 +0000428 if lvl > 1 or curpmode != mode[MODE_REGULAR]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000429 # not the way we started...
430 raise EOFError, 'premature end of file.' + lle(lvl, buf, where)
431 # the real ending of lvl-1 parse
432 return end, result
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000433
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000434 pos = rc_regular.search(buf, where)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000435
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000436 if pos < 0:
437 pos = end
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000438
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000439 if pos != where:
440 newpos, c = pos, chunk(PLAIN, where, (where, pos))
441 result.append(c)
442 continue
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000443
444
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000445 #
446 # ok, pos == where and pos != end
447 #
448 foundchar = buf[where]
449 if foundchar in my_cc[CC_LBRACE]:
450 # recursive subgroup parse...
451 newpos, data = parseit(buf, curpmode, where+1, lvl)
452 result.append(chunk(GROUP, where, data))
453
454 elif foundchar in my_cc[CC_RBRACE]:
455 if lvl <= 1:
456 raise error, 'ENDGROUP while in base level.' + lle(lvl, buf, where)
Guido van Rossum36f219d1996-09-11 21:30:40 +0000457 if lvl == 1 and mode != mode[MODE_REGULAR]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000458 raise error, 'endgroup while in math mode. +lin() + epsilon(buf, where)'
459 return where + 1, result
460
461 elif foundchar in my_cc[CC_ESCAPE]:
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000462 #
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000463 # call the routine that actually deals with
464 # this problem. If do_ret is None, than
465 # return the value of do_ret
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000466 #
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000467 # Note that handle_cs might call this routine
468 # recursively again...
469 #
470 do_ret, newpos = handlecs(buf, where,
471 curpmode, lvl, result, end)
472 if do_ret != None:
473 return do_ret
474
475 elif foundchar in my_cc[CC_COMMENT]:
476 newpos, data = parseit(buf,
Guido van Rossum36f219d1996-09-11 21:30:40 +0000477 mode[MODE_COMMENT], where+1, lvl)
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000478 result.append(chunk(COMMENT, where, data))
479
480 elif foundchar in my_cc[CC_MATHSHIFT]:
481 # note that recursive calls to math-mode
482 # scanning are called with recursion-level 0
483 # again, in order to check for bad mathend
484 #
485 if where + 1 != end and buf[where + 1] in my_cc[CC_MATHSHIFT]:
486 #
487 # double mathshift, e.g. '$$'
488 #
Guido van Rossum36f219d1996-09-11 21:30:40 +0000489 if curpmode == mode[MODE_REGULAR]:
490 newpos, data = parseit(buf, mode[MODE_DMATH],
491 where + 2, 0)
492 result.append(chunk(DMATH, where, data))
493 elif curpmode == mode[MODE_MATH]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000494 raise error, 'wrong math delimiiter' + lin() + epsilon(buf, where)
495 elif lvl != 1:
496 raise error, 'bad mathend.' + lle(lvl, buf, where)
497 else:
498 return where + 2, result
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000499 else:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000500 #
501 # single math shift, e.g. '$'
502 #
Guido van Rossum36f219d1996-09-11 21:30:40 +0000503 if curpmode == mode[MODE_REGULAR]:
504 newpos, data = parseit(buf, mode[MODE_MATH],
505 where + 1, 0)
506 result.append(chunk(MATH, where, data))
507 elif curpmode == mode[MODE_DMATH]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000508 raise error, 'wrong math delimiiter' + lin() + epsilon(buf, where)
509 elif lvl != 1:
510 raise error, 'bad mathend.' + lv(lvl, buf, where)
511 else:
512 return where + 1, result
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000513
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000514 elif foundchar in my_cc[CC_IGNORE]:
515 print 'warning: ignored char', `foundchar`
516 newpos = where + 1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000517
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000518 elif foundchar in my_cc[CC_ACTIVE]:
519 result.append(chunk(ACTIVE, where, foundchar))
520 newpos = where + 1
521
522 elif foundchar in my_cc[CC_INVALID]:
523 raise error, 'invalid char ' + `foundchar`
524 newpos = where + 1
525
526 elif foundchar in my_cc[CC_ENDLINE]:
527 #
528 # after an end of line, eat the rest of
529 # whitespace on the beginning of the next line
530 # this is what LaTeX more or less does
531 #
532 # also, try to indicate double newlines (\par)
533 #
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000534 lineno = lineno + 1
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000535 savedwhere = where
Guido van Rossum36f219d1996-09-11 21:30:40 +0000536 newpos, dummy = parseit(buf, mode[MODE_GOBBLEWHITE], where + 1, lvl)
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000537 if newpos != end and buf[newpos] in my_cc[CC_ENDLINE]:
538 result.append(chunk(DENDLINE, savedwhere, foundchar))
539 else:
540 result.append(chunk(ENDLINE, savedwhere, foundchar))
541 else:
542 result.append(chunk(OTHER, where, foundchar))
543 newpos = where + 1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000544
Guido van Rossum36f219d1996-09-11 21:30:40 +0000545 elif parsemode == mode[MODE_CS_SCAN]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000546 #
547 # scan for a control sequence token. `\ape', `\nut' or `\%'
548 #
549 if start == end:
550 raise EOFError, 'can\'t find end of csname'
551 pos = rc_cs_scan.search(buf, start)
552 if pos < 0:
553 pos = end
554 if pos == start:
555 # first non-letter right where we started the search
556 # ---> the control sequence name consists of one single
557 # character. Also: don't eat white space...
558 if buf[pos] in my_cc[CC_ENDLINE]:
559 lineno = lineno + 1
560 pos = pos + 1
561 return pos, (start, pos)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000562 else:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000563 spos = pos
564 if buf[pos] == '\n':
565 lineno = lineno + 1
566 spos = pos + 1
Guido van Rossum36f219d1996-09-11 21:30:40 +0000567 pos2, dummy = parseit(buf, mode[MODE_GOBBLEWHITE], spos, lvl)
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000568 return pos2, (start, pos)
569
Guido van Rossum36f219d1996-09-11 21:30:40 +0000570 elif parsemode == mode[MODE_GOBBLEWHITE]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000571 if start == end:
572 return start, ''
573 pos = rc_endwhite.search(buf, start)
574 if pos < 0:
575 pos = start
576 return pos, (start, pos)
577
Guido van Rossum36f219d1996-09-11 21:30:40 +0000578 elif parsemode == mode[MODE_COMMENT]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000579 pos = rc_comment.search(buf, start)
580 lineno = lineno + 1
581 if pos < 0:
582 print 'no newline perhaps?'
583 raise EOFError, 'can\'t find end of comment'
584 pos = pos + 1
Guido van Rossum36f219d1996-09-11 21:30:40 +0000585 pos2, dummy = parseit(buf, mode[MODE_GOBBLEWHITE], pos, lvl)
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000586 return pos2, (start, pos)
587
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000588 else:
589 raise error, 'Unknown mode (' + `parsemode` + ')'
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000590
591
592#moreresult = cswitch(buf[x1:x2], buf, newpos, parsemode, lvl)
593
594#boxcommands = 'mbox', 'fbox'
595#defcommands = 'def', 'newcommand'
596
597endverbstr = '\\end{verbatim}'
598
599re_endverb = regex.compile(un_re(endverbstr))
600
601#
602# handlecs: helper function for parseit, for the special thing we might
603# wanna do after certain command control sequences
604# returns: None or return_data, newpos
605#
606# in the latter case, the calling function is instructed to immediately
607# return with the data in return_data
608#
609def handlecs(buf, where, curpmode, lvl, result, end):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000610 global lineno
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000611
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000612 # get the control sequence name...
Guido van Rossum36f219d1996-09-11 21:30:40 +0000613 newpos, data = parseit(buf, mode[MODE_CS_SCAN], where+1, lvl)
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000614 saveddata = data
Guido van Rossum36f219d1996-09-11 21:30:40 +0000615 s_buf_data = s(buf, data)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000616
Guido van Rossum36f219d1996-09-11 21:30:40 +0000617 if s_buf_data in ('begin', 'end'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000618 # skip the expected '{' and get the LaTeX-envname '}'
Guido van Rossum36f219d1996-09-11 21:30:40 +0000619 newpos, data = parseit(buf, mode[MODE_REGULAR], newpos+1, lvl)
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000620 if len(data) != 1:
Guido van Rossum36f219d1996-09-11 21:30:40 +0000621 raise error, 'expected 1 chunk of data.' + lle(lvl, buf, where)
Guido van Rossum49604d31996-09-10 22:19:51 +0000622
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000623 # yucky, we've got an environment
624 envname = s(buf, data[0].data)
Guido van Rossum36f219d1996-09-11 21:30:40 +0000625 s_buf_saveddata = s(buf, saveddata)
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000626 ##print 'FOUND ' + s(buf, saveddata) + '. Name ' + `envname` + '.' + lv(lvl)
Guido van Rossum36f219d1996-09-11 21:30:40 +0000627 if s_buf_saveddata == 'begin' and envname == 'verbatim':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000628 # verbatim deserves special treatment
629 pos = re_endverb.search(buf, newpos)
630 if pos < 0:
Guido van Rossum36f219d1996-09-11 21:30:40 +0000631 raise error, "%s not found.%s" \
632 % (`endverbstr`, lle(lvl, buf, where))
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000633 result.append(chunk(ENV, where, (envname, [chunk(PLAIN, newpos, (newpos, pos))])))
634 newpos = pos + len(endverbstr)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000635
Guido van Rossum36f219d1996-09-11 21:30:40 +0000636 elif s_buf_saveddata == 'begin':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000637 # start parsing recursively... If that parse returns
638 # from an '\end{...}', then should the last item of
639 # the returned data be a string containing the ended
640 # environment
641 newpos, data = parseit(buf, curpmode, newpos, lvl)
642 if not data or type(data[-1]) is not StringType:
Guido van Rossum36f219d1996-09-11 21:30:40 +0000643 raise error, "missing 'end'" + lle(lvl, buf, where) \
644 + epsilon(buf, newpos)
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000645 retenv = data[-1]
646 del data[-1]
647 if retenv != envname:
648 #[`retenv`, `envname`]
Guido van Rossum36f219d1996-09-11 21:30:40 +0000649 raise error, 'environments do not match.%s%s' \
650 % (lle(lvl, buf, where), epsilon(buf, newpos))
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000651 result.append(chunk(ENV, where, (retenv, data)))
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000652 else:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000653 # 'end'... append the environment name, as just
654 # pointed out, and order parsit to return...
655 result.append(envname)
656 ##print 'POINT of return: ' + epsilon(buf, newpos)
657 # the tuple will be returned by parseit
658 return (newpos, result), newpos
659
660 # end of \begin ... \end handling
661
Guido van Rossum36f219d1996-09-11 21:30:40 +0000662 elif s_buf_data[0:2] == 'if':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000663 # another scary monster: the 'if' directive
Guido van Rossum36f219d1996-09-11 21:30:40 +0000664 flag = s_buf_data[2:]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000665
666 # recursively call parseit, just like environment above..
667 # the last item of data should contain the if-termination
668 # e.g., 'else' of 'fi'
669 newpos, data = parseit(buf, curpmode, newpos, lvl)
670 if not data or data[-1] not in ('else', 'fi'):
671 raise error, 'wrong if... termination' + \
672 lle(lvl, buf, where) + epsilon(buf, newpos)
673
674 ifterm = data[-1]
675 del data[-1]
676 # 0 means dont_negate flag
677 result.append(chunk(IF, where, (flag, 0, data)))
678 if ifterm == 'else':
679 # do the whole thing again, there is only one way
680 # to end this one, by 'fi'
681 newpos, data = parseit(buf, curpmode, newpos, lvl)
682 if not data or data[-1] not in ('fi', ):
683 raise error, 'wrong if...else... termination' \
Guido van Rossum36f219d1996-09-11 21:30:40 +0000684 + lle(lvl, buf, where) \
685 + epsilon(buf, newpos)
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000686
687 ifterm = data[-1]
688 del data[-1]
689 result.append(chunk(IF, where, (flag, 1, data)))
690 #done implicitely: return None, newpos
691
Guido van Rossum36f219d1996-09-11 21:30:40 +0000692 elif s_buf_data in ('else', 'fi'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000693 result.append(s(buf, data))
694 # order calling party to return tuple
695 return (newpos, result), newpos
696
697 # end of \if, \else, ... \fi handling
698
699 elif s(buf, saveddata) == 'verb':
700 x2 = saveddata[1]
701 result.append(chunk(CSNAME, where, data))
702 if x2 == end:
703 raise error, 'premature end of command.' + lle(lvl, buf, where)
704 delimchar = buf[x2]
705 ##print 'VERB: delimchar ' + `delimchar`
706 pos = regex.compile(un_re(delimchar)).search(buf, x2 + 1)
707 if pos < 0:
708 raise error, 'end of \'verb\' argument (' + \
Guido van Rossum36f219d1996-09-11 21:30:40 +0000709 `delimchar` + ') not found.' + \
710 lle(lvl, buf, where)
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000711 result.append(chunk(GROUP, x2, [chunk(PLAIN, x2+1, (x2+1, pos))]))
712 newpos = pos + 1
713 else:
714 result.append(chunk(CSNAME, where, data))
715 return None, newpos
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000716
717# this is just a function to get the string value if the possible data-tuple
718def s(buf, data):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000719 if type(data) is StringType:
720 return data
721 if len(data) != 2 or not (type(data[0]) is type(data[1]) is IntType):
722 raise TypeError, 'expected tuple of 2 integers'
723 x1, x2 = data
724 return buf[x1:x2]
Guido van Rossum49604d31996-09-10 22:19:51 +0000725
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000726
727##length, data1, i = getnextarg(length, buf, pp, i + 1)
728
729# make a deep-copy of some chunks
730def crcopy(r):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000731 return map(chunkcopy, r)
Guido van Rossum49604d31996-09-10 22:19:51 +0000732
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000733
734# copy a chunk, would better be a method of class Chunk...
735def chunkcopy(ch):
Guido van Rossum36f219d1996-09-11 21:30:40 +0000736 if ch.chtype == chunk_type[GROUP]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000737 return chunk(GROUP, ch.where, map(chunkcopy, ch.data))
738 else:
739 return chunk(ch.chtype, ch.where, ch.data)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000740
741
742# get next argument for TeX-macro, flatten a group (insert between)
743# or return Command Sequence token, or give back one character
744def getnextarg(length, buf, pp, item):
745
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000746 ##wobj = Wobj()
747 ##dumpit(buf, wobj.write, pp[item:min(length, item + 5)])
748 ##print 'GETNEXTARG, (len, item) =', `length, item` + ' ---> ' + wobj.data + ' <---'
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000749
Guido van Rossum36f219d1996-09-11 21:30:40 +0000750 while item < length and pp[item].chtype == chunk_type[ENDLINE]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000751 del pp[item]
752 length = length - 1
753 if item >= length:
754 raise error, 'no next arg.' + epsilon(buf, pp[-1].where)
Guido van Rossum36f219d1996-09-11 21:30:40 +0000755 if pp[item].chtype == chunk_type[GROUP]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000756 newpp = pp[item].data
757 del pp[item]
758 length = length - 1
759 changeit(buf, newpp)
760 length = length + len(newpp)
761 pp[item:item] = newpp
762 item = item + len(newpp)
763 if len(newpp) < 10:
764 wobj = Wobj()
765 dumpit(buf, wobj.write, newpp)
766 ##print 'GETNEXTARG: inserted ' + `wobj.data`
767 return length, item
Guido van Rossum36f219d1996-09-11 21:30:40 +0000768 elif pp[item].chtype == chunk_type[PLAIN]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000769 #grab one char
770 print 'WARNING: grabbing one char'
771 if len(s(buf, pp[item].data)) > 1:
772 pp.insert(item, chunk(PLAIN, pp[item].where, s(buf, pp[item].data)[:1]))
773 item, length = item+1, length+1
774 pp[item].data = s(buf, pp[item].data)[1:]
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000775 else:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000776 item = item+1
777 return length, item
778 else:
779 ch = pp[item]
780 try:
781 str = `s(buf, ch.data)`
782 except TypeError:
783 str = `ch.data`
784 if len(str) > 400:
785 str = str[:400] + '...'
786 print 'GETNEXTARG:', ch.chtype, 'not handled, data ' + str
787 return length, item
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000788
789
790# this one is needed to find the end of LaTeX's optional argument, like
791# item[...]
792re_endopt = regex.compile(']')
793
794# get a LaTeX-optional argument, you know, the square braces '[' and ']'
795def getoptarg(length, buf, pp, item):
796
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000797 wobj = Wobj()
798 dumpit(buf, wobj.write, pp[item:min(length, item + 5)])
799 ##print 'GETOPTARG, (len, item) =', `length, item` + ' ---> ' + wobj.data + ' <---'
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000800
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000801 if item >= length or \
Guido van Rossum36f219d1996-09-11 21:30:40 +0000802 pp[item].chtype != chunk_type[PLAIN] or \
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000803 s(buf, pp[item].data)[0] != '[':
804 return length, item
805
806 pp[item].data = s(buf, pp[item].data)[1:]
807 if len(pp[item].data) == 0:
808 del pp[item]
809 length = length-1
810
811 while 1:
812 if item == length:
813 raise error, 'No end of optional arg found'
Guido van Rossum36f219d1996-09-11 21:30:40 +0000814 if pp[item].chtype == chunk_type[PLAIN]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000815 text = s(buf, pp[item].data)
816 pos = re_endopt.search(text)
817 if pos >= 0:
818 pp[item].data = text[:pos]
819 if pos == 0:
820 del pp[item]
821 length = length-1
822 else:
823 item=item+1
824 text = text[pos+1:]
825
826 while text and text[0] in ' \t':
827 text = text[1:]
828
829 if text:
830 pp.insert(item, chunk(PLAIN, 0, text))
831 length = length + 1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000832 return length, item
833
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000834 item = item+1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000835
836
837# Wobj just add write-requests to the ``data'' attribute
838class Wobj:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000839 data = ''
Guido van Rossum49604d31996-09-10 22:19:51 +0000840
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000841 def write(self, data):
842 self.data = self.data + data
Guido van Rossumb819bdf1995-03-15 11:26:26 +0000843
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000844# ignore these commands
Fred Drake74a11e51998-02-26 05:52:37 +0000845ignoredcommands = ('hline', 'small', '/', 'tableofcontents', 'Large')
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000846# map commands like these to themselves as plaintext
Fred Drake74a11e51998-02-26 05:52:37 +0000847wordsselves = ('UNIX', 'ABC', 'C', 'ASCII', 'EOF', 'LaTeX', 'POSIX', 'TeX',
848 'SliTeX')
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000849# \{ --> {, \} --> }, etc
Guido van Rossum36f219d1996-09-11 21:30:40 +0000850themselves = ('{', '}', ',', '.', '@', ' ', '\n') + wordsselves
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000851# these ones also themselves (see argargs macro in myformat.sty)
852inargsselves = (',', '[', ']', '(', ')')
853# this is how *I* would show the difference between emph and strong
854# code 1 means: fold to uppercase
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000855markcmds = {'code': ('', ''), 'var': 1, 'emph': ('_', '_'),
Fred Drakee8b46131998-02-17 05:54:46 +0000856 'strong': ('*', '*')}
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000857
858# recognise patter {\FONTCHANGE-CMD TEXT} to \MAPPED-FC-CMD{TEXT}
859fontchanges = {'rm': 'r', 'it': 'i', 'em': 'emph', 'bf': 'b', 'tt': 't'}
860
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000861
862# try to remove macros and return flat text
863def flattext(buf, pp):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000864 pp = crcopy(pp)
865 ##print '---> FLATTEXT ' + `pp`
866 wobj = Wobj()
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000867
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000868 i, length = 0, len(pp)
869 while 1:
870 if len(pp) != length:
871 raise 'FATAL', 'inconsistent length'
872 if i >= length:
873 break
874 ch = pp[i]
875 i = i+1
Guido van Rossum36f219d1996-09-11 21:30:40 +0000876 if ch.chtype == chunk_type[PLAIN]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000877 pass
Guido van Rossum36f219d1996-09-11 21:30:40 +0000878 elif ch.chtype == chunk_type[CSNAME]:
879 s_buf_data = s(buf, ch.data)
Fred Drake74a11e51998-02-26 05:52:37 +0000880 if convertible_csname(s_buf_data):
881 ch.chtype, ch.data, nix = conversion(s_buf_data)
882 if hist.inargs and s_buf_data in inargsselves:
Guido van Rossum36f219d1996-09-11 21:30:40 +0000883 ch.chtype = chunk_type[PLAIN]
Guido van Rossum36f219d1996-09-11 21:30:40 +0000884 elif len(s_buf_data) == 1 \
885 and s_buf_data in onlylatexspecial:
886 ch.chtype = chunk_type[PLAIN]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000887 # if it is followed by an empty group,
888 # remove that group, it was needed for
889 # a true space
890 if i < length \
Guido van Rossum36f219d1996-09-11 21:30:40 +0000891 and pp[i].chtype==chunk_type[GROUP] \
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000892 and len(pp[i].data) == 0:
893 del pp[i]
894 length = length-1
895
Guido van Rossum36f219d1996-09-11 21:30:40 +0000896 elif s_buf_data in markcmds.keys():
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000897 length, newi = getnextarg(length, buf, pp, i)
898 str = flattext(buf, pp[i:newi])
899 del pp[i:newi]
900 length = length - (newi - i)
Guido van Rossum36f219d1996-09-11 21:30:40 +0000901 ch.chtype = chunk_type[PLAIN]
902 markcmd = s_buf_data
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000903 x = markcmds[markcmd]
904 if type(x) == TupleType:
905 pre, after = x
906 str = pre+str+after
907 elif x == 1:
908 str = string.upper(str)
909 else:
910 raise 'FATAL', 'corrupt markcmds'
911 ch.data = str
912 else:
Guido van Rossum36f219d1996-09-11 21:30:40 +0000913 if s_buf_data not in ignoredcommands:
914 print 'WARNING: deleting command ' + s_buf_data
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000915 print 'PP' + `pp[i-1]`
916 del pp[i-1]
917 i, length = i-1, length-1
Guido van Rossum36f219d1996-09-11 21:30:40 +0000918 elif ch.chtype == chunk_type[GROUP]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000919 length, newi = getnextarg(length, buf, pp, i-1)
920 i = i-1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000921## str = flattext(buf, crcopy(pp[i-1:newi]))
922## del pp[i:newi]
923## length = length - (newi - i)
Guido van Rossum36f219d1996-09-11 21:30:40 +0000924## ch.chtype = chunk_type[PLAIN]
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000925## ch.data = str
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000926 else:
927 pass
928
929 dumpit(buf, wobj.write, pp)
930 ##print 'FLATTEXT: RETURNING ' + `wobj.data`
931 return wobj.data
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000932
933# try to generate node names (a bit shorter than the chapter title)
934# note that the \nodename command (see elsewhere) overules these efforts
935def invent_node_names(text):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000936 words = string.split(text)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000937
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000938 ##print 'WORDS ' + `words`
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000939
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000940 if len(words) == 2 \
Guido van Rossum36f219d1996-09-11 21:30:40 +0000941 and string.lower(words[0]) == 'built-in' \
942 and string.lower(words[1]) not in ('modules', 'functions'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000943 return words[1]
944 if len(words) == 3 and string.lower(words[1]) == 'module':
945 return words[2]
946 if len(words) == 3 and string.lower(words[1]) == 'object':
947 return string.join(words[0:2])
Guido van Rossum36f219d1996-09-11 21:30:40 +0000948 if len(words) > 4 \
949 and (string.lower(string.join(words[-4:])) \
950 == 'methods and data attributes'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000951 return string.join(words[:2])
952 return text
953
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000954re_commas_etc = regex.compile('[,`\'@{}]')
955
956re_whitespace = regex.compile('[ \t]*')
957
958
959##nodenamecmd = next_command_p(length, buf, pp, newi, 'nodename')
960
961# look if the next non-white stuff is also a command, resulting in skipping
962# double endlines (DENDLINE) too, and thus omitting \par's
963# Sometimes this is too much, maybe consider DENDLINE's as stop
964def next_command_p(length, buf, pp, i, cmdname):
965
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000966 while 1:
967 if i >= len(pp):
968 break
969 ch = pp[i]
970 i = i+1
Guido van Rossum36f219d1996-09-11 21:30:40 +0000971 if ch.chtype == chunk_type[ENDLINE]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000972 continue
Guido van Rossum36f219d1996-09-11 21:30:40 +0000973 if ch.chtype == chunk_type[DENDLINE]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000974 continue
Guido van Rossum36f219d1996-09-11 21:30:40 +0000975 if ch.chtype == chunk_type[PLAIN]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000976 if re_whitespace.search(s(buf, ch.data)) == 0 and \
977 re_whitespace.match(s(buf, ch.data)) == len(s(buf, ch.data)):
978 continue
979 return -1
Guido van Rossum36f219d1996-09-11 21:30:40 +0000980 if ch.chtype == chunk_type[CSNAME]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000981 if s(buf, ch.data) == cmdname:
982 return i # _after_ the command
983 return -1
984 return -1
985
986
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000987# things that are special to LaTeX, but not to texi..
988onlylatexspecial = '_~^$#&%'
989
Guido van Rossum23301a91993-05-24 14:19:37 +0000990class Struct: pass
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000991
992hist = Struct()
993out = Struct()
994
995def startchange():
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000996 global hist, out
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000997
Fred Drake74a11e51998-02-26 05:52:37 +0000998 hist.chaptertype = "chapter"
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000999 hist.inenv = []
1000 hist.nodenames = []
1001 hist.cindex = []
1002 hist.inargs = 0
1003 hist.enumeratenesting, hist.itemizenesting = 0, 0
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001004
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001005 out.doublenodes = []
1006 out.doublecindeces = []
1007
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001008
1009spacech = [chunk(PLAIN, 0, ' ')]
1010commach = [chunk(PLAIN, 0, ', ')]
1011cindexch = [chunk(CSLINE, 0, 'cindex')]
1012
1013# the standard variation in symbols for itemize
1014itemizesymbols = ['bullet', 'minus', 'dots']
1015
1016# same for enumerate
1017enumeratesymbols = ['1', 'A', 'a']
1018
Fred Drake74a11e51998-02-26 05:52:37 +00001019# Map of things that convert one-to-one. Each entry is a 3-tuple:
1020#
1021# new_chtype, new_data, nix_trailing_empty_group
1022#
Fred Drake9c7c6be1998-02-19 21:40:22 +00001023d = {}
Fred Drake74a11e51998-02-26 05:52:37 +00001024# add stuff that converts from one name to another:
Fred Drake9c7c6be1998-02-19 21:40:22 +00001025for name in ('url', 'module', 'function', 'cfunction',
1026 'keyword', 'method', 'exception', 'constant',
1027 'email', 'class', 'member', 'cdata', 'ctype',
Fred Drake74a11e51998-02-26 05:52:37 +00001028 'member', 'sectcode', 'verb'):
1029 d[name] = chunk_type[CSNAME], 'code', 0
1030for name in ('emph', 'var', 'strong', 'code', 'kbd', 'key',
1031 'dfn', 'samp', 'file', 'r', 'i', 't'):
1032 d[name] = chunk_type[CSNAME], name, 0
1033d['program'] = chunk_type[CSNAME], 'strong', 0
1034d['\\'] = chunk_type[CSNAME], '*', 0
1035# add stuff that converts to text:
1036for name in themselves:
1037 d[name] = chunk_type[PLAIN], name, 0
1038for name in wordsselves:
1039 d[name] = chunk_type[PLAIN], name, 1
1040for name in ',[]()':
1041 d[name] = chunk_type[PLAIN], name, 0
1042# a lot of these are LaTeX2e additions
1043for name, value in [('quotedblbase', ',,'), ('quotesinglbase', ','),
1044 ('textquotedbl', '"'), ('LaTeXe', 'LaTeX2e'),
1045 ('e', '\\'), ('textquotedblleft', "``"),
1046 ('textquotedblright', "''"), ('textquoteleft', "`"),
1047 ('textquoteright', "'"), ('textbackslash', '\\'),
1048 ('textbar', '|'), ('textless', '<'),
1049 ('textgreater', '>'), ('textasciicircum', '^'),
1050 ('Cpp', 'C++'), ('copyright', '')]:
1051 d[name] = chunk_type[PLAIN], value, 1
Fred Drake9c7c6be1998-02-19 21:40:22 +00001052convertible_csname = d.has_key
1053conversion = d.get
Fred Drake74a11e51998-02-26 05:52:37 +00001054del d, name, value
Fred Drake9c7c6be1998-02-19 21:40:22 +00001055
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001056##
1057## \begin{ {func,data,exc}desc }{name}...
1058## the resulting texi-code is dependent on the contents of indexsubitem
1059##
1060
1061# indexsubitem: `['XXX', 'function']
1062# funcdesc:
1063# deffn {`idxsi`} NAME (FUNCARGS)
1064
1065# indexsubitem: `['XXX', 'method']`
1066# funcdesc:
1067# defmethod {`idxsi[0]`} NAME (FUNCARGS)
1068
1069# indexsubitem: `['in', 'module', 'MODNAME']'
1070# datadesc:
1071# defcv data {`idxsi[1:]`} NAME
1072# excdesc:
1073# defcv exception {`idxsi[1:]`} NAME
1074# funcdesc:
1075# deffn {function of `idxsi[1:]`} NAME (FUNCARGS)
1076
1077# indexsubitem: `['OBJECT', 'attribute']'
1078# datadesc
1079# defcv attribute {`OBJECT`} NAME
1080
1081
1082## this routine will be called on \begin{funcdesc}{NAME}{ARGS}
1083## or \funcline{NAME}{ARGS}
1084##
Fred Drakee8b46131998-02-17 05:54:46 +00001085def do_funcdesc(length, buf, pp, i, index=1):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001086 startpoint = i-1
1087 ch = pp[startpoint]
1088 wh = ch.where
1089 length, newi = getnextarg(length, buf, pp, i)
1090 funcname = chunk(GROUP, wh, pp[i:newi])
1091 del pp[i:newi]
1092 length = length - (newi-i)
1093 save = hist.inargs
1094 hist.inargs = 1
1095 length, newi = getnextarg(length, buf, pp, i)
1096 hist.inargs = save
1097 del save
1098 the_args = [chunk(PLAIN, wh, '()'[0])] + pp[i:newi] + \
Fred Drake7edd8d31996-10-09 16:11:26 +00001099 [chunk(PLAIN, wh, '()'[1])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001100 del pp[i:newi]
1101 length = length - (newi-i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001102
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001103 idxsi = hist.indexsubitem # words
1104 command = ''
1105 cat_class = ''
Fred Drakeacc87541996-10-14 16:20:42 +00001106 if idxsi and idxsi[-1] in ('method', 'protocol', 'attribute'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001107 command = 'defmethod'
1108 cat_class = string.join(idxsi[:-1])
1109 elif len(idxsi) == 2 and idxsi[1] == 'function':
1110 command = 'deffn'
1111 cat_class = string.join(idxsi)
1112 elif len(idxsi) == 3 and idxsi[:2] == ['in', 'module']:
1113 command = 'deffn'
1114 cat_class = 'function of ' + string.join(idxsi[1:])
Fred Drakea4541af1997-12-29 17:19:22 +00001115 elif len(idxsi) > 3 and idxsi[:2] == ['in', 'modules']:
1116 command = 'deffn'
1117 cat_class = 'function of ' + string.join(idxsi[1:])
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001118
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001119 if not command:
1120 raise error, 'don\'t know what to do with indexsubitem ' + `idxsi`
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001121
Guido van Rossum36f219d1996-09-11 21:30:40 +00001122 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001123 ch.data = command
1124
1125 cslinearg = [chunk(GROUP, wh, [chunk(PLAIN, wh, cat_class)])]
1126 cslinearg.append(chunk(PLAIN, wh, ' '))
1127 cslinearg.append(funcname)
1128 cslinearg.append(chunk(PLAIN, wh, ' '))
1129 l = len(cslinearg)
1130 cslinearg[l:l] = the_args
1131
1132 pp.insert(i, chunk(GROUP, wh, cslinearg))
1133 i, length = i+1, length+1
1134 hist.command = command
1135 return length, i
1136
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001137
1138## this routine will be called on \begin{excdesc}{NAME}
1139## or \excline{NAME}
1140##
1141def do_excdesc(length, buf, pp, i):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001142 startpoint = i-1
1143 ch = pp[startpoint]
1144 wh = ch.where
1145 length, newi = getnextarg(length, buf, pp, i)
1146 excname = chunk(GROUP, wh, pp[i:newi])
1147 del pp[i:newi]
1148 length = length - (newi-i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001149
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001150 idxsi = hist.indexsubitem # words
1151 command = ''
1152 cat_class = ''
1153 class_class = ''
1154 if len(idxsi) == 2 and idxsi[1] == 'exception':
1155 command = 'defvr'
1156 cat_class = string.join(idxsi)
1157 elif len(idxsi) == 3 and idxsi[:2] == ['in', 'module']:
1158 command = 'defcv'
1159 cat_class = 'exception'
1160 class_class = string.join(idxsi[1:])
1161 elif len(idxsi) == 4 and idxsi[:3] == ['exception', 'in', 'module']:
1162 command = 'defcv'
1163 cat_class = 'exception'
1164 class_class = string.join(idxsi[2:])
Fred Drakea4541af1997-12-29 17:19:22 +00001165 elif idxsi == ['built-in', 'exception', 'base', 'class']:
Fred Drakee8b46131998-02-17 05:54:46 +00001166 command = 'defvr'
1167 cat_class = 'exception base class'
Fred Drakea4541af1997-12-29 17:19:22 +00001168 else:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001169 raise error, 'don\'t know what to do with indexsubitem ' + `idxsi`
1170
Guido van Rossum36f219d1996-09-11 21:30:40 +00001171 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001172 ch.data = command
1173
1174 cslinearg = [chunk(GROUP, wh, [chunk(PLAIN, wh, cat_class)])]
1175 cslinearg.append(chunk(PLAIN, wh, ' '))
1176 if class_class:
1177 cslinearg.append(chunk(GROUP, wh, [chunk(PLAIN, wh, class_class)]))
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001178 cslinearg.append(chunk(PLAIN, wh, ' '))
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001179 cslinearg.append(excname)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001180
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001181 pp.insert(i, chunk(GROUP, wh, cslinearg))
1182 i, length = i+1, length+1
1183 hist.command = command
1184 return length, i
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001185
1186## same for datadesc or dataline...
Fred Drakee8b46131998-02-17 05:54:46 +00001187def do_datadesc(length, buf, pp, i, index=1):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001188 startpoint = i-1
1189 ch = pp[startpoint]
1190 wh = ch.where
1191 length, newi = getnextarg(length, buf, pp, i)
1192 dataname = chunk(GROUP, wh, pp[i:newi])
1193 del pp[i:newi]
1194 length = length - (newi-i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001195
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001196 idxsi = hist.indexsubitem # words
Fred Drakee8b46131998-02-17 05:54:46 +00001197 command = 'defcv'
1198 cat_class = 'data'
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001199 class_class = ''
1200 if idxsi[-1] in ('attribute', 'option'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001201 cat_class = idxsi[-1]
1202 class_class = string.join(idxsi[:-1])
1203 elif len(idxsi) == 3 and idxsi[:2] == ['in', 'module']:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001204 class_class = string.join(idxsi[1:])
1205 elif len(idxsi) == 4 and idxsi[:3] == ['data', 'in', 'module']:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001206 class_class = string.join(idxsi[2:])
Fred Drake11b6d241996-10-10 20:09:56 +00001207 else:
Fred Drake11b6d241996-10-10 20:09:56 +00001208 class_class = string.join(idxsi)
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001209
Guido van Rossum36f219d1996-09-11 21:30:40 +00001210 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001211 ch.data = command
1212
1213 cslinearg = [chunk(GROUP, wh, [chunk(PLAIN, wh, cat_class)])]
1214 cslinearg.append(chunk(PLAIN, wh, ' '))
1215 if class_class:
1216 cslinearg.append(chunk(GROUP, wh, [chunk(PLAIN, wh, class_class)]))
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001217 cslinearg.append(chunk(PLAIN, wh, ' '))
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001218 cslinearg.append(dataname)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001219
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001220 pp.insert(i, chunk(GROUP, wh, cslinearg))
1221 i, length = i+1, length+1
1222 hist.command = command
1223 return length, i
1224
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001225
Fred Drakea4541af1997-12-29 17:19:22 +00001226def do_opcodedesc(length, buf, pp, i):
1227 startpoint = i-1
1228 ch = pp[startpoint]
1229 wh = ch.where
1230 length, newi = getnextarg(length, buf, pp, i)
1231 dataname = chunk(GROUP, wh, pp[i:newi])
1232 del pp[i:newi]
1233 length = length - (newi-i)
1234
Fred Drake43c93501997-12-29 21:40:35 +00001235 ch.chtype = CSLINE
1236 ch.data = "deffn"
Fred Drakea4541af1997-12-29 17:19:22 +00001237
Fred Drakee8b46131998-02-17 05:54:46 +00001238 cslinearg = [chunk(PLAIN, wh, 'byte\ code\ instruction'),
Fred Drakea4541af1997-12-29 17:19:22 +00001239 chunk(GROUP, wh, [chunk(PLAIN, wh, "byte code instruction")]),
1240 chunk(PLAIN, wh, ' '),
1241 dataname,
Fred Drake43c93501997-12-29 21:40:35 +00001242 chunk(PLAIN, wh, ' '),
1243 pp[i],
Fred Drakea4541af1997-12-29 17:19:22 +00001244 ]
1245
Fred Drake43c93501997-12-29 21:40:35 +00001246 pp[i] = chunk(GROUP, wh, cslinearg)
Fred Drakea4541af1997-12-29 17:19:22 +00001247 hist.command = ch.data
1248 return length, i
1249
1250
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001251# regular indices: those that are not set in tt font by default....
1252regindices = ('cindex', )
1253
1254# remove illegal characters from node names
1255def rm_commas_etc(text):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001256 result = ''
1257 changed = 0
1258 while 1:
1259 pos = re_commas_etc.search(text)
1260 if pos >= 0:
1261 changed = 1
1262 result = result + text[:pos]
1263 text = text[pos+1:]
1264 else:
1265 result = result + text
1266 break
1267 if changed:
Fred Drake43c93501997-12-29 21:40:35 +00001268 print 'Warning: nodename changed to ' + `result`
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001269
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001270 return result
1271
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001272# boolean flags
1273flags = {'texi': 1}
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001274
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001275
Fred Drake43c93501997-12-29 21:40:35 +00001276# map of \label{} to node names
1277label_nodes = {}
1278
1279
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001280##
1281## changeit: the actual routine, that changes the contents of the parsed
1282## chunks
1283##
1284
1285def changeit(buf, pp):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001286 global onlylatexspecial, hist, out
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001287
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001288 i, length = 0, len(pp)
1289 while 1:
1290 # sanity check: length should always equal len(pp)
1291 if len(pp) != length:
Fred Drake74a11e51998-02-26 05:52:37 +00001292 print i, pp[i]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001293 raise 'FATAL', 'inconsistent length. thought ' + `length` + ', but should really be ' + `len(pp)`
1294 if i >= length:
1295 break
1296 ch = pp[i]
1297 i = i + 1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001298
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001299 if type(ch) is StringType:
1300 #normally, only chunks are present in pp,
1301 # but in some cases, some extra info
1302 # has been inserted, e.g., the \end{...} clauses
1303 raise 'FATAL', 'got string, probably too many ' + `end`
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001304
Guido van Rossum36f219d1996-09-11 21:30:40 +00001305 if ch.chtype == chunk_type[GROUP]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001306 # check for {\em ...} constructs
Fred Drakee8b46131998-02-17 05:54:46 +00001307 data = ch.data
1308 if data and \
1309 data[0].chtype == chunk_type[CSNAME] and \
1310 fontchanges.has_key(s(buf, data[0].data)):
1311 k = s(buf, data[0].data)
1312 del data[0]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001313 pp.insert(i-1, chunk(CSNAME, ch.where, fontchanges[k]))
1314 length, i = length+1, i+1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001315
Fred Drakee8b46131998-02-17 05:54:46 +00001316 elif data:
1317 if len(data) \
1318 and data[0].chtype == chunk_type[GROUP] \
1319 and len(data[0].data) \
1320 and data[0].data[0].chtype == chunk_type[CSNAME] \
1321 and s(buf, data[0].data[0].data) == 'e':
1322 data[0] = data[0].data[0]
1323 print "invoking \\e magic group transform..."
1324 else:
1325## print "GROUP -- ch.data[0].data =", ch.data[0].data
1326 k = s(buf, data[0].data)
1327 if k == "fulllineitems":
1328 del data[0]
1329 pp[i-1:i] = data
1330 i = i - 1
1331 length = length + len(data) - 1
1332 continue
Fred Drake43c93501997-12-29 21:40:35 +00001333
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001334 # recursively parse the contents of the group
Fred Drakee8b46131998-02-17 05:54:46 +00001335 changeit(buf, data)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001336
Guido van Rossum36f219d1996-09-11 21:30:40 +00001337 elif ch.chtype == chunk_type[IF]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001338 # \if...
1339 flag, negate, data = ch.data
1340 ##print 'IF: flag, negate = ' + `flag, negate`
1341 if flag not in flags.keys():
1342 raise error, 'unknown flag ' + `flag`
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001343
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001344 value = flags[flag]
1345 if negate:
1346 value = (not value)
1347 del pp[i-1]
1348 length, i = length-1, i-1
1349 if value:
1350 pp[i:i] = data
1351 length = length + len(data)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001352
1353
Guido van Rossum36f219d1996-09-11 21:30:40 +00001354 elif ch.chtype == chunk_type[ENV]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001355 # \begin{...} ....
1356 envname, data = ch.data
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001357
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001358 #push this environment name on stack
1359 hist.inenv.insert(0, envname)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001360
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001361 #append an endenv chunk after grouped data
1362 data.append(chunk(ENDENV, ch.where, envname))
1363 ##[`data`]
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001364
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001365 #delete this object
1366 del pp[i-1]
1367 i, length = i-1, length-1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001368
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001369 #insert found data
1370 pp[i:i] = data
1371 length = length + len(data)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001372
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001373 if envname == 'verbatim':
1374 pp[i:i] = [chunk(CSLINE, ch.where, 'example'),
1375 chunk(GROUP, ch.where, [])]
1376 length, i = length+2, i+2
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001377
Fred Drakeef058031998-02-19 15:20:30 +00001378 elif envname in ('itemize', 'list', 'fulllineitems'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001379 if hist.itemizenesting > len(itemizesymbols):
1380 raise error, 'too deep itemize nesting'
Fred Drakee8b46131998-02-17 05:54:46 +00001381 if envname == 'list':
1382 del pp[i:i+2]
1383 length = length - 2
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001384 ingroupch = [chunk(CSNAME, ch.where,
Fred Drakee8b46131998-02-17 05:54:46 +00001385 itemizesymbols[hist.itemizenesting])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001386 hist.itemizenesting = hist.itemizenesting + 1
1387 pp[i:i] = [chunk(CSLINE, ch.where, 'itemize'),
Fred Drakee8b46131998-02-17 05:54:46 +00001388 chunk(GROUP, ch.where, ingroupch)]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001389 length, i = length+2, i+2
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001390
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001391 elif envname == 'enumerate':
1392 if hist.enumeratenesting > len(enumeratesymbols):
1393 raise error, 'too deep enumerate nesting'
1394 ingroupch = [chunk(PLAIN, ch.where,
Fred Drakee8b46131998-02-17 05:54:46 +00001395 enumeratesymbols[hist.enumeratenesting])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001396 hist.enumeratenesting = hist.enumeratenesting + 1
1397 pp[i:i] = [chunk(CSLINE, ch.where, 'enumerate'),
Fred Drakee8b46131998-02-17 05:54:46 +00001398 chunk(GROUP, ch.where, ingroupch)]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001399 length, i = length+2, i+2
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001400
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001401 elif envname == 'description':
1402 ingroupch = [chunk(CSNAME, ch.where, 'b')]
1403 pp[i:i] = [chunk(CSLINE, ch.where, 'table'),
1404 chunk(GROUP, ch.where, ingroupch)]
1405 length, i = length+2, i+2
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001406
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001407 elif (envname == 'tableiii') or (envname == 'tableii'):
1408 if (envname == 'tableii'):
1409 ltable = 2
1410 else:
1411 ltable = 3
1412 wh = ch.where
1413 newcode = []
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001414
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001415 #delete tabular format description
1416 # e.g., {|l|c|l|}
1417 length, newi = getnextarg(length, buf, pp, i)
1418 del pp[i:newi]
1419 length = length - (newi-i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001420
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001421 newcode.append(chunk(CSLINE, wh, 'table'))
1422 ingroupch = [chunk(CSNAME, wh, 'asis')]
1423 newcode.append(chunk(GROUP, wh, ingroupch))
1424 newcode.append(chunk(CSLINE, wh, 'item'))
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001425
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001426 #get the name of macro for @item
1427 # e.g., {code}
1428 length, newi = getnextarg(length, buf, pp, i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001429
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001430 if newi-i != 1:
1431 raise error, 'Sorry, expected 1 chunk argument'
Guido van Rossum36f219d1996-09-11 21:30:40 +00001432 if pp[i].chtype != chunk_type[PLAIN]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001433 raise error, 'Sorry, expected plain text argument'
1434 hist.itemargmacro = s(buf, pp[i].data)
Fred Drake9c7c6be1998-02-19 21:40:22 +00001435 if convertible_csname(hist.itemargmacro):
Fred Drake74a11e51998-02-26 05:52:37 +00001436 hist.itemargmacro = conversion(hist.itemargmacro)[1]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001437 del pp[i:newi]
1438 length = length - (newi-i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001439
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001440 itembody = []
1441 for count in range(ltable):
1442 length, newi = getnextarg(length, buf, pp, i)
1443 emphgroup = [
1444 chunk(CSNAME, wh, 'emph'),
1445 chunk(GROUP, 0, pp[i:newi])]
1446 del pp[i:newi]
1447 length = length - (newi-i)
1448 if count == 0:
1449 itemarg = emphgroup
1450 elif count == ltable-1:
1451 itembody = itembody + \
1452 [chunk(PLAIN, wh, ' --- ')] + emphgroup
1453 else:
1454 itembody = emphgroup
1455 newcode.append(chunk(GROUP, wh, itemarg))
1456 newcode = newcode + itembody + [chunk(DENDLINE, wh, '\n')]
1457 pp[i:i] = newcode
1458 l = len(newcode)
1459 length, i = length+l, i+l
1460 del newcode, l
1461
1462 if length != len(pp):
1463 raise 'STILL, SOMETHING wrong', `i`
1464
Fred Drakeef058031998-02-19 15:20:30 +00001465 elif envname in ('funcdesc', 'funcdescni', 'classdesc'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001466 pp.insert(i, chunk(PLAIN, ch.where, ''))
1467 i, length = i+1, length+1
Fred Drakee8b46131998-02-17 05:54:46 +00001468 length, i = do_funcdesc(length, buf, pp, i,
Fred Drakeef058031998-02-19 15:20:30 +00001469 envname[-2:] != "ni")
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001470
1471 elif envname == 'excdesc':
1472 pp.insert(i, chunk(PLAIN, ch.where, ''))
1473 i, length = i+1, length+1
1474 length, i = do_excdesc(length, buf, pp, i)
1475
Fred Drakee8b46131998-02-17 05:54:46 +00001476 elif envname in ('datadesc', 'datadescni'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001477 pp.insert(i, chunk(PLAIN, ch.where, ''))
1478 i, length = i+1, length+1
Fred Drakee8b46131998-02-17 05:54:46 +00001479 length, i = do_datadesc(length, buf, pp, i,
Fred Drakeef058031998-02-19 15:20:30 +00001480 envname[-2:] != "ni")
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001481
Fred Drakea4541af1997-12-29 17:19:22 +00001482 elif envname == 'opcodedesc':
1483 pp.insert(i, chunk(PLAIN, ch.where, ''))
1484 i, length = i+1, length+1
1485 length, i = do_opcodedesc(length, buf, pp, i)
1486
1487 elif envname == 'seealso':
1488 chunks = [chunk(ENDLINE, ch.where, "\n"),
1489 chunk(CSNAME, ch.where, "b"),
1490 chunk(GROUP, ch.where, [
1491 chunk(PLAIN, ch.where, "See also: ")]),
1492 chunk(ENDLINE, ch.where, "\n"),
1493 chunk(ENDLINE, ch.where, "\n")]
1494 pp[i-1:i] = chunks
1495 length = length + len(chunks) - 1
1496 i = i + len(chunks) - 1
1497
Fred Drake74a11e51998-02-26 05:52:37 +00001498 elif envname in ('sloppypar', 'flushleft', 'document'):
Fred Drake43c93501997-12-29 21:40:35 +00001499 pass
1500
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001501 else:
1502 print 'WARNING: don\'t know what to do with env ' + `envname`
1503
Guido van Rossum36f219d1996-09-11 21:30:40 +00001504 elif ch.chtype == chunk_type[ENDENV]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001505 envname = ch.data
1506 if envname != hist.inenv[0]:
1507 raise error, '\'end\' does not match. Name ' + `envname` + ', expected ' + `hist.inenv[0]`
1508 del hist.inenv[0]
1509 del pp[i-1]
1510 i, length = i-1, length-1
1511
1512 if envname == 'verbatim':
Fred Drake43c93501997-12-29 21:40:35 +00001513 pp[i:i] = [chunk(CSLINE, ch.where, 'end'),
1514 chunk(GROUP, ch.where, [
1515 chunk(PLAIN, ch.where, 'example')])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001516 i, length = i+2, length+2
Fred Drakeef058031998-02-19 15:20:30 +00001517 elif envname in ('itemize', 'list', 'fulllineitems'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001518 hist.itemizenesting = hist.itemizenesting - 1
Fred Drake43c93501997-12-29 21:40:35 +00001519 pp[i:i] = [chunk(CSLINE, ch.where, 'end'),
1520 chunk(GROUP, ch.where, [
1521 chunk(PLAIN, ch.where, 'itemize')])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001522 i, length = i+2, length+2
1523 elif envname == 'enumerate':
1524 hist.enumeratenesting = hist.enumeratenesting-1
Fred Drake43c93501997-12-29 21:40:35 +00001525 pp[i:i] = [chunk(CSLINE, ch.where, 'end'),
1526 chunk(GROUP, ch.where, [
1527 chunk(PLAIN, ch.where, 'enumerate')])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001528 i, length = i+2, length+2
1529 elif envname == 'description':
Fred Drake43c93501997-12-29 21:40:35 +00001530 pp[i:i] = [chunk(CSLINE, ch.where, 'end'),
1531 chunk(GROUP, ch.where, [
1532 chunk(PLAIN, ch.where, 'table')])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001533 i, length = i+2, length+2
1534 elif (envname == 'tableiii') or (envname == 'tableii'):
Fred Drake43c93501997-12-29 21:40:35 +00001535 pp[i:i] = [chunk(CSLINE, ch.where, 'end'),
1536 chunk(GROUP, ch.where, [
1537 chunk(PLAIN, ch.where, 'table')])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001538 i, length = i+2, length + 2
1539 pp.insert(i, chunk(DENDLINE, ch.where, '\n'))
1540 i, length = i+1, length+1
1541
Fred Drakeef058031998-02-19 15:20:30 +00001542 elif envname in ('funcdesc', 'excdesc', 'datadesc', 'classdesc',
Fred Drakee8b46131998-02-17 05:54:46 +00001543 'funcdescni', 'datadescni'):
Fred Drake43c93501997-12-29 21:40:35 +00001544 pp[i:i] = [chunk(CSLINE, ch.where, 'end'),
1545 chunk(GROUP, ch.where, [
1546 chunk(PLAIN, ch.where, hist.command)])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001547 i, length = i+2, length+2
Fred Drakea4541af1997-12-29 17:19:22 +00001548
Fred Drake43c93501997-12-29 21:40:35 +00001549 elif envname == 'opcodedesc':
1550 pp[i:i] = [chunk(CSLINE, ch.where, 'end'),
1551 chunk(GROUP, ch.where, [
1552 chunk(PLAIN, ch.where, "deffn")])]
1553 i, length = i+2, length+2
1554
Fred Drake74a11e51998-02-26 05:52:37 +00001555 elif envname in ('seealso', 'sloppypar', 'flushleft', 'document'):
Fred Drakea4541af1997-12-29 17:19:22 +00001556 pass
1557
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001558 else:
Fred Drakea4541af1997-12-29 17:19:22 +00001559 print 'WARNING: ending env %s has no actions' % `envname`
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001560
Guido van Rossum36f219d1996-09-11 21:30:40 +00001561 elif ch.chtype == chunk_type[CSNAME]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001562 # control name transformations
Guido van Rossum36f219d1996-09-11 21:30:40 +00001563 s_buf_data = s(buf, ch.data)
1564 if s_buf_data == 'optional':
1565 pp[i-1].chtype = chunk_type[PLAIN]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001566 pp[i-1].data = '['
1567 if (i < length) and \
Guido van Rossum36f219d1996-09-11 21:30:40 +00001568 (pp[i].chtype == chunk_type[GROUP]):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001569 cp=pp[i].data
1570 pp[i:i+1]=cp + [
1571 chunk(PLAIN, ch.where, ']')]
1572 length = length+len(cp)
Fred Drake74a11e51998-02-26 05:52:37 +00001573
Guido van Rossum36f219d1996-09-11 21:30:40 +00001574 elif s_buf_data in ignoredcommands:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001575 del pp[i-1]
1576 i, length = i-1, length-1
Fred Drake74a11e51998-02-26 05:52:37 +00001577
Guido van Rossum36f219d1996-09-11 21:30:40 +00001578 elif s_buf_data == '@' and \
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001579 i != length and \
Guido van Rossum36f219d1996-09-11 21:30:40 +00001580 pp[i].chtype == chunk_type[PLAIN] and \
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001581 s(buf, pp[i].data)[0] == '.':
1582 # \@. --> \. --> @.
1583 ch.data = '.'
1584 del pp[i]
Fred Drake74a11e51998-02-26 05:52:37 +00001585 length = length - 1
1586
1587 elif convertible_csname(s_buf_data):
1588 ch.chtype, ch.data, nix = conversion(s_buf_data)
1589 try:
1590 if nix and pp[i].chtype == chunk_type[GROUP] \
1591 and len(pp[i].data) == 0:
1592 del pp[i]
1593 length = length - 1
1594 except IndexError:
1595 pass
1596
Guido van Rossum36f219d1996-09-11 21:30:40 +00001597 elif s_buf_data == '\\':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001598 # \\ --> \* --> @*
1599 ch.data = '*'
Fred Drake74a11e51998-02-26 05:52:37 +00001600
Guido van Rossum36f219d1996-09-11 21:30:40 +00001601 elif len(s_buf_data) == 1 and \
1602 s_buf_data in onlylatexspecial:
1603 ch.chtype = chunk_type[PLAIN]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001604 # check if such a command is followed by
1605 # an empty group: e.g., `\%{}'. If so, remove
1606 # this empty group too
1607 if i < length and \
Guido van Rossum36f219d1996-09-11 21:30:40 +00001608 pp[i].chtype == chunk_type[GROUP] \
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001609 and len(pp[i].data) == 0:
1610 del pp[i]
1611 length = length-1
1612
Fred Drake74a11e51998-02-26 05:52:37 +00001613 elif s_buf_data == "appendix":
1614 hist.chaptertype = "appendix"
1615 del pp[i-1]
1616 i, length = i-1, length-1
1617
Guido van Rossum36f219d1996-09-11 21:30:40 +00001618 elif hist.inargs and s_buf_data in inargsselves:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001619 # This is the special processing of the
1620 # arguments of the \begin{funcdesc}... or
1621 # \funcline... arguments
1622 # \, --> , \[ --> [, \] --> ]
Guido van Rossum36f219d1996-09-11 21:30:40 +00001623 ch.chtype = chunk_type[PLAIN]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001624
Fred Drakee8b46131998-02-17 05:54:46 +00001625 elif s_buf_data == 'setindexsubitem':
1626 stuff = pp[i].data
1627 if len(stuff) != 1:
1628 raise error, "parameter to \\setindexsubitem{} too long"
1629 if pp[i].chtype != chunk_type[GROUP]:
1630 raise error, "bad chunk type following \\setindexsubitem" \
1631 "\nexpected GROUP, got " + str(ch.chtype)
1632 text = s(buf, stuff[0].data)
1633 if text[:1] != '(' or text[-1:] != ')':
1634 raise error, \
1635 'expected indexsubitem enclosed in parenteses'
1636 hist.indexsubitem = string.split(text[1:-1])
1637 del stuff, text
1638 del pp[i-1:i+1]
1639 i = i - 1
1640 length = length - 2
1641
1642 elif s_buf_data == 'newcommand':
1643 print "ignoring definition of \\" + s(buf, pp[i].data[0].data)
1644 del pp[i-1:i+2]
1645 i = i - 1
1646 length = length - 3
1647
Fred Drake74a11e51998-02-26 05:52:37 +00001648 elif s_buf_data == 'renewcommand':
1649 print "ignoring redefinition of \\" \
1650 + s(buf, pp[i].data[0].data)
1651 del pp[i-1:i+2]
1652 i = i - 1
1653 length = length - 3
1654
Fred Drakee8b46131998-02-17 05:54:46 +00001655 elif s_buf_data == 'mbox':
1656 stuff = pp[i].data
1657 pp[i-1:i+1] = stuff
1658 i = i - 1
1659 length = length + len(stuff) - 2
1660
1661 elif s_buf_data == 'version':
1662 ch.chtype = chunk_type[PLAIN]
1663 ch.data = release_version
1664
Guido van Rossum36f219d1996-09-11 21:30:40 +00001665 elif s_buf_data == 'item':
1666 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001667 length, newi = getoptarg(length, buf, pp, i)
1668 ingroupch = pp[i:newi]
1669 del pp[i:newi]
1670 length = length - (newi-i)
Fred Drake893e5e01996-10-25 22:13:10 +00001671 changeit(buf, ingroupch) # catch stuff inside the optional arg
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001672 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1673 i, length = i+1, length+1
1674
Guido van Rossum36f219d1996-09-11 21:30:40 +00001675 elif s_buf_data == 'ttindex':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001676 idxsi = hist.indexsubitem
1677
1678 cat_class = ''
1679 if len(idxsi) >= 2 and idxsi[1] in \
1680 ('method', 'function', 'protocol'):
1681 command = 'findex'
1682 elif len(idxsi) >= 2 and idxsi[1] in \
1683 ('exception', 'object'):
1684 command = 'vindex'
Fred Drake7edd8d31996-10-09 16:11:26 +00001685 elif len(idxsi) == 3 and idxsi[:2] == ['in', 'module']:
1686 command = 'cindex'
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001687 else:
Fred Drake7edd8d31996-10-09 16:11:26 +00001688 print 'WARNING: can\'t categorize ' + `idxsi` \
1689 + ' for \'ttindex\' command'
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001690 command = 'cindex'
1691
1692 if not cat_class:
1693 cat_class = '('+string.join(idxsi)+')'
1694
Guido van Rossum36f219d1996-09-11 21:30:40 +00001695 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001696 ch.data = command
1697
1698 length, newi = getnextarg(length, buf, pp, i)
1699 arg = pp[i:newi]
1700 del pp[i:newi]
1701 length = length - (newi-i)
1702
1703 cat_arg = [chunk(PLAIN, ch.where, cat_class)]
1704
1705 # determine what should be set in roman, and
1706 # what in tt-font
1707 if command in regindices:
1708
1709 arg = [chunk(CSNAME, ch.where, 't'),
Fred Drake9c7c6be1998-02-19 21:40:22 +00001710 chunk(GROUP, ch.where, arg)]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001711 else:
1712 cat_arg = [chunk(CSNAME, ch.where, 'r'),
Fred Drake9c7c6be1998-02-19 21:40:22 +00001713 chunk(GROUP, ch.where, cat_arg)]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001714
1715 ingroupch = arg + \
1716 [chunk(PLAIN, ch.where, ' ')] + \
1717 cat_arg
1718
1719 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1720 length, i = length+1, i+1
1721
Guido van Rossum36f219d1996-09-11 21:30:40 +00001722 elif s_buf_data == 'ldots':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001723 # \ldots --> \dots{} --> @dots{}
1724 ch.data = 'dots'
1725 if i == length \
Guido van Rossum36f219d1996-09-11 21:30:40 +00001726 or pp[i].chtype != chunk_type[GROUP] \
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001727 or pp[i].data != []:
1728 pp.insert(i, chunk(GROUP, ch.where, []))
1729 i, length = i+1, length+1
Fred Drake74a11e51998-02-26 05:52:37 +00001730
Guido van Rossum36f219d1996-09-11 21:30:40 +00001731 elif s_buf_data in themselves:
Fred Drakee8b46131998-02-17 05:54:46 +00001732 # \UNIX --> &UNIX;
Guido van Rossum36f219d1996-09-11 21:30:40 +00001733 ch.chtype = chunk_type[PLAIN]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001734 if i != length \
Guido van Rossum36f219d1996-09-11 21:30:40 +00001735 and pp[i].chtype == chunk_type[GROUP] \
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001736 and pp[i].data == []:
1737 del pp[i]
1738 length = length-1
Fred Drake74a11e51998-02-26 05:52:37 +00001739
1740## elif s_buf_data == 'copyright':
1741## if (pp[i].chtype == chunk_type[GROUP]
1742## and not pp[i].data):
1743## del pp[i]
1744## length = length - 1
1745## del pp[i-1]
1746## i, length = i-1, length-1
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001747
Fred Drakee8b46131998-02-17 05:54:46 +00001748 elif s_buf_data == 'manpage':
1749 ch.data = 'emph'
1750 sect = s(buf, pp[i+1].data[0].data)
1751 pp[i+1].data = "(%s)" % sect
1752 pp[i+1].chtype = chunk_type[PLAIN]
1753
Guido van Rossum36f219d1996-09-11 21:30:40 +00001754 elif s_buf_data in ('lineiii', 'lineii'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001755 # This is the most tricky one
1756 # \lineiii{a1}{a2}[{a3}] -->
1757 # @item @<cts. of itemargmacro>{a1}
1758 # a2 [ -- a3]
1759 #
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001760 if not hist.inenv:
1761 raise error, 'no environment for lineiii'
1762 if (hist.inenv[0] != 'tableiii') and \
1763 (hist.inenv[0] != 'tableii'):
1764 raise error, \
Guido van Rossum36f219d1996-09-11 21:30:40 +00001765 'wrong command (%s) in wrong environment (%s)' \
1766 % (s_buf_data, `hist.inenv[0]`)
1767 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001768 ch.data = 'item'
1769 length, newi = getnextarg(length, buf, pp, i)
Guido van Rossum36f219d1996-09-11 21:30:40 +00001770 ingroupch = [chunk(CSNAME, 0, hist.itemargmacro),
1771 chunk(GROUP, 0, pp[i:newi])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001772 del pp[i:newi]
1773 length = length - (newi-i)
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001774 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1775 grouppos = i
1776 i, length = i+1, length+1
1777 length, i = getnextarg(length, buf, pp, i)
1778 length, newi = getnextarg(length, buf, pp, i)
1779 if newi > i:
1780 # we have a 3rd arg
1781 pp.insert(i, chunk(PLAIN, ch.where, ' --- '))
1782 i = newi + 1
1783 length = length + 1
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001784 if length != len(pp):
1785 raise 'IN LINEIII IS THE ERR', `i`
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001786
Fred Drake74a11e51998-02-26 05:52:37 +00001787 elif s_buf_data in ('chapter', 'section',
1788 'subsection', 'subsubsection'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001789 #\xxxsection{A} ---->
1790 # @node A, , ,
1791 # @xxxsection A
1792 ## also: remove commas and quotes
Fred Drake74a11e51998-02-26 05:52:37 +00001793 if s_buf_data == "chapter":
1794 ch.data = hist.chaptertype
Guido van Rossum36f219d1996-09-11 21:30:40 +00001795 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001796 length, newi = getnextarg(length, buf, pp, i)
Fred Drakee8b46131998-02-17 05:54:46 +00001797 afternodenamecmd = next_command_p(length, buf,
1798 pp, newi, 'nodename')
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001799 if afternodenamecmd < 0:
1800 cp1 = crcopy(pp[i:newi])
Guido van Rossum36f219d1996-09-11 21:30:40 +00001801 pp[i:newi] = [chunk(GROUP, ch.where, pp[i:newi])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001802 length, newi = length - (newi-i) + 1, i+1
1803 text = flattext(buf, cp1)
1804 text = invent_node_names(text)
1805 else:
Fred Drakee8b46131998-02-17 05:54:46 +00001806 length, endarg = getnextarg(length, buf,
1807 pp, afternodenamecmd)
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001808 cp1 = crcopy(pp[afternodenamecmd:endarg])
1809 del pp[newi:endarg]
1810 length = length - (endarg-newi)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001811
Guido van Rossum36f219d1996-09-11 21:30:40 +00001812 pp[i:newi] = [chunk(GROUP, ch.where, pp[i:newi])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001813 length, newi = length - (newi-i) + 1, i + 1
1814 text = flattext(buf, cp1)
1815 if text[-1] == '.':
1816 text = text[:-1]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001817 if text in hist.nodenames:
1818 print 'WARNING: node name ' + `text` + ' already used'
1819 out.doublenodes.append(text)
1820 else:
1821 hist.nodenames.append(text)
1822 text = rm_commas_etc(text)
Guido van Rossum36f219d1996-09-11 21:30:40 +00001823 pp[i-1:i-1] = [chunk(CSLINE, ch.where, 'node'),
1824 chunk(GROUP, ch.where, [
1825 chunk(PLAIN, ch.where, text+', , ,')
1826 ])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001827 i, length = newi+2, length+2
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001828
Guido van Rossum36f219d1996-09-11 21:30:40 +00001829 elif s_buf_data == 'funcline':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001830 # fold it to a very short environment
Guido van Rossum36f219d1996-09-11 21:30:40 +00001831 pp[i-1:i-1] = [chunk(CSLINE, ch.where, 'end'),
1832 chunk(GROUP, ch.where, [
1833 chunk(PLAIN, ch.where, hist.command)])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001834 i, length = i+2, length+2
1835 length, i = do_funcdesc(length, buf, pp, i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001836
Guido van Rossum36f219d1996-09-11 21:30:40 +00001837 elif s_buf_data == 'dataline':
1838 pp[i-1:i-1] = [chunk(CSLINE, ch.where, 'end'),
1839 chunk(GROUP, ch.where, [
1840 chunk(PLAIN, ch.where, hist.command)])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001841 i, length = i+2, length+2
1842 length, i = do_datadesc(length, buf, pp, i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001843
Guido van Rossum36f219d1996-09-11 21:30:40 +00001844 elif s_buf_data == 'excline':
1845 pp[i-1:i-1] = [chunk(CSLINE, ch.where, 'end'),
1846 chunk(GROUP, ch.where, [
1847 chunk(PLAIN, ch.where, hist.command)])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001848 i, length = i+2, length+2
1849 length, i = do_excdesc(length, buf, pp, i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001850
Guido van Rossum36f219d1996-09-11 21:30:40 +00001851 elif s_buf_data == 'index':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001852 #\index{A} --->
1853 # @cindex A
Guido van Rossum36f219d1996-09-11 21:30:40 +00001854 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001855 ch.data = 'cindex'
1856 length, newi = getnextarg(length, buf, pp, i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001857
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001858 ingroupch = pp[i:newi]
1859 del pp[i:newi]
1860 length = length - (newi-i)
1861 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1862 length, i = length+1, i+1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001863
Guido van Rossum36f219d1996-09-11 21:30:40 +00001864 elif s_buf_data == 'bifuncindex':
1865 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001866 ch.data = 'findex'
1867 length, newi = getnextarg(length, buf, pp, i)
1868 ingroupch = pp[i:newi]
1869 del pp[i:newi]
1870 length = length - (newi-i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001871
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001872 ingroupch.append(chunk(PLAIN, ch.where, ' '))
1873 ingroupch.append(chunk(CSNAME, ch.where, 'r'))
1874 ingroupch.append(chunk(GROUP, ch.where, [
1875 chunk(PLAIN, ch.where,
1876 '(built-in function)')]))
1877
1878 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1879 length, i = length+1, i+1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001880
Guido van Rossum36f219d1996-09-11 21:30:40 +00001881 elif s_buf_data == 'obindex':
1882 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001883 ch.data = 'findex'
1884 length, newi = getnextarg(length, buf, pp, i)
1885 ingroupch = pp[i:newi]
1886 del pp[i:newi]
1887 length = length - (newi-i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001888
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001889 ingroupch.append(chunk(PLAIN, ch.where, ' '))
1890 ingroupch.append(chunk(CSNAME, ch.where, 'r'))
1891 ingroupch.append(chunk(GROUP, ch.where, [
1892 chunk(PLAIN, ch.where,
1893 '(object)')]))
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001894
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001895 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1896 length, i = length+1, i+1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001897
Guido van Rossum36f219d1996-09-11 21:30:40 +00001898 elif s_buf_data == 'opindex':
1899 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001900 ch.data = 'findex'
1901 length, newi = getnextarg(length, buf, pp, i)
1902 ingroupch = pp[i:newi]
1903 del pp[i:newi]
1904 length = length - (newi-i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001905
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001906 ingroupch.append(chunk(PLAIN, ch.where, ' '))
1907 ingroupch.append(chunk(CSNAME, ch.where, 'r'))
1908 ingroupch.append(chunk(GROUP, ch.where, [
1909 chunk(PLAIN, ch.where,
1910 '(operator)')]))
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001911
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001912 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1913 length, i = length+1, i+1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001914
Fred Drakea4541af1997-12-29 17:19:22 +00001915 elif s_buf_data in ('bimodindex', 'refbimodindex'):
Guido van Rossum36f219d1996-09-11 21:30:40 +00001916 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001917 ch.data = 'pindex'
1918 length, newi = getnextarg(length, buf, pp, i)
1919 ingroupch = pp[i:newi]
1920 del pp[i:newi]
1921 length = length - (newi-i)
1922
1923 ingroupch.append(chunk(PLAIN, ch.where, ' '))
1924 ingroupch.append(chunk(CSNAME, ch.where, 'r'))
1925 ingroupch.append(chunk(GROUP, ch.where, [
1926 chunk(PLAIN, ch.where,
1927 '(built-in)')]))
1928
1929 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1930 length, i = length+1, i+1
1931
Fred Drakea4541af1997-12-29 17:19:22 +00001932 elif s_buf_data == 'refmodindex':
1933 ch.chtype = chunk_type[CSLINE]
1934 ch.data = 'pindex'
1935 length, newi = getnextarg(length, buf, pp, i)
1936 ingroupch = pp[i:newi]
1937 del pp[i:newi]
1938 length = length - (newi-i)
1939
Fred Drakea4541af1997-12-29 17:19:22 +00001940 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1941 length, i = length+1, i+1
1942
Fred Drakea4541af1997-12-29 17:19:22 +00001943 elif s_buf_data in ('stmodindex', 'refstmodindex'):
Guido van Rossum36f219d1996-09-11 21:30:40 +00001944 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001945 # use the program index as module index
1946 ch.data = 'pindex'
1947 length, newi = getnextarg(length, buf, pp, i)
1948 ingroupch = pp[i:newi]
1949 del pp[i:newi]
1950 length = length - (newi-i)
1951
1952 ingroupch.append(chunk(PLAIN, ch.where, ' '))
1953 ingroupch.append(chunk(CSNAME, ch.where, 'r'))
1954 ingroupch.append(chunk(GROUP, ch.where, [
1955 chunk(PLAIN, ch.where,
1956 '(standard)')]))
1957
1958 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1959 length, i = length+1, i+1
1960
Fred Drakea4541af1997-12-29 17:19:22 +00001961 elif s_buf_data in ('stmodindex', 'refstmodindex'):
1962 ch.chtype = chunk_type[CSLINE]
1963 # use the program index as module index
1964 ch.data = 'pindex'
1965 length, newi = getnextarg(length, buf, pp, i)
1966 ingroupch = pp[i:newi]
1967 del pp[i:newi]
1968 length = length - (newi-i)
1969
1970 ingroupch.append(chunk(PLAIN, ch.where, ' '))
1971 ingroupch.append(chunk(CSNAME, ch.where, 'r'))
1972 ingroupch.append(chunk(GROUP, ch.where, [
1973 chunk(PLAIN, ch.where,
1974 '(standard)')]))
1975
1976 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1977 length, i = length+1, i+1
1978
Fred Drake74a11e51998-02-26 05:52:37 +00001979 elif s_buf_data == 'stindex':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001980 # XXX must actually go to newindex st
Fred Drakea4541af1997-12-29 17:19:22 +00001981 what = (s_buf_data[:2] == "st") and "statement" or "keyword"
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001982 wh = ch.where
Guido van Rossum36f219d1996-09-11 21:30:40 +00001983 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001984 ch.data = 'cindex'
1985 length, newi = getnextarg(length, buf, pp, i)
1986 ingroupch = [chunk(CSNAME, wh, 'code'),
Fred Drakee8b46131998-02-17 05:54:46 +00001987 chunk(GROUP, wh, pp[i:newi])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001988
1989 del pp[i:newi]
1990 length = length - (newi-i)
1991
1992 t = ingroupch[:]
Fred Drakea4541af1997-12-29 17:19:22 +00001993 t.append(chunk(PLAIN, wh, ' ' + what))
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001994
1995 pp.insert(i, chunk(GROUP, wh, t))
1996 i, length = i+1, length+1
1997
1998 pp.insert(i, chunk(CSLINE, wh, 'cindex'))
1999 i, length = i+1, length+1
2000
2001 t = ingroupch[:]
Fred Drakea4541af1997-12-29 17:19:22 +00002002 t.insert(0, chunk(PLAIN, wh, what + ', '))
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002003
2004 pp.insert(i, chunk(GROUP, wh, t))
2005 i, length = i+1, length+1
2006
Guido van Rossum36f219d1996-09-11 21:30:40 +00002007 elif s_buf_data == 'indexii':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002008 #\indexii{A}{B} --->
2009 # @cindex A B
2010 # @cindex B, A
2011 length, newi = getnextarg(length, buf, pp, i)
2012 cp11 = pp[i:newi]
2013 cp21 = crcopy(pp[i:newi])
2014 del pp[i:newi]
2015 length = length - (newi-i)
2016 length, newi = getnextarg(length, buf, pp, i)
2017 cp12 = pp[i:newi]
2018 cp22 = crcopy(pp[i:newi])
2019 del pp[i:newi]
2020 length = length - (newi-i)
2021
Guido van Rossum36f219d1996-09-11 21:30:40 +00002022 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002023 ch.data = 'cindex'
2024 pp.insert(i, chunk(GROUP, ch.where, cp11 + [
2025 chunk(PLAIN, ch.where, ' ')] + cp12))
2026 i, length = i+1, length+1
2027 pp[i:i] = [chunk(CSLINE, ch.where, 'cindex'),
2028 chunk(GROUP, ch.where, cp22 + [
2029 chunk(PLAIN, ch.where, ', ')]+ cp21)]
2030 i, length = i+2, length+2
2031
Guido van Rossum36f219d1996-09-11 21:30:40 +00002032 elif s_buf_data == 'indexiii':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002033 length, newi = getnextarg(length, buf, pp, i)
2034 cp11 = pp[i:newi]
2035 cp21 = crcopy(pp[i:newi])
2036 cp31 = crcopy(pp[i:newi])
2037 del pp[i:newi]
2038 length = length - (newi-i)
2039 length, newi = getnextarg(length, buf, pp, i)
2040 cp12 = pp[i:newi]
2041 cp22 = crcopy(pp[i:newi])
2042 cp32 = crcopy(pp[i:newi])
2043 del pp[i:newi]
2044 length = length - (newi-i)
2045 length, newi = getnextarg(length, buf, pp, i)
2046 cp13 = pp[i:newi]
2047 cp23 = crcopy(pp[i:newi])
2048 cp33 = crcopy(pp[i:newi])
2049 del pp[i:newi]
2050 length = length - (newi-i)
2051
Guido van Rossum36f219d1996-09-11 21:30:40 +00002052 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002053 ch.data = 'cindex'
2054 pp.insert(i, chunk(GROUP, ch.where, cp11 + [
2055 chunk(PLAIN, ch.where, ' ')] + cp12
2056 + [chunk(PLAIN, ch.where, ' ')]
2057 + cp13))
2058 i, length = i+1, length+1
2059 pp[i:i] = [chunk(CSLINE, ch.where, 'cindex'),
2060 chunk(GROUP, ch.where, cp22 + [
2061 chunk(PLAIN, ch.where, ' ')]+ cp23
2062 + [chunk(PLAIN, ch.where, ', ')] +
2063 cp21)]
2064 i, length = i+2, length+2
2065 pp[i:i] = [chunk(CSLINE, ch.where, 'cindex'),
2066 chunk(GROUP, ch.where, cp33 + [
2067 chunk(PLAIN, ch.where, ', ')]+ cp31
2068 + [chunk(PLAIN, ch.where, ' ')] +
2069 cp32)]
2070 i, length = i+2, length+2
2071
Guido van Rossum36f219d1996-09-11 21:30:40 +00002072 elif s_buf_data == 'indexiv':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002073 length, newi = getnextarg(length, buf, pp, i)
2074 cp11 = pp[i:newi]
2075 cp21 = crcopy(pp[i:newi])
2076 cp31 = crcopy(pp[i:newi])
2077 cp41 = crcopy(pp[i:newi])
2078 del pp[i:newi]
2079 length = length - (newi-i)
2080 length, newi = getnextarg(length, buf, pp, i)
2081 cp12 = pp[i:newi]
2082 cp22 = crcopy(pp[i:newi])
2083 cp32 = crcopy(pp[i:newi])
2084 cp42 = crcopy(pp[i:newi])
2085 del pp[i:newi]
2086 length = length - (newi-i)
2087 length, newi = getnextarg(length, buf, pp, i)
2088 cp13 = pp[i:newi]
2089 cp23 = crcopy(pp[i:newi])
2090 cp33 = crcopy(pp[i:newi])
2091 cp43 = crcopy(pp[i:newi])
2092 del pp[i:newi]
2093 length = length - (newi-i)
2094 length, newi = getnextarg(length, buf, pp, i)
2095 cp14 = pp[i:newi]
2096 cp24 = crcopy(pp[i:newi])
2097 cp34 = crcopy(pp[i:newi])
2098 cp44 = crcopy(pp[i:newi])
2099 del pp[i:newi]
2100 length = length - (newi-i)
2101
Guido van Rossum36f219d1996-09-11 21:30:40 +00002102 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002103 ch.data = 'cindex'
2104 ingroupch = cp11 + \
2105 spacech + cp12 + \
2106 spacech + cp13 + \
2107 spacech + cp14
2108 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
2109 i, length = i+1, length+1
2110 ingroupch = cp22 + \
2111 spacech + cp23 + \
2112 spacech + cp24 + \
2113 commach + cp21
2114 pp[i:i] = cindexch + [
2115 chunk(GROUP, ch.where, ingroupch)]
2116 i, length = i+2, length+2
2117 ingroupch = cp33 + \
2118 spacech + cp34 + \
2119 commach + cp31 + \
2120 spacech + cp32
2121 pp[i:i] = cindexch + [
2122 chunk(GROUP, ch.where, ingroupch)]
2123 i, length = i+2, length+2
2124 ingroupch = cp44 + \
2125 commach + cp41 + \
2126 spacech + cp42 + \
2127 spacech + cp43
2128 pp[i:i] = cindexch + [
2129 chunk(GROUP, ch.where, ingroupch)]
2130 i, length = i+2, length+2
2131
Fred Drakea4541af1997-12-29 17:19:22 +00002132 elif s_buf_data == 'seemodule':
2133 ch.data = "code"
Fred Drakee8b46131998-02-17 05:54:46 +00002134 # this is needed for just one of the input files... -sigh-
2135 while pp[i+1].chtype == chunk_type[COMMENT]:
2136 i = i + 1
Fred Drakea4541af1997-12-29 17:19:22 +00002137 data = pp[i+1].data
Fred Drakee8b46131998-02-17 05:54:46 +00002138 oparen = chunk(PLAIN, ch.where, " (")
2139 data.insert(0, oparen)
Fred Drakea4541af1997-12-29 17:19:22 +00002140 data.append(chunk(PLAIN, ch.where, ")"))
2141 pp[i+1:i+2] = data
2142 length = length + len(data) - 1
2143
2144 elif s_buf_data == 'seetext':
2145 data = pp[i].data
2146 data.insert(0, chunk(ENDLINE, ch.where, "\n"))
2147 pp[i-1:i+1] = data
2148 i = i - 1
2149 length = length + len(data) - 2
2150
Fred Drakeb98cd391998-03-04 06:33:43 +00002151 elif s_buf_data == 'deprecated':
2152 length, newi = getnextarg(length, buf, pp, i)
2153 version = pp[i:newi][0]
Fred Drakeb98cd391998-03-04 06:33:43 +00002154 length, newi2 = getnextarg(length, buf, pp, newi)
2155 action = pp[newi:newi2]
Fred Drakeb98cd391998-03-04 06:33:43 +00002156 del pp[i-1:newi2]
2157 length = length - (newi2 - i) - 1
2158 stuff = [chunk(PLAIN, ch.where, 'Deprecated since release '),
2159 version,
2160 chunk(PLAIN, ch.where, '.')]
2161 chunks = [chunk(CSNAME, ch.where, 'strong'),
2162 chunk(GROUP, ch.where, stuff),
2163 chunk(PLAIN, ch.where, ' ')] + action \
2164 + [chunk(DENDLINE, ch.where, '\n')]
2165 i = i - 1
2166 pp[i:i] = chunks
2167 length = length + len(chunks)
2168
Fred Drake43c93501997-12-29 21:40:35 +00002169 elif s_buf_data == "quad":
2170 ch.chtype = PLAIN
2171 ch.data = " "
2172
Fred Drake74a11e51998-02-26 05:52:37 +00002173 elif s_buf_data in ('usepackage', 'input'):
2174 del pp[i-1:i+1]
2175 i, length = i-1, length-2
2176
Fred Drakee8b46131998-02-17 05:54:46 +00002177 elif s_buf_data in ('noindent', 'indexsubitem', 'footnote'):
Guido van Rossum36f219d1996-09-11 21:30:40 +00002178 pass
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002179
Fred Drakea4541af1997-12-29 17:19:22 +00002180 elif s_buf_data == 'label':
Fred Drake43c93501997-12-29 21:40:35 +00002181 name = s(buf, pp[i].data[0].data)
Fred Drakea4541af1997-12-29 17:19:22 +00002182 del pp[i-1:i+1]
2183 length = length - 2
2184 i = i - 1
Fred Drake43c93501997-12-29 21:40:35 +00002185 label_nodes[name] = hist.nodenames[-1]
2186
Fred Drakee8b46131998-02-17 05:54:46 +00002187 elif s_buf_data == 'rfc':
2188 ch.chtype = chunk_type[PLAIN]
2189 ch.data = "RFC " + s(buf, pp[i].data[0].data)
2190 del pp[i]
2191 length = length - 1
2192
Fred Drake43c93501997-12-29 21:40:35 +00002193 elif s_buf_data == 'ref':
2194 name = s(buf, pp[i].data[0].data)
2195 if label_nodes.has_key(name):
2196 pp[i].data[0].data = label_nodes[name]
2197 else:
2198 pp[i-1:i+1] = [
2199 chunk(PLAIN, ch.where,
2200 "(unknown node reference: %s)" % name)]
2201 length = length - 1
2202 print "WARNING: unknown node label", `name`
Fred Drakea4541af1997-12-29 17:19:22 +00002203
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002204 else:
Guido van Rossum36f219d1996-09-11 21:30:40 +00002205 print "don't know what to do with keyword " + s_buf_data
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002206
2207
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002208re_atsign = regex.compile('[@{}]')
2209re_newline = regex.compile('\n')
2210
2211def dumpit(buf, wm, pp):
2212
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002213 global out
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002214
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002215 i, length = 0, len(pp)
2216
2217 addspace = 0
2218
2219 while 1:
2220 if len(pp) != length:
2221 raise 'FATAL', 'inconsistent length'
2222 if i == length:
2223 break
2224 ch = pp[i]
2225 i = i + 1
2226
Guido van Rossum36f219d1996-09-11 21:30:40 +00002227 dospace = addspace
2228 addspace = 0
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002229
Guido van Rossum36f219d1996-09-11 21:30:40 +00002230 if ch.chtype == chunk_type[CSNAME]:
2231 s_buf_data = s(buf, ch.data)
Fred Drake74a11e51998-02-26 05:52:37 +00002232## if s_buf_data == 'e':
2233## wm('\\')
2234## continue
2235## if s_buf_data == '$':
2236## wm('$')
2237## continue
Guido van Rossum36f219d1996-09-11 21:30:40 +00002238 wm('@' + s_buf_data)
2239 if s_buf_data == 'node' and \
2240 pp[i].chtype == chunk_type[PLAIN] and \
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002241 s(buf, pp[i].data) in out.doublenodes:
2242 ##XXX doesnt work yet??
2243 wm(' ZZZ-' + zfill(`i`, 4))
Guido van Rossum36f219d1996-09-11 21:30:40 +00002244 if s_buf_data[0] in string.letters:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002245 addspace = 1
Guido van Rossum36f219d1996-09-11 21:30:40 +00002246 elif ch.chtype == chunk_type[PLAIN]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002247 if dospace and s(buf, ch.data) not in (' ', '\t'):
2248 wm(' ')
2249 text = s(buf, ch.data)
2250 while 1:
2251 pos = re_atsign.search(text)
2252 if pos < 0:
2253 break
2254 wm(text[:pos] + '@' + text[pos])
2255 text = text[pos+1:]
2256 wm(text)
Guido van Rossum36f219d1996-09-11 21:30:40 +00002257 elif ch.chtype == chunk_type[GROUP]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002258 wm('{')
2259 dumpit(buf, wm, ch.data)
2260 wm('}')
Guido van Rossum36f219d1996-09-11 21:30:40 +00002261 elif ch.chtype == chunk_type[DENDLINE]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002262 wm('\n\n')
2263 while i != length and pp[i].chtype in \
Guido van Rossum36f219d1996-09-11 21:30:40 +00002264 (chunk_type[DENDLINE], chunk_type[ENDLINE]):
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002265 i = i + 1
Guido van Rossum36f219d1996-09-11 21:30:40 +00002266 elif ch.chtype == chunk_type[OTHER]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002267 wm(s(buf, ch.data))
Guido van Rossum36f219d1996-09-11 21:30:40 +00002268 elif ch.chtype == chunk_type[ACTIVE]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002269 wm(s(buf, ch.data))
Guido van Rossum36f219d1996-09-11 21:30:40 +00002270 elif ch.chtype == chunk_type[ENDLINE]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002271 wm('\n')
Guido van Rossum36f219d1996-09-11 21:30:40 +00002272 elif ch.chtype == chunk_type[CSLINE]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002273 if i >= 2 and pp[i-2].chtype not in \
Guido van Rossum36f219d1996-09-11 21:30:40 +00002274 (chunk_type[ENDLINE], chunk_type[DENDLINE]) \
2275 and (pp[i-2].chtype != chunk_type[PLAIN]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002276 or s(buf, pp[i-2].data)[-1] != '\n'):
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002277
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002278 wm('\n')
2279 wm('@' + s(buf, ch.data))
2280 if i == length:
2281 raise error, 'CSLINE expected another chunk'
Guido van Rossum36f219d1996-09-11 21:30:40 +00002282 if pp[i].chtype != chunk_type[GROUP]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002283 raise error, 'CSLINE expected GROUP'
2284 if type(pp[i].data) != ListType:
2285 raise error, 'GROUP chould contain []-data'
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002286
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002287 wobj = Wobj()
2288 dumpit(buf, wobj.write, pp[i].data)
2289 i = i + 1
2290 text = wobj.data
2291 del wobj
2292 if text:
2293 wm(' ')
2294 while 1:
2295 pos = re_newline.search(text)
2296 if pos < 0:
2297 break
2298 print 'WARNING: found newline in csline arg'
2299 wm(text[:pos] + ' ')
2300 text = text[pos+1:]
2301 wm(text)
2302 if i >= length or \
Guido van Rossum36f219d1996-09-11 21:30:40 +00002303 pp[i].chtype not in (chunk_type[CSLINE],
2304 chunk_type[ENDLINE], chunk_type[DENDLINE]) \
2305 and (pp[i].chtype != chunk_type[PLAIN]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002306 or s(buf, pp[i].data)[0] != '\n'):
2307 wm('\n')
Guido van Rossum49604d31996-09-10 22:19:51 +00002308
Guido van Rossum36f219d1996-09-11 21:30:40 +00002309 elif ch.chtype == chunk_type[COMMENT]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002310 if s(buf, ch.data) and \
2311 regex.match('^[ \t]*$', s(buf, ch.data)) < 0:
Guido van Rossum36f219d1996-09-11 21:30:40 +00002312 if i >= 2 \
Fred Drake74a11e51998-02-26 05:52:37 +00002313 and pp[i-2].chtype not in (chunk_type[ENDLINE],
2314 chunk_type[DENDLINE]) \
Guido van Rossum36f219d1996-09-11 21:30:40 +00002315 and not (pp[i-2].chtype == chunk_type[PLAIN]
2316 and regex.match('\\(.\\|\n\\)*[ \t]*\n$', s(buf, pp[i-2].data)) >= 0):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002317 wm('\n')
2318 wm('@c ' + s(buf, ch.data))
Guido van Rossum36f219d1996-09-11 21:30:40 +00002319 elif ch.chtype == chunk_type[IGNORE]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002320 pass
2321 else:
2322 try:
2323 str = `s(buf, ch.data)`
2324 except TypeError:
2325 str = `ch.data`
2326 if len(str) > 400:
2327 str = str[:400] + '...'
2328 print 'warning:', ch.chtype, 'not handled, data ' + str
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002329
2330
2331
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002332def main():
Fred Drakee8b46131998-02-17 05:54:46 +00002333 global release_version
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002334 outfile = None
2335 headerfile = 'texipre.dat'
2336 trailerfile = 'texipost.dat'
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002337
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002338 try:
Fred Drakee8b46131998-02-17 05:54:46 +00002339 opts, args = getopt.getopt(sys.argv[1:], 'o:h:t:v:')
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002340 except getopt.error:
2341 args = []
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002342
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002343 if not args:
2344 print 'usage: partparse [-o outfile] [-h headerfile]',
2345 print '[-t trailerfile] file ...'
2346 sys.exit(2)
Guido van Rossum7a2dba21993-11-05 14:45:11 +00002347
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002348 for opt, arg in opts:
2349 if opt == '-o': outfile = arg
2350 if opt == '-h': headerfile = arg
2351 if opt == '-t': trailerfile = arg
Fred Drakee8b46131998-02-17 05:54:46 +00002352 if opt == '-v': release_version = arg
Guido van Rossum7a2dba21993-11-05 14:45:11 +00002353
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002354 if not outfile:
2355 root, ext = os.path.splitext(args[0])
2356 outfile = root + '.texi'
Guido van Rossum7a2dba21993-11-05 14:45:11 +00002357
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002358 if outfile in args:
2359 print 'will not overwrite input file', outfile
2360 sys.exit(2)
Guido van Rossum7a2dba21993-11-05 14:45:11 +00002361
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002362 outf = open(outfile, 'w')
2363 outf.write(open(headerfile, 'r').read())
Guido van Rossum7a2dba21993-11-05 14:45:11 +00002364
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002365 for file in args:
2366 if len(args) > 1: print '='*20, file, '='*20
2367 buf = open(file, 'r').read()
Fred Drakeb98cd391998-03-04 06:33:43 +00002368 chunk.buf = buf
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002369 w, pp = parseit(buf)
2370 startchange()
2371 changeit(buf, pp)
2372 dumpit(buf, outf.write, pp)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002373
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002374 outf.write(open(trailerfile, 'r').read())
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002375
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002376 outf.close()
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002377
Guido van Rossum49604d31996-09-10 22:19:51 +00002378if __name__ == "__main__":
2379 main()