blob: 8fdd95b1dd263897e0ca78ce5dfc9fca04ebaed8 [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 Drakee8b46131998-02-17 05:54:46 +00001337 elif envname in ('itemize', 'list'):
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
1422
Fred Drakee8b46131998-02-17 05:54:46 +00001423 elif envname in ('funcdesc', 'funcdescni'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001424 pp.insert(i, chunk(PLAIN, ch.where, ''))
1425 i, length = i+1, length+1
Fred Drakee8b46131998-02-17 05:54:46 +00001426 length, i = do_funcdesc(length, buf, pp, i,
1427 envname=="funcdesc")
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001428
1429 elif envname == 'excdesc':
1430 pp.insert(i, chunk(PLAIN, ch.where, ''))
1431 i, length = i+1, length+1
1432 length, i = do_excdesc(length, buf, pp, i)
1433
Fred Drakee8b46131998-02-17 05:54:46 +00001434 elif envname in ('datadesc', 'datadescni'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001435 pp.insert(i, chunk(PLAIN, ch.where, ''))
1436 i, length = i+1, length+1
Fred Drakee8b46131998-02-17 05:54:46 +00001437 length, i = do_datadesc(length, buf, pp, i,
1438 envname=="datadesc")
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001439
Fred Drakea4541af1997-12-29 17:19:22 +00001440 elif envname == 'opcodedesc':
1441 pp.insert(i, chunk(PLAIN, ch.where, ''))
1442 i, length = i+1, length+1
1443 length, i = do_opcodedesc(length, buf, pp, i)
1444
1445 elif envname == 'seealso':
1446 chunks = [chunk(ENDLINE, ch.where, "\n"),
1447 chunk(CSNAME, ch.where, "b"),
1448 chunk(GROUP, ch.where, [
1449 chunk(PLAIN, ch.where, "See also: ")]),
1450 chunk(ENDLINE, ch.where, "\n"),
1451 chunk(ENDLINE, ch.where, "\n")]
1452 pp[i-1:i] = chunks
1453 length = length + len(chunks) - 1
1454 i = i + len(chunks) - 1
1455
Fred Drake43c93501997-12-29 21:40:35 +00001456 elif envname in ('sloppypar', 'flushleft'):
1457 pass
1458
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001459 else:
1460 print 'WARNING: don\'t know what to do with env ' + `envname`
1461
Guido van Rossum36f219d1996-09-11 21:30:40 +00001462 elif ch.chtype == chunk_type[ENDENV]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001463 envname = ch.data
1464 if envname != hist.inenv[0]:
1465 raise error, '\'end\' does not match. Name ' + `envname` + ', expected ' + `hist.inenv[0]`
1466 del hist.inenv[0]
1467 del pp[i-1]
1468 i, length = i-1, length-1
1469
1470 if envname == 'verbatim':
Fred Drake43c93501997-12-29 21:40:35 +00001471 pp[i:i] = [chunk(CSLINE, ch.where, 'end'),
1472 chunk(GROUP, ch.where, [
1473 chunk(PLAIN, ch.where, 'example')])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001474 i, length = i+2, length+2
Fred Drakee8b46131998-02-17 05:54:46 +00001475 elif envname in ('itemize', 'list'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001476 hist.itemizenesting = hist.itemizenesting - 1
Fred Drake43c93501997-12-29 21:40:35 +00001477 pp[i:i] = [chunk(CSLINE, ch.where, 'end'),
1478 chunk(GROUP, ch.where, [
1479 chunk(PLAIN, ch.where, 'itemize')])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001480 i, length = i+2, length+2
1481 elif envname == 'enumerate':
1482 hist.enumeratenesting = hist.enumeratenesting-1
Fred Drake43c93501997-12-29 21:40:35 +00001483 pp[i:i] = [chunk(CSLINE, ch.where, 'end'),
1484 chunk(GROUP, ch.where, [
1485 chunk(PLAIN, ch.where, 'enumerate')])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001486 i, length = i+2, length+2
1487 elif envname == 'description':
Fred Drake43c93501997-12-29 21:40:35 +00001488 pp[i:i] = [chunk(CSLINE, ch.where, 'end'),
1489 chunk(GROUP, ch.where, [
1490 chunk(PLAIN, ch.where, 'table')])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001491 i, length = i+2, length+2
1492 elif (envname == 'tableiii') or (envname == 'tableii'):
Fred Drake43c93501997-12-29 21:40:35 +00001493 pp[i:i] = [chunk(CSLINE, ch.where, 'end'),
1494 chunk(GROUP, ch.where, [
1495 chunk(PLAIN, ch.where, 'table')])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001496 i, length = i+2, length + 2
1497 pp.insert(i, chunk(DENDLINE, ch.where, '\n'))
1498 i, length = i+1, length+1
1499
Fred Drakee8b46131998-02-17 05:54:46 +00001500 elif envname in ('funcdesc', 'excdesc', 'datadesc',
1501 'funcdescni', 'datadescni'):
Fred Drake43c93501997-12-29 21:40:35 +00001502 pp[i:i] = [chunk(CSLINE, ch.where, 'end'),
1503 chunk(GROUP, ch.where, [
1504 chunk(PLAIN, ch.where, hist.command)])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001505 i, length = i+2, length+2
Fred Drakea4541af1997-12-29 17:19:22 +00001506
Fred Drake43c93501997-12-29 21:40:35 +00001507 elif envname == 'opcodedesc':
1508 pp[i:i] = [chunk(CSLINE, ch.where, 'end'),
1509 chunk(GROUP, ch.where, [
1510 chunk(PLAIN, ch.where, "deffn")])]
1511 i, length = i+2, length+2
1512
1513 elif envname in ('seealso', 'sloppypar', 'flushleft'):
Fred Drakea4541af1997-12-29 17:19:22 +00001514 pass
1515
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001516 else:
Fred Drakea4541af1997-12-29 17:19:22 +00001517 print 'WARNING: ending env %s has no actions' % `envname`
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001518
Guido van Rossum36f219d1996-09-11 21:30:40 +00001519 elif ch.chtype == chunk_type[CSNAME]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001520 # control name transformations
Guido van Rossum36f219d1996-09-11 21:30:40 +00001521 s_buf_data = s(buf, ch.data)
1522 if s_buf_data == 'optional':
1523 pp[i-1].chtype = chunk_type[PLAIN]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001524 pp[i-1].data = '['
1525 if (i < length) and \
Guido van Rossum36f219d1996-09-11 21:30:40 +00001526 (pp[i].chtype == chunk_type[GROUP]):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001527 cp=pp[i].data
1528 pp[i:i+1]=cp + [
1529 chunk(PLAIN, ch.where, ']')]
1530 length = length+len(cp)
Guido van Rossum36f219d1996-09-11 21:30:40 +00001531 elif s_buf_data in ignoredcommands:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001532 del pp[i-1]
1533 i, length = i-1, length-1
Guido van Rossum36f219d1996-09-11 21:30:40 +00001534 elif s_buf_data == '@' and \
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001535 i != length and \
Guido van Rossum36f219d1996-09-11 21:30:40 +00001536 pp[i].chtype == chunk_type[PLAIN] and \
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001537 s(buf, pp[i].data)[0] == '.':
1538 # \@. --> \. --> @.
1539 ch.data = '.'
1540 del pp[i]
1541 length = length-1
Guido van Rossum36f219d1996-09-11 21:30:40 +00001542 elif s_buf_data == '\\':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001543 # \\ --> \* --> @*
1544 ch.data = '*'
Guido van Rossum36f219d1996-09-11 21:30:40 +00001545 elif len(s_buf_data) == 1 and \
1546 s_buf_data in onlylatexspecial:
1547 ch.chtype = chunk_type[PLAIN]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001548 # check if such a command is followed by
1549 # an empty group: e.g., `\%{}'. If so, remove
1550 # this empty group too
1551 if i < length and \
Guido van Rossum36f219d1996-09-11 21:30:40 +00001552 pp[i].chtype == chunk_type[GROUP] \
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001553 and len(pp[i].data) == 0:
1554 del pp[i]
1555 length = length-1
1556
Guido van Rossum36f219d1996-09-11 21:30:40 +00001557 elif hist.inargs and s_buf_data in inargsselves:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001558 # This is the special processing of the
1559 # arguments of the \begin{funcdesc}... or
1560 # \funcline... arguments
1561 # \, --> , \[ --> [, \] --> ]
Guido van Rossum36f219d1996-09-11 21:30:40 +00001562 ch.chtype = chunk_type[PLAIN]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001563
Fred Drakee8b46131998-02-17 05:54:46 +00001564 elif s_buf_data == 'setindexsubitem':
1565 stuff = pp[i].data
1566 if len(stuff) != 1:
1567 raise error, "parameter to \\setindexsubitem{} too long"
1568 if pp[i].chtype != chunk_type[GROUP]:
1569 raise error, "bad chunk type following \\setindexsubitem" \
1570 "\nexpected GROUP, got " + str(ch.chtype)
1571 text = s(buf, stuff[0].data)
1572 if text[:1] != '(' or text[-1:] != ')':
1573 raise error, \
1574 'expected indexsubitem enclosed in parenteses'
1575 hist.indexsubitem = string.split(text[1:-1])
1576 del stuff, text
1577 del pp[i-1:i+1]
1578 i = i - 1
1579 length = length - 2
1580
1581 elif s_buf_data == 'newcommand':
1582 print "ignoring definition of \\" + s(buf, pp[i].data[0].data)
1583 del pp[i-1:i+2]
1584 i = i - 1
1585 length = length - 3
1586
1587 elif s_buf_data == 'mbox':
1588 stuff = pp[i].data
1589 pp[i-1:i+1] = stuff
1590 i = i - 1
1591 length = length + len(stuff) - 2
1592
1593 elif s_buf_data == 'version':
1594 ch.chtype = chunk_type[PLAIN]
1595 ch.data = release_version
1596
1597 elif s_buf_data == 'program':
1598 ch.data = "strong"
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001599
Fred Drake43c93501997-12-29 21:40:35 +00001600 elif s_buf_data == "fulllineitems":
1601 del pp[i-1]
1602 i, length = i-1, length-1
1603
Guido van Rossum36f219d1996-09-11 21:30:40 +00001604 elif s_buf_data == 'item':
1605 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001606 length, newi = getoptarg(length, buf, pp, i)
1607 ingroupch = pp[i:newi]
1608 del pp[i:newi]
1609 length = length - (newi-i)
Fred Drake893e5e01996-10-25 22:13:10 +00001610 changeit(buf, ingroupch) # catch stuff inside the optional arg
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001611 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1612 i, length = i+1, length+1
1613
Guido van Rossum36f219d1996-09-11 21:30:40 +00001614 elif s_buf_data == 'ttindex':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001615 idxsi = hist.indexsubitem
1616
1617 cat_class = ''
1618 if len(idxsi) >= 2 and idxsi[1] in \
1619 ('method', 'function', 'protocol'):
1620 command = 'findex'
1621 elif len(idxsi) >= 2 and idxsi[1] in \
1622 ('exception', 'object'):
1623 command = 'vindex'
Fred Drake7edd8d31996-10-09 16:11:26 +00001624 elif len(idxsi) == 3 and idxsi[:2] == ['in', 'module']:
1625 command = 'cindex'
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001626 else:
Fred Drake7edd8d31996-10-09 16:11:26 +00001627 print 'WARNING: can\'t categorize ' + `idxsi` \
1628 + ' for \'ttindex\' command'
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001629 command = 'cindex'
1630
1631 if not cat_class:
1632 cat_class = '('+string.join(idxsi)+')'
1633
Guido van Rossum36f219d1996-09-11 21:30:40 +00001634 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001635 ch.data = command
1636
1637 length, newi = getnextarg(length, buf, pp, i)
1638 arg = pp[i:newi]
1639 del pp[i:newi]
1640 length = length - (newi-i)
1641
1642 cat_arg = [chunk(PLAIN, ch.where, cat_class)]
1643
1644 # determine what should be set in roman, and
1645 # what in tt-font
1646 if command in regindices:
1647
1648 arg = [chunk(CSNAME, ch.where, 't'),
1649 chunk(GROUP, ch.where, arg)]
1650 else:
1651 cat_arg = [chunk(CSNAME, ch.where, 'r'),
1652 chunk(GROUP, ch.where, cat_arg)]
1653
1654 ingroupch = arg + \
1655 [chunk(PLAIN, ch.where, ' ')] + \
1656 cat_arg
1657
1658 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1659 length, i = length+1, i+1
1660
Guido van Rossum36f219d1996-09-11 21:30:40 +00001661 elif s_buf_data == 'ldots':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001662 # \ldots --> \dots{} --> @dots{}
1663 ch.data = 'dots'
1664 if i == length \
Guido van Rossum36f219d1996-09-11 21:30:40 +00001665 or pp[i].chtype != chunk_type[GROUP] \
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001666 or pp[i].data != []:
1667 pp.insert(i, chunk(GROUP, ch.where, []))
1668 i, length = i+1, length+1
Guido van Rossum36f219d1996-09-11 21:30:40 +00001669 elif s_buf_data in themselves:
Fred Drakee8b46131998-02-17 05:54:46 +00001670 # \UNIX --> &UNIX;
Guido van Rossum36f219d1996-09-11 21:30:40 +00001671 ch.chtype = chunk_type[PLAIN]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001672 if i != length \
Guido van Rossum36f219d1996-09-11 21:30:40 +00001673 and pp[i].chtype == chunk_type[GROUP] \
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001674 and pp[i].data == []:
1675 del pp[i]
1676 length = length-1
Guido van Rossum36f219d1996-09-11 21:30:40 +00001677 elif s_buf_data in for_texi:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001678 pass
1679
Fred Drakee8b46131998-02-17 05:54:46 +00001680 elif s_buf_data == 'manpage':
1681 ch.data = 'emph'
1682 sect = s(buf, pp[i+1].data[0].data)
1683 pp[i+1].data = "(%s)" % sect
1684 pp[i+1].chtype = chunk_type[PLAIN]
1685
Guido van Rossum36f219d1996-09-11 21:30:40 +00001686 elif s_buf_data == 'e':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001687 # "\e" --> "\"
1688 ch.data = '\\'
Guido van Rossum36f219d1996-09-11 21:30:40 +00001689 ch.chtype = chunk_type[PLAIN]
1690 elif s_buf_data in ('lineiii', 'lineii'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001691 # This is the most tricky one
1692 # \lineiii{a1}{a2}[{a3}] -->
1693 # @item @<cts. of itemargmacro>{a1}
1694 # a2 [ -- a3]
1695 #
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001696 if not hist.inenv:
1697 raise error, 'no environment for lineiii'
1698 if (hist.inenv[0] != 'tableiii') and \
1699 (hist.inenv[0] != 'tableii'):
1700 raise error, \
Guido van Rossum36f219d1996-09-11 21:30:40 +00001701 'wrong command (%s) in wrong environment (%s)' \
1702 % (s_buf_data, `hist.inenv[0]`)
1703 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001704 ch.data = 'item'
1705 length, newi = getnextarg(length, buf, pp, i)
Guido van Rossum36f219d1996-09-11 21:30:40 +00001706 ingroupch = [chunk(CSNAME, 0, hist.itemargmacro),
1707 chunk(GROUP, 0, pp[i:newi])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001708 del pp[i:newi]
1709 length = length - (newi-i)
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001710 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1711 grouppos = i
1712 i, length = i+1, length+1
1713 length, i = getnextarg(length, buf, pp, i)
1714 length, newi = getnextarg(length, buf, pp, i)
1715 if newi > i:
1716 # we have a 3rd arg
1717 pp.insert(i, chunk(PLAIN, ch.where, ' --- '))
1718 i = newi + 1
1719 length = length + 1
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001720 if length != len(pp):
1721 raise 'IN LINEIII IS THE ERR', `i`
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001722
Guido van Rossum36f219d1996-09-11 21:30:40 +00001723 elif s_buf_data in ('chapter', 'section', 'subsection', 'subsubsection'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001724 #\xxxsection{A} ---->
1725 # @node A, , ,
1726 # @xxxsection A
1727 ## also: remove commas and quotes
Guido van Rossum36f219d1996-09-11 21:30:40 +00001728 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001729 length, newi = getnextarg(length, buf, pp, i)
Fred Drakee8b46131998-02-17 05:54:46 +00001730 afternodenamecmd = next_command_p(length, buf,
1731 pp, newi, 'nodename')
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001732 if afternodenamecmd < 0:
1733 cp1 = crcopy(pp[i:newi])
Guido van Rossum36f219d1996-09-11 21:30:40 +00001734 pp[i:newi] = [chunk(GROUP, ch.where, pp[i:newi])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001735 length, newi = length - (newi-i) + 1, i+1
1736 text = flattext(buf, cp1)
1737 text = invent_node_names(text)
1738 else:
Fred Drakee8b46131998-02-17 05:54:46 +00001739 length, endarg = getnextarg(length, buf,
1740 pp, afternodenamecmd)
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001741 cp1 = crcopy(pp[afternodenamecmd:endarg])
1742 del pp[newi:endarg]
1743 length = length - (endarg-newi)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001744
Guido van Rossum36f219d1996-09-11 21:30:40 +00001745 pp[i:newi] = [chunk(GROUP, ch.where, pp[i:newi])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001746 length, newi = length - (newi-i) + 1, i + 1
1747 text = flattext(buf, cp1)
1748 if text[-1] == '.':
1749 text = text[:-1]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001750 if text in hist.nodenames:
1751 print 'WARNING: node name ' + `text` + ' already used'
1752 out.doublenodes.append(text)
1753 else:
1754 hist.nodenames.append(text)
1755 text = rm_commas_etc(text)
Guido van Rossum36f219d1996-09-11 21:30:40 +00001756 pp[i-1:i-1] = [chunk(CSLINE, ch.where, 'node'),
1757 chunk(GROUP, ch.where, [
1758 chunk(PLAIN, ch.where, text+', , ,')
1759 ])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001760 i, length = newi+2, length+2
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001761
Guido van Rossum36f219d1996-09-11 21:30:40 +00001762 elif s_buf_data == 'funcline':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001763 # fold it to a very short environment
Guido van Rossum36f219d1996-09-11 21:30:40 +00001764 pp[i-1:i-1] = [chunk(CSLINE, ch.where, 'end'),
1765 chunk(GROUP, ch.where, [
1766 chunk(PLAIN, ch.where, hist.command)])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001767 i, length = i+2, length+2
1768 length, i = do_funcdesc(length, buf, pp, i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001769
Guido van Rossum36f219d1996-09-11 21:30:40 +00001770 elif s_buf_data == 'dataline':
1771 pp[i-1:i-1] = [chunk(CSLINE, ch.where, 'end'),
1772 chunk(GROUP, ch.where, [
1773 chunk(PLAIN, ch.where, hist.command)])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001774 i, length = i+2, length+2
1775 length, i = do_datadesc(length, buf, pp, i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001776
Guido van Rossum36f219d1996-09-11 21:30:40 +00001777 elif s_buf_data == 'excline':
1778 pp[i-1:i-1] = [chunk(CSLINE, ch.where, 'end'),
1779 chunk(GROUP, ch.where, [
1780 chunk(PLAIN, ch.where, hist.command)])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001781 i, length = i+2, length+2
1782 length, i = do_excdesc(length, buf, pp, i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001783
Guido van Rossum36f219d1996-09-11 21:30:40 +00001784 elif s_buf_data == 'index':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001785 #\index{A} --->
1786 # @cindex A
Guido van Rossum36f219d1996-09-11 21:30:40 +00001787 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001788 ch.data = 'cindex'
1789 length, newi = getnextarg(length, buf, pp, i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001790
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001791 ingroupch = pp[i:newi]
1792 del pp[i:newi]
1793 length = length - (newi-i)
1794 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1795 length, i = length+1, i+1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001796
Guido van Rossum36f219d1996-09-11 21:30:40 +00001797 elif s_buf_data == 'bifuncindex':
1798 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001799 ch.data = 'findex'
1800 length, newi = getnextarg(length, buf, pp, i)
1801 ingroupch = pp[i:newi]
1802 del pp[i:newi]
1803 length = length - (newi-i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001804
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001805 ingroupch.append(chunk(PLAIN, ch.where, ' '))
1806 ingroupch.append(chunk(CSNAME, ch.where, 'r'))
1807 ingroupch.append(chunk(GROUP, ch.where, [
1808 chunk(PLAIN, ch.where,
1809 '(built-in function)')]))
1810
1811 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1812 length, i = length+1, i+1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001813
Guido van Rossum36f219d1996-09-11 21:30:40 +00001814 elif s_buf_data == 'obindex':
1815 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001816 ch.data = 'findex'
1817 length, newi = getnextarg(length, buf, pp, i)
1818 ingroupch = pp[i:newi]
1819 del pp[i:newi]
1820 length = length - (newi-i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001821
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001822 ingroupch.append(chunk(PLAIN, ch.where, ' '))
1823 ingroupch.append(chunk(CSNAME, ch.where, 'r'))
1824 ingroupch.append(chunk(GROUP, ch.where, [
1825 chunk(PLAIN, ch.where,
1826 '(object)')]))
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001827
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001828 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1829 length, i = length+1, i+1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001830
Guido van Rossum36f219d1996-09-11 21:30:40 +00001831 elif s_buf_data == 'opindex':
1832 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001833 ch.data = 'findex'
1834 length, newi = getnextarg(length, buf, pp, i)
1835 ingroupch = pp[i:newi]
1836 del pp[i:newi]
1837 length = length - (newi-i)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001838
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001839 ingroupch.append(chunk(PLAIN, ch.where, ' '))
1840 ingroupch.append(chunk(CSNAME, ch.where, 'r'))
1841 ingroupch.append(chunk(GROUP, ch.where, [
1842 chunk(PLAIN, ch.where,
1843 '(operator)')]))
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001844
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001845 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1846 length, i = length+1, i+1
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00001847
Fred Drakea4541af1997-12-29 17:19:22 +00001848 elif s_buf_data in ('bimodindex', 'refbimodindex'):
Guido van Rossum36f219d1996-09-11 21:30:40 +00001849 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001850 ch.data = 'pindex'
1851 length, newi = getnextarg(length, buf, pp, i)
1852 ingroupch = pp[i:newi]
1853 del pp[i:newi]
1854 length = length - (newi-i)
1855
1856 ingroupch.append(chunk(PLAIN, ch.where, ' '))
1857 ingroupch.append(chunk(CSNAME, ch.where, 'r'))
1858 ingroupch.append(chunk(GROUP, ch.where, [
1859 chunk(PLAIN, ch.where,
1860 '(built-in)')]))
1861
1862 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1863 length, i = length+1, i+1
1864
Fred Drakea4541af1997-12-29 17:19:22 +00001865 elif s_buf_data == 'refmodindex':
1866 ch.chtype = chunk_type[CSLINE]
1867 ch.data = 'pindex'
1868 length, newi = getnextarg(length, buf, pp, i)
1869 ingroupch = pp[i:newi]
1870 del pp[i:newi]
1871 length = length - (newi-i)
1872
Fred Drakea4541af1997-12-29 17:19:22 +00001873 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1874 length, i = length+1, i+1
1875
Guido van Rossum36f219d1996-09-11 21:30:40 +00001876 elif s_buf_data == 'sectcode':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001877 ch.data = 'code'
1878
Fred Drakea4541af1997-12-29 17:19:22 +00001879 elif s_buf_data in ('stmodindex', 'refstmodindex'):
Guido van Rossum36f219d1996-09-11 21:30:40 +00001880 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001881 # use the program index as module index
1882 ch.data = 'pindex'
1883 length, newi = getnextarg(length, buf, pp, i)
1884 ingroupch = pp[i:newi]
1885 del pp[i:newi]
1886 length = length - (newi-i)
1887
1888 ingroupch.append(chunk(PLAIN, ch.where, ' '))
1889 ingroupch.append(chunk(CSNAME, ch.where, 'r'))
1890 ingroupch.append(chunk(GROUP, ch.where, [
1891 chunk(PLAIN, ch.where,
1892 '(standard)')]))
1893
1894 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1895 length, i = length+1, i+1
1896
Fred Drakea4541af1997-12-29 17:19:22 +00001897 elif s_buf_data in ('stmodindex', 'refstmodindex'):
1898 ch.chtype = chunk_type[CSLINE]
1899 # use the program index as module index
1900 ch.data = 'pindex'
1901 length, newi = getnextarg(length, buf, pp, i)
1902 ingroupch = pp[i:newi]
1903 del pp[i:newi]
1904 length = length - (newi-i)
1905
1906 ingroupch.append(chunk(PLAIN, ch.where, ' '))
1907 ingroupch.append(chunk(CSNAME, ch.where, 'r'))
1908 ingroupch.append(chunk(GROUP, ch.where, [
1909 chunk(PLAIN, ch.where,
1910 '(standard)')]))
1911
1912 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
1913 length, i = length+1, i+1
1914
1915 elif s_buf_data in ('stindex', 'kwindex'):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001916 # XXX must actually go to newindex st
Fred Drakea4541af1997-12-29 17:19:22 +00001917 what = (s_buf_data[:2] == "st") and "statement" or "keyword"
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001918 wh = ch.where
Guido van Rossum36f219d1996-09-11 21:30:40 +00001919 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001920 ch.data = 'cindex'
1921 length, newi = getnextarg(length, buf, pp, i)
1922 ingroupch = [chunk(CSNAME, wh, 'code'),
Fred Drakee8b46131998-02-17 05:54:46 +00001923 chunk(GROUP, wh, pp[i:newi])]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001924
1925 del pp[i:newi]
1926 length = length - (newi-i)
1927
1928 t = ingroupch[:]
Fred Drakea4541af1997-12-29 17:19:22 +00001929 t.append(chunk(PLAIN, wh, ' ' + what))
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001930
1931 pp.insert(i, chunk(GROUP, wh, t))
1932 i, length = i+1, length+1
1933
1934 pp.insert(i, chunk(CSLINE, wh, 'cindex'))
1935 i, length = i+1, length+1
1936
1937 t = ingroupch[:]
Fred Drakea4541af1997-12-29 17:19:22 +00001938 t.insert(0, chunk(PLAIN, wh, what + ', '))
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001939
1940 pp.insert(i, chunk(GROUP, wh, t))
1941 i, length = i+1, length+1
1942
Guido van Rossum36f219d1996-09-11 21:30:40 +00001943 elif s_buf_data == 'indexii':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001944 #\indexii{A}{B} --->
1945 # @cindex A B
1946 # @cindex B, A
1947 length, newi = getnextarg(length, buf, pp, i)
1948 cp11 = pp[i:newi]
1949 cp21 = crcopy(pp[i:newi])
1950 del pp[i:newi]
1951 length = length - (newi-i)
1952 length, newi = getnextarg(length, buf, pp, i)
1953 cp12 = pp[i:newi]
1954 cp22 = crcopy(pp[i:newi])
1955 del pp[i:newi]
1956 length = length - (newi-i)
1957
Guido van Rossum36f219d1996-09-11 21:30:40 +00001958 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001959 ch.data = 'cindex'
1960 pp.insert(i, chunk(GROUP, ch.where, cp11 + [
1961 chunk(PLAIN, ch.where, ' ')] + cp12))
1962 i, length = i+1, length+1
1963 pp[i:i] = [chunk(CSLINE, ch.where, 'cindex'),
1964 chunk(GROUP, ch.where, cp22 + [
1965 chunk(PLAIN, ch.where, ', ')]+ cp21)]
1966 i, length = i+2, length+2
1967
Guido van Rossum36f219d1996-09-11 21:30:40 +00001968 elif s_buf_data == 'indexiii':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001969 length, newi = getnextarg(length, buf, pp, i)
1970 cp11 = pp[i:newi]
1971 cp21 = crcopy(pp[i:newi])
1972 cp31 = crcopy(pp[i:newi])
1973 del pp[i:newi]
1974 length = length - (newi-i)
1975 length, newi = getnextarg(length, buf, pp, i)
1976 cp12 = pp[i:newi]
1977 cp22 = crcopy(pp[i:newi])
1978 cp32 = crcopy(pp[i:newi])
1979 del pp[i:newi]
1980 length = length - (newi-i)
1981 length, newi = getnextarg(length, buf, pp, i)
1982 cp13 = pp[i:newi]
1983 cp23 = crcopy(pp[i:newi])
1984 cp33 = crcopy(pp[i:newi])
1985 del pp[i:newi]
1986 length = length - (newi-i)
1987
Guido van Rossum36f219d1996-09-11 21:30:40 +00001988 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00001989 ch.data = 'cindex'
1990 pp.insert(i, chunk(GROUP, ch.where, cp11 + [
1991 chunk(PLAIN, ch.where, ' ')] + cp12
1992 + [chunk(PLAIN, ch.where, ' ')]
1993 + cp13))
1994 i, length = i+1, length+1
1995 pp[i:i] = [chunk(CSLINE, ch.where, 'cindex'),
1996 chunk(GROUP, ch.where, cp22 + [
1997 chunk(PLAIN, ch.where, ' ')]+ cp23
1998 + [chunk(PLAIN, ch.where, ', ')] +
1999 cp21)]
2000 i, length = i+2, length+2
2001 pp[i:i] = [chunk(CSLINE, ch.where, 'cindex'),
2002 chunk(GROUP, ch.where, cp33 + [
2003 chunk(PLAIN, ch.where, ', ')]+ cp31
2004 + [chunk(PLAIN, ch.where, ' ')] +
2005 cp32)]
2006 i, length = i+2, length+2
2007
Guido van Rossum36f219d1996-09-11 21:30:40 +00002008 elif s_buf_data == 'indexiv':
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002009 length, newi = getnextarg(length, buf, pp, i)
2010 cp11 = pp[i:newi]
2011 cp21 = crcopy(pp[i:newi])
2012 cp31 = crcopy(pp[i:newi])
2013 cp41 = crcopy(pp[i:newi])
2014 del pp[i:newi]
2015 length = length - (newi-i)
2016 length, newi = getnextarg(length, buf, pp, i)
2017 cp12 = pp[i:newi]
2018 cp22 = crcopy(pp[i:newi])
2019 cp32 = crcopy(pp[i:newi])
2020 cp42 = crcopy(pp[i:newi])
2021 del pp[i:newi]
2022 length = length - (newi-i)
2023 length, newi = getnextarg(length, buf, pp, i)
2024 cp13 = pp[i:newi]
2025 cp23 = crcopy(pp[i:newi])
2026 cp33 = crcopy(pp[i:newi])
2027 cp43 = crcopy(pp[i:newi])
2028 del pp[i:newi]
2029 length = length - (newi-i)
2030 length, newi = getnextarg(length, buf, pp, i)
2031 cp14 = pp[i:newi]
2032 cp24 = crcopy(pp[i:newi])
2033 cp34 = crcopy(pp[i:newi])
2034 cp44 = crcopy(pp[i:newi])
2035 del pp[i:newi]
2036 length = length - (newi-i)
2037
Guido van Rossum36f219d1996-09-11 21:30:40 +00002038 ch.chtype = chunk_type[CSLINE]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002039 ch.data = 'cindex'
2040 ingroupch = cp11 + \
2041 spacech + cp12 + \
2042 spacech + cp13 + \
2043 spacech + cp14
2044 pp.insert(i, chunk(GROUP, ch.where, ingroupch))
2045 i, length = i+1, length+1
2046 ingroupch = cp22 + \
2047 spacech + cp23 + \
2048 spacech + cp24 + \
2049 commach + cp21
2050 pp[i:i] = cindexch + [
2051 chunk(GROUP, ch.where, ingroupch)]
2052 i, length = i+2, length+2
2053 ingroupch = cp33 + \
2054 spacech + cp34 + \
2055 commach + cp31 + \
2056 spacech + cp32
2057 pp[i:i] = cindexch + [
2058 chunk(GROUP, ch.where, ingroupch)]
2059 i, length = i+2, length+2
2060 ingroupch = cp44 + \
2061 commach + cp41 + \
2062 spacech + cp42 + \
2063 spacech + cp43
2064 pp[i:i] = cindexch + [
2065 chunk(GROUP, ch.where, ingroupch)]
2066 i, length = i+2, length+2
2067
Fred Drakea4541af1997-12-29 17:19:22 +00002068 elif s_buf_data == 'seemodule':
2069 ch.data = "code"
Fred Drakee8b46131998-02-17 05:54:46 +00002070 # this is needed for just one of the input files... -sigh-
2071 while pp[i+1].chtype == chunk_type[COMMENT]:
2072 i = i + 1
Fred Drakea4541af1997-12-29 17:19:22 +00002073 data = pp[i+1].data
Fred Drakee8b46131998-02-17 05:54:46 +00002074 oparen = chunk(PLAIN, ch.where, " (")
2075 data.insert(0, oparen)
Fred Drakea4541af1997-12-29 17:19:22 +00002076 data.append(chunk(PLAIN, ch.where, ")"))
2077 pp[i+1:i+2] = data
2078 length = length + len(data) - 1
2079
2080 elif s_buf_data == 'seetext':
2081 data = pp[i].data
2082 data.insert(0, chunk(ENDLINE, ch.where, "\n"))
2083 pp[i-1:i+1] = data
2084 i = i - 1
2085 length = length + len(data) - 2
2086
Fred Drake43c93501997-12-29 21:40:35 +00002087 elif s_buf_data == "quad":
2088 ch.chtype = PLAIN
2089 ch.data = " "
2090
Fred Drakee8b46131998-02-17 05:54:46 +00002091 elif s_buf_data in ('noindent', 'indexsubitem', 'footnote'):
Guido van Rossum36f219d1996-09-11 21:30:40 +00002092 pass
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002093
Fred Drakee8b46131998-02-17 05:54:46 +00002094 elif s_buf_data in ('url', 'module', 'function', 'cfunction',
2095 'keyword', 'method', 'exception', 'constant',
2096 'email', 'class'):
2097 ch.data = "code"
2098
Fred Drakea4541af1997-12-29 17:19:22 +00002099 elif s_buf_data == 'label':
Fred Drake43c93501997-12-29 21:40:35 +00002100 name = s(buf, pp[i].data[0].data)
Fred Drakea4541af1997-12-29 17:19:22 +00002101 del pp[i-1:i+1]
2102 length = length - 2
2103 i = i - 1
Fred Drake43c93501997-12-29 21:40:35 +00002104 label_nodes[name] = hist.nodenames[-1]
2105
Fred Drakee8b46131998-02-17 05:54:46 +00002106 elif s_buf_data == 'rfc':
2107 ch.chtype = chunk_type[PLAIN]
2108 ch.data = "RFC " + s(buf, pp[i].data[0].data)
2109 del pp[i]
2110 length = length - 1
2111
2112 elif s_buf_data == 'Large':
2113 del pp[i-1]
2114 i = i - 1
2115 length = length - 1
2116
Fred Drake43c93501997-12-29 21:40:35 +00002117 elif s_buf_data == 'ref':
2118 name = s(buf, pp[i].data[0].data)
2119 if label_nodes.has_key(name):
2120 pp[i].data[0].data = label_nodes[name]
2121 else:
2122 pp[i-1:i+1] = [
2123 chunk(PLAIN, ch.where,
2124 "(unknown node reference: %s)" % name)]
2125 length = length - 1
2126 print "WARNING: unknown node label", `name`
Fred Drakea4541af1997-12-29 17:19:22 +00002127
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002128 else:
Guido van Rossum36f219d1996-09-11 21:30:40 +00002129 print "don't know what to do with keyword " + s_buf_data
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002130
2131
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002132re_atsign = regex.compile('[@{}]')
2133re_newline = regex.compile('\n')
2134
2135def dumpit(buf, wm, pp):
2136
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002137 global out
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002138
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002139 i, length = 0, len(pp)
2140
2141 addspace = 0
2142
2143 while 1:
2144 if len(pp) != length:
2145 raise 'FATAL', 'inconsistent length'
2146 if i == length:
2147 break
2148 ch = pp[i]
2149 i = i + 1
2150
Guido van Rossum36f219d1996-09-11 21:30:40 +00002151 dospace = addspace
2152 addspace = 0
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002153
Guido van Rossum36f219d1996-09-11 21:30:40 +00002154 if ch.chtype == chunk_type[CSNAME]:
2155 s_buf_data = s(buf, ch.data)
Fred Drake4b3f0311996-12-13 22:04:31 +00002156 if s_buf_data == 'e':
2157 wm('\\')
2158 continue
2159 if s_buf_data == '$':
2160 wm('$')
2161 continue
Guido van Rossum36f219d1996-09-11 21:30:40 +00002162 wm('@' + s_buf_data)
2163 if s_buf_data == 'node' and \
2164 pp[i].chtype == chunk_type[PLAIN] and \
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002165 s(buf, pp[i].data) in out.doublenodes:
2166 ##XXX doesnt work yet??
2167 wm(' ZZZ-' + zfill(`i`, 4))
Guido van Rossum36f219d1996-09-11 21:30:40 +00002168 if s_buf_data[0] in string.letters:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002169 addspace = 1
Guido van Rossum36f219d1996-09-11 21:30:40 +00002170 elif ch.chtype == chunk_type[PLAIN]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002171 if dospace and s(buf, ch.data) not in (' ', '\t'):
2172 wm(' ')
2173 text = s(buf, ch.data)
2174 while 1:
2175 pos = re_atsign.search(text)
2176 if pos < 0:
2177 break
2178 wm(text[:pos] + '@' + text[pos])
2179 text = text[pos+1:]
2180 wm(text)
Guido van Rossum36f219d1996-09-11 21:30:40 +00002181 elif ch.chtype == chunk_type[GROUP]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002182 wm('{')
2183 dumpit(buf, wm, ch.data)
2184 wm('}')
Guido van Rossum36f219d1996-09-11 21:30:40 +00002185 elif ch.chtype == chunk_type[DENDLINE]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002186 wm('\n\n')
2187 while i != length and pp[i].chtype in \
Guido van Rossum36f219d1996-09-11 21:30:40 +00002188 (chunk_type[DENDLINE], chunk_type[ENDLINE]):
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002189 i = i + 1
Guido van Rossum36f219d1996-09-11 21:30:40 +00002190 elif ch.chtype == chunk_type[OTHER]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002191 wm(s(buf, ch.data))
Guido van Rossum36f219d1996-09-11 21:30:40 +00002192 elif ch.chtype == chunk_type[ACTIVE]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002193 wm(s(buf, ch.data))
Guido van Rossum36f219d1996-09-11 21:30:40 +00002194 elif ch.chtype == chunk_type[ENDLINE]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002195 wm('\n')
Guido van Rossum36f219d1996-09-11 21:30:40 +00002196 elif ch.chtype == chunk_type[CSLINE]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002197 if i >= 2 and pp[i-2].chtype not in \
Guido van Rossum36f219d1996-09-11 21:30:40 +00002198 (chunk_type[ENDLINE], chunk_type[DENDLINE]) \
2199 and (pp[i-2].chtype != chunk_type[PLAIN]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002200 or s(buf, pp[i-2].data)[-1] != '\n'):
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002201
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002202 wm('\n')
2203 wm('@' + s(buf, ch.data))
2204 if i == length:
2205 raise error, 'CSLINE expected another chunk'
Guido van Rossum36f219d1996-09-11 21:30:40 +00002206 if pp[i].chtype != chunk_type[GROUP]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002207 raise error, 'CSLINE expected GROUP'
2208 if type(pp[i].data) != ListType:
2209 raise error, 'GROUP chould contain []-data'
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002210
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002211 wobj = Wobj()
2212 dumpit(buf, wobj.write, pp[i].data)
2213 i = i + 1
2214 text = wobj.data
2215 del wobj
2216 if text:
2217 wm(' ')
2218 while 1:
2219 pos = re_newline.search(text)
2220 if pos < 0:
2221 break
2222 print 'WARNING: found newline in csline arg'
2223 wm(text[:pos] + ' ')
2224 text = text[pos+1:]
2225 wm(text)
2226 if i >= length or \
Guido van Rossum36f219d1996-09-11 21:30:40 +00002227 pp[i].chtype not in (chunk_type[CSLINE],
2228 chunk_type[ENDLINE], chunk_type[DENDLINE]) \
2229 and (pp[i].chtype != chunk_type[PLAIN]
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002230 or s(buf, pp[i].data)[0] != '\n'):
2231 wm('\n')
Guido van Rossum49604d31996-09-10 22:19:51 +00002232
Guido van Rossum36f219d1996-09-11 21:30:40 +00002233 elif ch.chtype == chunk_type[COMMENT]:
2234## print 'COMMENT: previous chunk =', pp[i-2]
2235## if pp[i-2].chtype == chunk_type[PLAIN]:
2236## print 'PLAINTEXT =', `s(buf, pp[i-2].data)`
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002237 if s(buf, ch.data) and \
2238 regex.match('^[ \t]*$', s(buf, ch.data)) < 0:
Guido van Rossum36f219d1996-09-11 21:30:40 +00002239 if i >= 2 \
2240 and pp[i-2].chtype not in (chunk_type[ENDLINE], chunk_type[DENDLINE]) \
2241 and not (pp[i-2].chtype == chunk_type[PLAIN]
2242 and regex.match('\\(.\\|\n\\)*[ \t]*\n$', s(buf, pp[i-2].data)) >= 0):
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002243 wm('\n')
2244 wm('@c ' + s(buf, ch.data))
Guido van Rossum36f219d1996-09-11 21:30:40 +00002245 elif ch.chtype == chunk_type[IGNORE]:
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002246 pass
2247 else:
2248 try:
2249 str = `s(buf, ch.data)`
2250 except TypeError:
2251 str = `ch.data`
2252 if len(str) > 400:
2253 str = str[:400] + '...'
2254 print 'warning:', ch.chtype, 'not handled, data ' + str
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002255
2256
2257
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002258def main():
Fred Drakee8b46131998-02-17 05:54:46 +00002259 global release_version
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002260 outfile = None
2261 headerfile = 'texipre.dat'
2262 trailerfile = 'texipost.dat'
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002263
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002264 try:
Fred Drakee8b46131998-02-17 05:54:46 +00002265 opts, args = getopt.getopt(sys.argv[1:], 'o:h:t:v:')
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002266 except getopt.error:
2267 args = []
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002268
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002269 if not args:
2270 print 'usage: partparse [-o outfile] [-h headerfile]',
2271 print '[-t trailerfile] file ...'
2272 sys.exit(2)
Guido van Rossum7a2dba21993-11-05 14:45:11 +00002273
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002274 for opt, arg in opts:
2275 if opt == '-o': outfile = arg
2276 if opt == '-h': headerfile = arg
2277 if opt == '-t': trailerfile = arg
Fred Drakee8b46131998-02-17 05:54:46 +00002278 if opt == '-v': release_version = arg
Guido van Rossum7a2dba21993-11-05 14:45:11 +00002279
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002280 if not outfile:
2281 root, ext = os.path.splitext(args[0])
2282 outfile = root + '.texi'
Guido van Rossum7a2dba21993-11-05 14:45:11 +00002283
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002284 if outfile in args:
2285 print 'will not overwrite input file', outfile
2286 sys.exit(2)
Guido van Rossum7a2dba21993-11-05 14:45:11 +00002287
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002288 outf = open(outfile, 'w')
2289 outf.write(open(headerfile, 'r').read())
Guido van Rossum7a2dba21993-11-05 14:45:11 +00002290
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002291 for file in args:
2292 if len(args) > 1: print '='*20, file, '='*20
2293 buf = open(file, 'r').read()
2294 w, pp = parseit(buf)
2295 startchange()
2296 changeit(buf, pp)
2297 dumpit(buf, outf.write, pp)
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002298
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002299 outf.write(open(trailerfile, 'r').read())
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002300
Guido van Rossum5f18d6c1996-09-10 22:34:20 +00002301 outf.close()
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002302
Guido van Rossum49604d31996-09-10 22:19:51 +00002303if __name__ == "__main__":
2304 main()