blob: 21e20fcd528ca8ddf28fffb0b87102d6df5fe574 [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
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000200 def __repr__(self):
201 return 'chunk' + `self.chtype, self.where, self.data`
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000202
203# and the wrapper
Guido van Rossum49604d31996-09-10 22:19:51 +0000204chunk = Chunk
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000205
206
207error = 'partparse.error'
208
209#
210# TeX's catcodes...
211#
212CC_ESCAPE = 0
213CC_LBRACE = 1
214CC_RBRACE = 2
215CC_MATHSHIFT = 3
216CC_ALIGNMENT = 4
217CC_ENDLINE = 5
218CC_PARAMETER = 6
219CC_SUPERSCRIPT = 7
220CC_SUBSCRIPT = 8
221CC_IGNORE = 9
222CC_WHITE = 10
223CC_LETTER = 11
224CC_OTHER = 12
225CC_ACTIVE = 13
226CC_COMMENT = 14
227CC_INVALID = 15
228
229# and the names
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000230cc_names = [
231 'CC_ESCAPE',
232 'CC_LBRACE',
233 'CC_RBRACE',
234 'CC_MATHSHIFT',
235 'CC_ALIGNMENT',
236 'CC_ENDLINE',
237 'CC_PARAMETER',
238 'CC_SUPERSCRIPT',
239 'CC_SUBSCRIPT',
240 'CC_IGNORE',
241 'CC_WHITE',
242 'CC_LETTER',
243 'CC_OTHER',
244 'CC_ACTIVE',
245 'CC_COMMENT',
246 'CC_INVALID',
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000247 ]
248
249# Show a list of catcode-name-symbols
250def pcl(codelist):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000251 result = ''
252 for i in codelist:
253 result = result + cc_names[i] + ', '
254 return '[' + result[:-2] + ']'
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000255
256# the name of the catcode (ACTIVE, OTHER, etc.)
257def pc(code):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000258 return cc_names[code]
259
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000260
261# Which catcodes make the parser stop parsing regular plaintext
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000262regular_stopcodes = [CC_ESCAPE, CC_LBRACE, CC_RBRACE, CC_MATHSHIFT,
263 CC_ALIGNMENT, CC_PARAMETER, CC_SUPERSCRIPT, CC_SUBSCRIPT,
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000264 CC_IGNORE, CC_ACTIVE, CC_COMMENT, CC_INVALID, CC_ENDLINE]
265
266# same for scanning a control sequence name
267csname_scancodes = [CC_LETTER]
268
269# same for gobbling LWSP
270white_scancodes = [CC_WHITE]
271##white_scancodes = [CC_WHITE, CC_ENDLINE]
272
273# make a list of all catcode id's, except for catcode ``other''
274all_but_other_codes = range(16)
275del all_but_other_codes[CC_OTHER]
276##print all_but_other_codes
277
278# when does a comment end
279comment_stopcodes = [CC_ENDLINE]
280
281# gather all characters together, specified by a list of catcodes
282def code2string(cc, codelist):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000283 ##print 'code2string: codelist = ' + pcl(codelist),
284 result = ''
285 for category in codelist:
286 if cc[category]:
287 result = result + cc[category]
288 ##print 'result = ' + `result`
289 return result
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000290
291# automatically generate all characters of catcode other, being the
292# complement set in the ASCII range (128 characters)
293def make_other_codes(cc):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000294 otherchars = range(256) # could be made 256, no problem
295 for category in all_but_other_codes:
296 if cc[category]:
297 for c in cc[category]:
298 otherchars[ord(c)] = None
299 result = ''
300 for i in otherchars:
301 if i != None:
302 result = result + chr(i)
303 return result
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000304
305# catcode dump (which characters have which catcodes).
306def dump_cc(name, cc):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000307 ##print '\t' + name
308 ##print '=' * (8+len(name))
309 if len(cc) != 16:
310 raise TypeError, 'cc not good cat class'
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000311## for i in range(16):
312## print pc(i) + '\t' + `cc[i]`
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000313
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000314
315# In the beginning,....
316epoch_cc = [None] * 16
317##dump_cc('epoch_cc', epoch_cc)
318
319
320# INITEX
321initex_cc = epoch_cc[:]
322initex_cc[CC_ESCAPE] = '\\'
323initex_cc[CC_ENDLINE], initex_cc[CC_IGNORE], initex_cc[CC_WHITE] = \
324 '\n', '\0', ' '
325initex_cc[CC_LETTER] = string.uppercase + string.lowercase
326initex_cc[CC_COMMENT], initex_cc[CC_INVALID] = '%', '\x7F'
327#initex_cc[CC_OTHER] = make_other_codes(initex_cc) I don't need them, anyway
328##dump_cc('initex_cc', initex_cc)
329
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000330
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000331# LPLAIN: LaTeX catcode setting (see lplain.tex)
332lplain_cc = initex_cc[:]
333lplain_cc[CC_LBRACE], lplain_cc[CC_RBRACE] = '{', '}'
334lplain_cc[CC_MATHSHIFT] = '$'
335lplain_cc[CC_ALIGNMENT] = '&'
336lplain_cc[CC_PARAMETER] = '#'
337lplain_cc[CC_SUPERSCRIPT] = '^\x0B' # '^' and C-k
338lplain_cc[CC_SUBSCRIPT] = '_\x01' # '_' and C-a
339lplain_cc[CC_WHITE] = lplain_cc[CC_WHITE] + '\t'
340lplain_cc[CC_ACTIVE] = '~\x0C' # '~' and C-l
341lplain_cc[CC_OTHER] = make_other_codes(lplain_cc)
342##dump_cc('lplain_cc', lplain_cc)
343
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000344
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000345# Guido's LaTeX environment catcoded '_' as ``other''
346# my own purpose catlist
347my_cc = lplain_cc[:]
348my_cc[CC_SUBSCRIPT] = my_cc[CC_SUBSCRIPT][1:] # remove '_' here
349my_cc[CC_OTHER] = my_cc[CC_OTHER] + '_' # add it to OTHER list
350dump_cc('my_cc', my_cc)
351
352
353
354# needed for un_re, my equivalent for regexp-quote in Emacs
355re_meaning = '\\[]^$'
356
357def un_re(str):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000358 result = ''
359 for i in str:
360 if i in re_meaning:
361 result = result + '\\'
362 result = result + i
363 return result
364
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000365# NOTE the negate ('^') operator in *some* of the regexps below
366def make_rc_regular(cc):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000367 # problems here if '[]' are included!!
368 return regex.compile('[' + code2string(cc, regular_stopcodes) + ']')
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000369
370def make_rc_cs_scan(cc):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000371 return regex.compile('[^' + code2string(cc, csname_scancodes) + ']')
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000372
373def make_rc_comment(cc):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000374 return regex.compile('[' + code2string(cc, comment_stopcodes) + ']')
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000375
376def make_rc_endwhite(cc):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000377 return regex.compile('[^' + code2string(cc, white_scancodes) + ']')
378
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000379
380
381# regular: normal mode:
382rc_regular = make_rc_regular(my_cc)
383
384# scan: scan a command sequence e.g. `newlength' or `mbox' or `;', `,' or `$'
385rc_cs_scan = make_rc_cs_scan(my_cc)
386rc_comment = make_rc_comment(my_cc)
387rc_endwhite = make_rc_endwhite(my_cc)
388
389
Guido van Rossum36f219d1996-09-11 21:30:40 +0000390# parseit (BUF, PARSEMODE=mode[MODE_REGULAR], START=0, RECURSION-LEVEL=0)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000391# RECURSION-LEVEL will is incremented on entry.
392# result contains the list of chunks returned
393# together with this list, the buffer position is returned
394
395# RECURSION-LEVEL will be set to zero *again*, when recursively a
396# {,D}MATH-mode scan has been enetered.
397# This has been done in order to better check for environment-mismatches
398
Guido van Rossum36f219d1996-09-11 21:30:40 +0000399def parseit(buf, parsemode=mode[MODE_REGULAR], start=0, lvl=0):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000400 global lineno
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000401
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000402 result = []
403 end = len(buf)
Guido van Rossum36f219d1996-09-11 21:30:40 +0000404 if lvl == 0 and parsemode == mode[MODE_REGULAR]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000405 lineno = 1
406 lvl = lvl + 1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000407
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000408 ##print 'parseit(' + epsilon(buf, start) + ', ' + `parsemode` + ', ' + `start` + ', ' + `lvl` + ')'
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000409
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000410 #
411 # some of the more regular modes...
412 #
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000413
Guido van Rossum36f219d1996-09-11 21:30:40 +0000414 if parsemode in (mode[MODE_REGULAR], mode[MODE_DMATH], mode[MODE_MATH]):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000415 cstate = []
416 newpos = start
417 curpmode = parsemode
418 while 1:
419 where = newpos
420 #print '\tnew round: ' + epsilon(buf, where)
421 if where == end:
Guido van Rossum36f219d1996-09-11 21:30:40 +0000422 if lvl > 1 or curpmode != mode[MODE_REGULAR]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000423 # not the way we started...
424 raise EOFError, 'premature end of file.' + lle(lvl, buf, where)
425 # the real ending of lvl-1 parse
426 return end, result
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000427
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000428 pos = rc_regular.search(buf, where)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000429
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000430 if pos < 0:
431 pos = end
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000432
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000433 if pos != where:
434 newpos, c = pos, chunk(PLAIN, where, (where, pos))
435 result.append(c)
436 continue
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000437
438
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000439 #
440 # ok, pos == where and pos != end
441 #
442 foundchar = buf[where]
443 if foundchar in my_cc[CC_LBRACE]:
444 # recursive subgroup parse...
445 newpos, data = parseit(buf, curpmode, where+1, lvl)
446 result.append(chunk(GROUP, where, data))
447
448 elif foundchar in my_cc[CC_RBRACE]:
449 if lvl <= 1:
450 raise error, 'ENDGROUP while in base level.' + lle(lvl, buf, where)
Guido van Rossum36f219d1996-09-11 21:30:40 +0000451 if lvl == 1 and mode != mode[MODE_REGULAR]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000452 raise error, 'endgroup while in math mode. +lin() + epsilon(buf, where)'
453 return where + 1, result
454
455 elif foundchar in my_cc[CC_ESCAPE]:
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000456 #
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000457 # call the routine that actually deals with
458 # this problem. If do_ret is None, than
459 # return the value of do_ret
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000460 #
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000461 # Note that handle_cs might call this routine
462 # recursively again...
463 #
464 do_ret, newpos = handlecs(buf, where,
465 curpmode, lvl, result, end)
466 if do_ret != None:
467 return do_ret
468
469 elif foundchar in my_cc[CC_COMMENT]:
470 newpos, data = parseit(buf,
Guido van Rossum36f219d1996-09-11 21:30:40 +0000471 mode[MODE_COMMENT], where+1, lvl)
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000472 result.append(chunk(COMMENT, where, data))
473
474 elif foundchar in my_cc[CC_MATHSHIFT]:
475 # note that recursive calls to math-mode
476 # scanning are called with recursion-level 0
477 # again, in order to check for bad mathend
478 #
479 if where + 1 != end and buf[where + 1] in my_cc[CC_MATHSHIFT]:
480 #
481 # double mathshift, e.g. '$$'
482 #
Guido van Rossum36f219d1996-09-11 21:30:40 +0000483 if curpmode == mode[MODE_REGULAR]:
484 newpos, data = parseit(buf, mode[MODE_DMATH],
485 where + 2, 0)
486 result.append(chunk(DMATH, where, data))
487 elif curpmode == mode[MODE_MATH]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000488 raise error, 'wrong math delimiiter' + lin() + epsilon(buf, where)
489 elif lvl != 1:
490 raise error, 'bad mathend.' + lle(lvl, buf, where)
491 else:
492 return where + 2, result
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000493 else:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000494 #
495 # single math shift, e.g. '$'
496 #
Guido van Rossum36f219d1996-09-11 21:30:40 +0000497 if curpmode == mode[MODE_REGULAR]:
498 newpos, data = parseit(buf, mode[MODE_MATH],
499 where + 1, 0)
500 result.append(chunk(MATH, where, data))
501 elif curpmode == mode[MODE_DMATH]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000502 raise error, 'wrong math delimiiter' + lin() + epsilon(buf, where)
503 elif lvl != 1:
504 raise error, 'bad mathend.' + lv(lvl, buf, where)
505 else:
506 return where + 1, result
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000507
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000508 elif foundchar in my_cc[CC_IGNORE]:
509 print 'warning: ignored char', `foundchar`
510 newpos = where + 1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000511
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000512 elif foundchar in my_cc[CC_ACTIVE]:
513 result.append(chunk(ACTIVE, where, foundchar))
514 newpos = where + 1
515
516 elif foundchar in my_cc[CC_INVALID]:
517 raise error, 'invalid char ' + `foundchar`
518 newpos = where + 1
519
520 elif foundchar in my_cc[CC_ENDLINE]:
521 #
522 # after an end of line, eat the rest of
523 # whitespace on the beginning of the next line
524 # this is what LaTeX more or less does
525 #
526 # also, try to indicate double newlines (\par)
527 #
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000528 lineno = lineno + 1
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000529 savedwhere = where
Guido van Rossum36f219d1996-09-11 21:30:40 +0000530 newpos, dummy = parseit(buf, mode[MODE_GOBBLEWHITE], where + 1, lvl)
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000531 if newpos != end and buf[newpos] in my_cc[CC_ENDLINE]:
532 result.append(chunk(DENDLINE, savedwhere, foundchar))
533 else:
534 result.append(chunk(ENDLINE, savedwhere, foundchar))
535 else:
536 result.append(chunk(OTHER, where, foundchar))
537 newpos = where + 1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000538
Guido van Rossum36f219d1996-09-11 21:30:40 +0000539 elif parsemode == mode[MODE_CS_SCAN]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000540 #
541 # scan for a control sequence token. `\ape', `\nut' or `\%'
542 #
543 if start == end:
544 raise EOFError, 'can\'t find end of csname'
545 pos = rc_cs_scan.search(buf, start)
546 if pos < 0:
547 pos = end
548 if pos == start:
549 # first non-letter right where we started the search
550 # ---> the control sequence name consists of one single
551 # character. Also: don't eat white space...
552 if buf[pos] in my_cc[CC_ENDLINE]:
553 lineno = lineno + 1
554 pos = pos + 1
555 return pos, (start, pos)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000556 else:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000557 spos = pos
558 if buf[pos] == '\n':
559 lineno = lineno + 1
560 spos = pos + 1
Guido van Rossum36f219d1996-09-11 21:30:40 +0000561 pos2, dummy = parseit(buf, mode[MODE_GOBBLEWHITE], spos, lvl)
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000562 return pos2, (start, pos)
563
Guido van Rossum36f219d1996-09-11 21:30:40 +0000564 elif parsemode == mode[MODE_GOBBLEWHITE]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000565 if start == end:
566 return start, ''
567 pos = rc_endwhite.search(buf, start)
568 if pos < 0:
569 pos = start
570 return pos, (start, pos)
571
Guido van Rossum36f219d1996-09-11 21:30:40 +0000572 elif parsemode == mode[MODE_COMMENT]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000573 pos = rc_comment.search(buf, start)
574 lineno = lineno + 1
575 if pos < 0:
576 print 'no newline perhaps?'
577 raise EOFError, 'can\'t find end of comment'
578 pos = pos + 1
Guido van Rossum36f219d1996-09-11 21:30:40 +0000579 pos2, dummy = parseit(buf, mode[MODE_GOBBLEWHITE], pos, lvl)
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000580 return pos2, (start, pos)
581
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000582 else:
583 raise error, 'Unknown mode (' + `parsemode` + ')'
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000584
585
586#moreresult = cswitch(buf[x1:x2], buf, newpos, parsemode, lvl)
587
588#boxcommands = 'mbox', 'fbox'
589#defcommands = 'def', 'newcommand'
590
591endverbstr = '\\end{verbatim}'
592
593re_endverb = regex.compile(un_re(endverbstr))
594
595#
596# handlecs: helper function for parseit, for the special thing we might
597# wanna do after certain command control sequences
598# returns: None or return_data, newpos
599#
600# in the latter case, the calling function is instructed to immediately
601# return with the data in return_data
602#
603def handlecs(buf, where, curpmode, lvl, result, end):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000604 global lineno
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000605
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000606 # get the control sequence name...
Guido van Rossum36f219d1996-09-11 21:30:40 +0000607 newpos, data = parseit(buf, mode[MODE_CS_SCAN], where+1, lvl)
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000608 saveddata = data
Guido van Rossum36f219d1996-09-11 21:30:40 +0000609 s_buf_data = s(buf, data)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000610
Guido van Rossum36f219d1996-09-11 21:30:40 +0000611 if s_buf_data in ('begin', 'end'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000612 # skip the expected '{' and get the LaTeX-envname '}'
Guido van Rossum36f219d1996-09-11 21:30:40 +0000613 newpos, data = parseit(buf, mode[MODE_REGULAR], newpos+1, lvl)
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000614 if len(data) != 1:
Guido van Rossum36f219d1996-09-11 21:30:40 +0000615 raise error, 'expected 1 chunk of data.' + lle(lvl, buf, where)
Guido van Rossum49604d31996-09-10 22:19:51 +0000616
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000617 # yucky, we've got an environment
618 envname = s(buf, data[0].data)
Guido van Rossum36f219d1996-09-11 21:30:40 +0000619 s_buf_saveddata = s(buf, saveddata)
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000620 ##print 'FOUND ' + s(buf, saveddata) + '. Name ' + `envname` + '.' + lv(lvl)
Guido van Rossum36f219d1996-09-11 21:30:40 +0000621 if s_buf_saveddata == 'begin' and envname == 'verbatim':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000622 # verbatim deserves special treatment
623 pos = re_endverb.search(buf, newpos)
624 if pos < 0:
Guido van Rossum36f219d1996-09-11 21:30:40 +0000625 raise error, "%s not found.%s" \
626 % (`endverbstr`, lle(lvl, buf, where))
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000627 result.append(chunk(ENV, where, (envname, [chunk(PLAIN, newpos, (newpos, pos))])))
628 newpos = pos + len(endverbstr)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000629
Guido van Rossum36f219d1996-09-11 21:30:40 +0000630 elif s_buf_saveddata == 'begin':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000631 # start parsing recursively... If that parse returns
632 # from an '\end{...}', then should the last item of
633 # the returned data be a string containing the ended
634 # environment
635 newpos, data = parseit(buf, curpmode, newpos, lvl)
636 if not data or type(data[-1]) is not StringType:
Guido van Rossum36f219d1996-09-11 21:30:40 +0000637 raise error, "missing 'end'" + lle(lvl, buf, where) \
638 + epsilon(buf, newpos)
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000639 retenv = data[-1]
640 del data[-1]
641 if retenv != envname:
642 #[`retenv`, `envname`]
Guido van Rossum36f219d1996-09-11 21:30:40 +0000643 raise error, 'environments do not match.%s%s' \
644 % (lle(lvl, buf, where), epsilon(buf, newpos))
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000645 result.append(chunk(ENV, where, (retenv, data)))
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000646 else:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000647 # 'end'... append the environment name, as just
648 # pointed out, and order parsit to return...
649 result.append(envname)
650 ##print 'POINT of return: ' + epsilon(buf, newpos)
651 # the tuple will be returned by parseit
652 return (newpos, result), newpos
653
654 # end of \begin ... \end handling
655
Guido van Rossum36f219d1996-09-11 21:30:40 +0000656 elif s_buf_data[0:2] == 'if':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000657 # another scary monster: the 'if' directive
Guido van Rossum36f219d1996-09-11 21:30:40 +0000658 flag = s_buf_data[2:]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000659
660 # recursively call parseit, just like environment above..
661 # the last item of data should contain the if-termination
662 # e.g., 'else' of 'fi'
663 newpos, data = parseit(buf, curpmode, newpos, lvl)
664 if not data or data[-1] not in ('else', 'fi'):
665 raise error, 'wrong if... termination' + \
666 lle(lvl, buf, where) + epsilon(buf, newpos)
667
668 ifterm = data[-1]
669 del data[-1]
670 # 0 means dont_negate flag
671 result.append(chunk(IF, where, (flag, 0, data)))
672 if ifterm == 'else':
673 # do the whole thing again, there is only one way
674 # to end this one, by 'fi'
675 newpos, data = parseit(buf, curpmode, newpos, lvl)
676 if not data or data[-1] not in ('fi', ):
677 raise error, 'wrong if...else... termination' \
Guido van Rossum36f219d1996-09-11 21:30:40 +0000678 + lle(lvl, buf, where) \
679 + epsilon(buf, newpos)
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000680
681 ifterm = data[-1]
682 del data[-1]
683 result.append(chunk(IF, where, (flag, 1, data)))
684 #done implicitely: return None, newpos
685
Guido van Rossum36f219d1996-09-11 21:30:40 +0000686 elif s_buf_data in ('else', 'fi'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000687 result.append(s(buf, data))
688 # order calling party to return tuple
689 return (newpos, result), newpos
690
691 # end of \if, \else, ... \fi handling
692
693 elif s(buf, saveddata) == 'verb':
694 x2 = saveddata[1]
695 result.append(chunk(CSNAME, where, data))
696 if x2 == end:
697 raise error, 'premature end of command.' + lle(lvl, buf, where)
698 delimchar = buf[x2]
699 ##print 'VERB: delimchar ' + `delimchar`
700 pos = regex.compile(un_re(delimchar)).search(buf, x2 + 1)
701 if pos < 0:
702 raise error, 'end of \'verb\' argument (' + \
Guido van Rossum36f219d1996-09-11 21:30:40 +0000703 `delimchar` + ') not found.' + \
704 lle(lvl, buf, where)
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000705 result.append(chunk(GROUP, x2, [chunk(PLAIN, x2+1, (x2+1, pos))]))
706 newpos = pos + 1
707 else:
708 result.append(chunk(CSNAME, where, data))
709 return None, newpos
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000710
711# this is just a function to get the string value if the possible data-tuple
712def s(buf, data):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000713 if type(data) is StringType:
714 return data
715 if len(data) != 2 or not (type(data[0]) is type(data[1]) is IntType):
716 raise TypeError, 'expected tuple of 2 integers'
717 x1, x2 = data
718 return buf[x1:x2]
Guido van Rossum49604d31996-09-10 22:19:51 +0000719
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000720
721##length, data1, i = getnextarg(length, buf, pp, i + 1)
722
723# make a deep-copy of some chunks
724def crcopy(r):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000725 return map(chunkcopy, r)
Guido van Rossum49604d31996-09-10 22:19:51 +0000726
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000727
728# copy a chunk, would better be a method of class Chunk...
729def chunkcopy(ch):
Guido van Rossum36f219d1996-09-11 21:30:40 +0000730 if ch.chtype == chunk_type[GROUP]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000731 return chunk(GROUP, ch.where, map(chunkcopy, ch.data))
732 else:
733 return chunk(ch.chtype, ch.where, ch.data)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000734
735
736# get next argument for TeX-macro, flatten a group (insert between)
737# or return Command Sequence token, or give back one character
738def getnextarg(length, buf, pp, item):
739
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000740 ##wobj = Wobj()
741 ##dumpit(buf, wobj.write, pp[item:min(length, item + 5)])
742 ##print 'GETNEXTARG, (len, item) =', `length, item` + ' ---> ' + wobj.data + ' <---'
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000743
Guido van Rossum36f219d1996-09-11 21:30:40 +0000744 while item < length and pp[item].chtype == chunk_type[ENDLINE]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000745 del pp[item]
746 length = length - 1
747 if item >= length:
748 raise error, 'no next arg.' + epsilon(buf, pp[-1].where)
Guido van Rossum36f219d1996-09-11 21:30:40 +0000749 if pp[item].chtype == chunk_type[GROUP]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000750 newpp = pp[item].data
751 del pp[item]
752 length = length - 1
753 changeit(buf, newpp)
754 length = length + len(newpp)
755 pp[item:item] = newpp
756 item = item + len(newpp)
757 if len(newpp) < 10:
758 wobj = Wobj()
759 dumpit(buf, wobj.write, newpp)
760 ##print 'GETNEXTARG: inserted ' + `wobj.data`
761 return length, item
Guido van Rossum36f219d1996-09-11 21:30:40 +0000762 elif pp[item].chtype == chunk_type[PLAIN]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000763 #grab one char
764 print 'WARNING: grabbing one char'
765 if len(s(buf, pp[item].data)) > 1:
766 pp.insert(item, chunk(PLAIN, pp[item].where, s(buf, pp[item].data)[:1]))
767 item, length = item+1, length+1
768 pp[item].data = s(buf, pp[item].data)[1:]
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000769 else:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000770 item = item+1
771 return length, item
772 else:
773 ch = pp[item]
774 try:
775 str = `s(buf, ch.data)`
776 except TypeError:
777 str = `ch.data`
778 if len(str) > 400:
779 str = str[:400] + '...'
780 print 'GETNEXTARG:', ch.chtype, 'not handled, data ' + str
781 return length, item
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000782
783
784# this one is needed to find the end of LaTeX's optional argument, like
785# item[...]
786re_endopt = regex.compile(']')
787
788# get a LaTeX-optional argument, you know, the square braces '[' and ']'
789def getoptarg(length, buf, pp, item):
790
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000791 wobj = Wobj()
792 dumpit(buf, wobj.write, pp[item:min(length, item + 5)])
793 ##print 'GETOPTARG, (len, item) =', `length, item` + ' ---> ' + wobj.data + ' <---'
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000794
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000795 if item >= length or \
Guido van Rossum36f219d1996-09-11 21:30:40 +0000796 pp[item].chtype != chunk_type[PLAIN] or \
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000797 s(buf, pp[item].data)[0] != '[':
798 return length, item
799
800 pp[item].data = s(buf, pp[item].data)[1:]
801 if len(pp[item].data) == 0:
802 del pp[item]
803 length = length-1
804
805 while 1:
806 if item == length:
807 raise error, 'No end of optional arg found'
Guido van Rossum36f219d1996-09-11 21:30:40 +0000808 if pp[item].chtype == chunk_type[PLAIN]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000809 text = s(buf, pp[item].data)
810 pos = re_endopt.search(text)
811 if pos >= 0:
812 pp[item].data = text[:pos]
813 if pos == 0:
814 del pp[item]
815 length = length-1
816 else:
817 item=item+1
818 text = text[pos+1:]
819
820 while text and text[0] in ' \t':
821 text = text[1:]
822
823 if text:
824 pp.insert(item, chunk(PLAIN, 0, text))
825 length = length + 1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000826 return length, item
827
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000828 item = item+1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000829
830
831# Wobj just add write-requests to the ``data'' attribute
832class Wobj:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000833 data = ''
Guido van Rossum49604d31996-09-10 22:19:51 +0000834
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000835 def write(self, data):
836 self.data = self.data + data
Guido van Rossumb819bdf1995-03-15 11:26:26 +0000837
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000838# ignore these commands
Fred Drake43c93501997-12-29 21:40:35 +0000839ignoredcommands = ('bcode', 'ecode', 'hline', 'small', '/')
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000840# map commands like these to themselves as plaintext
Fred Drakee8b46131998-02-17 05:54:46 +0000841wordsselves = ('UNIX', 'ABC', 'C', 'ASCII', 'EOF', 'LaTeX', 'POSIX')
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000842# \{ --> {, \} --> }, etc
Guido van Rossum36f219d1996-09-11 21:30:40 +0000843themselves = ('{', '}', ',', '.', '@', ' ', '\n') + wordsselves
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000844# these ones also themselves (see argargs macro in myformat.sty)
845inargsselves = (',', '[', ']', '(', ')')
846# this is how *I* would show the difference between emph and strong
847# code 1 means: fold to uppercase
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000848markcmds = {'code': ('', ''), 'var': 1, 'emph': ('_', '_'),
Fred Drakee8b46131998-02-17 05:54:46 +0000849 'strong': ('*', '*')}
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000850
851# recognise patter {\FONTCHANGE-CMD TEXT} to \MAPPED-FC-CMD{TEXT}
852fontchanges = {'rm': 'r', 'it': 'i', 'em': 'emph', 'bf': 'b', 'tt': 't'}
853
854# transparent for these commands
Guido van Rossum7760cde1995-03-17 16:03:11 +0000855for_texi = ('emph', 'var', 'strong', 'code', 'kbd', 'key', 'dfn', 'samp',
856 'file', 'r', 'i', 't')
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000857
858
859# try to remove macros and return flat text
860def flattext(buf, pp):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000861 pp = crcopy(pp)
862 ##print '---> FLATTEXT ' + `pp`
863 wobj = Wobj()
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000864
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000865 i, length = 0, len(pp)
866 while 1:
867 if len(pp) != length:
868 raise 'FATAL', 'inconsistent length'
869 if i >= length:
870 break
871 ch = pp[i]
872 i = i+1
Guido van Rossum36f219d1996-09-11 21:30:40 +0000873 if ch.chtype == chunk_type[PLAIN]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000874 pass
Guido van Rossum36f219d1996-09-11 21:30:40 +0000875 elif ch.chtype == chunk_type[CSNAME]:
876 s_buf_data = s(buf, ch.data)
877 if s_buf_data in themselves or hist.inargs and s_buf_data in inargsselves:
878 ch.chtype = chunk_type[PLAIN]
879 elif s_buf_data == 'e':
880 ch.chtype = chunk_type[PLAIN]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000881 ch.data = '\\'
Guido van Rossum36f219d1996-09-11 21:30:40 +0000882 elif len(s_buf_data) == 1 \
883 and s_buf_data in onlylatexspecial:
884 ch.chtype = chunk_type[PLAIN]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000885 # if it is followed by an empty group,
886 # remove that group, it was needed for
887 # a true space
888 if i < length \
Guido van Rossum36f219d1996-09-11 21:30:40 +0000889 and pp[i].chtype==chunk_type[GROUP] \
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000890 and len(pp[i].data) == 0:
891 del pp[i]
892 length = length-1
893
Guido van Rossum36f219d1996-09-11 21:30:40 +0000894 elif s_buf_data in markcmds.keys():
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000895 length, newi = getnextarg(length, buf, pp, i)
896 str = flattext(buf, pp[i:newi])
897 del pp[i:newi]
898 length = length - (newi - i)
Guido van Rossum36f219d1996-09-11 21:30:40 +0000899 ch.chtype = chunk_type[PLAIN]
900 markcmd = s_buf_data
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000901 x = markcmds[markcmd]
902 if type(x) == TupleType:
903 pre, after = x
904 str = pre+str+after
905 elif x == 1:
906 str = string.upper(str)
907 else:
908 raise 'FATAL', 'corrupt markcmds'
909 ch.data = str
910 else:
Guido van Rossum36f219d1996-09-11 21:30:40 +0000911 if s_buf_data not in ignoredcommands:
912 print 'WARNING: deleting command ' + s_buf_data
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000913 print 'PP' + `pp[i-1]`
914 del pp[i-1]
915 i, length = i-1, length-1
Guido van Rossum36f219d1996-09-11 21:30:40 +0000916 elif ch.chtype == chunk_type[GROUP]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000917 length, newi = getnextarg(length, buf, pp, i-1)
918 i = i-1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000919## str = flattext(buf, crcopy(pp[i-1:newi]))
920## del pp[i:newi]
921## length = length - (newi - i)
Guido van Rossum36f219d1996-09-11 21:30:40 +0000922## ch.chtype = chunk_type[PLAIN]
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000923## ch.data = str
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000924 else:
925 pass
926
927 dumpit(buf, wobj.write, pp)
928 ##print 'FLATTEXT: RETURNING ' + `wobj.data`
929 return wobj.data
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000930
931# try to generate node names (a bit shorter than the chapter title)
932# note that the \nodename command (see elsewhere) overules these efforts
933def invent_node_names(text):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000934 words = string.split(text)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000935
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000936 ##print 'WORDS ' + `words`
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000937
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000938 if len(words) == 2 \
Guido van Rossum36f219d1996-09-11 21:30:40 +0000939 and string.lower(words[0]) == 'built-in' \
940 and string.lower(words[1]) not in ('modules', 'functions'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000941 return words[1]
942 if len(words) == 3 and string.lower(words[1]) == 'module':
943 return words[2]
944 if len(words) == 3 and string.lower(words[1]) == 'object':
945 return string.join(words[0:2])
Guido van Rossum36f219d1996-09-11 21:30:40 +0000946 if len(words) > 4 \
947 and (string.lower(string.join(words[-4:])) \
948 == 'methods and data attributes'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000949 return string.join(words[:2])
950 return text
951
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000952re_commas_etc = regex.compile('[,`\'@{}]')
953
954re_whitespace = regex.compile('[ \t]*')
955
956
957##nodenamecmd = next_command_p(length, buf, pp, newi, 'nodename')
958
959# look if the next non-white stuff is also a command, resulting in skipping
960# double endlines (DENDLINE) too, and thus omitting \par's
961# Sometimes this is too much, maybe consider DENDLINE's as stop
962def next_command_p(length, buf, pp, i, cmdname):
963
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000964 while 1:
965 if i >= len(pp):
966 break
967 ch = pp[i]
968 i = i+1
Guido van Rossum36f219d1996-09-11 21:30:40 +0000969 if ch.chtype == chunk_type[ENDLINE]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000970 continue
Guido van Rossum36f219d1996-09-11 21:30:40 +0000971 if ch.chtype == chunk_type[DENDLINE]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000972 continue
Guido van Rossum36f219d1996-09-11 21:30:40 +0000973 if ch.chtype == chunk_type[PLAIN]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000974 if re_whitespace.search(s(buf, ch.data)) == 0 and \
975 re_whitespace.match(s(buf, ch.data)) == len(s(buf, ch.data)):
976 continue
977 return -1
Guido van Rossum36f219d1996-09-11 21:30:40 +0000978 if ch.chtype == chunk_type[CSNAME]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000979 if s(buf, ch.data) == cmdname:
980 return i # _after_ the command
981 return -1
982 return -1
983
984
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000985# things that are special to LaTeX, but not to texi..
986onlylatexspecial = '_~^$#&%'
987
Guido van Rossum23301a91993-05-24 14:19:37 +0000988class Struct: pass
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000989
990hist = Struct()
991out = Struct()
992
993def startchange():
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000994 global hist, out
Guido van Rossum95cd2ef1992-12-08 14:37:55 +0000995
Guido van Rossum5f18d6c1996-09-10 22:34:20 +0000996 hist.inenv = []
997 hist.nodenames = []
998 hist.cindex = []
999 hist.inargs = 0
1000 hist.enumeratenesting, hist.itemizenesting = 0, 0
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001001
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001002 out.doublenodes = []
1003 out.doublecindeces = []
1004
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001005
1006spacech = [chunk(PLAIN, 0, ' ')]
1007commach = [chunk(PLAIN, 0, ', ')]
1008cindexch = [chunk(CSLINE, 0, 'cindex')]
1009
1010# the standard variation in symbols for itemize
1011itemizesymbols = ['bullet', 'minus', 'dots']
1012
1013# same for enumerate
1014enumeratesymbols = ['1', 'A', 'a']
1015
1016##
1017## \begin{ {func,data,exc}desc }{name}...
1018## the resulting texi-code is dependent on the contents of indexsubitem
1019##
1020
1021# indexsubitem: `['XXX', 'function']
1022# funcdesc:
1023# deffn {`idxsi`} NAME (FUNCARGS)
1024
1025# indexsubitem: `['XXX', 'method']`
1026# funcdesc:
1027# defmethod {`idxsi[0]`} NAME (FUNCARGS)
1028
1029# indexsubitem: `['in', 'module', 'MODNAME']'
1030# datadesc:
1031# defcv data {`idxsi[1:]`} NAME
1032# excdesc:
1033# defcv exception {`idxsi[1:]`} NAME
1034# funcdesc:
1035# deffn {function of `idxsi[1:]`} NAME (FUNCARGS)
1036
1037# indexsubitem: `['OBJECT', 'attribute']'
1038# datadesc
1039# defcv attribute {`OBJECT`} NAME
1040
1041
1042## this routine will be called on \begin{funcdesc}{NAME}{ARGS}
1043## or \funcline{NAME}{ARGS}
1044##
Fred Drakee8b46131998-02-17 05:54:46 +00001045def do_funcdesc(length, buf, pp, i, index=1):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001046 startpoint = i-1
1047 ch = pp[startpoint]
1048 wh = ch.where
1049 length, newi = getnextarg(length, buf, pp, i)
1050 funcname = chunk(GROUP, wh, pp[i:newi])
1051 del pp[i:newi]
1052 length = length - (newi-i)
1053 save = hist.inargs
1054 hist.inargs = 1
1055 length, newi = getnextarg(length, buf, pp, i)
1056 hist.inargs = save
1057 del save
1058 the_args = [chunk(PLAIN, wh, '()'[0])] + pp[i:newi] + \
Fred Drake7edd8d31996-10-09 16:11:26 +00001059 [chunk(PLAIN, wh, '()'[1])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001060 del pp[i:newi]
1061 length = length - (newi-i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001062
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001063 idxsi = hist.indexsubitem # words
1064 command = ''
1065 cat_class = ''
Fred Drakeacc87541996-10-14 16:20:42 +00001066 if idxsi and idxsi[-1] in ('method', 'protocol', 'attribute'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001067 command = 'defmethod'
1068 cat_class = string.join(idxsi[:-1])
1069 elif len(idxsi) == 2 and idxsi[1] == 'function':
1070 command = 'deffn'
1071 cat_class = string.join(idxsi)
1072 elif len(idxsi) == 3 and idxsi[:2] == ['in', 'module']:
1073 command = 'deffn'
1074 cat_class = 'function of ' + string.join(idxsi[1:])
Fred Drakea4541af1997-12-29 17:19:22 +00001075 elif len(idxsi) > 3 and idxsi[:2] == ['in', 'modules']:
1076 command = 'deffn'
1077 cat_class = 'function of ' + string.join(idxsi[1:])
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001078
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001079 if not command:
1080 raise error, 'don\'t know what to do with indexsubitem ' + `idxsi`
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001081
Guido van Rossum36f219d1996-09-11 21:30:40 +00001082 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001083 ch.data = command
1084
1085 cslinearg = [chunk(GROUP, wh, [chunk(PLAIN, wh, cat_class)])]
1086 cslinearg.append(chunk(PLAIN, wh, ' '))
1087 cslinearg.append(funcname)
1088 cslinearg.append(chunk(PLAIN, wh, ' '))
1089 l = len(cslinearg)
1090 cslinearg[l:l] = the_args
1091
1092 pp.insert(i, chunk(GROUP, wh, cslinearg))
1093 i, length = i+1, length+1
1094 hist.command = command
1095 return length, i
1096
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001097
1098## this routine will be called on \begin{excdesc}{NAME}
1099## or \excline{NAME}
1100##
1101def do_excdesc(length, buf, pp, i):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001102 startpoint = i-1
1103 ch = pp[startpoint]
1104 wh = ch.where
1105 length, newi = getnextarg(length, buf, pp, i)
1106 excname = chunk(GROUP, wh, pp[i:newi])
1107 del pp[i:newi]
1108 length = length - (newi-i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001109
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001110 idxsi = hist.indexsubitem # words
1111 command = ''
1112 cat_class = ''
1113 class_class = ''
1114 if len(idxsi) == 2 and idxsi[1] == 'exception':
1115 command = 'defvr'
1116 cat_class = string.join(idxsi)
1117 elif len(idxsi) == 3 and idxsi[:2] == ['in', 'module']:
1118 command = 'defcv'
1119 cat_class = 'exception'
1120 class_class = string.join(idxsi[1:])
1121 elif len(idxsi) == 4 and idxsi[:3] == ['exception', 'in', 'module']:
1122 command = 'defcv'
1123 cat_class = 'exception'
1124 class_class = string.join(idxsi[2:])
Fred Drakea4541af1997-12-29 17:19:22 +00001125 elif idxsi == ['built-in', 'exception', 'base', 'class']:
Fred Drakee8b46131998-02-17 05:54:46 +00001126 command = 'defvr'
1127 cat_class = 'exception base class'
Fred Drakea4541af1997-12-29 17:19:22 +00001128 else:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001129 raise error, 'don\'t know what to do with indexsubitem ' + `idxsi`
1130
Guido van Rossum36f219d1996-09-11 21:30:40 +00001131 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001132 ch.data = command
1133
1134 cslinearg = [chunk(GROUP, wh, [chunk(PLAIN, wh, cat_class)])]
1135 cslinearg.append(chunk(PLAIN, wh, ' '))
1136 if class_class:
1137 cslinearg.append(chunk(GROUP, wh, [chunk(PLAIN, wh, class_class)]))
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001138 cslinearg.append(chunk(PLAIN, wh, ' '))
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001139 cslinearg.append(excname)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001140
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001141 pp.insert(i, chunk(GROUP, wh, cslinearg))
1142 i, length = i+1, length+1
1143 hist.command = command
1144 return length, i
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001145
1146## same for datadesc or dataline...
Fred Drakee8b46131998-02-17 05:54:46 +00001147def do_datadesc(length, buf, pp, i, index=1):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001148 startpoint = i-1
1149 ch = pp[startpoint]
1150 wh = ch.where
1151 length, newi = getnextarg(length, buf, pp, i)
1152 dataname = chunk(GROUP, wh, pp[i:newi])
1153 del pp[i:newi]
1154 length = length - (newi-i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001155
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001156 idxsi = hist.indexsubitem # words
Fred Drakee8b46131998-02-17 05:54:46 +00001157 command = 'defcv'
1158 cat_class = 'data'
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001159 class_class = ''
1160 if idxsi[-1] in ('attribute', 'option'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001161 cat_class = idxsi[-1]
1162 class_class = string.join(idxsi[:-1])
1163 elif len(idxsi) == 3 and idxsi[:2] == ['in', 'module']:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001164 class_class = string.join(idxsi[1:])
1165 elif len(idxsi) == 4 and idxsi[:3] == ['data', 'in', 'module']:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001166 class_class = string.join(idxsi[2:])
Fred Drake11b6d241996-10-10 20:09:56 +00001167 else:
Fred Drake11b6d241996-10-10 20:09:56 +00001168 class_class = string.join(idxsi)
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001169
Guido van Rossum36f219d1996-09-11 21:30:40 +00001170 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001171 ch.data = command
1172
1173 cslinearg = [chunk(GROUP, wh, [chunk(PLAIN, wh, cat_class)])]
1174 cslinearg.append(chunk(PLAIN, wh, ' '))
1175 if class_class:
1176 cslinearg.append(chunk(GROUP, wh, [chunk(PLAIN, wh, class_class)]))
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001177 cslinearg.append(chunk(PLAIN, wh, ' '))
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001178 cslinearg.append(dataname)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001179
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001180 pp.insert(i, chunk(GROUP, wh, cslinearg))
1181 i, length = i+1, length+1
1182 hist.command = command
1183 return length, i
1184
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001185
Fred Drakea4541af1997-12-29 17:19:22 +00001186def do_opcodedesc(length, buf, pp, i):
1187 startpoint = i-1
1188 ch = pp[startpoint]
1189 wh = ch.where
1190 length, newi = getnextarg(length, buf, pp, i)
1191 dataname = chunk(GROUP, wh, pp[i:newi])
1192 del pp[i:newi]
1193 length = length - (newi-i)
1194
Fred Drake43c93501997-12-29 21:40:35 +00001195 ch.chtype = CSLINE
1196 ch.data = "deffn"
Fred Drakea4541af1997-12-29 17:19:22 +00001197
Fred Drakee8b46131998-02-17 05:54:46 +00001198 cslinearg = [chunk(PLAIN, wh, 'byte\ code\ instruction'),
Fred Drakea4541af1997-12-29 17:19:22 +00001199 chunk(GROUP, wh, [chunk(PLAIN, wh, "byte code instruction")]),
1200 chunk(PLAIN, wh, ' '),
1201 dataname,
Fred Drake43c93501997-12-29 21:40:35 +00001202 chunk(PLAIN, wh, ' '),
1203 pp[i],
Fred Drakea4541af1997-12-29 17:19:22 +00001204 ]
1205
Fred Drake43c93501997-12-29 21:40:35 +00001206 pp[i] = chunk(GROUP, wh, cslinearg)
Fred Drakea4541af1997-12-29 17:19:22 +00001207 hist.command = ch.data
1208 return length, i
1209
1210
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001211# regular indices: those that are not set in tt font by default....
1212regindices = ('cindex', )
1213
1214# remove illegal characters from node names
1215def rm_commas_etc(text):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001216 result = ''
1217 changed = 0
1218 while 1:
1219 pos = re_commas_etc.search(text)
1220 if pos >= 0:
1221 changed = 1
1222 result = result + text[:pos]
1223 text = text[pos+1:]
1224 else:
1225 result = result + text
1226 break
1227 if changed:
Fred Drake43c93501997-12-29 21:40:35 +00001228 print 'Warning: nodename changed to ' + `result`
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001229
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001230 return result
1231
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001232# boolean flags
1233flags = {'texi': 1}
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001234
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001235
Fred Drake43c93501997-12-29 21:40:35 +00001236# map of \label{} to node names
1237label_nodes = {}
1238
1239
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001240##
1241## changeit: the actual routine, that changes the contents of the parsed
1242## chunks
1243##
1244
1245def changeit(buf, pp):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001246 global onlylatexspecial, hist, out
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001247
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001248 i, length = 0, len(pp)
1249 while 1:
1250 # sanity check: length should always equal len(pp)
1251 if len(pp) != length:
1252 raise 'FATAL', 'inconsistent length. thought ' + `length` + ', but should really be ' + `len(pp)`
1253 if i >= length:
1254 break
1255 ch = pp[i]
1256 i = i + 1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001257
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001258 if type(ch) is StringType:
1259 #normally, only chunks are present in pp,
1260 # but in some cases, some extra info
1261 # has been inserted, e.g., the \end{...} clauses
1262 raise 'FATAL', 'got string, probably too many ' + `end`
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001263
Guido van Rossum36f219d1996-09-11 21:30:40 +00001264 if ch.chtype == chunk_type[GROUP]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001265 # check for {\em ...} constructs
Fred Drakee8b46131998-02-17 05:54:46 +00001266 data = ch.data
1267 if data and \
1268 data[0].chtype == chunk_type[CSNAME] and \
1269 fontchanges.has_key(s(buf, data[0].data)):
1270 k = s(buf, data[0].data)
1271 del data[0]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001272 pp.insert(i-1, chunk(CSNAME, ch.where, fontchanges[k]))
1273 length, i = length+1, i+1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001274
Fred Drakee8b46131998-02-17 05:54:46 +00001275 elif data:
1276 if len(data) \
1277 and data[0].chtype == chunk_type[GROUP] \
1278 and len(data[0].data) \
1279 and data[0].data[0].chtype == chunk_type[CSNAME] \
1280 and s(buf, data[0].data[0].data) == 'e':
1281 data[0] = data[0].data[0]
1282 print "invoking \\e magic group transform..."
1283 else:
1284## print "GROUP -- ch.data[0].data =", ch.data[0].data
1285 k = s(buf, data[0].data)
1286 if k == "fulllineitems":
1287 del data[0]
1288 pp[i-1:i] = data
1289 i = i - 1
1290 length = length + len(data) - 1
1291 continue
Fred Drake43c93501997-12-29 21:40:35 +00001292
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001293 # recursively parse the contents of the group
Fred Drakee8b46131998-02-17 05:54:46 +00001294 changeit(buf, data)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001295
Guido van Rossum36f219d1996-09-11 21:30:40 +00001296 elif ch.chtype == chunk_type[IF]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001297 # \if...
1298 flag, negate, data = ch.data
1299 ##print 'IF: flag, negate = ' + `flag, negate`
1300 if flag not in flags.keys():
1301 raise error, 'unknown flag ' + `flag`
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001302
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001303 value = flags[flag]
1304 if negate:
1305 value = (not value)
1306 del pp[i-1]
1307 length, i = length-1, i-1
1308 if value:
1309 pp[i:i] = data
1310 length = length + len(data)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001311
1312
Guido van Rossum36f219d1996-09-11 21:30:40 +00001313 elif ch.chtype == chunk_type[ENV]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001314 # \begin{...} ....
1315 envname, data = ch.data
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001316
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001317 #push this environment name on stack
1318 hist.inenv.insert(0, envname)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001319
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001320 #append an endenv chunk after grouped data
1321 data.append(chunk(ENDENV, ch.where, envname))
1322 ##[`data`]
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001323
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001324 #delete this object
1325 del pp[i-1]
1326 i, length = i-1, length-1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001327
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001328 #insert found data
1329 pp[i:i] = data
1330 length = length + len(data)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001331
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001332 if envname == 'verbatim':
1333 pp[i:i] = [chunk(CSLINE, ch.where, 'example'),
1334 chunk(GROUP, ch.where, [])]
1335 length, i = length+2, i+2
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001336
Fred Drakeef058031998-02-19 15:20:30 +00001337 elif envname in ('itemize', 'list', 'fulllineitems'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001338 if hist.itemizenesting > len(itemizesymbols):
1339 raise error, 'too deep itemize nesting'
Fred Drakee8b46131998-02-17 05:54:46 +00001340 if envname == 'list':
1341 del pp[i:i+2]
1342 length = length - 2
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001343 ingroupch = [chunk(CSNAME, ch.where,
Fred Drakee8b46131998-02-17 05:54:46 +00001344 itemizesymbols[hist.itemizenesting])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001345 hist.itemizenesting = hist.itemizenesting + 1
1346 pp[i:i] = [chunk(CSLINE, ch.where, 'itemize'),
Fred Drakee8b46131998-02-17 05:54:46 +00001347 chunk(GROUP, ch.where, ingroupch)]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001348 length, i = length+2, i+2
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001349
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001350 elif envname == 'enumerate':
1351 if hist.enumeratenesting > len(enumeratesymbols):
1352 raise error, 'too deep enumerate nesting'
1353 ingroupch = [chunk(PLAIN, ch.where,
Fred Drakee8b46131998-02-17 05:54:46 +00001354 enumeratesymbols[hist.enumeratenesting])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001355 hist.enumeratenesting = hist.enumeratenesting + 1
1356 pp[i:i] = [chunk(CSLINE, ch.where, 'enumerate'),
Fred Drakee8b46131998-02-17 05:54:46 +00001357 chunk(GROUP, ch.where, ingroupch)]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001358 length, i = length+2, i+2
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001359
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001360 elif envname == 'description':
1361 ingroupch = [chunk(CSNAME, ch.where, 'b')]
1362 pp[i:i] = [chunk(CSLINE, ch.where, 'table'),
1363 chunk(GROUP, ch.where, ingroupch)]
1364 length, i = length+2, i+2
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001365
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001366 elif (envname == 'tableiii') or (envname == 'tableii'):
1367 if (envname == 'tableii'):
1368 ltable = 2
1369 else:
1370 ltable = 3
1371 wh = ch.where
1372 newcode = []
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001373
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001374 #delete tabular format description
1375 # e.g., {|l|c|l|}
1376 length, newi = getnextarg(length, buf, pp, i)
1377 del pp[i:newi]
1378 length = length - (newi-i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001379
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001380 newcode.append(chunk(CSLINE, wh, 'table'))
1381 ingroupch = [chunk(CSNAME, wh, 'asis')]
1382 newcode.append(chunk(GROUP, wh, ingroupch))
1383 newcode.append(chunk(CSLINE, wh, 'item'))
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001384
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001385 #get the name of macro for @item
1386 # e.g., {code}
1387 length, newi = getnextarg(length, buf, pp, i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001388
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001389 if newi-i != 1:
1390 raise error, 'Sorry, expected 1 chunk argument'
Guido van Rossum36f219d1996-09-11 21:30:40 +00001391 if pp[i].chtype != chunk_type[PLAIN]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001392 raise error, 'Sorry, expected plain text argument'
1393 hist.itemargmacro = s(buf, pp[i].data)
1394 del pp[i:newi]
1395 length = length - (newi-i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001396
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001397 itembody = []
1398 for count in range(ltable):
1399 length, newi = getnextarg(length, buf, pp, i)
1400 emphgroup = [
1401 chunk(CSNAME, wh, 'emph'),
1402 chunk(GROUP, 0, pp[i:newi])]
1403 del pp[i:newi]
1404 length = length - (newi-i)
1405 if count == 0:
1406 itemarg = emphgroup
1407 elif count == ltable-1:
1408 itembody = itembody + \
1409 [chunk(PLAIN, wh, ' --- ')] + emphgroup
1410 else:
1411 itembody = emphgroup
1412 newcode.append(chunk(GROUP, wh, itemarg))
1413 newcode = newcode + itembody + [chunk(DENDLINE, wh, '\n')]
1414 pp[i:i] = newcode
1415 l = len(newcode)
1416 length, i = length+l, i+l
1417 del newcode, l
1418
1419 if length != len(pp):
1420 raise 'STILL, SOMETHING wrong', `i`
1421
Fred Drakeef058031998-02-19 15:20:30 +00001422 elif envname in ('funcdesc', 'funcdescni', 'classdesc'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001423 pp.insert(i, chunk(PLAIN, ch.where, ''))
1424 i, length = i+1, length+1
Fred Drakee8b46131998-02-17 05:54:46 +00001425 length, i = do_funcdesc(length, buf, pp, i,
Fred Drakeef058031998-02-19 15:20:30 +00001426 envname[-2:] != "ni")
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001427
1428 elif envname == 'excdesc':
1429 pp.insert(i, chunk(PLAIN, ch.where, ''))
1430 i, length = i+1, length+1
1431 length, i = do_excdesc(length, buf, pp, i)
1432
Fred Drakee8b46131998-02-17 05:54:46 +00001433 elif envname in ('datadesc', 'datadescni'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001434 pp.insert(i, chunk(PLAIN, ch.where, ''))
1435 i, length = i+1, length+1
Fred Drakee8b46131998-02-17 05:54:46 +00001436 length, i = do_datadesc(length, buf, pp, i,
Fred Drakeef058031998-02-19 15:20:30 +00001437 envname[-2:] != "ni")
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001438
Fred Drakea4541af1997-12-29 17:19:22 +00001439 elif envname == 'opcodedesc':
1440 pp.insert(i, chunk(PLAIN, ch.where, ''))
1441 i, length = i+1, length+1
1442 length, i = do_opcodedesc(length, buf, pp, i)
1443
1444 elif envname == 'seealso':
1445 chunks = [chunk(ENDLINE, ch.where, "\n"),
1446 chunk(CSNAME, ch.where, "b"),
1447 chunk(GROUP, ch.where, [
1448 chunk(PLAIN, ch.where, "See also: ")]),
1449 chunk(ENDLINE, ch.where, "\n"),
1450 chunk(ENDLINE, ch.where, "\n")]
1451 pp[i-1:i] = chunks
1452 length = length + len(chunks) - 1
1453 i = i + len(chunks) - 1
1454
Fred Drake43c93501997-12-29 21:40:35 +00001455 elif envname in ('sloppypar', 'flushleft'):
1456 pass
1457
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001458 else:
1459 print 'WARNING: don\'t know what to do with env ' + `envname`
1460
Guido van Rossum36f219d1996-09-11 21:30:40 +00001461 elif ch.chtype == chunk_type[ENDENV]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001462 envname = ch.data
1463 if envname != hist.inenv[0]:
1464 raise error, '\'end\' does not match. Name ' + `envname` + ', expected ' + `hist.inenv[0]`
1465 del hist.inenv[0]
1466 del pp[i-1]
1467 i, length = i-1, length-1
1468
1469 if envname == 'verbatim':
Fred Drake43c93501997-12-29 21:40:35 +00001470 pp[i:i] = [chunk(CSLINE, ch.where, 'end'),
1471 chunk(GROUP, ch.where, [
1472 chunk(PLAIN, ch.where, 'example')])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001473 i, length = i+2, length+2
Fred Drakeef058031998-02-19 15:20:30 +00001474 elif envname in ('itemize', 'list', 'fulllineitems'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001475 hist.itemizenesting = hist.itemizenesting - 1
Fred Drake43c93501997-12-29 21:40:35 +00001476 pp[i:i] = [chunk(CSLINE, ch.where, 'end'),
1477 chunk(GROUP, ch.where, [
1478 chunk(PLAIN, ch.where, 'itemize')])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001479 i, length = i+2, length+2
1480 elif envname == 'enumerate':
1481 hist.enumeratenesting = hist.enumeratenesting-1
Fred Drake43c93501997-12-29 21:40:35 +00001482 pp[i:i] = [chunk(CSLINE, ch.where, 'end'),
1483 chunk(GROUP, ch.where, [
1484 chunk(PLAIN, ch.where, 'enumerate')])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001485 i, length = i+2, length+2
1486 elif envname == 'description':
Fred Drake43c93501997-12-29 21:40:35 +00001487 pp[i:i] = [chunk(CSLINE, ch.where, 'end'),
1488 chunk(GROUP, ch.where, [
1489 chunk(PLAIN, ch.where, 'table')])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001490 i, length = i+2, length+2
1491 elif (envname == 'tableiii') or (envname == 'tableii'):
Fred Drake43c93501997-12-29 21:40:35 +00001492 pp[i:i] = [chunk(CSLINE, ch.where, 'end'),
1493 chunk(GROUP, ch.where, [
1494 chunk(PLAIN, ch.where, 'table')])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001495 i, length = i+2, length + 2
1496 pp.insert(i, chunk(DENDLINE, ch.where, '\n'))
1497 i, length = i+1, length+1
1498
Fred Drakeef058031998-02-19 15:20:30 +00001499 elif envname in ('funcdesc', 'excdesc', 'datadesc', 'classdesc',
Fred Drakee8b46131998-02-17 05:54:46 +00001500 'funcdescni', 'datadescni'):
Fred Drake43c93501997-12-29 21:40:35 +00001501 pp[i:i] = [chunk(CSLINE, ch.where, 'end'),
1502 chunk(GROUP, ch.where, [
1503 chunk(PLAIN, ch.where, hist.command)])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001504 i, length = i+2, length+2
Fred Drakea4541af1997-12-29 17:19:22 +00001505
Fred Drake43c93501997-12-29 21:40:35 +00001506 elif envname == 'opcodedesc':
1507 pp[i:i] = [chunk(CSLINE, ch.where, 'end'),
1508 chunk(GROUP, ch.where, [
1509 chunk(PLAIN, ch.where, "deffn")])]
1510 i, length = i+2, length+2
1511
1512 elif envname in ('seealso', 'sloppypar', 'flushleft'):
Fred Drakea4541af1997-12-29 17:19:22 +00001513 pass
1514
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001515 else:
Fred Drakea4541af1997-12-29 17:19:22 +00001516 print 'WARNING: ending env %s has no actions' % `envname`
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001517
Guido van Rossum36f219d1996-09-11 21:30:40 +00001518 elif ch.chtype == chunk_type[CSNAME]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001519 # control name transformations
Guido van Rossum36f219d1996-09-11 21:30:40 +00001520 s_buf_data = s(buf, ch.data)
1521 if s_buf_data == 'optional':
1522 pp[i-1].chtype = chunk_type[PLAIN]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001523 pp[i-1].data = '['
1524 if (i < length) and \
Guido van Rossum36f219d1996-09-11 21:30:40 +00001525 (pp[i].chtype == chunk_type[GROUP]):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001526 cp=pp[i].data
1527 pp[i:i+1]=cp + [
1528 chunk(PLAIN, ch.where, ']')]
1529 length = length+len(cp)
Guido van Rossum36f219d1996-09-11 21:30:40 +00001530 elif s_buf_data in ignoredcommands:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001531 del pp[i-1]
1532 i, length = i-1, length-1
Guido van Rossum36f219d1996-09-11 21:30:40 +00001533 elif s_buf_data == '@' and \
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001534 i != length and \
Guido van Rossum36f219d1996-09-11 21:30:40 +00001535 pp[i].chtype == chunk_type[PLAIN] and \
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001536 s(buf, pp[i].data)[0] == '.':
1537 # \@. --> \. --> @.
1538 ch.data = '.'
1539 del pp[i]
1540 length = length-1
Guido van Rossum36f219d1996-09-11 21:30:40 +00001541 elif s_buf_data == '\\':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001542 # \\ --> \* --> @*
1543 ch.data = '*'
Guido van Rossum36f219d1996-09-11 21:30:40 +00001544 elif len(s_buf_data) == 1 and \
1545 s_buf_data in onlylatexspecial:
1546 ch.chtype = chunk_type[PLAIN]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001547 # check if such a command is followed by
1548 # an empty group: e.g., `\%{}'. If so, remove
1549 # this empty group too
1550 if i < length and \
Guido van Rossum36f219d1996-09-11 21:30:40 +00001551 pp[i].chtype == chunk_type[GROUP] \
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001552 and len(pp[i].data) == 0:
1553 del pp[i]
1554 length = length-1
1555
Guido van Rossum36f219d1996-09-11 21:30:40 +00001556 elif hist.inargs and s_buf_data in inargsselves:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001557 # This is the special processing of the
1558 # arguments of the \begin{funcdesc}... or
1559 # \funcline... arguments
1560 # \, --> , \[ --> [, \] --> ]
Guido van Rossum36f219d1996-09-11 21:30:40 +00001561 ch.chtype = chunk_type[PLAIN]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001562
Fred Drakee8b46131998-02-17 05:54:46 +00001563 elif s_buf_data == 'setindexsubitem':
1564 stuff = pp[i].data
1565 if len(stuff) != 1:
1566 raise error, "parameter to \\setindexsubitem{} too long"
1567 if pp[i].chtype != chunk_type[GROUP]:
1568 raise error, "bad chunk type following \\setindexsubitem" \
1569 "\nexpected GROUP, got " + str(ch.chtype)
1570 text = s(buf, stuff[0].data)
1571 if text[:1] != '(' or text[-1:] != ')':
1572 raise error, \
1573 'expected indexsubitem enclosed in parenteses'
1574 hist.indexsubitem = string.split(text[1:-1])
1575 del stuff, text
1576 del pp[i-1:i+1]
1577 i = i - 1
1578 length = length - 2
1579
1580 elif s_buf_data == 'newcommand':
1581 print "ignoring definition of \\" + s(buf, pp[i].data[0].data)
1582 del pp[i-1:i+2]
1583 i = i - 1
1584 length = length - 3
1585
1586 elif s_buf_data == 'mbox':
1587 stuff = pp[i].data
1588 pp[i-1:i+1] = stuff
1589 i = i - 1
1590 length = length + len(stuff) - 2
1591
1592 elif s_buf_data == 'version':
1593 ch.chtype = chunk_type[PLAIN]
1594 ch.data = release_version
1595
1596 elif s_buf_data == 'program':
1597 ch.data = "strong"
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001598
Guido van Rossum36f219d1996-09-11 21:30:40 +00001599 elif s_buf_data == 'item':
1600 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001601 length, newi = getoptarg(length, buf, pp, i)
1602 ingroupch = pp[i:newi]
1603 del pp[i:newi]
1604 length = length - (newi-i)
Fred Drake893e5e01996-10-25 22:13:10 +00001605 changeit(buf, ingroupch) # catch stuff inside the optional arg
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001606 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1607 i, length = i+1, length+1
1608
Guido van Rossum36f219d1996-09-11 21:30:40 +00001609 elif s_buf_data == 'ttindex':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001610 idxsi = hist.indexsubitem
1611
1612 cat_class = ''
1613 if len(idxsi) >= 2 and idxsi[1] in \
1614 ('method', 'function', 'protocol'):
1615 command = 'findex'
1616 elif len(idxsi) >= 2 and idxsi[1] in \
1617 ('exception', 'object'):
1618 command = 'vindex'
Fred Drake7edd8d31996-10-09 16:11:26 +00001619 elif len(idxsi) == 3 and idxsi[:2] == ['in', 'module']:
1620 command = 'cindex'
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001621 else:
Fred Drake7edd8d31996-10-09 16:11:26 +00001622 print 'WARNING: can\'t categorize ' + `idxsi` \
1623 + ' for \'ttindex\' command'
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001624 command = 'cindex'
1625
1626 if not cat_class:
1627 cat_class = '('+string.join(idxsi)+')'
1628
Guido van Rossum36f219d1996-09-11 21:30:40 +00001629 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001630 ch.data = command
1631
1632 length, newi = getnextarg(length, buf, pp, i)
1633 arg = pp[i:newi]
1634 del pp[i:newi]
1635 length = length - (newi-i)
1636
1637 cat_arg = [chunk(PLAIN, ch.where, cat_class)]
1638
1639 # determine what should be set in roman, and
1640 # what in tt-font
1641 if command in regindices:
1642
1643 arg = [chunk(CSNAME, ch.where, 't'),
1644 chunk(GROUP, ch.where, arg)]
1645 else:
1646 cat_arg = [chunk(CSNAME, ch.where, 'r'),
1647 chunk(GROUP, ch.where, cat_arg)]
1648
1649 ingroupch = arg + \
1650 [chunk(PLAIN, ch.where, ' ')] + \
1651 cat_arg
1652
1653 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1654 length, i = length+1, i+1
1655
Guido van Rossum36f219d1996-09-11 21:30:40 +00001656 elif s_buf_data == 'ldots':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001657 # \ldots --> \dots{} --> @dots{}
1658 ch.data = 'dots'
1659 if i == length \
Guido van Rossum36f219d1996-09-11 21:30:40 +00001660 or pp[i].chtype != chunk_type[GROUP] \
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001661 or pp[i].data != []:
1662 pp.insert(i, chunk(GROUP, ch.where, []))
1663 i, length = i+1, length+1
Guido van Rossum36f219d1996-09-11 21:30:40 +00001664 elif s_buf_data in themselves:
Fred Drakee8b46131998-02-17 05:54:46 +00001665 # \UNIX --> &UNIX;
Guido van Rossum36f219d1996-09-11 21:30:40 +00001666 ch.chtype = chunk_type[PLAIN]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001667 if i != length \
Guido van Rossum36f219d1996-09-11 21:30:40 +00001668 and pp[i].chtype == chunk_type[GROUP] \
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001669 and pp[i].data == []:
1670 del pp[i]
1671 length = length-1
Guido van Rossum36f219d1996-09-11 21:30:40 +00001672 elif s_buf_data in for_texi:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001673 pass
1674
Fred Drakee8b46131998-02-17 05:54:46 +00001675 elif s_buf_data == 'manpage':
1676 ch.data = 'emph'
1677 sect = s(buf, pp[i+1].data[0].data)
1678 pp[i+1].data = "(%s)" % sect
1679 pp[i+1].chtype = chunk_type[PLAIN]
1680
Guido van Rossum36f219d1996-09-11 21:30:40 +00001681 elif s_buf_data == 'e':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001682 # "\e" --> "\"
1683 ch.data = '\\'
Guido van Rossum36f219d1996-09-11 21:30:40 +00001684 ch.chtype = chunk_type[PLAIN]
1685 elif s_buf_data in ('lineiii', 'lineii'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001686 # This is the most tricky one
1687 # \lineiii{a1}{a2}[{a3}] -->
1688 # @item @<cts. of itemargmacro>{a1}
1689 # a2 [ -- a3]
1690 #
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001691 if not hist.inenv:
1692 raise error, 'no environment for lineiii'
1693 if (hist.inenv[0] != 'tableiii') and \
1694 (hist.inenv[0] != 'tableii'):
1695 raise error, \
Guido van Rossum36f219d1996-09-11 21:30:40 +00001696 'wrong command (%s) in wrong environment (%s)' \
1697 % (s_buf_data, `hist.inenv[0]`)
1698 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001699 ch.data = 'item'
1700 length, newi = getnextarg(length, buf, pp, i)
Guido van Rossum36f219d1996-09-11 21:30:40 +00001701 ingroupch = [chunk(CSNAME, 0, hist.itemargmacro),
1702 chunk(GROUP, 0, pp[i:newi])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001703 del pp[i:newi]
1704 length = length - (newi-i)
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001705 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1706 grouppos = i
1707 i, length = i+1, length+1
1708 length, i = getnextarg(length, buf, pp, i)
1709 length, newi = getnextarg(length, buf, pp, i)
1710 if newi > i:
1711 # we have a 3rd arg
1712 pp.insert(i, chunk(PLAIN, ch.where, ' --- '))
1713 i = newi + 1
1714 length = length + 1
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001715 if length != len(pp):
1716 raise 'IN LINEIII IS THE ERR', `i`
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001717
Guido van Rossum36f219d1996-09-11 21:30:40 +00001718 elif s_buf_data in ('chapter', 'section', 'subsection', 'subsubsection'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001719 #\xxxsection{A} ---->
1720 # @node A, , ,
1721 # @xxxsection A
1722 ## also: remove commas and quotes
Guido van Rossum36f219d1996-09-11 21:30:40 +00001723 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001724 length, newi = getnextarg(length, buf, pp, i)
Fred Drakee8b46131998-02-17 05:54:46 +00001725 afternodenamecmd = next_command_p(length, buf,
1726 pp, newi, 'nodename')
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001727 if afternodenamecmd < 0:
1728 cp1 = crcopy(pp[i:newi])
Guido van Rossum36f219d1996-09-11 21:30:40 +00001729 pp[i:newi] = [chunk(GROUP, ch.where, pp[i:newi])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001730 length, newi = length - (newi-i) + 1, i+1
1731 text = flattext(buf, cp1)
1732 text = invent_node_names(text)
1733 else:
Fred Drakee8b46131998-02-17 05:54:46 +00001734 length, endarg = getnextarg(length, buf,
1735 pp, afternodenamecmd)
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001736 cp1 = crcopy(pp[afternodenamecmd:endarg])
1737 del pp[newi:endarg]
1738 length = length - (endarg-newi)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001739
Guido van Rossum36f219d1996-09-11 21:30:40 +00001740 pp[i:newi] = [chunk(GROUP, ch.where, pp[i:newi])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001741 length, newi = length - (newi-i) + 1, i + 1
1742 text = flattext(buf, cp1)
1743 if text[-1] == '.':
1744 text = text[:-1]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001745 if text in hist.nodenames:
1746 print 'WARNING: node name ' + `text` + ' already used'
1747 out.doublenodes.append(text)
1748 else:
1749 hist.nodenames.append(text)
1750 text = rm_commas_etc(text)
Guido van Rossum36f219d1996-09-11 21:30:40 +00001751 pp[i-1:i-1] = [chunk(CSLINE, ch.where, 'node'),
1752 chunk(GROUP, ch.where, [
1753 chunk(PLAIN, ch.where, text+', , ,')
1754 ])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001755 i, length = newi+2, length+2
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001756
Guido van Rossum36f219d1996-09-11 21:30:40 +00001757 elif s_buf_data == 'funcline':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001758 # fold it to a very short environment
Guido van Rossum36f219d1996-09-11 21:30:40 +00001759 pp[i-1:i-1] = [chunk(CSLINE, ch.where, 'end'),
1760 chunk(GROUP, ch.where, [
1761 chunk(PLAIN, ch.where, hist.command)])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001762 i, length = i+2, length+2
1763 length, i = do_funcdesc(length, buf, pp, i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001764
Guido van Rossum36f219d1996-09-11 21:30:40 +00001765 elif s_buf_data == 'dataline':
1766 pp[i-1:i-1] = [chunk(CSLINE, ch.where, 'end'),
1767 chunk(GROUP, ch.where, [
1768 chunk(PLAIN, ch.where, hist.command)])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001769 i, length = i+2, length+2
1770 length, i = do_datadesc(length, buf, pp, i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001771
Guido van Rossum36f219d1996-09-11 21:30:40 +00001772 elif s_buf_data == 'excline':
1773 pp[i-1:i-1] = [chunk(CSLINE, ch.where, 'end'),
1774 chunk(GROUP, ch.where, [
1775 chunk(PLAIN, ch.where, hist.command)])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001776 i, length = i+2, length+2
1777 length, i = do_excdesc(length, buf, pp, i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001778
Guido van Rossum36f219d1996-09-11 21:30:40 +00001779 elif s_buf_data == 'index':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001780 #\index{A} --->
1781 # @cindex A
Guido van Rossum36f219d1996-09-11 21:30:40 +00001782 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001783 ch.data = 'cindex'
1784 length, newi = getnextarg(length, buf, pp, i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001785
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001786 ingroupch = pp[i:newi]
1787 del pp[i:newi]
1788 length = length - (newi-i)
1789 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1790 length, i = length+1, i+1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001791
Guido van Rossum36f219d1996-09-11 21:30:40 +00001792 elif s_buf_data == 'bifuncindex':
1793 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001794 ch.data = 'findex'
1795 length, newi = getnextarg(length, buf, pp, i)
1796 ingroupch = pp[i:newi]
1797 del pp[i:newi]
1798 length = length - (newi-i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001799
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001800 ingroupch.append(chunk(PLAIN, ch.where, ' '))
1801 ingroupch.append(chunk(CSNAME, ch.where, 'r'))
1802 ingroupch.append(chunk(GROUP, ch.where, [
1803 chunk(PLAIN, ch.where,
1804 '(built-in function)')]))
1805
1806 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1807 length, i = length+1, i+1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001808
Guido van Rossum36f219d1996-09-11 21:30:40 +00001809 elif s_buf_data == 'obindex':
1810 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001811 ch.data = 'findex'
1812 length, newi = getnextarg(length, buf, pp, i)
1813 ingroupch = pp[i:newi]
1814 del pp[i:newi]
1815 length = length - (newi-i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001816
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001817 ingroupch.append(chunk(PLAIN, ch.where, ' '))
1818 ingroupch.append(chunk(CSNAME, ch.where, 'r'))
1819 ingroupch.append(chunk(GROUP, ch.where, [
1820 chunk(PLAIN, ch.where,
1821 '(object)')]))
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001822
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001823 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1824 length, i = length+1, i+1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001825
Guido van Rossum36f219d1996-09-11 21:30:40 +00001826 elif s_buf_data == 'opindex':
1827 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001828 ch.data = 'findex'
1829 length, newi = getnextarg(length, buf, pp, i)
1830 ingroupch = pp[i:newi]
1831 del pp[i:newi]
1832 length = length - (newi-i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001833
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001834 ingroupch.append(chunk(PLAIN, ch.where, ' '))
1835 ingroupch.append(chunk(CSNAME, ch.where, 'r'))
1836 ingroupch.append(chunk(GROUP, ch.where, [
1837 chunk(PLAIN, ch.where,
1838 '(operator)')]))
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001839
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001840 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1841 length, i = length+1, i+1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001842
Fred Drakea4541af1997-12-29 17:19:22 +00001843 elif s_buf_data in ('bimodindex', 'refbimodindex'):
Guido van Rossum36f219d1996-09-11 21:30:40 +00001844 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001845 ch.data = 'pindex'
1846 length, newi = getnextarg(length, buf, pp, i)
1847 ingroupch = pp[i:newi]
1848 del pp[i:newi]
1849 length = length - (newi-i)
1850
1851 ingroupch.append(chunk(PLAIN, ch.where, ' '))
1852 ingroupch.append(chunk(CSNAME, ch.where, 'r'))
1853 ingroupch.append(chunk(GROUP, ch.where, [
1854 chunk(PLAIN, ch.where,
1855 '(built-in)')]))
1856
1857 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1858 length, i = length+1, i+1
1859
Fred Drakea4541af1997-12-29 17:19:22 +00001860 elif s_buf_data == 'refmodindex':
1861 ch.chtype = chunk_type[CSLINE]
1862 ch.data = 'pindex'
1863 length, newi = getnextarg(length, buf, pp, i)
1864 ingroupch = pp[i:newi]
1865 del pp[i:newi]
1866 length = length - (newi-i)
1867
Fred Drakea4541af1997-12-29 17:19:22 +00001868 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1869 length, i = length+1, i+1
1870
Guido van Rossum36f219d1996-09-11 21:30:40 +00001871 elif s_buf_data == 'sectcode':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001872 ch.data = 'code'
1873
Fred Drakea4541af1997-12-29 17:19:22 +00001874 elif s_buf_data in ('stmodindex', 'refstmodindex'):
Guido van Rossum36f219d1996-09-11 21:30:40 +00001875 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001876 # use the program index as module index
1877 ch.data = 'pindex'
1878 length, newi = getnextarg(length, buf, pp, i)
1879 ingroupch = pp[i:newi]
1880 del pp[i:newi]
1881 length = length - (newi-i)
1882
1883 ingroupch.append(chunk(PLAIN, ch.where, ' '))
1884 ingroupch.append(chunk(CSNAME, ch.where, 'r'))
1885 ingroupch.append(chunk(GROUP, ch.where, [
1886 chunk(PLAIN, ch.where,
1887 '(standard)')]))
1888
1889 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1890 length, i = length+1, i+1
1891
Fred Drakea4541af1997-12-29 17:19:22 +00001892 elif s_buf_data in ('stmodindex', 'refstmodindex'):
1893 ch.chtype = chunk_type[CSLINE]
1894 # use the program index as module index
1895 ch.data = 'pindex'
1896 length, newi = getnextarg(length, buf, pp, i)
1897 ingroupch = pp[i:newi]
1898 del pp[i:newi]
1899 length = length - (newi-i)
1900
1901 ingroupch.append(chunk(PLAIN, ch.where, ' '))
1902 ingroupch.append(chunk(CSNAME, ch.where, 'r'))
1903 ingroupch.append(chunk(GROUP, ch.where, [
1904 chunk(PLAIN, ch.where,
1905 '(standard)')]))
1906
1907 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1908 length, i = length+1, i+1
1909
1910 elif s_buf_data in ('stindex', 'kwindex'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001911 # XXX must actually go to newindex st
Fred Drakea4541af1997-12-29 17:19:22 +00001912 what = (s_buf_data[:2] == "st") and "statement" or "keyword"
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001913 wh = ch.where
Guido van Rossum36f219d1996-09-11 21:30:40 +00001914 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001915 ch.data = 'cindex'
1916 length, newi = getnextarg(length, buf, pp, i)
1917 ingroupch = [chunk(CSNAME, wh, 'code'),
Fred Drakee8b46131998-02-17 05:54:46 +00001918 chunk(GROUP, wh, pp[i:newi])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001919
1920 del pp[i:newi]
1921 length = length - (newi-i)
1922
1923 t = ingroupch[:]
Fred Drakea4541af1997-12-29 17:19:22 +00001924 t.append(chunk(PLAIN, wh, ' ' + what))
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001925
1926 pp.insert(i, chunk(GROUP, wh, t))
1927 i, length = i+1, length+1
1928
1929 pp.insert(i, chunk(CSLINE, wh, 'cindex'))
1930 i, length = i+1, length+1
1931
1932 t = ingroupch[:]
Fred Drakea4541af1997-12-29 17:19:22 +00001933 t.insert(0, chunk(PLAIN, wh, what + ', '))
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001934
1935 pp.insert(i, chunk(GROUP, wh, t))
1936 i, length = i+1, length+1
1937
Guido van Rossum36f219d1996-09-11 21:30:40 +00001938 elif s_buf_data == 'indexii':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001939 #\indexii{A}{B} --->
1940 # @cindex A B
1941 # @cindex B, A
1942 length, newi = getnextarg(length, buf, pp, i)
1943 cp11 = pp[i:newi]
1944 cp21 = crcopy(pp[i:newi])
1945 del pp[i:newi]
1946 length = length - (newi-i)
1947 length, newi = getnextarg(length, buf, pp, i)
1948 cp12 = pp[i:newi]
1949 cp22 = crcopy(pp[i:newi])
1950 del pp[i:newi]
1951 length = length - (newi-i)
1952
Guido van Rossum36f219d1996-09-11 21:30:40 +00001953 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001954 ch.data = 'cindex'
1955 pp.insert(i, chunk(GROUP, ch.where, cp11 + [
1956 chunk(PLAIN, ch.where, ' ')] + cp12))
1957 i, length = i+1, length+1
1958 pp[i:i] = [chunk(CSLINE, ch.where, 'cindex'),
1959 chunk(GROUP, ch.where, cp22 + [
1960 chunk(PLAIN, ch.where, ', ')]+ cp21)]
1961 i, length = i+2, length+2
1962
Guido van Rossum36f219d1996-09-11 21:30:40 +00001963 elif s_buf_data == 'indexiii':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001964 length, newi = getnextarg(length, buf, pp, i)
1965 cp11 = pp[i:newi]
1966 cp21 = crcopy(pp[i:newi])
1967 cp31 = crcopy(pp[i:newi])
1968 del pp[i:newi]
1969 length = length - (newi-i)
1970 length, newi = getnextarg(length, buf, pp, i)
1971 cp12 = pp[i:newi]
1972 cp22 = crcopy(pp[i:newi])
1973 cp32 = crcopy(pp[i:newi])
1974 del pp[i:newi]
1975 length = length - (newi-i)
1976 length, newi = getnextarg(length, buf, pp, i)
1977 cp13 = pp[i:newi]
1978 cp23 = crcopy(pp[i:newi])
1979 cp33 = crcopy(pp[i:newi])
1980 del pp[i:newi]
1981 length = length - (newi-i)
1982
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 pp.insert(i, chunk(GROUP, ch.where, cp11 + [
1986 chunk(PLAIN, ch.where, ' ')] + cp12
1987 + [chunk(PLAIN, ch.where, ' ')]
1988 + cp13))
1989 i, length = i+1, length+1
1990 pp[i:i] = [chunk(CSLINE, ch.where, 'cindex'),
1991 chunk(GROUP, ch.where, cp22 + [
1992 chunk(PLAIN, ch.where, ' ')]+ cp23
1993 + [chunk(PLAIN, ch.where, ', ')] +
1994 cp21)]
1995 i, length = i+2, length+2
1996 pp[i:i] = [chunk(CSLINE, ch.where, 'cindex'),
1997 chunk(GROUP, ch.where, cp33 + [
1998 chunk(PLAIN, ch.where, ', ')]+ cp31
1999 + [chunk(PLAIN, ch.where, ' ')] +
2000 cp32)]
2001 i, length = i+2, length+2
2002
Guido van Rossum36f219d1996-09-11 21:30:40 +00002003 elif s_buf_data == 'indexiv':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002004 length, newi = getnextarg(length, buf, pp, i)
2005 cp11 = pp[i:newi]
2006 cp21 = crcopy(pp[i:newi])
2007 cp31 = crcopy(pp[i:newi])
2008 cp41 = crcopy(pp[i:newi])
2009 del pp[i:newi]
2010 length = length - (newi-i)
2011 length, newi = getnextarg(length, buf, pp, i)
2012 cp12 = pp[i:newi]
2013 cp22 = crcopy(pp[i:newi])
2014 cp32 = crcopy(pp[i:newi])
2015 cp42 = crcopy(pp[i:newi])
2016 del pp[i:newi]
2017 length = length - (newi-i)
2018 length, newi = getnextarg(length, buf, pp, i)
2019 cp13 = pp[i:newi]
2020 cp23 = crcopy(pp[i:newi])
2021 cp33 = crcopy(pp[i:newi])
2022 cp43 = crcopy(pp[i:newi])
2023 del pp[i:newi]
2024 length = length - (newi-i)
2025 length, newi = getnextarg(length, buf, pp, i)
2026 cp14 = pp[i:newi]
2027 cp24 = crcopy(pp[i:newi])
2028 cp34 = crcopy(pp[i:newi])
2029 cp44 = crcopy(pp[i:newi])
2030 del pp[i:newi]
2031 length = length - (newi-i)
2032
Guido van Rossum36f219d1996-09-11 21:30:40 +00002033 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002034 ch.data = 'cindex'
2035 ingroupch = cp11 + \
2036 spacech + cp12 + \
2037 spacech + cp13 + \
2038 spacech + cp14
2039 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
2040 i, length = i+1, length+1
2041 ingroupch = cp22 + \
2042 spacech + cp23 + \
2043 spacech + cp24 + \
2044 commach + cp21
2045 pp[i:i] = cindexch + [
2046 chunk(GROUP, ch.where, ingroupch)]
2047 i, length = i+2, length+2
2048 ingroupch = cp33 + \
2049 spacech + cp34 + \
2050 commach + cp31 + \
2051 spacech + cp32
2052 pp[i:i] = cindexch + [
2053 chunk(GROUP, ch.where, ingroupch)]
2054 i, length = i+2, length+2
2055 ingroupch = cp44 + \
2056 commach + cp41 + \
2057 spacech + cp42 + \
2058 spacech + cp43
2059 pp[i:i] = cindexch + [
2060 chunk(GROUP, ch.where, ingroupch)]
2061 i, length = i+2, length+2
2062
Fred Drakea4541af1997-12-29 17:19:22 +00002063 elif s_buf_data == 'seemodule':
2064 ch.data = "code"
Fred Drakee8b46131998-02-17 05:54:46 +00002065 # this is needed for just one of the input files... -sigh-
2066 while pp[i+1].chtype == chunk_type[COMMENT]:
2067 i = i + 1
Fred Drakea4541af1997-12-29 17:19:22 +00002068 data = pp[i+1].data
Fred Drakee8b46131998-02-17 05:54:46 +00002069 oparen = chunk(PLAIN, ch.where, " (")
2070 data.insert(0, oparen)
Fred Drakea4541af1997-12-29 17:19:22 +00002071 data.append(chunk(PLAIN, ch.where, ")"))
2072 pp[i+1:i+2] = data
2073 length = length + len(data) - 1
2074
2075 elif s_buf_data == 'seetext':
2076 data = pp[i].data
2077 data.insert(0, chunk(ENDLINE, ch.where, "\n"))
2078 pp[i-1:i+1] = data
2079 i = i - 1
2080 length = length + len(data) - 2
2081
Fred Drake43c93501997-12-29 21:40:35 +00002082 elif s_buf_data == "quad":
2083 ch.chtype = PLAIN
2084 ch.data = " "
2085
Fred Drakee8b46131998-02-17 05:54:46 +00002086 elif s_buf_data in ('noindent', 'indexsubitem', 'footnote'):
Guido van Rossum36f219d1996-09-11 21:30:40 +00002087 pass
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002088
Fred Drakee8b46131998-02-17 05:54:46 +00002089 elif s_buf_data in ('url', 'module', 'function', 'cfunction',
2090 'keyword', 'method', 'exception', 'constant',
Fred Drakeef058031998-02-19 15:20:30 +00002091 'email', 'class', 'member', 'cdata', 'ctype'):
Fred Drakee8b46131998-02-17 05:54:46 +00002092 ch.data = "code"
2093
Fred Drakea4541af1997-12-29 17:19:22 +00002094 elif s_buf_data == 'label':
Fred Drake43c93501997-12-29 21:40:35 +00002095 name = s(buf, pp[i].data[0].data)
Fred Drakea4541af1997-12-29 17:19:22 +00002096 del pp[i-1:i+1]
2097 length = length - 2
2098 i = i - 1
Fred Drake43c93501997-12-29 21:40:35 +00002099 label_nodes[name] = hist.nodenames[-1]
2100
Fred Drakee8b46131998-02-17 05:54:46 +00002101 elif s_buf_data == 'rfc':
2102 ch.chtype = chunk_type[PLAIN]
2103 ch.data = "RFC " + s(buf, pp[i].data[0].data)
2104 del pp[i]
2105 length = length - 1
2106
2107 elif s_buf_data == 'Large':
2108 del pp[i-1]
2109 i = i - 1
2110 length = length - 1
2111
Fred Drake43c93501997-12-29 21:40:35 +00002112 elif s_buf_data == 'ref':
2113 name = s(buf, pp[i].data[0].data)
2114 if label_nodes.has_key(name):
2115 pp[i].data[0].data = label_nodes[name]
2116 else:
2117 pp[i-1:i+1] = [
2118 chunk(PLAIN, ch.where,
2119 "(unknown node reference: %s)" % name)]
2120 length = length - 1
2121 print "WARNING: unknown node label", `name`
Fred Drakea4541af1997-12-29 17:19:22 +00002122
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002123 else:
Guido van Rossum36f219d1996-09-11 21:30:40 +00002124 print "don't know what to do with keyword " + s_buf_data
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002125
2126
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002127re_atsign = regex.compile('[@{}]')
2128re_newline = regex.compile('\n')
2129
2130def dumpit(buf, wm, pp):
2131
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002132 global out
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002133
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002134 i, length = 0, len(pp)
2135
2136 addspace = 0
2137
2138 while 1:
2139 if len(pp) != length:
2140 raise 'FATAL', 'inconsistent length'
2141 if i == length:
2142 break
2143 ch = pp[i]
2144 i = i + 1
2145
Guido van Rossum36f219d1996-09-11 21:30:40 +00002146 dospace = addspace
2147 addspace = 0
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002148
Guido van Rossum36f219d1996-09-11 21:30:40 +00002149 if ch.chtype == chunk_type[CSNAME]:
2150 s_buf_data = s(buf, ch.data)
Fred Drake4b3f0311996-12-13 22:04:31 +00002151 if s_buf_data == 'e':
2152 wm('\\')
2153 continue
2154 if s_buf_data == '$':
2155 wm('$')
2156 continue
Guido van Rossum36f219d1996-09-11 21:30:40 +00002157 wm('@' + s_buf_data)
2158 if s_buf_data == 'node' and \
2159 pp[i].chtype == chunk_type[PLAIN] and \
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002160 s(buf, pp[i].data) in out.doublenodes:
2161 ##XXX doesnt work yet??
2162 wm(' ZZZ-' + zfill(`i`, 4))
Guido van Rossum36f219d1996-09-11 21:30:40 +00002163 if s_buf_data[0] in string.letters:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002164 addspace = 1
Guido van Rossum36f219d1996-09-11 21:30:40 +00002165 elif ch.chtype == chunk_type[PLAIN]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002166 if dospace and s(buf, ch.data) not in (' ', '\t'):
2167 wm(' ')
2168 text = s(buf, ch.data)
2169 while 1:
2170 pos = re_atsign.search(text)
2171 if pos < 0:
2172 break
2173 wm(text[:pos] + '@' + text[pos])
2174 text = text[pos+1:]
2175 wm(text)
Guido van Rossum36f219d1996-09-11 21:30:40 +00002176 elif ch.chtype == chunk_type[GROUP]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002177 wm('{')
2178 dumpit(buf, wm, ch.data)
2179 wm('}')
Guido van Rossum36f219d1996-09-11 21:30:40 +00002180 elif ch.chtype == chunk_type[DENDLINE]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002181 wm('\n\n')
2182 while i != length and pp[i].chtype in \
Guido van Rossum36f219d1996-09-11 21:30:40 +00002183 (chunk_type[DENDLINE], chunk_type[ENDLINE]):
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002184 i = i + 1
Guido van Rossum36f219d1996-09-11 21:30:40 +00002185 elif ch.chtype == chunk_type[OTHER]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002186 wm(s(buf, ch.data))
Guido van Rossum36f219d1996-09-11 21:30:40 +00002187 elif ch.chtype == chunk_type[ACTIVE]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002188 wm(s(buf, ch.data))
Guido van Rossum36f219d1996-09-11 21:30:40 +00002189 elif ch.chtype == chunk_type[ENDLINE]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002190 wm('\n')
Guido van Rossum36f219d1996-09-11 21:30:40 +00002191 elif ch.chtype == chunk_type[CSLINE]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002192 if i >= 2 and pp[i-2].chtype not in \
Guido van Rossum36f219d1996-09-11 21:30:40 +00002193 (chunk_type[ENDLINE], chunk_type[DENDLINE]) \
2194 and (pp[i-2].chtype != chunk_type[PLAIN]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002195 or s(buf, pp[i-2].data)[-1] != '\n'):
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002196
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002197 wm('\n')
2198 wm('@' + s(buf, ch.data))
2199 if i == length:
2200 raise error, 'CSLINE expected another chunk'
Guido van Rossum36f219d1996-09-11 21:30:40 +00002201 if pp[i].chtype != chunk_type[GROUP]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002202 raise error, 'CSLINE expected GROUP'
2203 if type(pp[i].data) != ListType:
2204 raise error, 'GROUP chould contain []-data'
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002205
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002206 wobj = Wobj()
2207 dumpit(buf, wobj.write, pp[i].data)
2208 i = i + 1
2209 text = wobj.data
2210 del wobj
2211 if text:
2212 wm(' ')
2213 while 1:
2214 pos = re_newline.search(text)
2215 if pos < 0:
2216 break
2217 print 'WARNING: found newline in csline arg'
2218 wm(text[:pos] + ' ')
2219 text = text[pos+1:]
2220 wm(text)
2221 if i >= length or \
Guido van Rossum36f219d1996-09-11 21:30:40 +00002222 pp[i].chtype not in (chunk_type[CSLINE],
2223 chunk_type[ENDLINE], chunk_type[DENDLINE]) \
2224 and (pp[i].chtype != chunk_type[PLAIN]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002225 or s(buf, pp[i].data)[0] != '\n'):
2226 wm('\n')
Guido van Rossum49604d31996-09-10 22:19:51 +00002227
Guido van Rossum36f219d1996-09-11 21:30:40 +00002228 elif ch.chtype == chunk_type[COMMENT]:
2229## print 'COMMENT: previous chunk =', pp[i-2]
2230## if pp[i-2].chtype == chunk_type[PLAIN]:
2231## print 'PLAINTEXT =', `s(buf, pp[i-2].data)`
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002232 if s(buf, ch.data) and \
2233 regex.match('^[ \t]*$', s(buf, ch.data)) < 0:
Guido van Rossum36f219d1996-09-11 21:30:40 +00002234 if i >= 2 \
2235 and pp[i-2].chtype not in (chunk_type[ENDLINE], chunk_type[DENDLINE]) \
2236 and not (pp[i-2].chtype == chunk_type[PLAIN]
2237 and regex.match('\\(.\\|\n\\)*[ \t]*\n$', s(buf, pp[i-2].data)) >= 0):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002238 wm('\n')
2239 wm('@c ' + s(buf, ch.data))
Guido van Rossum36f219d1996-09-11 21:30:40 +00002240 elif ch.chtype == chunk_type[IGNORE]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002241 pass
2242 else:
2243 try:
2244 str = `s(buf, ch.data)`
2245 except TypeError:
2246 str = `ch.data`
2247 if len(str) > 400:
2248 str = str[:400] + '...'
2249 print 'warning:', ch.chtype, 'not handled, data ' + str
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002250
2251
2252
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002253def main():
Fred Drakee8b46131998-02-17 05:54:46 +00002254 global release_version
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002255 outfile = None
2256 headerfile = 'texipre.dat'
2257 trailerfile = 'texipost.dat'
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002258
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002259 try:
Fred Drakee8b46131998-02-17 05:54:46 +00002260 opts, args = getopt.getopt(sys.argv[1:], 'o:h:t:v:')
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002261 except getopt.error:
2262 args = []
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002263
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002264 if not args:
2265 print 'usage: partparse [-o outfile] [-h headerfile]',
2266 print '[-t trailerfile] file ...'
2267 sys.exit(2)
Guido van Rossum7a2dba21993-11-05 14:45:11 +00002268
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002269 for opt, arg in opts:
2270 if opt == '-o': outfile = arg
2271 if opt == '-h': headerfile = arg
2272 if opt == '-t': trailerfile = arg
Fred Drakee8b46131998-02-17 05:54:46 +00002273 if opt == '-v': release_version = arg
Guido van Rossum7a2dba21993-11-05 14:45:11 +00002274
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002275 if not outfile:
2276 root, ext = os.path.splitext(args[0])
2277 outfile = root + '.texi'
Guido van Rossum7a2dba21993-11-05 14:45:11 +00002278
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002279 if outfile in args:
2280 print 'will not overwrite input file', outfile
2281 sys.exit(2)
Guido van Rossum7a2dba21993-11-05 14:45:11 +00002282
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002283 outf = open(outfile, 'w')
2284 outf.write(open(headerfile, 'r').read())
Guido van Rossum7a2dba21993-11-05 14:45:11 +00002285
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002286 for file in args:
2287 if len(args) > 1: print '='*20, file, '='*20
2288 buf = open(file, 'r').read()
2289 w, pp = parseit(buf)
2290 startchange()
2291 changeit(buf, pp)
2292 dumpit(buf, outf.write, pp)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002293
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002294 outf.write(open(trailerfile, 'r').read())
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002295
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002296 outf.close()
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002297
Guido van Rossum49604d31996-09-10 22:19:51 +00002298if __name__ == "__main__":
2299 main()