Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1 | # |
| 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 Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 7 | # easily extensible and maintainable. |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 8 | # |
| 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 Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 15 | # |
| 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 Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 20 | # |
| 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 Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 27 | |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 28 | import sys, string, regex, getopt, os |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | 49604d3 | 1996-09-10 22:19:51 +0000 | [diff] [blame] | 30 | from types import IntType, ListType, StringType, TupleType |
| 31 | |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 32 | release_version = sys.version[:3] |
| 33 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 34 | # Different parse modes for phase 1 |
| 35 | MODE_REGULAR = 0 |
| 36 | MODE_VERBATIM = 1 |
| 37 | MODE_CS_SCAN = 2 |
| 38 | MODE_COMMENT = 3 |
| 39 | MODE_MATH = 4 |
| 40 | MODE_DMATH = 5 |
| 41 | MODE_GOBBLEWHITE = 6 |
| 42 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 43 | the_modes = (MODE_REGULAR, MODE_VERBATIM, MODE_CS_SCAN, MODE_COMMENT, |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 44 | MODE_MATH, MODE_DMATH, MODE_GOBBLEWHITE) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 45 | |
| 46 | # Show the neighbourhood of the scanned buffer |
| 47 | def epsilon(buf, where): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 48 | 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 Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 54 | |
| 55 | # Should return the line number. never worked |
| 56 | def lin(): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 57 | global lineno |
| 58 | return ' Line ' + `lineno` + '.' |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 59 | |
| 60 | # Displays the recursion level. |
| 61 | def lv(lvl): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 62 | return ' Level ' + `lvl` + '.' |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 63 | |
| 64 | # Combine the three previous functions. Used often. |
| 65 | def lle(lvl, buf, where): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 66 | return lv(lvl) + lin() + epsilon(buf, where) |
| 67 | |
| 68 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 69 | # This class is only needed for _symbolic_ representation of the parse mode. |
| 70 | class Mode: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 71 | 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 Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 76 | def __cmp__(self, other): |
| 77 | if type(self) != type(other): |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 78 | other = mode[other] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 79 | return cmp(self.mode, other.mode) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 80 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 81 | 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 Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 98 | |
| 99 | # just a wrapper around a class initialisation |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 100 | mode = {} |
| 101 | for t in the_modes: |
| 102 | mode[t] = Mode(t) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 103 | |
| 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 |
| 112 | PLAIN = 0 # ASSUME PLAINTEXT, data = the text |
| 113 | GROUP = 1 # GROUP ({}), data = [chunk, chunk,..] |
| 114 | CSNAME = 2 # CONTROL SEQ TOKEN, data = the command |
| 115 | COMMENT = 3 # data is the actual comment |
| 116 | DMATH = 4 # DISPLAYMATH, data = [chunk, chunk,..] |
| 117 | MATH = 5 # MATH, see DISPLAYMATH |
| 118 | OTHER = 6 # CHAR WITH CATCODE OTHER, data = char |
| 119 | ACTIVE = 7 # ACTIVE CHAR |
| 120 | GOBBLEDWHITE = 8 # Gobbled LWSP, after CSNAME |
| 121 | ENDLINE = 9 # END-OF-LINE, data = '\n' |
| 122 | DENDLINE = 10 # DOUBLE EOL, data='\n', indicates \par |
| 123 | ENV = 11 # LaTeX-environment |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 124 | # data =(envname,[ch,ch,ch,.]) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 125 | CSLINE = 12 # for texi: next chunk will be one group |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 126 | # of args. Will be set all on 1 line |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 127 | IGNORE = 13 # IGNORE this data |
| 128 | ENDENV = 14 # TEMP END OF GROUP INDICATOR |
| 129 | IF = 15 # IF-directive |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 130 | # data = (flag,negate,[ch, ch, ch,...]) |
| 131 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 132 | the_types = (PLAIN, GROUP, CSNAME, COMMENT, DMATH, MATH, OTHER, ACTIVE, |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 133 | GOBBLEDWHITE, ENDLINE, DENDLINE, ENV, CSLINE, IGNORE, ENDENV, IF) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 134 | |
| 135 | # class, just to display symbolic name |
| 136 | class ChunkType: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 137 | 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 Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 141 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 142 | def __cmp__(self, other): |
| 143 | if type(self) != type(other): |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 144 | other = chunk_type[other] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 145 | return cmp(self.chunk_type, other.chunk_type) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 146 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 147 | 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 Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 182 | |
| 183 | # ...and the wrapper |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 184 | chunk_type = {} |
Guido van Rossum | 49604d3 | 1996-09-10 22:19:51 +0000 | [diff] [blame] | 185 | for t in the_types: |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 186 | chunk_type[t] = ChunkType(t) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 187 | |
| 188 | # store a type object of the ChunkType-class-instance... |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 189 | chunk_type_type = type(chunk_type[PLAIN]) |
Guido van Rossum | 49604d3 | 1996-09-10 22:19:51 +0000 | [diff] [blame] | 190 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 191 | # this class contains a part of the parsed buffer |
| 192 | class Chunk: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 193 | def __init__(self, chtype, where, data): |
| 194 | if type(chtype) != chunk_type_type: |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 195 | chtype = chunk_type[chtype] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 196 | self.chtype = chtype |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 197 | self.where = where |
| 198 | self.data = data |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 199 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 200 | def __repr__(self): |
| 201 | return 'chunk' + `self.chtype, self.where, self.data` |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 202 | |
| 203 | # and the wrapper |
Guido van Rossum | 49604d3 | 1996-09-10 22:19:51 +0000 | [diff] [blame] | 204 | chunk = Chunk |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 205 | |
| 206 | |
| 207 | error = 'partparse.error' |
| 208 | |
| 209 | # |
| 210 | # TeX's catcodes... |
| 211 | # |
| 212 | CC_ESCAPE = 0 |
| 213 | CC_LBRACE = 1 |
| 214 | CC_RBRACE = 2 |
| 215 | CC_MATHSHIFT = 3 |
| 216 | CC_ALIGNMENT = 4 |
| 217 | CC_ENDLINE = 5 |
| 218 | CC_PARAMETER = 6 |
| 219 | CC_SUPERSCRIPT = 7 |
| 220 | CC_SUBSCRIPT = 8 |
| 221 | CC_IGNORE = 9 |
| 222 | CC_WHITE = 10 |
| 223 | CC_LETTER = 11 |
| 224 | CC_OTHER = 12 |
| 225 | CC_ACTIVE = 13 |
| 226 | CC_COMMENT = 14 |
| 227 | CC_INVALID = 15 |
| 228 | |
| 229 | # and the names |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 230 | cc_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 Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 247 | ] |
| 248 | |
| 249 | # Show a list of catcode-name-symbols |
| 250 | def pcl(codelist): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 251 | result = '' |
| 252 | for i in codelist: |
| 253 | result = result + cc_names[i] + ', ' |
| 254 | return '[' + result[:-2] + ']' |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 255 | |
| 256 | # the name of the catcode (ACTIVE, OTHER, etc.) |
| 257 | def pc(code): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 258 | return cc_names[code] |
| 259 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 260 | |
| 261 | # Which catcodes make the parser stop parsing regular plaintext |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 262 | regular_stopcodes = [CC_ESCAPE, CC_LBRACE, CC_RBRACE, CC_MATHSHIFT, |
| 263 | CC_ALIGNMENT, CC_PARAMETER, CC_SUPERSCRIPT, CC_SUBSCRIPT, |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 264 | CC_IGNORE, CC_ACTIVE, CC_COMMENT, CC_INVALID, CC_ENDLINE] |
| 265 | |
| 266 | # same for scanning a control sequence name |
| 267 | csname_scancodes = [CC_LETTER] |
| 268 | |
| 269 | # same for gobbling LWSP |
| 270 | white_scancodes = [CC_WHITE] |
| 271 | ##white_scancodes = [CC_WHITE, CC_ENDLINE] |
| 272 | |
| 273 | # make a list of all catcode id's, except for catcode ``other'' |
| 274 | all_but_other_codes = range(16) |
| 275 | del all_but_other_codes[CC_OTHER] |
| 276 | ##print all_but_other_codes |
| 277 | |
| 278 | # when does a comment end |
| 279 | comment_stopcodes = [CC_ENDLINE] |
| 280 | |
| 281 | # gather all characters together, specified by a list of catcodes |
| 282 | def code2string(cc, codelist): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 283 | ##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 Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 290 | |
| 291 | # automatically generate all characters of catcode other, being the |
| 292 | # complement set in the ASCII range (128 characters) |
| 293 | def make_other_codes(cc): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 294 | 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 Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 304 | |
| 305 | # catcode dump (which characters have which catcodes). |
| 306 | def dump_cc(name, cc): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 307 | ##print '\t' + name |
| 308 | ##print '=' * (8+len(name)) |
| 309 | if len(cc) != 16: |
| 310 | raise TypeError, 'cc not good cat class' |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 311 | ## for i in range(16): |
| 312 | ## print pc(i) + '\t' + `cc[i]` |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 313 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 314 | |
| 315 | # In the beginning,.... |
| 316 | epoch_cc = [None] * 16 |
| 317 | ##dump_cc('epoch_cc', epoch_cc) |
| 318 | |
| 319 | |
| 320 | # INITEX |
| 321 | initex_cc = epoch_cc[:] |
| 322 | initex_cc[CC_ESCAPE] = '\\' |
| 323 | initex_cc[CC_ENDLINE], initex_cc[CC_IGNORE], initex_cc[CC_WHITE] = \ |
| 324 | '\n', '\0', ' ' |
| 325 | initex_cc[CC_LETTER] = string.uppercase + string.lowercase |
| 326 | initex_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 Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 330 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 331 | # LPLAIN: LaTeX catcode setting (see lplain.tex) |
| 332 | lplain_cc = initex_cc[:] |
| 333 | lplain_cc[CC_LBRACE], lplain_cc[CC_RBRACE] = '{', '}' |
| 334 | lplain_cc[CC_MATHSHIFT] = '$' |
| 335 | lplain_cc[CC_ALIGNMENT] = '&' |
| 336 | lplain_cc[CC_PARAMETER] = '#' |
| 337 | lplain_cc[CC_SUPERSCRIPT] = '^\x0B' # '^' and C-k |
| 338 | lplain_cc[CC_SUBSCRIPT] = '_\x01' # '_' and C-a |
| 339 | lplain_cc[CC_WHITE] = lplain_cc[CC_WHITE] + '\t' |
| 340 | lplain_cc[CC_ACTIVE] = '~\x0C' # '~' and C-l |
| 341 | lplain_cc[CC_OTHER] = make_other_codes(lplain_cc) |
| 342 | ##dump_cc('lplain_cc', lplain_cc) |
| 343 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 344 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 345 | # Guido's LaTeX environment catcoded '_' as ``other'' |
| 346 | # my own purpose catlist |
| 347 | my_cc = lplain_cc[:] |
| 348 | my_cc[CC_SUBSCRIPT] = my_cc[CC_SUBSCRIPT][1:] # remove '_' here |
| 349 | my_cc[CC_OTHER] = my_cc[CC_OTHER] + '_' # add it to OTHER list |
| 350 | dump_cc('my_cc', my_cc) |
| 351 | |
| 352 | |
| 353 | |
| 354 | # needed for un_re, my equivalent for regexp-quote in Emacs |
| 355 | re_meaning = '\\[]^$' |
| 356 | |
| 357 | def un_re(str): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 358 | 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 Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 365 | # NOTE the negate ('^') operator in *some* of the regexps below |
| 366 | def make_rc_regular(cc): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 367 | # problems here if '[]' are included!! |
| 368 | return regex.compile('[' + code2string(cc, regular_stopcodes) + ']') |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 369 | |
| 370 | def make_rc_cs_scan(cc): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 371 | return regex.compile('[^' + code2string(cc, csname_scancodes) + ']') |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 372 | |
| 373 | def make_rc_comment(cc): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 374 | return regex.compile('[' + code2string(cc, comment_stopcodes) + ']') |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 375 | |
| 376 | def make_rc_endwhite(cc): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 377 | return regex.compile('[^' + code2string(cc, white_scancodes) + ']') |
| 378 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 379 | |
| 380 | |
| 381 | # regular: normal mode: |
| 382 | rc_regular = make_rc_regular(my_cc) |
| 383 | |
| 384 | # scan: scan a command sequence e.g. `newlength' or `mbox' or `;', `,' or `$' |
| 385 | rc_cs_scan = make_rc_cs_scan(my_cc) |
| 386 | rc_comment = make_rc_comment(my_cc) |
| 387 | rc_endwhite = make_rc_endwhite(my_cc) |
| 388 | |
| 389 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 390 | # parseit (BUF, PARSEMODE=mode[MODE_REGULAR], START=0, RECURSION-LEVEL=0) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 391 | # 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 Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 399 | def parseit(buf, parsemode=mode[MODE_REGULAR], start=0, lvl=0): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 400 | global lineno |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 401 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 402 | result = [] |
| 403 | end = len(buf) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 404 | if lvl == 0 and parsemode == mode[MODE_REGULAR]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 405 | lineno = 1 |
| 406 | lvl = lvl + 1 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 407 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 408 | ##print 'parseit(' + epsilon(buf, start) + ', ' + `parsemode` + ', ' + `start` + ', ' + `lvl` + ')' |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 409 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 410 | # |
| 411 | # some of the more regular modes... |
| 412 | # |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 413 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 414 | if parsemode in (mode[MODE_REGULAR], mode[MODE_DMATH], mode[MODE_MATH]): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 415 | 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 Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 422 | if lvl > 1 or curpmode != mode[MODE_REGULAR]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 423 | # 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 Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 427 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 428 | pos = rc_regular.search(buf, where) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 429 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 430 | if pos < 0: |
| 431 | pos = end |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 432 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 433 | if pos != where: |
| 434 | newpos, c = pos, chunk(PLAIN, where, (where, pos)) |
| 435 | result.append(c) |
| 436 | continue |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 437 | |
| 438 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 439 | # |
| 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 Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 451 | if lvl == 1 and mode != mode[MODE_REGULAR]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 452 | 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 Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 456 | # |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 457 | # 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 Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 460 | # |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 461 | # 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 Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 471 | mode[MODE_COMMENT], where+1, lvl) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 472 | 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 Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 483 | 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 Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 488 | 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 Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 493 | else: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 494 | # |
| 495 | # single math shift, e.g. '$' |
| 496 | # |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 497 | 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 Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 502 | 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 Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 507 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 508 | elif foundchar in my_cc[CC_IGNORE]: |
| 509 | print 'warning: ignored char', `foundchar` |
| 510 | newpos = where + 1 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 511 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 512 | 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 Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 528 | lineno = lineno + 1 |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 529 | savedwhere = where |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 530 | newpos, dummy = parseit(buf, mode[MODE_GOBBLEWHITE], where + 1, lvl) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 531 | 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 Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 538 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 539 | elif parsemode == mode[MODE_CS_SCAN]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 540 | # |
| 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 Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 556 | else: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 557 | spos = pos |
| 558 | if buf[pos] == '\n': |
| 559 | lineno = lineno + 1 |
| 560 | spos = pos + 1 |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 561 | pos2, dummy = parseit(buf, mode[MODE_GOBBLEWHITE], spos, lvl) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 562 | return pos2, (start, pos) |
| 563 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 564 | elif parsemode == mode[MODE_GOBBLEWHITE]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 565 | 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 Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 572 | elif parsemode == mode[MODE_COMMENT]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 573 | 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 Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 579 | pos2, dummy = parseit(buf, mode[MODE_GOBBLEWHITE], pos, lvl) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 580 | return pos2, (start, pos) |
| 581 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 582 | else: |
| 583 | raise error, 'Unknown mode (' + `parsemode` + ')' |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 584 | |
| 585 | |
| 586 | #moreresult = cswitch(buf[x1:x2], buf, newpos, parsemode, lvl) |
| 587 | |
| 588 | #boxcommands = 'mbox', 'fbox' |
| 589 | #defcommands = 'def', 'newcommand' |
| 590 | |
| 591 | endverbstr = '\\end{verbatim}' |
| 592 | |
| 593 | re_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 | # |
| 603 | def handlecs(buf, where, curpmode, lvl, result, end): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 604 | global lineno |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 605 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 606 | # get the control sequence name... |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 607 | newpos, data = parseit(buf, mode[MODE_CS_SCAN], where+1, lvl) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 608 | saveddata = data |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 609 | s_buf_data = s(buf, data) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 610 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 611 | if s_buf_data in ('begin', 'end'): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 612 | # skip the expected '{' and get the LaTeX-envname '}' |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 613 | newpos, data = parseit(buf, mode[MODE_REGULAR], newpos+1, lvl) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 614 | if len(data) != 1: |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 615 | raise error, 'expected 1 chunk of data.' + lle(lvl, buf, where) |
Guido van Rossum | 49604d3 | 1996-09-10 22:19:51 +0000 | [diff] [blame] | 616 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 617 | # yucky, we've got an environment |
| 618 | envname = s(buf, data[0].data) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 619 | s_buf_saveddata = s(buf, saveddata) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 620 | ##print 'FOUND ' + s(buf, saveddata) + '. Name ' + `envname` + '.' + lv(lvl) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 621 | if s_buf_saveddata == 'begin' and envname == 'verbatim': |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 622 | # verbatim deserves special treatment |
| 623 | pos = re_endverb.search(buf, newpos) |
| 624 | if pos < 0: |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 625 | raise error, "%s not found.%s" \ |
| 626 | % (`endverbstr`, lle(lvl, buf, where)) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 627 | result.append(chunk(ENV, where, (envname, [chunk(PLAIN, newpos, (newpos, pos))]))) |
| 628 | newpos = pos + len(endverbstr) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 629 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 630 | elif s_buf_saveddata == 'begin': |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 631 | # 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 Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 637 | raise error, "missing 'end'" + lle(lvl, buf, where) \ |
| 638 | + epsilon(buf, newpos) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 639 | retenv = data[-1] |
| 640 | del data[-1] |
| 641 | if retenv != envname: |
| 642 | #[`retenv`, `envname`] |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 643 | raise error, 'environments do not match.%s%s' \ |
| 644 | % (lle(lvl, buf, where), epsilon(buf, newpos)) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 645 | result.append(chunk(ENV, where, (retenv, data))) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 646 | else: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 647 | # '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 Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 656 | elif s_buf_data[0:2] == 'if': |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 657 | # another scary monster: the 'if' directive |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 658 | flag = s_buf_data[2:] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 659 | |
| 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 Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 678 | + lle(lvl, buf, where) \ |
| 679 | + epsilon(buf, newpos) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 680 | |
| 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 Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 686 | elif s_buf_data in ('else', 'fi'): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 687 | 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 Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 703 | `delimchar` + ') not found.' + \ |
| 704 | lle(lvl, buf, where) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 705 | 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 Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 710 | |
| 711 | # this is just a function to get the string value if the possible data-tuple |
| 712 | def s(buf, data): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 713 | 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 Rossum | 49604d3 | 1996-09-10 22:19:51 +0000 | [diff] [blame] | 719 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 720 | |
| 721 | ##length, data1, i = getnextarg(length, buf, pp, i + 1) |
| 722 | |
| 723 | # make a deep-copy of some chunks |
| 724 | def crcopy(r): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 725 | return map(chunkcopy, r) |
Guido van Rossum | 49604d3 | 1996-09-10 22:19:51 +0000 | [diff] [blame] | 726 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 727 | |
| 728 | # copy a chunk, would better be a method of class Chunk... |
| 729 | def chunkcopy(ch): |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 730 | if ch.chtype == chunk_type[GROUP]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 731 | return chunk(GROUP, ch.where, map(chunkcopy, ch.data)) |
| 732 | else: |
| 733 | return chunk(ch.chtype, ch.where, ch.data) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 734 | |
| 735 | |
| 736 | # get next argument for TeX-macro, flatten a group (insert between) |
| 737 | # or return Command Sequence token, or give back one character |
| 738 | def getnextarg(length, buf, pp, item): |
| 739 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 740 | ##wobj = Wobj() |
| 741 | ##dumpit(buf, wobj.write, pp[item:min(length, item + 5)]) |
| 742 | ##print 'GETNEXTARG, (len, item) =', `length, item` + ' ---> ' + wobj.data + ' <---' |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 743 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 744 | while item < length and pp[item].chtype == chunk_type[ENDLINE]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 745 | del pp[item] |
| 746 | length = length - 1 |
| 747 | if item >= length: |
| 748 | raise error, 'no next arg.' + epsilon(buf, pp[-1].where) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 749 | if pp[item].chtype == chunk_type[GROUP]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 750 | 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 Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 762 | elif pp[item].chtype == chunk_type[PLAIN]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 763 | #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 Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 769 | else: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 770 | 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 Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 782 | |
| 783 | |
| 784 | # this one is needed to find the end of LaTeX's optional argument, like |
| 785 | # item[...] |
| 786 | re_endopt = regex.compile(']') |
| 787 | |
| 788 | # get a LaTeX-optional argument, you know, the square braces '[' and ']' |
| 789 | def getoptarg(length, buf, pp, item): |
| 790 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 791 | wobj = Wobj() |
| 792 | dumpit(buf, wobj.write, pp[item:min(length, item + 5)]) |
| 793 | ##print 'GETOPTARG, (len, item) =', `length, item` + ' ---> ' + wobj.data + ' <---' |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 794 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 795 | if item >= length or \ |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 796 | pp[item].chtype != chunk_type[PLAIN] or \ |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 797 | 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 Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 808 | if pp[item].chtype == chunk_type[PLAIN]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 809 | 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 Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 826 | return length, item |
| 827 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 828 | item = item+1 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 829 | |
| 830 | |
| 831 | # Wobj just add write-requests to the ``data'' attribute |
| 832 | class Wobj: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 833 | data = '' |
Guido van Rossum | 49604d3 | 1996-09-10 22:19:51 +0000 | [diff] [blame] | 834 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 835 | def write(self, data): |
| 836 | self.data = self.data + data |
Guido van Rossum | b819bdf | 1995-03-15 11:26:26 +0000 | [diff] [blame] | 837 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 838 | # ignore these commands |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 839 | ignoredcommands = ('bcode', 'ecode', 'hline', 'small', '/') |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 840 | # map commands like these to themselves as plaintext |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 841 | wordsselves = ('UNIX', 'ABC', 'C', 'ASCII', 'EOF', 'LaTeX', 'POSIX') |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 842 | # \{ --> {, \} --> }, etc |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 843 | themselves = ('{', '}', ',', '.', '@', ' ', '\n') + wordsselves |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 844 | # these ones also themselves (see argargs macro in myformat.sty) |
| 845 | inargsselves = (',', '[', ']', '(', ')') |
| 846 | # this is how *I* would show the difference between emph and strong |
| 847 | # code 1 means: fold to uppercase |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 848 | markcmds = {'code': ('', ''), 'var': 1, 'emph': ('_', '_'), |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 849 | 'strong': ('*', '*')} |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 850 | |
| 851 | # recognise patter {\FONTCHANGE-CMD TEXT} to \MAPPED-FC-CMD{TEXT} |
| 852 | fontchanges = {'rm': 'r', 'it': 'i', 'em': 'emph', 'bf': 'b', 'tt': 't'} |
| 853 | |
| 854 | # transparent for these commands |
Guido van Rossum | 7760cde | 1995-03-17 16:03:11 +0000 | [diff] [blame] | 855 | for_texi = ('emph', 'var', 'strong', 'code', 'kbd', 'key', 'dfn', 'samp', |
| 856 | 'file', 'r', 'i', 't') |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 857 | |
| 858 | |
| 859 | # try to remove macros and return flat text |
| 860 | def flattext(buf, pp): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 861 | pp = crcopy(pp) |
| 862 | ##print '---> FLATTEXT ' + `pp` |
| 863 | wobj = Wobj() |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 864 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 865 | 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 Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 873 | if ch.chtype == chunk_type[PLAIN]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 874 | pass |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 875 | 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 Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 881 | ch.data = '\\' |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 882 | elif len(s_buf_data) == 1 \ |
| 883 | and s_buf_data in onlylatexspecial: |
| 884 | ch.chtype = chunk_type[PLAIN] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 885 | # 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 Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 889 | and pp[i].chtype==chunk_type[GROUP] \ |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 890 | and len(pp[i].data) == 0: |
| 891 | del pp[i] |
| 892 | length = length-1 |
| 893 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 894 | elif s_buf_data in markcmds.keys(): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 895 | 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 Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 899 | ch.chtype = chunk_type[PLAIN] |
| 900 | markcmd = s_buf_data |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 901 | 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 Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 911 | if s_buf_data not in ignoredcommands: |
| 912 | print 'WARNING: deleting command ' + s_buf_data |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 913 | print 'PP' + `pp[i-1]` |
| 914 | del pp[i-1] |
| 915 | i, length = i-1, length-1 |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 916 | elif ch.chtype == chunk_type[GROUP]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 917 | length, newi = getnextarg(length, buf, pp, i-1) |
| 918 | i = i-1 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 919 | ## str = flattext(buf, crcopy(pp[i-1:newi])) |
| 920 | ## del pp[i:newi] |
| 921 | ## length = length - (newi - i) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 922 | ## ch.chtype = chunk_type[PLAIN] |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 923 | ## ch.data = str |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 924 | else: |
| 925 | pass |
| 926 | |
| 927 | dumpit(buf, wobj.write, pp) |
| 928 | ##print 'FLATTEXT: RETURNING ' + `wobj.data` |
| 929 | return wobj.data |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 930 | |
| 931 | # try to generate node names (a bit shorter than the chapter title) |
| 932 | # note that the \nodename command (see elsewhere) overules these efforts |
| 933 | def invent_node_names(text): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 934 | words = string.split(text) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 935 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 936 | ##print 'WORDS ' + `words` |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 937 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 938 | if len(words) == 2 \ |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 939 | and string.lower(words[0]) == 'built-in' \ |
| 940 | and string.lower(words[1]) not in ('modules', 'functions'): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 941 | 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 Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 946 | if len(words) > 4 \ |
| 947 | and (string.lower(string.join(words[-4:])) \ |
| 948 | == 'methods and data attributes'): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 949 | return string.join(words[:2]) |
| 950 | return text |
| 951 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 952 | re_commas_etc = regex.compile('[,`\'@{}]') |
| 953 | |
| 954 | re_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 |
| 962 | def next_command_p(length, buf, pp, i, cmdname): |
| 963 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 964 | while 1: |
| 965 | if i >= len(pp): |
| 966 | break |
| 967 | ch = pp[i] |
| 968 | i = i+1 |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 969 | if ch.chtype == chunk_type[ENDLINE]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 970 | continue |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 971 | if ch.chtype == chunk_type[DENDLINE]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 972 | continue |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 973 | if ch.chtype == chunk_type[PLAIN]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 974 | 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 Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 978 | if ch.chtype == chunk_type[CSNAME]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 979 | if s(buf, ch.data) == cmdname: |
| 980 | return i # _after_ the command |
| 981 | return -1 |
| 982 | return -1 |
| 983 | |
| 984 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 985 | # things that are special to LaTeX, but not to texi.. |
| 986 | onlylatexspecial = '_~^$#&%' |
| 987 | |
Guido van Rossum | 23301a9 | 1993-05-24 14:19:37 +0000 | [diff] [blame] | 988 | class Struct: pass |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 989 | |
| 990 | hist = Struct() |
| 991 | out = Struct() |
| 992 | |
| 993 | def startchange(): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 994 | global hist, out |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 995 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 996 | hist.inenv = [] |
| 997 | hist.nodenames = [] |
| 998 | hist.cindex = [] |
| 999 | hist.inargs = 0 |
| 1000 | hist.enumeratenesting, hist.itemizenesting = 0, 0 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1001 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1002 | out.doublenodes = [] |
| 1003 | out.doublecindeces = [] |
| 1004 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1005 | |
| 1006 | spacech = [chunk(PLAIN, 0, ' ')] |
| 1007 | commach = [chunk(PLAIN, 0, ', ')] |
| 1008 | cindexch = [chunk(CSLINE, 0, 'cindex')] |
| 1009 | |
| 1010 | # the standard variation in symbols for itemize |
| 1011 | itemizesymbols = ['bullet', 'minus', 'dots'] |
| 1012 | |
| 1013 | # same for enumerate |
| 1014 | enumeratesymbols = ['1', 'A', 'a'] |
| 1015 | |
Fred Drake | 9c7c6be | 1998-02-19 21:40:22 +0000 | [diff] [blame^] | 1016 | d = {} |
| 1017 | for name in ('url', 'module', 'function', 'cfunction', |
| 1018 | 'keyword', 'method', 'exception', 'constant', |
| 1019 | 'email', 'class', 'member', 'cdata', 'ctype', |
| 1020 | 'member'): |
| 1021 | d[name] = 'code' |
| 1022 | d['program'] = 'strong' |
| 1023 | d['sectcode'] = 'code' |
| 1024 | convertible_csname = d.has_key |
| 1025 | conversion = d.get |
| 1026 | del d, name |
| 1027 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1028 | ## |
| 1029 | ## \begin{ {func,data,exc}desc }{name}... |
| 1030 | ## the resulting texi-code is dependent on the contents of indexsubitem |
| 1031 | ## |
| 1032 | |
| 1033 | # indexsubitem: `['XXX', 'function'] |
| 1034 | # funcdesc: |
| 1035 | # deffn {`idxsi`} NAME (FUNCARGS) |
| 1036 | |
| 1037 | # indexsubitem: `['XXX', 'method']` |
| 1038 | # funcdesc: |
| 1039 | # defmethod {`idxsi[0]`} NAME (FUNCARGS) |
| 1040 | |
| 1041 | # indexsubitem: `['in', 'module', 'MODNAME']' |
| 1042 | # datadesc: |
| 1043 | # defcv data {`idxsi[1:]`} NAME |
| 1044 | # excdesc: |
| 1045 | # defcv exception {`idxsi[1:]`} NAME |
| 1046 | # funcdesc: |
| 1047 | # deffn {function of `idxsi[1:]`} NAME (FUNCARGS) |
| 1048 | |
| 1049 | # indexsubitem: `['OBJECT', 'attribute']' |
| 1050 | # datadesc |
| 1051 | # defcv attribute {`OBJECT`} NAME |
| 1052 | |
| 1053 | |
| 1054 | ## this routine will be called on \begin{funcdesc}{NAME}{ARGS} |
| 1055 | ## or \funcline{NAME}{ARGS} |
| 1056 | ## |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1057 | def do_funcdesc(length, buf, pp, i, index=1): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1058 | startpoint = i-1 |
| 1059 | ch = pp[startpoint] |
| 1060 | wh = ch.where |
| 1061 | length, newi = getnextarg(length, buf, pp, i) |
| 1062 | funcname = chunk(GROUP, wh, pp[i:newi]) |
| 1063 | del pp[i:newi] |
| 1064 | length = length - (newi-i) |
| 1065 | save = hist.inargs |
| 1066 | hist.inargs = 1 |
| 1067 | length, newi = getnextarg(length, buf, pp, i) |
| 1068 | hist.inargs = save |
| 1069 | del save |
| 1070 | the_args = [chunk(PLAIN, wh, '()'[0])] + pp[i:newi] + \ |
Fred Drake | 7edd8d3 | 1996-10-09 16:11:26 +0000 | [diff] [blame] | 1071 | [chunk(PLAIN, wh, '()'[1])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1072 | del pp[i:newi] |
| 1073 | length = length - (newi-i) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1074 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1075 | idxsi = hist.indexsubitem # words |
| 1076 | command = '' |
| 1077 | cat_class = '' |
Fred Drake | acc8754 | 1996-10-14 16:20:42 +0000 | [diff] [blame] | 1078 | if idxsi and idxsi[-1] in ('method', 'protocol', 'attribute'): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1079 | command = 'defmethod' |
| 1080 | cat_class = string.join(idxsi[:-1]) |
| 1081 | elif len(idxsi) == 2 and idxsi[1] == 'function': |
| 1082 | command = 'deffn' |
| 1083 | cat_class = string.join(idxsi) |
| 1084 | elif len(idxsi) == 3 and idxsi[:2] == ['in', 'module']: |
| 1085 | command = 'deffn' |
| 1086 | cat_class = 'function of ' + string.join(idxsi[1:]) |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1087 | elif len(idxsi) > 3 and idxsi[:2] == ['in', 'modules']: |
| 1088 | command = 'deffn' |
| 1089 | cat_class = 'function of ' + string.join(idxsi[1:]) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1090 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1091 | if not command: |
| 1092 | raise error, 'don\'t know what to do with indexsubitem ' + `idxsi` |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1093 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1094 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1095 | ch.data = command |
| 1096 | |
| 1097 | cslinearg = [chunk(GROUP, wh, [chunk(PLAIN, wh, cat_class)])] |
| 1098 | cslinearg.append(chunk(PLAIN, wh, ' ')) |
| 1099 | cslinearg.append(funcname) |
| 1100 | cslinearg.append(chunk(PLAIN, wh, ' ')) |
| 1101 | l = len(cslinearg) |
| 1102 | cslinearg[l:l] = the_args |
| 1103 | |
| 1104 | pp.insert(i, chunk(GROUP, wh, cslinearg)) |
| 1105 | i, length = i+1, length+1 |
| 1106 | hist.command = command |
| 1107 | return length, i |
| 1108 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1109 | |
| 1110 | ## this routine will be called on \begin{excdesc}{NAME} |
| 1111 | ## or \excline{NAME} |
| 1112 | ## |
| 1113 | def do_excdesc(length, buf, pp, i): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1114 | startpoint = i-1 |
| 1115 | ch = pp[startpoint] |
| 1116 | wh = ch.where |
| 1117 | length, newi = getnextarg(length, buf, pp, i) |
| 1118 | excname = chunk(GROUP, wh, pp[i:newi]) |
| 1119 | del pp[i:newi] |
| 1120 | length = length - (newi-i) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1121 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1122 | idxsi = hist.indexsubitem # words |
| 1123 | command = '' |
| 1124 | cat_class = '' |
| 1125 | class_class = '' |
| 1126 | if len(idxsi) == 2 and idxsi[1] == 'exception': |
| 1127 | command = 'defvr' |
| 1128 | cat_class = string.join(idxsi) |
| 1129 | elif len(idxsi) == 3 and idxsi[:2] == ['in', 'module']: |
| 1130 | command = 'defcv' |
| 1131 | cat_class = 'exception' |
| 1132 | class_class = string.join(idxsi[1:]) |
| 1133 | elif len(idxsi) == 4 and idxsi[:3] == ['exception', 'in', 'module']: |
| 1134 | command = 'defcv' |
| 1135 | cat_class = 'exception' |
| 1136 | class_class = string.join(idxsi[2:]) |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1137 | elif idxsi == ['built-in', 'exception', 'base', 'class']: |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1138 | command = 'defvr' |
| 1139 | cat_class = 'exception base class' |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1140 | else: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1141 | raise error, 'don\'t know what to do with indexsubitem ' + `idxsi` |
| 1142 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1143 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1144 | ch.data = command |
| 1145 | |
| 1146 | cslinearg = [chunk(GROUP, wh, [chunk(PLAIN, wh, cat_class)])] |
| 1147 | cslinearg.append(chunk(PLAIN, wh, ' ')) |
| 1148 | if class_class: |
| 1149 | cslinearg.append(chunk(GROUP, wh, [chunk(PLAIN, wh, class_class)])) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1150 | cslinearg.append(chunk(PLAIN, wh, ' ')) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1151 | cslinearg.append(excname) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1152 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1153 | pp.insert(i, chunk(GROUP, wh, cslinearg)) |
| 1154 | i, length = i+1, length+1 |
| 1155 | hist.command = command |
| 1156 | return length, i |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1157 | |
| 1158 | ## same for datadesc or dataline... |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1159 | def do_datadesc(length, buf, pp, i, index=1): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1160 | startpoint = i-1 |
| 1161 | ch = pp[startpoint] |
| 1162 | wh = ch.where |
| 1163 | length, newi = getnextarg(length, buf, pp, i) |
| 1164 | dataname = chunk(GROUP, wh, pp[i:newi]) |
| 1165 | del pp[i:newi] |
| 1166 | length = length - (newi-i) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1167 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1168 | idxsi = hist.indexsubitem # words |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1169 | command = 'defcv' |
| 1170 | cat_class = 'data' |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1171 | class_class = '' |
| 1172 | if idxsi[-1] in ('attribute', 'option'): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1173 | cat_class = idxsi[-1] |
| 1174 | class_class = string.join(idxsi[:-1]) |
| 1175 | elif len(idxsi) == 3 and idxsi[:2] == ['in', 'module']: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1176 | class_class = string.join(idxsi[1:]) |
| 1177 | elif len(idxsi) == 4 and idxsi[:3] == ['data', 'in', 'module']: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1178 | class_class = string.join(idxsi[2:]) |
Fred Drake | 11b6d24 | 1996-10-10 20:09:56 +0000 | [diff] [blame] | 1179 | else: |
Fred Drake | 11b6d24 | 1996-10-10 20:09:56 +0000 | [diff] [blame] | 1180 | class_class = string.join(idxsi) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1181 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1182 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1183 | ch.data = command |
| 1184 | |
| 1185 | cslinearg = [chunk(GROUP, wh, [chunk(PLAIN, wh, cat_class)])] |
| 1186 | cslinearg.append(chunk(PLAIN, wh, ' ')) |
| 1187 | if class_class: |
| 1188 | cslinearg.append(chunk(GROUP, wh, [chunk(PLAIN, wh, class_class)])) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1189 | cslinearg.append(chunk(PLAIN, wh, ' ')) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1190 | cslinearg.append(dataname) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1191 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1192 | pp.insert(i, chunk(GROUP, wh, cslinearg)) |
| 1193 | i, length = i+1, length+1 |
| 1194 | hist.command = command |
| 1195 | return length, i |
| 1196 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1197 | |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1198 | def do_opcodedesc(length, buf, pp, i): |
| 1199 | startpoint = i-1 |
| 1200 | ch = pp[startpoint] |
| 1201 | wh = ch.where |
| 1202 | length, newi = getnextarg(length, buf, pp, i) |
| 1203 | dataname = chunk(GROUP, wh, pp[i:newi]) |
| 1204 | del pp[i:newi] |
| 1205 | length = length - (newi-i) |
| 1206 | |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 1207 | ch.chtype = CSLINE |
| 1208 | ch.data = "deffn" |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1209 | |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1210 | cslinearg = [chunk(PLAIN, wh, 'byte\ code\ instruction'), |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1211 | chunk(GROUP, wh, [chunk(PLAIN, wh, "byte code instruction")]), |
| 1212 | chunk(PLAIN, wh, ' '), |
| 1213 | dataname, |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 1214 | chunk(PLAIN, wh, ' '), |
| 1215 | pp[i], |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1216 | ] |
| 1217 | |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 1218 | pp[i] = chunk(GROUP, wh, cslinearg) |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1219 | hist.command = ch.data |
| 1220 | return length, i |
| 1221 | |
| 1222 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1223 | # regular indices: those that are not set in tt font by default.... |
| 1224 | regindices = ('cindex', ) |
| 1225 | |
| 1226 | # remove illegal characters from node names |
| 1227 | def rm_commas_etc(text): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1228 | result = '' |
| 1229 | changed = 0 |
| 1230 | while 1: |
| 1231 | pos = re_commas_etc.search(text) |
| 1232 | if pos >= 0: |
| 1233 | changed = 1 |
| 1234 | result = result + text[:pos] |
| 1235 | text = text[pos+1:] |
| 1236 | else: |
| 1237 | result = result + text |
| 1238 | break |
| 1239 | if changed: |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 1240 | print 'Warning: nodename changed to ' + `result` |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1241 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1242 | return result |
| 1243 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1244 | # boolean flags |
| 1245 | flags = {'texi': 1} |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1246 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1247 | |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 1248 | # map of \label{} to node names |
| 1249 | label_nodes = {} |
| 1250 | |
| 1251 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1252 | ## |
| 1253 | ## changeit: the actual routine, that changes the contents of the parsed |
| 1254 | ## chunks |
| 1255 | ## |
| 1256 | |
| 1257 | def changeit(buf, pp): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1258 | global onlylatexspecial, hist, out |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1259 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1260 | i, length = 0, len(pp) |
| 1261 | while 1: |
| 1262 | # sanity check: length should always equal len(pp) |
| 1263 | if len(pp) != length: |
| 1264 | raise 'FATAL', 'inconsistent length. thought ' + `length` + ', but should really be ' + `len(pp)` |
| 1265 | if i >= length: |
| 1266 | break |
| 1267 | ch = pp[i] |
| 1268 | i = i + 1 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1269 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1270 | if type(ch) is StringType: |
| 1271 | #normally, only chunks are present in pp, |
| 1272 | # but in some cases, some extra info |
| 1273 | # has been inserted, e.g., the \end{...} clauses |
| 1274 | raise 'FATAL', 'got string, probably too many ' + `end` |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1275 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1276 | if ch.chtype == chunk_type[GROUP]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1277 | # check for {\em ...} constructs |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1278 | data = ch.data |
| 1279 | if data and \ |
| 1280 | data[0].chtype == chunk_type[CSNAME] and \ |
| 1281 | fontchanges.has_key(s(buf, data[0].data)): |
| 1282 | k = s(buf, data[0].data) |
| 1283 | del data[0] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1284 | pp.insert(i-1, chunk(CSNAME, ch.where, fontchanges[k])) |
| 1285 | length, i = length+1, i+1 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1286 | |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1287 | elif data: |
| 1288 | if len(data) \ |
| 1289 | and data[0].chtype == chunk_type[GROUP] \ |
| 1290 | and len(data[0].data) \ |
| 1291 | and data[0].data[0].chtype == chunk_type[CSNAME] \ |
| 1292 | and s(buf, data[0].data[0].data) == 'e': |
| 1293 | data[0] = data[0].data[0] |
| 1294 | print "invoking \\e magic group transform..." |
| 1295 | else: |
| 1296 | ## print "GROUP -- ch.data[0].data =", ch.data[0].data |
| 1297 | k = s(buf, data[0].data) |
| 1298 | if k == "fulllineitems": |
| 1299 | del data[0] |
| 1300 | pp[i-1:i] = data |
| 1301 | i = i - 1 |
| 1302 | length = length + len(data) - 1 |
| 1303 | continue |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 1304 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1305 | # recursively parse the contents of the group |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1306 | changeit(buf, data) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1307 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1308 | elif ch.chtype == chunk_type[IF]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1309 | # \if... |
| 1310 | flag, negate, data = ch.data |
| 1311 | ##print 'IF: flag, negate = ' + `flag, negate` |
| 1312 | if flag not in flags.keys(): |
| 1313 | raise error, 'unknown flag ' + `flag` |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1314 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1315 | value = flags[flag] |
| 1316 | if negate: |
| 1317 | value = (not value) |
| 1318 | del pp[i-1] |
| 1319 | length, i = length-1, i-1 |
| 1320 | if value: |
| 1321 | pp[i:i] = data |
| 1322 | length = length + len(data) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1323 | |
| 1324 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1325 | elif ch.chtype == chunk_type[ENV]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1326 | # \begin{...} .... |
| 1327 | envname, data = ch.data |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1328 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1329 | #push this environment name on stack |
| 1330 | hist.inenv.insert(0, envname) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1331 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1332 | #append an endenv chunk after grouped data |
| 1333 | data.append(chunk(ENDENV, ch.where, envname)) |
| 1334 | ##[`data`] |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1335 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1336 | #delete this object |
| 1337 | del pp[i-1] |
| 1338 | i, length = i-1, length-1 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1339 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1340 | #insert found data |
| 1341 | pp[i:i] = data |
| 1342 | length = length + len(data) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1343 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1344 | if envname == 'verbatim': |
| 1345 | pp[i:i] = [chunk(CSLINE, ch.where, 'example'), |
| 1346 | chunk(GROUP, ch.where, [])] |
| 1347 | length, i = length+2, i+2 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1348 | |
Fred Drake | ef05803 | 1998-02-19 15:20:30 +0000 | [diff] [blame] | 1349 | elif envname in ('itemize', 'list', 'fulllineitems'): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1350 | if hist.itemizenesting > len(itemizesymbols): |
| 1351 | raise error, 'too deep itemize nesting' |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1352 | if envname == 'list': |
| 1353 | del pp[i:i+2] |
| 1354 | length = length - 2 |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1355 | ingroupch = [chunk(CSNAME, ch.where, |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1356 | itemizesymbols[hist.itemizenesting])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1357 | hist.itemizenesting = hist.itemizenesting + 1 |
| 1358 | pp[i:i] = [chunk(CSLINE, ch.where, 'itemize'), |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1359 | chunk(GROUP, ch.where, ingroupch)] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1360 | length, i = length+2, i+2 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1361 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1362 | elif envname == 'enumerate': |
| 1363 | if hist.enumeratenesting > len(enumeratesymbols): |
| 1364 | raise error, 'too deep enumerate nesting' |
| 1365 | ingroupch = [chunk(PLAIN, ch.where, |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1366 | enumeratesymbols[hist.enumeratenesting])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1367 | hist.enumeratenesting = hist.enumeratenesting + 1 |
| 1368 | pp[i:i] = [chunk(CSLINE, ch.where, 'enumerate'), |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1369 | chunk(GROUP, ch.where, ingroupch)] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1370 | length, i = length+2, i+2 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1371 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1372 | elif envname == 'description': |
| 1373 | ingroupch = [chunk(CSNAME, ch.where, 'b')] |
| 1374 | pp[i:i] = [chunk(CSLINE, ch.where, 'table'), |
| 1375 | chunk(GROUP, ch.where, ingroupch)] |
| 1376 | length, i = length+2, i+2 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1377 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1378 | elif (envname == 'tableiii') or (envname == 'tableii'): |
| 1379 | if (envname == 'tableii'): |
| 1380 | ltable = 2 |
| 1381 | else: |
| 1382 | ltable = 3 |
| 1383 | wh = ch.where |
| 1384 | newcode = [] |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1385 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1386 | #delete tabular format description |
| 1387 | # e.g., {|l|c|l|} |
| 1388 | length, newi = getnextarg(length, buf, pp, i) |
| 1389 | del pp[i:newi] |
| 1390 | length = length - (newi-i) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1391 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1392 | newcode.append(chunk(CSLINE, wh, 'table')) |
| 1393 | ingroupch = [chunk(CSNAME, wh, 'asis')] |
| 1394 | newcode.append(chunk(GROUP, wh, ingroupch)) |
| 1395 | newcode.append(chunk(CSLINE, wh, 'item')) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1396 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1397 | #get the name of macro for @item |
| 1398 | # e.g., {code} |
| 1399 | length, newi = getnextarg(length, buf, pp, i) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1400 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1401 | if newi-i != 1: |
| 1402 | raise error, 'Sorry, expected 1 chunk argument' |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1403 | if pp[i].chtype != chunk_type[PLAIN]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1404 | raise error, 'Sorry, expected plain text argument' |
| 1405 | hist.itemargmacro = s(buf, pp[i].data) |
Fred Drake | 9c7c6be | 1998-02-19 21:40:22 +0000 | [diff] [blame^] | 1406 | if convertible_csname(hist.itemargmacro): |
| 1407 | hist.itemargmacro = conversion(hist.itemargmacro) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1408 | del pp[i:newi] |
| 1409 | length = length - (newi-i) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1410 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1411 | itembody = [] |
| 1412 | for count in range(ltable): |
| 1413 | length, newi = getnextarg(length, buf, pp, i) |
| 1414 | emphgroup = [ |
| 1415 | chunk(CSNAME, wh, 'emph'), |
| 1416 | chunk(GROUP, 0, pp[i:newi])] |
| 1417 | del pp[i:newi] |
| 1418 | length = length - (newi-i) |
| 1419 | if count == 0: |
| 1420 | itemarg = emphgroup |
| 1421 | elif count == ltable-1: |
| 1422 | itembody = itembody + \ |
| 1423 | [chunk(PLAIN, wh, ' --- ')] + emphgroup |
| 1424 | else: |
| 1425 | itembody = emphgroup |
| 1426 | newcode.append(chunk(GROUP, wh, itemarg)) |
| 1427 | newcode = newcode + itembody + [chunk(DENDLINE, wh, '\n')] |
| 1428 | pp[i:i] = newcode |
| 1429 | l = len(newcode) |
| 1430 | length, i = length+l, i+l |
| 1431 | del newcode, l |
| 1432 | |
| 1433 | if length != len(pp): |
| 1434 | raise 'STILL, SOMETHING wrong', `i` |
| 1435 | |
Fred Drake | ef05803 | 1998-02-19 15:20:30 +0000 | [diff] [blame] | 1436 | elif envname in ('funcdesc', 'funcdescni', 'classdesc'): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1437 | pp.insert(i, chunk(PLAIN, ch.where, '')) |
| 1438 | i, length = i+1, length+1 |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1439 | length, i = do_funcdesc(length, buf, pp, i, |
Fred Drake | ef05803 | 1998-02-19 15:20:30 +0000 | [diff] [blame] | 1440 | envname[-2:] != "ni") |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1441 | |
| 1442 | elif envname == 'excdesc': |
| 1443 | pp.insert(i, chunk(PLAIN, ch.where, '')) |
| 1444 | i, length = i+1, length+1 |
| 1445 | length, i = do_excdesc(length, buf, pp, i) |
| 1446 | |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1447 | elif envname in ('datadesc', 'datadescni'): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1448 | pp.insert(i, chunk(PLAIN, ch.where, '')) |
| 1449 | i, length = i+1, length+1 |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1450 | length, i = do_datadesc(length, buf, pp, i, |
Fred Drake | ef05803 | 1998-02-19 15:20:30 +0000 | [diff] [blame] | 1451 | envname[-2:] != "ni") |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1452 | |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1453 | elif envname == 'opcodedesc': |
| 1454 | pp.insert(i, chunk(PLAIN, ch.where, '')) |
| 1455 | i, length = i+1, length+1 |
| 1456 | length, i = do_opcodedesc(length, buf, pp, i) |
| 1457 | |
| 1458 | elif envname == 'seealso': |
| 1459 | chunks = [chunk(ENDLINE, ch.where, "\n"), |
| 1460 | chunk(CSNAME, ch.where, "b"), |
| 1461 | chunk(GROUP, ch.where, [ |
| 1462 | chunk(PLAIN, ch.where, "See also: ")]), |
| 1463 | chunk(ENDLINE, ch.where, "\n"), |
| 1464 | chunk(ENDLINE, ch.where, "\n")] |
| 1465 | pp[i-1:i] = chunks |
| 1466 | length = length + len(chunks) - 1 |
| 1467 | i = i + len(chunks) - 1 |
| 1468 | |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 1469 | elif envname in ('sloppypar', 'flushleft'): |
| 1470 | pass |
| 1471 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1472 | else: |
| 1473 | print 'WARNING: don\'t know what to do with env ' + `envname` |
| 1474 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1475 | elif ch.chtype == chunk_type[ENDENV]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1476 | envname = ch.data |
| 1477 | if envname != hist.inenv[0]: |
| 1478 | raise error, '\'end\' does not match. Name ' + `envname` + ', expected ' + `hist.inenv[0]` |
| 1479 | del hist.inenv[0] |
| 1480 | del pp[i-1] |
| 1481 | i, length = i-1, length-1 |
| 1482 | |
| 1483 | if envname == 'verbatim': |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 1484 | pp[i:i] = [chunk(CSLINE, ch.where, 'end'), |
| 1485 | chunk(GROUP, ch.where, [ |
| 1486 | chunk(PLAIN, ch.where, 'example')])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1487 | i, length = i+2, length+2 |
Fred Drake | ef05803 | 1998-02-19 15:20:30 +0000 | [diff] [blame] | 1488 | elif envname in ('itemize', 'list', 'fulllineitems'): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1489 | hist.itemizenesting = hist.itemizenesting - 1 |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 1490 | pp[i:i] = [chunk(CSLINE, ch.where, 'end'), |
| 1491 | chunk(GROUP, ch.where, [ |
| 1492 | chunk(PLAIN, ch.where, 'itemize')])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1493 | i, length = i+2, length+2 |
| 1494 | elif envname == 'enumerate': |
| 1495 | hist.enumeratenesting = hist.enumeratenesting-1 |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 1496 | pp[i:i] = [chunk(CSLINE, ch.where, 'end'), |
| 1497 | chunk(GROUP, ch.where, [ |
| 1498 | chunk(PLAIN, ch.where, 'enumerate')])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1499 | i, length = i+2, length+2 |
| 1500 | elif envname == 'description': |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 1501 | pp[i:i] = [chunk(CSLINE, ch.where, 'end'), |
| 1502 | chunk(GROUP, ch.where, [ |
| 1503 | chunk(PLAIN, ch.where, 'table')])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1504 | i, length = i+2, length+2 |
| 1505 | elif (envname == 'tableiii') or (envname == 'tableii'): |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 1506 | pp[i:i] = [chunk(CSLINE, ch.where, 'end'), |
| 1507 | chunk(GROUP, ch.where, [ |
| 1508 | chunk(PLAIN, ch.where, 'table')])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1509 | i, length = i+2, length + 2 |
| 1510 | pp.insert(i, chunk(DENDLINE, ch.where, '\n')) |
| 1511 | i, length = i+1, length+1 |
| 1512 | |
Fred Drake | ef05803 | 1998-02-19 15:20:30 +0000 | [diff] [blame] | 1513 | elif envname in ('funcdesc', 'excdesc', 'datadesc', 'classdesc', |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1514 | 'funcdescni', 'datadescni'): |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 1515 | pp[i:i] = [chunk(CSLINE, ch.where, 'end'), |
| 1516 | chunk(GROUP, ch.where, [ |
| 1517 | chunk(PLAIN, ch.where, hist.command)])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1518 | i, length = i+2, length+2 |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1519 | |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 1520 | elif envname == 'opcodedesc': |
| 1521 | pp[i:i] = [chunk(CSLINE, ch.where, 'end'), |
| 1522 | chunk(GROUP, ch.where, [ |
| 1523 | chunk(PLAIN, ch.where, "deffn")])] |
| 1524 | i, length = i+2, length+2 |
| 1525 | |
| 1526 | elif envname in ('seealso', 'sloppypar', 'flushleft'): |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1527 | pass |
| 1528 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1529 | else: |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1530 | print 'WARNING: ending env %s has no actions' % `envname` |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1531 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1532 | elif ch.chtype == chunk_type[CSNAME]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1533 | # control name transformations |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1534 | s_buf_data = s(buf, ch.data) |
| 1535 | if s_buf_data == 'optional': |
| 1536 | pp[i-1].chtype = chunk_type[PLAIN] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1537 | pp[i-1].data = '[' |
| 1538 | if (i < length) and \ |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1539 | (pp[i].chtype == chunk_type[GROUP]): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1540 | cp=pp[i].data |
| 1541 | pp[i:i+1]=cp + [ |
| 1542 | chunk(PLAIN, ch.where, ']')] |
| 1543 | length = length+len(cp) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1544 | elif s_buf_data in ignoredcommands: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1545 | del pp[i-1] |
| 1546 | i, length = i-1, length-1 |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1547 | elif s_buf_data == '@' and \ |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1548 | i != length and \ |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1549 | pp[i].chtype == chunk_type[PLAIN] and \ |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1550 | s(buf, pp[i].data)[0] == '.': |
| 1551 | # \@. --> \. --> @. |
| 1552 | ch.data = '.' |
| 1553 | del pp[i] |
| 1554 | length = length-1 |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1555 | elif s_buf_data == '\\': |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1556 | # \\ --> \* --> @* |
| 1557 | ch.data = '*' |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1558 | elif len(s_buf_data) == 1 and \ |
| 1559 | s_buf_data in onlylatexspecial: |
| 1560 | ch.chtype = chunk_type[PLAIN] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1561 | # check if such a command is followed by |
| 1562 | # an empty group: e.g., `\%{}'. If so, remove |
| 1563 | # this empty group too |
| 1564 | if i < length and \ |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1565 | pp[i].chtype == chunk_type[GROUP] \ |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1566 | and len(pp[i].data) == 0: |
| 1567 | del pp[i] |
| 1568 | length = length-1 |
| 1569 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1570 | elif hist.inargs and s_buf_data in inargsselves: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1571 | # This is the special processing of the |
| 1572 | # arguments of the \begin{funcdesc}... or |
| 1573 | # \funcline... arguments |
| 1574 | # \, --> , \[ --> [, \] --> ] |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1575 | ch.chtype = chunk_type[PLAIN] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1576 | |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1577 | elif s_buf_data == 'setindexsubitem': |
| 1578 | stuff = pp[i].data |
| 1579 | if len(stuff) != 1: |
| 1580 | raise error, "parameter to \\setindexsubitem{} too long" |
| 1581 | if pp[i].chtype != chunk_type[GROUP]: |
| 1582 | raise error, "bad chunk type following \\setindexsubitem" \ |
| 1583 | "\nexpected GROUP, got " + str(ch.chtype) |
| 1584 | text = s(buf, stuff[0].data) |
| 1585 | if text[:1] != '(' or text[-1:] != ')': |
| 1586 | raise error, \ |
| 1587 | 'expected indexsubitem enclosed in parenteses' |
| 1588 | hist.indexsubitem = string.split(text[1:-1]) |
| 1589 | del stuff, text |
| 1590 | del pp[i-1:i+1] |
| 1591 | i = i - 1 |
| 1592 | length = length - 2 |
| 1593 | |
| 1594 | elif s_buf_data == 'newcommand': |
| 1595 | print "ignoring definition of \\" + s(buf, pp[i].data[0].data) |
| 1596 | del pp[i-1:i+2] |
| 1597 | i = i - 1 |
| 1598 | length = length - 3 |
| 1599 | |
| 1600 | elif s_buf_data == 'mbox': |
| 1601 | stuff = pp[i].data |
| 1602 | pp[i-1:i+1] = stuff |
| 1603 | i = i - 1 |
| 1604 | length = length + len(stuff) - 2 |
| 1605 | |
| 1606 | elif s_buf_data == 'version': |
| 1607 | ch.chtype = chunk_type[PLAIN] |
| 1608 | ch.data = release_version |
| 1609 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1610 | elif s_buf_data == 'item': |
| 1611 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1612 | length, newi = getoptarg(length, buf, pp, i) |
| 1613 | ingroupch = pp[i:newi] |
| 1614 | del pp[i:newi] |
| 1615 | length = length - (newi-i) |
Fred Drake | 893e5e0 | 1996-10-25 22:13:10 +0000 | [diff] [blame] | 1616 | changeit(buf, ingroupch) # catch stuff inside the optional arg |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1617 | pp.insert(i, chunk(GROUP, ch.where, ingroupch)) |
| 1618 | i, length = i+1, length+1 |
| 1619 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1620 | elif s_buf_data == 'ttindex': |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1621 | idxsi = hist.indexsubitem |
| 1622 | |
| 1623 | cat_class = '' |
| 1624 | if len(idxsi) >= 2 and idxsi[1] in \ |
| 1625 | ('method', 'function', 'protocol'): |
| 1626 | command = 'findex' |
| 1627 | elif len(idxsi) >= 2 and idxsi[1] in \ |
| 1628 | ('exception', 'object'): |
| 1629 | command = 'vindex' |
Fred Drake | 7edd8d3 | 1996-10-09 16:11:26 +0000 | [diff] [blame] | 1630 | elif len(idxsi) == 3 and idxsi[:2] == ['in', 'module']: |
| 1631 | command = 'cindex' |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1632 | else: |
Fred Drake | 7edd8d3 | 1996-10-09 16:11:26 +0000 | [diff] [blame] | 1633 | print 'WARNING: can\'t categorize ' + `idxsi` \ |
| 1634 | + ' for \'ttindex\' command' |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1635 | command = 'cindex' |
| 1636 | |
| 1637 | if not cat_class: |
| 1638 | cat_class = '('+string.join(idxsi)+')' |
| 1639 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1640 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1641 | ch.data = command |
| 1642 | |
| 1643 | length, newi = getnextarg(length, buf, pp, i) |
| 1644 | arg = pp[i:newi] |
| 1645 | del pp[i:newi] |
| 1646 | length = length - (newi-i) |
| 1647 | |
| 1648 | cat_arg = [chunk(PLAIN, ch.where, cat_class)] |
| 1649 | |
| 1650 | # determine what should be set in roman, and |
| 1651 | # what in tt-font |
| 1652 | if command in regindices: |
| 1653 | |
| 1654 | arg = [chunk(CSNAME, ch.where, 't'), |
Fred Drake | 9c7c6be | 1998-02-19 21:40:22 +0000 | [diff] [blame^] | 1655 | chunk(GROUP, ch.where, arg)] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1656 | else: |
| 1657 | cat_arg = [chunk(CSNAME, ch.where, 'r'), |
Fred Drake | 9c7c6be | 1998-02-19 21:40:22 +0000 | [diff] [blame^] | 1658 | chunk(GROUP, ch.where, cat_arg)] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1659 | |
| 1660 | ingroupch = arg + \ |
| 1661 | [chunk(PLAIN, ch.where, ' ')] + \ |
| 1662 | cat_arg |
| 1663 | |
| 1664 | pp.insert(i, chunk(GROUP, ch.where, ingroupch)) |
| 1665 | length, i = length+1, i+1 |
| 1666 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1667 | elif s_buf_data == 'ldots': |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1668 | # \ldots --> \dots{} --> @dots{} |
| 1669 | ch.data = 'dots' |
| 1670 | if i == length \ |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1671 | or pp[i].chtype != chunk_type[GROUP] \ |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1672 | or pp[i].data != []: |
| 1673 | pp.insert(i, chunk(GROUP, ch.where, [])) |
| 1674 | i, length = i+1, length+1 |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1675 | elif s_buf_data in themselves: |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1676 | # \UNIX --> &UNIX; |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1677 | ch.chtype = chunk_type[PLAIN] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1678 | if i != length \ |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1679 | and pp[i].chtype == chunk_type[GROUP] \ |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1680 | and pp[i].data == []: |
| 1681 | del pp[i] |
| 1682 | length = length-1 |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1683 | elif s_buf_data in for_texi: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1684 | pass |
| 1685 | |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1686 | elif s_buf_data == 'manpage': |
| 1687 | ch.data = 'emph' |
| 1688 | sect = s(buf, pp[i+1].data[0].data) |
| 1689 | pp[i+1].data = "(%s)" % sect |
| 1690 | pp[i+1].chtype = chunk_type[PLAIN] |
| 1691 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1692 | elif s_buf_data == 'e': |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1693 | # "\e" --> "\" |
| 1694 | ch.data = '\\' |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1695 | ch.chtype = chunk_type[PLAIN] |
| 1696 | elif s_buf_data in ('lineiii', 'lineii'): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1697 | # This is the most tricky one |
| 1698 | # \lineiii{a1}{a2}[{a3}] --> |
| 1699 | # @item @<cts. of itemargmacro>{a1} |
| 1700 | # a2 [ -- a3] |
| 1701 | # |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1702 | if not hist.inenv: |
| 1703 | raise error, 'no environment for lineiii' |
| 1704 | if (hist.inenv[0] != 'tableiii') and \ |
| 1705 | (hist.inenv[0] != 'tableii'): |
| 1706 | raise error, \ |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1707 | 'wrong command (%s) in wrong environment (%s)' \ |
| 1708 | % (s_buf_data, `hist.inenv[0]`) |
| 1709 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1710 | ch.data = 'item' |
| 1711 | length, newi = getnextarg(length, buf, pp, i) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1712 | ingroupch = [chunk(CSNAME, 0, hist.itemargmacro), |
| 1713 | chunk(GROUP, 0, pp[i:newi])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1714 | del pp[i:newi] |
| 1715 | length = length - (newi-i) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1716 | pp.insert(i, chunk(GROUP, ch.where, ingroupch)) |
| 1717 | grouppos = i |
| 1718 | i, length = i+1, length+1 |
| 1719 | length, i = getnextarg(length, buf, pp, i) |
| 1720 | length, newi = getnextarg(length, buf, pp, i) |
| 1721 | if newi > i: |
| 1722 | # we have a 3rd arg |
| 1723 | pp.insert(i, chunk(PLAIN, ch.where, ' --- ')) |
| 1724 | i = newi + 1 |
| 1725 | length = length + 1 |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1726 | if length != len(pp): |
| 1727 | raise 'IN LINEIII IS THE ERR', `i` |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1728 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1729 | elif s_buf_data in ('chapter', 'section', 'subsection', 'subsubsection'): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1730 | #\xxxsection{A} ----> |
| 1731 | # @node A, , , |
| 1732 | # @xxxsection A |
| 1733 | ## also: remove commas and quotes |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1734 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1735 | length, newi = getnextarg(length, buf, pp, i) |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1736 | afternodenamecmd = next_command_p(length, buf, |
| 1737 | pp, newi, 'nodename') |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1738 | if afternodenamecmd < 0: |
| 1739 | cp1 = crcopy(pp[i:newi]) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1740 | pp[i:newi] = [chunk(GROUP, ch.where, pp[i:newi])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1741 | length, newi = length - (newi-i) + 1, i+1 |
| 1742 | text = flattext(buf, cp1) |
| 1743 | text = invent_node_names(text) |
| 1744 | else: |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1745 | length, endarg = getnextarg(length, buf, |
| 1746 | pp, afternodenamecmd) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1747 | cp1 = crcopy(pp[afternodenamecmd:endarg]) |
| 1748 | del pp[newi:endarg] |
| 1749 | length = length - (endarg-newi) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1750 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1751 | pp[i:newi] = [chunk(GROUP, ch.where, pp[i:newi])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1752 | length, newi = length - (newi-i) + 1, i + 1 |
| 1753 | text = flattext(buf, cp1) |
| 1754 | if text[-1] == '.': |
| 1755 | text = text[:-1] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1756 | if text in hist.nodenames: |
| 1757 | print 'WARNING: node name ' + `text` + ' already used' |
| 1758 | out.doublenodes.append(text) |
| 1759 | else: |
| 1760 | hist.nodenames.append(text) |
| 1761 | text = rm_commas_etc(text) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1762 | pp[i-1:i-1] = [chunk(CSLINE, ch.where, 'node'), |
| 1763 | chunk(GROUP, ch.where, [ |
| 1764 | chunk(PLAIN, ch.where, text+', , ,') |
| 1765 | ])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1766 | i, length = newi+2, length+2 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1767 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1768 | elif s_buf_data == 'funcline': |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1769 | # fold it to a very short environment |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1770 | pp[i-1:i-1] = [chunk(CSLINE, ch.where, 'end'), |
| 1771 | chunk(GROUP, ch.where, [ |
| 1772 | chunk(PLAIN, ch.where, hist.command)])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1773 | i, length = i+2, length+2 |
| 1774 | length, i = do_funcdesc(length, buf, pp, i) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1775 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1776 | elif s_buf_data == 'dataline': |
| 1777 | pp[i-1:i-1] = [chunk(CSLINE, ch.where, 'end'), |
| 1778 | chunk(GROUP, ch.where, [ |
| 1779 | chunk(PLAIN, ch.where, hist.command)])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1780 | i, length = i+2, length+2 |
| 1781 | length, i = do_datadesc(length, buf, pp, i) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1782 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1783 | elif s_buf_data == 'excline': |
| 1784 | pp[i-1:i-1] = [chunk(CSLINE, ch.where, 'end'), |
| 1785 | chunk(GROUP, ch.where, [ |
| 1786 | chunk(PLAIN, ch.where, hist.command)])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1787 | i, length = i+2, length+2 |
| 1788 | length, i = do_excdesc(length, buf, pp, i) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1789 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1790 | elif s_buf_data == 'index': |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1791 | #\index{A} ---> |
| 1792 | # @cindex A |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1793 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1794 | ch.data = 'cindex' |
| 1795 | length, newi = getnextarg(length, buf, pp, i) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1796 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1797 | ingroupch = pp[i:newi] |
| 1798 | del pp[i:newi] |
| 1799 | length = length - (newi-i) |
| 1800 | pp.insert(i, chunk(GROUP, ch.where, ingroupch)) |
| 1801 | length, i = length+1, i+1 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1802 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1803 | elif s_buf_data == 'bifuncindex': |
| 1804 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1805 | ch.data = 'findex' |
| 1806 | length, newi = getnextarg(length, buf, pp, i) |
| 1807 | ingroupch = pp[i:newi] |
| 1808 | del pp[i:newi] |
| 1809 | length = length - (newi-i) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1810 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1811 | ingroupch.append(chunk(PLAIN, ch.where, ' ')) |
| 1812 | ingroupch.append(chunk(CSNAME, ch.where, 'r')) |
| 1813 | ingroupch.append(chunk(GROUP, ch.where, [ |
| 1814 | chunk(PLAIN, ch.where, |
| 1815 | '(built-in function)')])) |
| 1816 | |
| 1817 | pp.insert(i, chunk(GROUP, ch.where, ingroupch)) |
| 1818 | length, i = length+1, i+1 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1819 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1820 | elif s_buf_data == 'obindex': |
| 1821 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1822 | ch.data = 'findex' |
| 1823 | length, newi = getnextarg(length, buf, pp, i) |
| 1824 | ingroupch = pp[i:newi] |
| 1825 | del pp[i:newi] |
| 1826 | length = length - (newi-i) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1827 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1828 | ingroupch.append(chunk(PLAIN, ch.where, ' ')) |
| 1829 | ingroupch.append(chunk(CSNAME, ch.where, 'r')) |
| 1830 | ingroupch.append(chunk(GROUP, ch.where, [ |
| 1831 | chunk(PLAIN, ch.where, |
| 1832 | '(object)')])) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1833 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1834 | pp.insert(i, chunk(GROUP, ch.where, ingroupch)) |
| 1835 | length, i = length+1, i+1 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1836 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1837 | elif s_buf_data == 'opindex': |
| 1838 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1839 | ch.data = 'findex' |
| 1840 | length, newi = getnextarg(length, buf, pp, i) |
| 1841 | ingroupch = pp[i:newi] |
| 1842 | del pp[i:newi] |
| 1843 | length = length - (newi-i) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1844 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1845 | ingroupch.append(chunk(PLAIN, ch.where, ' ')) |
| 1846 | ingroupch.append(chunk(CSNAME, ch.where, 'r')) |
| 1847 | ingroupch.append(chunk(GROUP, ch.where, [ |
| 1848 | chunk(PLAIN, ch.where, |
| 1849 | '(operator)')])) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1850 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1851 | pp.insert(i, chunk(GROUP, ch.where, ingroupch)) |
| 1852 | length, i = length+1, i+1 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1853 | |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1854 | elif s_buf_data in ('bimodindex', 'refbimodindex'): |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1855 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1856 | ch.data = 'pindex' |
| 1857 | length, newi = getnextarg(length, buf, pp, i) |
| 1858 | ingroupch = pp[i:newi] |
| 1859 | del pp[i:newi] |
| 1860 | length = length - (newi-i) |
| 1861 | |
| 1862 | ingroupch.append(chunk(PLAIN, ch.where, ' ')) |
| 1863 | ingroupch.append(chunk(CSNAME, ch.where, 'r')) |
| 1864 | ingroupch.append(chunk(GROUP, ch.where, [ |
| 1865 | chunk(PLAIN, ch.where, |
| 1866 | '(built-in)')])) |
| 1867 | |
| 1868 | pp.insert(i, chunk(GROUP, ch.where, ingroupch)) |
| 1869 | length, i = length+1, i+1 |
| 1870 | |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1871 | elif s_buf_data == 'refmodindex': |
| 1872 | ch.chtype = chunk_type[CSLINE] |
| 1873 | ch.data = 'pindex' |
| 1874 | length, newi = getnextarg(length, buf, pp, i) |
| 1875 | ingroupch = pp[i:newi] |
| 1876 | del pp[i:newi] |
| 1877 | length = length - (newi-i) |
| 1878 | |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1879 | pp.insert(i, chunk(GROUP, ch.where, ingroupch)) |
| 1880 | length, i = length+1, i+1 |
| 1881 | |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1882 | elif s_buf_data in ('stmodindex', 'refstmodindex'): |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1883 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1884 | # use the program index as module index |
| 1885 | ch.data = 'pindex' |
| 1886 | length, newi = getnextarg(length, buf, pp, i) |
| 1887 | ingroupch = pp[i:newi] |
| 1888 | del pp[i:newi] |
| 1889 | length = length - (newi-i) |
| 1890 | |
| 1891 | ingroupch.append(chunk(PLAIN, ch.where, ' ')) |
| 1892 | ingroupch.append(chunk(CSNAME, ch.where, 'r')) |
| 1893 | ingroupch.append(chunk(GROUP, ch.where, [ |
| 1894 | chunk(PLAIN, ch.where, |
| 1895 | '(standard)')])) |
| 1896 | |
| 1897 | pp.insert(i, chunk(GROUP, ch.where, ingroupch)) |
| 1898 | length, i = length+1, i+1 |
| 1899 | |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1900 | elif s_buf_data in ('stmodindex', 'refstmodindex'): |
| 1901 | ch.chtype = chunk_type[CSLINE] |
| 1902 | # use the program index as module index |
| 1903 | ch.data = 'pindex' |
| 1904 | length, newi = getnextarg(length, buf, pp, i) |
| 1905 | ingroupch = pp[i:newi] |
| 1906 | del pp[i:newi] |
| 1907 | length = length - (newi-i) |
| 1908 | |
| 1909 | ingroupch.append(chunk(PLAIN, ch.where, ' ')) |
| 1910 | ingroupch.append(chunk(CSNAME, ch.where, 'r')) |
| 1911 | ingroupch.append(chunk(GROUP, ch.where, [ |
| 1912 | chunk(PLAIN, ch.where, |
| 1913 | '(standard)')])) |
| 1914 | |
| 1915 | pp.insert(i, chunk(GROUP, ch.where, ingroupch)) |
| 1916 | length, i = length+1, i+1 |
| 1917 | |
| 1918 | elif s_buf_data in ('stindex', 'kwindex'): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1919 | # XXX must actually go to newindex st |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1920 | what = (s_buf_data[:2] == "st") and "statement" or "keyword" |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1921 | wh = ch.where |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1922 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1923 | ch.data = 'cindex' |
| 1924 | length, newi = getnextarg(length, buf, pp, i) |
| 1925 | ingroupch = [chunk(CSNAME, wh, 'code'), |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1926 | chunk(GROUP, wh, pp[i:newi])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1927 | |
| 1928 | del pp[i:newi] |
| 1929 | length = length - (newi-i) |
| 1930 | |
| 1931 | t = ingroupch[:] |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1932 | t.append(chunk(PLAIN, wh, ' ' + what)) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1933 | |
| 1934 | pp.insert(i, chunk(GROUP, wh, t)) |
| 1935 | i, length = i+1, length+1 |
| 1936 | |
| 1937 | pp.insert(i, chunk(CSLINE, wh, 'cindex')) |
| 1938 | i, length = i+1, length+1 |
| 1939 | |
| 1940 | t = ingroupch[:] |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1941 | t.insert(0, chunk(PLAIN, wh, what + ', ')) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1942 | |
| 1943 | pp.insert(i, chunk(GROUP, wh, t)) |
| 1944 | i, length = i+1, length+1 |
| 1945 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1946 | elif s_buf_data == 'indexii': |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1947 | #\indexii{A}{B} ---> |
| 1948 | # @cindex A B |
| 1949 | # @cindex B, A |
| 1950 | length, newi = getnextarg(length, buf, pp, i) |
| 1951 | cp11 = pp[i:newi] |
| 1952 | cp21 = crcopy(pp[i:newi]) |
| 1953 | del pp[i:newi] |
| 1954 | length = length - (newi-i) |
| 1955 | length, newi = getnextarg(length, buf, pp, i) |
| 1956 | cp12 = pp[i:newi] |
| 1957 | cp22 = crcopy(pp[i:newi]) |
| 1958 | del pp[i:newi] |
| 1959 | length = length - (newi-i) |
| 1960 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1961 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1962 | ch.data = 'cindex' |
| 1963 | pp.insert(i, chunk(GROUP, ch.where, cp11 + [ |
| 1964 | chunk(PLAIN, ch.where, ' ')] + cp12)) |
| 1965 | i, length = i+1, length+1 |
| 1966 | pp[i:i] = [chunk(CSLINE, ch.where, 'cindex'), |
| 1967 | chunk(GROUP, ch.where, cp22 + [ |
| 1968 | chunk(PLAIN, ch.where, ', ')]+ cp21)] |
| 1969 | i, length = i+2, length+2 |
| 1970 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1971 | elif s_buf_data == 'indexiii': |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1972 | length, newi = getnextarg(length, buf, pp, i) |
| 1973 | cp11 = pp[i:newi] |
| 1974 | cp21 = crcopy(pp[i:newi]) |
| 1975 | cp31 = crcopy(pp[i:newi]) |
| 1976 | del pp[i:newi] |
| 1977 | length = length - (newi-i) |
| 1978 | length, newi = getnextarg(length, buf, pp, i) |
| 1979 | cp12 = pp[i:newi] |
| 1980 | cp22 = crcopy(pp[i:newi]) |
| 1981 | cp32 = crcopy(pp[i:newi]) |
| 1982 | del pp[i:newi] |
| 1983 | length = length - (newi-i) |
| 1984 | length, newi = getnextarg(length, buf, pp, i) |
| 1985 | cp13 = pp[i:newi] |
| 1986 | cp23 = crcopy(pp[i:newi]) |
| 1987 | cp33 = crcopy(pp[i:newi]) |
| 1988 | del pp[i:newi] |
| 1989 | length = length - (newi-i) |
| 1990 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1991 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1992 | ch.data = 'cindex' |
| 1993 | pp.insert(i, chunk(GROUP, ch.where, cp11 + [ |
| 1994 | chunk(PLAIN, ch.where, ' ')] + cp12 |
| 1995 | + [chunk(PLAIN, ch.where, ' ')] |
| 1996 | + cp13)) |
| 1997 | i, length = i+1, length+1 |
| 1998 | pp[i:i] = [chunk(CSLINE, ch.where, 'cindex'), |
| 1999 | chunk(GROUP, ch.where, cp22 + [ |
| 2000 | chunk(PLAIN, ch.where, ' ')]+ cp23 |
| 2001 | + [chunk(PLAIN, ch.where, ', ')] + |
| 2002 | cp21)] |
| 2003 | i, length = i+2, length+2 |
| 2004 | pp[i:i] = [chunk(CSLINE, ch.where, 'cindex'), |
| 2005 | chunk(GROUP, ch.where, cp33 + [ |
| 2006 | chunk(PLAIN, ch.where, ', ')]+ cp31 |
| 2007 | + [chunk(PLAIN, ch.where, ' ')] + |
| 2008 | cp32)] |
| 2009 | i, length = i+2, length+2 |
| 2010 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2011 | elif s_buf_data == 'indexiv': |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2012 | length, newi = getnextarg(length, buf, pp, i) |
| 2013 | cp11 = pp[i:newi] |
| 2014 | cp21 = crcopy(pp[i:newi]) |
| 2015 | cp31 = crcopy(pp[i:newi]) |
| 2016 | cp41 = crcopy(pp[i:newi]) |
| 2017 | del pp[i:newi] |
| 2018 | length = length - (newi-i) |
| 2019 | length, newi = getnextarg(length, buf, pp, i) |
| 2020 | cp12 = pp[i:newi] |
| 2021 | cp22 = crcopy(pp[i:newi]) |
| 2022 | cp32 = crcopy(pp[i:newi]) |
| 2023 | cp42 = crcopy(pp[i:newi]) |
| 2024 | del pp[i:newi] |
| 2025 | length = length - (newi-i) |
| 2026 | length, newi = getnextarg(length, buf, pp, i) |
| 2027 | cp13 = pp[i:newi] |
| 2028 | cp23 = crcopy(pp[i:newi]) |
| 2029 | cp33 = crcopy(pp[i:newi]) |
| 2030 | cp43 = crcopy(pp[i:newi]) |
| 2031 | del pp[i:newi] |
| 2032 | length = length - (newi-i) |
| 2033 | length, newi = getnextarg(length, buf, pp, i) |
| 2034 | cp14 = pp[i:newi] |
| 2035 | cp24 = crcopy(pp[i:newi]) |
| 2036 | cp34 = crcopy(pp[i:newi]) |
| 2037 | cp44 = crcopy(pp[i:newi]) |
| 2038 | del pp[i:newi] |
| 2039 | length = length - (newi-i) |
| 2040 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2041 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2042 | ch.data = 'cindex' |
| 2043 | ingroupch = cp11 + \ |
| 2044 | spacech + cp12 + \ |
| 2045 | spacech + cp13 + \ |
| 2046 | spacech + cp14 |
| 2047 | pp.insert(i, chunk(GROUP, ch.where, ingroupch)) |
| 2048 | i, length = i+1, length+1 |
| 2049 | ingroupch = cp22 + \ |
| 2050 | spacech + cp23 + \ |
| 2051 | spacech + cp24 + \ |
| 2052 | commach + cp21 |
| 2053 | pp[i:i] = cindexch + [ |
| 2054 | chunk(GROUP, ch.where, ingroupch)] |
| 2055 | i, length = i+2, length+2 |
| 2056 | ingroupch = cp33 + \ |
| 2057 | spacech + cp34 + \ |
| 2058 | commach + cp31 + \ |
| 2059 | spacech + cp32 |
| 2060 | pp[i:i] = cindexch + [ |
| 2061 | chunk(GROUP, ch.where, ingroupch)] |
| 2062 | i, length = i+2, length+2 |
| 2063 | ingroupch = cp44 + \ |
| 2064 | commach + cp41 + \ |
| 2065 | spacech + cp42 + \ |
| 2066 | spacech + cp43 |
| 2067 | pp[i:i] = cindexch + [ |
| 2068 | chunk(GROUP, ch.where, ingroupch)] |
| 2069 | i, length = i+2, length+2 |
| 2070 | |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 2071 | elif s_buf_data == 'seemodule': |
| 2072 | ch.data = "code" |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 2073 | # this is needed for just one of the input files... -sigh- |
| 2074 | while pp[i+1].chtype == chunk_type[COMMENT]: |
| 2075 | i = i + 1 |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 2076 | data = pp[i+1].data |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 2077 | oparen = chunk(PLAIN, ch.where, " (") |
| 2078 | data.insert(0, oparen) |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 2079 | data.append(chunk(PLAIN, ch.where, ")")) |
| 2080 | pp[i+1:i+2] = data |
| 2081 | length = length + len(data) - 1 |
| 2082 | |
| 2083 | elif s_buf_data == 'seetext': |
| 2084 | data = pp[i].data |
| 2085 | data.insert(0, chunk(ENDLINE, ch.where, "\n")) |
| 2086 | pp[i-1:i+1] = data |
| 2087 | i = i - 1 |
| 2088 | length = length + len(data) - 2 |
| 2089 | |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 2090 | elif s_buf_data == "quad": |
| 2091 | ch.chtype = PLAIN |
| 2092 | ch.data = " " |
| 2093 | |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 2094 | elif s_buf_data in ('noindent', 'indexsubitem', 'footnote'): |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2095 | pass |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2096 | |
Fred Drake | 9c7c6be | 1998-02-19 21:40:22 +0000 | [diff] [blame^] | 2097 | elif convertible_csname(s_buf_data): |
| 2098 | ch.data = conversion(s_buf_data) |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 2099 | |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 2100 | elif s_buf_data == 'label': |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 2101 | name = s(buf, pp[i].data[0].data) |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 2102 | del pp[i-1:i+1] |
| 2103 | length = length - 2 |
| 2104 | i = i - 1 |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 2105 | label_nodes[name] = hist.nodenames[-1] |
| 2106 | |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 2107 | elif s_buf_data == 'rfc': |
| 2108 | ch.chtype = chunk_type[PLAIN] |
| 2109 | ch.data = "RFC " + s(buf, pp[i].data[0].data) |
| 2110 | del pp[i] |
| 2111 | length = length - 1 |
| 2112 | |
| 2113 | elif s_buf_data == 'Large': |
| 2114 | del pp[i-1] |
| 2115 | i = i - 1 |
| 2116 | length = length - 1 |
| 2117 | |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 2118 | elif s_buf_data == 'ref': |
| 2119 | name = s(buf, pp[i].data[0].data) |
| 2120 | if label_nodes.has_key(name): |
| 2121 | pp[i].data[0].data = label_nodes[name] |
| 2122 | else: |
| 2123 | pp[i-1:i+1] = [ |
| 2124 | chunk(PLAIN, ch.where, |
| 2125 | "(unknown node reference: %s)" % name)] |
| 2126 | length = length - 1 |
| 2127 | print "WARNING: unknown node label", `name` |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 2128 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2129 | else: |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2130 | print "don't know what to do with keyword " + s_buf_data |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2131 | |
| 2132 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 2133 | re_atsign = regex.compile('[@{}]') |
| 2134 | re_newline = regex.compile('\n') |
| 2135 | |
| 2136 | def dumpit(buf, wm, pp): |
| 2137 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2138 | global out |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 2139 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2140 | i, length = 0, len(pp) |
| 2141 | |
| 2142 | addspace = 0 |
| 2143 | |
| 2144 | while 1: |
| 2145 | if len(pp) != length: |
| 2146 | raise 'FATAL', 'inconsistent length' |
| 2147 | if i == length: |
| 2148 | break |
| 2149 | ch = pp[i] |
| 2150 | i = i + 1 |
| 2151 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2152 | dospace = addspace |
| 2153 | addspace = 0 |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2154 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2155 | if ch.chtype == chunk_type[CSNAME]: |
| 2156 | s_buf_data = s(buf, ch.data) |
Fred Drake | 4b3f031 | 1996-12-13 22:04:31 +0000 | [diff] [blame] | 2157 | if s_buf_data == 'e': |
| 2158 | wm('\\') |
| 2159 | continue |
| 2160 | if s_buf_data == '$': |
| 2161 | wm('$') |
| 2162 | continue |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2163 | wm('@' + s_buf_data) |
| 2164 | if s_buf_data == 'node' and \ |
| 2165 | pp[i].chtype == chunk_type[PLAIN] and \ |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2166 | s(buf, pp[i].data) in out.doublenodes: |
| 2167 | ##XXX doesnt work yet?? |
| 2168 | wm(' ZZZ-' + zfill(`i`, 4)) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2169 | if s_buf_data[0] in string.letters: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2170 | addspace = 1 |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2171 | elif ch.chtype == chunk_type[PLAIN]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2172 | if dospace and s(buf, ch.data) not in (' ', '\t'): |
| 2173 | wm(' ') |
| 2174 | text = s(buf, ch.data) |
| 2175 | while 1: |
| 2176 | pos = re_atsign.search(text) |
| 2177 | if pos < 0: |
| 2178 | break |
| 2179 | wm(text[:pos] + '@' + text[pos]) |
| 2180 | text = text[pos+1:] |
| 2181 | wm(text) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2182 | elif ch.chtype == chunk_type[GROUP]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2183 | wm('{') |
| 2184 | dumpit(buf, wm, ch.data) |
| 2185 | wm('}') |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2186 | elif ch.chtype == chunk_type[DENDLINE]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2187 | wm('\n\n') |
| 2188 | while i != length and pp[i].chtype in \ |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2189 | (chunk_type[DENDLINE], chunk_type[ENDLINE]): |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 2190 | i = i + 1 |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2191 | elif ch.chtype == chunk_type[OTHER]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2192 | wm(s(buf, ch.data)) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2193 | elif ch.chtype == chunk_type[ACTIVE]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2194 | wm(s(buf, ch.data)) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2195 | elif ch.chtype == chunk_type[ENDLINE]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2196 | wm('\n') |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2197 | elif ch.chtype == chunk_type[CSLINE]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2198 | if i >= 2 and pp[i-2].chtype not in \ |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2199 | (chunk_type[ENDLINE], chunk_type[DENDLINE]) \ |
| 2200 | and (pp[i-2].chtype != chunk_type[PLAIN] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2201 | or s(buf, pp[i-2].data)[-1] != '\n'): |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 2202 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2203 | wm('\n') |
| 2204 | wm('@' + s(buf, ch.data)) |
| 2205 | if i == length: |
| 2206 | raise error, 'CSLINE expected another chunk' |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2207 | if pp[i].chtype != chunk_type[GROUP]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2208 | raise error, 'CSLINE expected GROUP' |
| 2209 | if type(pp[i].data) != ListType: |
| 2210 | raise error, 'GROUP chould contain []-data' |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 2211 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2212 | wobj = Wobj() |
| 2213 | dumpit(buf, wobj.write, pp[i].data) |
| 2214 | i = i + 1 |
| 2215 | text = wobj.data |
| 2216 | del wobj |
| 2217 | if text: |
| 2218 | wm(' ') |
| 2219 | while 1: |
| 2220 | pos = re_newline.search(text) |
| 2221 | if pos < 0: |
| 2222 | break |
| 2223 | print 'WARNING: found newline in csline arg' |
| 2224 | wm(text[:pos] + ' ') |
| 2225 | text = text[pos+1:] |
| 2226 | wm(text) |
| 2227 | if i >= length or \ |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2228 | pp[i].chtype not in (chunk_type[CSLINE], |
| 2229 | chunk_type[ENDLINE], chunk_type[DENDLINE]) \ |
| 2230 | and (pp[i].chtype != chunk_type[PLAIN] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2231 | or s(buf, pp[i].data)[0] != '\n'): |
| 2232 | wm('\n') |
Guido van Rossum | 49604d3 | 1996-09-10 22:19:51 +0000 | [diff] [blame] | 2233 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2234 | elif ch.chtype == chunk_type[COMMENT]: |
| 2235 | ## print 'COMMENT: previous chunk =', pp[i-2] |
| 2236 | ## if pp[i-2].chtype == chunk_type[PLAIN]: |
| 2237 | ## print 'PLAINTEXT =', `s(buf, pp[i-2].data)` |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2238 | if s(buf, ch.data) and \ |
| 2239 | regex.match('^[ \t]*$', s(buf, ch.data)) < 0: |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2240 | if i >= 2 \ |
| 2241 | and pp[i-2].chtype not in (chunk_type[ENDLINE], chunk_type[DENDLINE]) \ |
| 2242 | and not (pp[i-2].chtype == chunk_type[PLAIN] |
| 2243 | and regex.match('\\(.\\|\n\\)*[ \t]*\n$', s(buf, pp[i-2].data)) >= 0): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2244 | wm('\n') |
| 2245 | wm('@c ' + s(buf, ch.data)) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2246 | elif ch.chtype == chunk_type[IGNORE]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2247 | pass |
| 2248 | else: |
| 2249 | try: |
| 2250 | str = `s(buf, ch.data)` |
| 2251 | except TypeError: |
| 2252 | str = `ch.data` |
| 2253 | if len(str) > 400: |
| 2254 | str = str[:400] + '...' |
| 2255 | print 'warning:', ch.chtype, 'not handled, data ' + str |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 2256 | |
| 2257 | |
| 2258 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 2259 | def main(): |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 2260 | global release_version |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2261 | outfile = None |
| 2262 | headerfile = 'texipre.dat' |
| 2263 | trailerfile = 'texipost.dat' |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 2264 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2265 | try: |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 2266 | opts, args = getopt.getopt(sys.argv[1:], 'o:h:t:v:') |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2267 | except getopt.error: |
| 2268 | args = [] |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 2269 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2270 | if not args: |
| 2271 | print 'usage: partparse [-o outfile] [-h headerfile]', |
| 2272 | print '[-t trailerfile] file ...' |
| 2273 | sys.exit(2) |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 2274 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2275 | for opt, arg in opts: |
| 2276 | if opt == '-o': outfile = arg |
| 2277 | if opt == '-h': headerfile = arg |
| 2278 | if opt == '-t': trailerfile = arg |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 2279 | if opt == '-v': release_version = arg |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 2280 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2281 | if not outfile: |
| 2282 | root, ext = os.path.splitext(args[0]) |
| 2283 | outfile = root + '.texi' |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 2284 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2285 | if outfile in args: |
| 2286 | print 'will not overwrite input file', outfile |
| 2287 | sys.exit(2) |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 2288 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2289 | outf = open(outfile, 'w') |
| 2290 | outf.write(open(headerfile, 'r').read()) |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 2291 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2292 | for file in args: |
| 2293 | if len(args) > 1: print '='*20, file, '='*20 |
| 2294 | buf = open(file, 'r').read() |
| 2295 | w, pp = parseit(buf) |
| 2296 | startchange() |
| 2297 | changeit(buf, pp) |
| 2298 | dumpit(buf, outf.write, pp) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 2299 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2300 | outf.write(open(trailerfile, 'r').read()) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 2301 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2302 | outf.close() |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 2303 | |
Guido van Rossum | 49604d3 | 1996-09-10 22:19:51 +0000 | [diff] [blame] | 2304 | if __name__ == "__main__": |
| 2305 | main() |