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 | e87ab1f | 1998-05-19 19:37:55 +0000 | [diff] [blame] | 32 | release_version = string.split(sys.version)[0] |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 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 | |
Fred Drake | b98cd39 | 1998-03-04 06:33:43 +0000 | [diff] [blame] | 200 | __datatypes = [chunk_type[CSNAME], chunk_type[PLAIN], chunk_type[CSLINE]] |
| 201 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 202 | def __repr__(self): |
Fred Drake | b98cd39 | 1998-03-04 06:33:43 +0000 | [diff] [blame] | 203 | if self.chtype in self.__datatypes: |
| 204 | data = s(self.buf, self.data) |
| 205 | else: |
| 206 | data = self.data |
| 207 | return 'chunk' + `self.chtype, self.where, data` |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 208 | |
| 209 | # and the wrapper |
Guido van Rossum | 49604d3 | 1996-09-10 22:19:51 +0000 | [diff] [blame] | 210 | chunk = Chunk |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 211 | |
| 212 | |
| 213 | error = 'partparse.error' |
| 214 | |
| 215 | # |
| 216 | # TeX's catcodes... |
| 217 | # |
| 218 | CC_ESCAPE = 0 |
| 219 | CC_LBRACE = 1 |
| 220 | CC_RBRACE = 2 |
| 221 | CC_MATHSHIFT = 3 |
| 222 | CC_ALIGNMENT = 4 |
| 223 | CC_ENDLINE = 5 |
| 224 | CC_PARAMETER = 6 |
| 225 | CC_SUPERSCRIPT = 7 |
| 226 | CC_SUBSCRIPT = 8 |
| 227 | CC_IGNORE = 9 |
| 228 | CC_WHITE = 10 |
| 229 | CC_LETTER = 11 |
| 230 | CC_OTHER = 12 |
| 231 | CC_ACTIVE = 13 |
| 232 | CC_COMMENT = 14 |
| 233 | CC_INVALID = 15 |
| 234 | |
| 235 | # and the names |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 236 | cc_names = [ |
| 237 | 'CC_ESCAPE', |
| 238 | 'CC_LBRACE', |
| 239 | 'CC_RBRACE', |
| 240 | 'CC_MATHSHIFT', |
| 241 | 'CC_ALIGNMENT', |
| 242 | 'CC_ENDLINE', |
| 243 | 'CC_PARAMETER', |
| 244 | 'CC_SUPERSCRIPT', |
| 245 | 'CC_SUBSCRIPT', |
| 246 | 'CC_IGNORE', |
| 247 | 'CC_WHITE', |
| 248 | 'CC_LETTER', |
| 249 | 'CC_OTHER', |
| 250 | 'CC_ACTIVE', |
| 251 | 'CC_COMMENT', |
| 252 | 'CC_INVALID', |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 253 | ] |
| 254 | |
| 255 | # Show a list of catcode-name-symbols |
| 256 | def pcl(codelist): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 257 | result = '' |
| 258 | for i in codelist: |
| 259 | result = result + cc_names[i] + ', ' |
| 260 | return '[' + result[:-2] + ']' |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 261 | |
| 262 | # the name of the catcode (ACTIVE, OTHER, etc.) |
| 263 | def pc(code): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 264 | return cc_names[code] |
| 265 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 266 | |
| 267 | # Which catcodes make the parser stop parsing regular plaintext |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 268 | regular_stopcodes = [CC_ESCAPE, CC_LBRACE, CC_RBRACE, CC_MATHSHIFT, |
| 269 | CC_ALIGNMENT, CC_PARAMETER, CC_SUPERSCRIPT, CC_SUBSCRIPT, |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 270 | CC_IGNORE, CC_ACTIVE, CC_COMMENT, CC_INVALID, CC_ENDLINE] |
| 271 | |
| 272 | # same for scanning a control sequence name |
| 273 | csname_scancodes = [CC_LETTER] |
| 274 | |
| 275 | # same for gobbling LWSP |
| 276 | white_scancodes = [CC_WHITE] |
| 277 | ##white_scancodes = [CC_WHITE, CC_ENDLINE] |
| 278 | |
| 279 | # make a list of all catcode id's, except for catcode ``other'' |
| 280 | all_but_other_codes = range(16) |
| 281 | del all_but_other_codes[CC_OTHER] |
| 282 | ##print all_but_other_codes |
| 283 | |
| 284 | # when does a comment end |
| 285 | comment_stopcodes = [CC_ENDLINE] |
| 286 | |
| 287 | # gather all characters together, specified by a list of catcodes |
| 288 | def code2string(cc, codelist): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 289 | ##print 'code2string: codelist = ' + pcl(codelist), |
| 290 | result = '' |
| 291 | for category in codelist: |
| 292 | if cc[category]: |
| 293 | result = result + cc[category] |
| 294 | ##print 'result = ' + `result` |
| 295 | return result |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 296 | |
| 297 | # automatically generate all characters of catcode other, being the |
| 298 | # complement set in the ASCII range (128 characters) |
| 299 | def make_other_codes(cc): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 300 | otherchars = range(256) # could be made 256, no problem |
| 301 | for category in all_but_other_codes: |
| 302 | if cc[category]: |
| 303 | for c in cc[category]: |
| 304 | otherchars[ord(c)] = None |
| 305 | result = '' |
| 306 | for i in otherchars: |
| 307 | if i != None: |
| 308 | result = result + chr(i) |
| 309 | return result |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 310 | |
| 311 | # catcode dump (which characters have which catcodes). |
| 312 | def dump_cc(name, cc): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 313 | ##print '\t' + name |
| 314 | ##print '=' * (8+len(name)) |
| 315 | if len(cc) != 16: |
| 316 | raise TypeError, 'cc not good cat class' |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 317 | ## for i in range(16): |
| 318 | ## print pc(i) + '\t' + `cc[i]` |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 319 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 320 | |
| 321 | # In the beginning,.... |
| 322 | epoch_cc = [None] * 16 |
| 323 | ##dump_cc('epoch_cc', epoch_cc) |
| 324 | |
| 325 | |
| 326 | # INITEX |
| 327 | initex_cc = epoch_cc[:] |
| 328 | initex_cc[CC_ESCAPE] = '\\' |
| 329 | initex_cc[CC_ENDLINE], initex_cc[CC_IGNORE], initex_cc[CC_WHITE] = \ |
| 330 | '\n', '\0', ' ' |
| 331 | initex_cc[CC_LETTER] = string.uppercase + string.lowercase |
| 332 | initex_cc[CC_COMMENT], initex_cc[CC_INVALID] = '%', '\x7F' |
| 333 | #initex_cc[CC_OTHER] = make_other_codes(initex_cc) I don't need them, anyway |
| 334 | ##dump_cc('initex_cc', initex_cc) |
| 335 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 336 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 337 | # LPLAIN: LaTeX catcode setting (see lplain.tex) |
| 338 | lplain_cc = initex_cc[:] |
| 339 | lplain_cc[CC_LBRACE], lplain_cc[CC_RBRACE] = '{', '}' |
| 340 | lplain_cc[CC_MATHSHIFT] = '$' |
| 341 | lplain_cc[CC_ALIGNMENT] = '&' |
| 342 | lplain_cc[CC_PARAMETER] = '#' |
| 343 | lplain_cc[CC_SUPERSCRIPT] = '^\x0B' # '^' and C-k |
| 344 | lplain_cc[CC_SUBSCRIPT] = '_\x01' # '_' and C-a |
| 345 | lplain_cc[CC_WHITE] = lplain_cc[CC_WHITE] + '\t' |
| 346 | lplain_cc[CC_ACTIVE] = '~\x0C' # '~' and C-l |
| 347 | lplain_cc[CC_OTHER] = make_other_codes(lplain_cc) |
| 348 | ##dump_cc('lplain_cc', lplain_cc) |
| 349 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 350 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 351 | # Guido's LaTeX environment catcoded '_' as ``other'' |
| 352 | # my own purpose catlist |
| 353 | my_cc = lplain_cc[:] |
| 354 | my_cc[CC_SUBSCRIPT] = my_cc[CC_SUBSCRIPT][1:] # remove '_' here |
| 355 | my_cc[CC_OTHER] = my_cc[CC_OTHER] + '_' # add it to OTHER list |
| 356 | dump_cc('my_cc', my_cc) |
| 357 | |
| 358 | |
| 359 | |
| 360 | # needed for un_re, my equivalent for regexp-quote in Emacs |
| 361 | re_meaning = '\\[]^$' |
| 362 | |
| 363 | def un_re(str): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 364 | result = '' |
| 365 | for i in str: |
| 366 | if i in re_meaning: |
| 367 | result = result + '\\' |
| 368 | result = result + i |
| 369 | return result |
| 370 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 371 | # NOTE the negate ('^') operator in *some* of the regexps below |
| 372 | def make_rc_regular(cc): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 373 | # problems here if '[]' are included!! |
| 374 | return regex.compile('[' + code2string(cc, regular_stopcodes) + ']') |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 375 | |
| 376 | def make_rc_cs_scan(cc): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 377 | return regex.compile('[^' + code2string(cc, csname_scancodes) + ']') |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 378 | |
| 379 | def make_rc_comment(cc): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 380 | return regex.compile('[' + code2string(cc, comment_stopcodes) + ']') |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 381 | |
| 382 | def make_rc_endwhite(cc): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 383 | return regex.compile('[^' + code2string(cc, white_scancodes) + ']') |
| 384 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 385 | |
| 386 | |
| 387 | # regular: normal mode: |
| 388 | rc_regular = make_rc_regular(my_cc) |
| 389 | |
| 390 | # scan: scan a command sequence e.g. `newlength' or `mbox' or `;', `,' or `$' |
| 391 | rc_cs_scan = make_rc_cs_scan(my_cc) |
| 392 | rc_comment = make_rc_comment(my_cc) |
| 393 | rc_endwhite = make_rc_endwhite(my_cc) |
| 394 | |
| 395 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 396 | # parseit (BUF, PARSEMODE=mode[MODE_REGULAR], START=0, RECURSION-LEVEL=0) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 397 | # RECURSION-LEVEL will is incremented on entry. |
| 398 | # result contains the list of chunks returned |
| 399 | # together with this list, the buffer position is returned |
| 400 | |
| 401 | # RECURSION-LEVEL will be set to zero *again*, when recursively a |
| 402 | # {,D}MATH-mode scan has been enetered. |
| 403 | # This has been done in order to better check for environment-mismatches |
| 404 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 405 | def parseit(buf, parsemode=mode[MODE_REGULAR], start=0, lvl=0): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 406 | global lineno |
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 | result = [] |
| 409 | end = len(buf) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 410 | if lvl == 0 and parsemode == mode[MODE_REGULAR]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 411 | lineno = 1 |
| 412 | lvl = lvl + 1 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 413 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 414 | ##print 'parseit(' + epsilon(buf, start) + ', ' + `parsemode` + ', ' + `start` + ', ' + `lvl` + ')' |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 415 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 416 | # |
| 417 | # some of the more regular modes... |
| 418 | # |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 419 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 420 | 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] | 421 | cstate = [] |
| 422 | newpos = start |
| 423 | curpmode = parsemode |
| 424 | while 1: |
| 425 | where = newpos |
| 426 | #print '\tnew round: ' + epsilon(buf, where) |
| 427 | if where == end: |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 428 | if lvl > 1 or curpmode != mode[MODE_REGULAR]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 429 | # not the way we started... |
| 430 | raise EOFError, 'premature end of file.' + lle(lvl, buf, where) |
| 431 | # the real ending of lvl-1 parse |
| 432 | return end, result |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 433 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 434 | pos = rc_regular.search(buf, where) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 435 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 436 | if pos < 0: |
| 437 | pos = end |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 438 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 439 | if pos != where: |
| 440 | newpos, c = pos, chunk(PLAIN, where, (where, pos)) |
| 441 | result.append(c) |
| 442 | continue |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 443 | |
| 444 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 445 | # |
| 446 | # ok, pos == where and pos != end |
| 447 | # |
| 448 | foundchar = buf[where] |
| 449 | if foundchar in my_cc[CC_LBRACE]: |
| 450 | # recursive subgroup parse... |
| 451 | newpos, data = parseit(buf, curpmode, where+1, lvl) |
| 452 | result.append(chunk(GROUP, where, data)) |
| 453 | |
| 454 | elif foundchar in my_cc[CC_RBRACE]: |
| 455 | if lvl <= 1: |
| 456 | raise error, 'ENDGROUP while in base level.' + lle(lvl, buf, where) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 457 | if lvl == 1 and mode != mode[MODE_REGULAR]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 458 | raise error, 'endgroup while in math mode. +lin() + epsilon(buf, where)' |
| 459 | return where + 1, result |
| 460 | |
| 461 | elif foundchar in my_cc[CC_ESCAPE]: |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 462 | # |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 463 | # call the routine that actually deals with |
| 464 | # this problem. If do_ret is None, than |
| 465 | # return the value of do_ret |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 466 | # |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 467 | # Note that handle_cs might call this routine |
| 468 | # recursively again... |
| 469 | # |
| 470 | do_ret, newpos = handlecs(buf, where, |
| 471 | curpmode, lvl, result, end) |
| 472 | if do_ret != None: |
| 473 | return do_ret |
| 474 | |
| 475 | elif foundchar in my_cc[CC_COMMENT]: |
| 476 | newpos, data = parseit(buf, |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 477 | mode[MODE_COMMENT], where+1, lvl) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 478 | result.append(chunk(COMMENT, where, data)) |
| 479 | |
| 480 | elif foundchar in my_cc[CC_MATHSHIFT]: |
| 481 | # note that recursive calls to math-mode |
| 482 | # scanning are called with recursion-level 0 |
| 483 | # again, in order to check for bad mathend |
| 484 | # |
| 485 | if where + 1 != end and buf[where + 1] in my_cc[CC_MATHSHIFT]: |
| 486 | # |
| 487 | # double mathshift, e.g. '$$' |
| 488 | # |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 489 | if curpmode == mode[MODE_REGULAR]: |
| 490 | newpos, data = parseit(buf, mode[MODE_DMATH], |
| 491 | where + 2, 0) |
| 492 | result.append(chunk(DMATH, where, data)) |
| 493 | elif curpmode == mode[MODE_MATH]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 494 | raise error, 'wrong math delimiiter' + lin() + epsilon(buf, where) |
| 495 | elif lvl != 1: |
| 496 | raise error, 'bad mathend.' + lle(lvl, buf, where) |
| 497 | else: |
| 498 | return where + 2, result |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 499 | else: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 500 | # |
| 501 | # single math shift, e.g. '$' |
| 502 | # |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 503 | if curpmode == mode[MODE_REGULAR]: |
| 504 | newpos, data = parseit(buf, mode[MODE_MATH], |
| 505 | where + 1, 0) |
| 506 | result.append(chunk(MATH, where, data)) |
| 507 | elif curpmode == mode[MODE_DMATH]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 508 | raise error, 'wrong math delimiiter' + lin() + epsilon(buf, where) |
| 509 | elif lvl != 1: |
| 510 | raise error, 'bad mathend.' + lv(lvl, buf, where) |
| 511 | else: |
| 512 | return where + 1, result |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 513 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 514 | elif foundchar in my_cc[CC_IGNORE]: |
| 515 | print 'warning: ignored char', `foundchar` |
| 516 | newpos = where + 1 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 517 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 518 | elif foundchar in my_cc[CC_ACTIVE]: |
| 519 | result.append(chunk(ACTIVE, where, foundchar)) |
| 520 | newpos = where + 1 |
| 521 | |
| 522 | elif foundchar in my_cc[CC_INVALID]: |
| 523 | raise error, 'invalid char ' + `foundchar` |
| 524 | newpos = where + 1 |
| 525 | |
| 526 | elif foundchar in my_cc[CC_ENDLINE]: |
| 527 | # |
| 528 | # after an end of line, eat the rest of |
| 529 | # whitespace on the beginning of the next line |
| 530 | # this is what LaTeX more or less does |
| 531 | # |
| 532 | # also, try to indicate double newlines (\par) |
| 533 | # |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 534 | lineno = lineno + 1 |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 535 | savedwhere = where |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 536 | newpos, dummy = parseit(buf, mode[MODE_GOBBLEWHITE], where + 1, lvl) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 537 | if newpos != end and buf[newpos] in my_cc[CC_ENDLINE]: |
| 538 | result.append(chunk(DENDLINE, savedwhere, foundchar)) |
| 539 | else: |
| 540 | result.append(chunk(ENDLINE, savedwhere, foundchar)) |
| 541 | else: |
| 542 | result.append(chunk(OTHER, where, foundchar)) |
| 543 | newpos = where + 1 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 544 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 545 | elif parsemode == mode[MODE_CS_SCAN]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 546 | # |
| 547 | # scan for a control sequence token. `\ape', `\nut' or `\%' |
| 548 | # |
| 549 | if start == end: |
| 550 | raise EOFError, 'can\'t find end of csname' |
| 551 | pos = rc_cs_scan.search(buf, start) |
| 552 | if pos < 0: |
| 553 | pos = end |
| 554 | if pos == start: |
| 555 | # first non-letter right where we started the search |
| 556 | # ---> the control sequence name consists of one single |
| 557 | # character. Also: don't eat white space... |
| 558 | if buf[pos] in my_cc[CC_ENDLINE]: |
| 559 | lineno = lineno + 1 |
| 560 | pos = pos + 1 |
| 561 | return pos, (start, pos) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 562 | else: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 563 | spos = pos |
| 564 | if buf[pos] == '\n': |
| 565 | lineno = lineno + 1 |
| 566 | spos = pos + 1 |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 567 | pos2, dummy = parseit(buf, mode[MODE_GOBBLEWHITE], spos, lvl) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 568 | return pos2, (start, pos) |
| 569 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 570 | elif parsemode == mode[MODE_GOBBLEWHITE]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 571 | if start == end: |
| 572 | return start, '' |
| 573 | pos = rc_endwhite.search(buf, start) |
| 574 | if pos < 0: |
| 575 | pos = start |
| 576 | return pos, (start, pos) |
| 577 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 578 | elif parsemode == mode[MODE_COMMENT]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 579 | pos = rc_comment.search(buf, start) |
| 580 | lineno = lineno + 1 |
| 581 | if pos < 0: |
| 582 | print 'no newline perhaps?' |
| 583 | raise EOFError, 'can\'t find end of comment' |
| 584 | pos = pos + 1 |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 585 | pos2, dummy = parseit(buf, mode[MODE_GOBBLEWHITE], pos, lvl) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 586 | return pos2, (start, pos) |
| 587 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 588 | else: |
| 589 | raise error, 'Unknown mode (' + `parsemode` + ')' |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 590 | |
| 591 | |
| 592 | #moreresult = cswitch(buf[x1:x2], buf, newpos, parsemode, lvl) |
| 593 | |
| 594 | #boxcommands = 'mbox', 'fbox' |
| 595 | #defcommands = 'def', 'newcommand' |
| 596 | |
| 597 | endverbstr = '\\end{verbatim}' |
| 598 | |
| 599 | re_endverb = regex.compile(un_re(endverbstr)) |
| 600 | |
| 601 | # |
| 602 | # handlecs: helper function for parseit, for the special thing we might |
| 603 | # wanna do after certain command control sequences |
| 604 | # returns: None or return_data, newpos |
| 605 | # |
| 606 | # in the latter case, the calling function is instructed to immediately |
| 607 | # return with the data in return_data |
| 608 | # |
| 609 | def handlecs(buf, where, curpmode, lvl, result, end): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 610 | global lineno |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 611 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 612 | # get the control sequence name... |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 613 | newpos, data = parseit(buf, mode[MODE_CS_SCAN], where+1, lvl) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 614 | saveddata = data |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 615 | s_buf_data = s(buf, data) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 616 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 617 | if s_buf_data in ('begin', 'end'): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 618 | # skip the expected '{' and get the LaTeX-envname '}' |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 619 | newpos, data = parseit(buf, mode[MODE_REGULAR], newpos+1, lvl) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 620 | if len(data) != 1: |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 621 | raise error, 'expected 1 chunk of data.' + lle(lvl, buf, where) |
Guido van Rossum | 49604d3 | 1996-09-10 22:19:51 +0000 | [diff] [blame] | 622 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 623 | # yucky, we've got an environment |
| 624 | envname = s(buf, data[0].data) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 625 | s_buf_saveddata = s(buf, saveddata) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 626 | ##print 'FOUND ' + s(buf, saveddata) + '. Name ' + `envname` + '.' + lv(lvl) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 627 | if s_buf_saveddata == 'begin' and envname == 'verbatim': |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 628 | # verbatim deserves special treatment |
| 629 | pos = re_endverb.search(buf, newpos) |
| 630 | if pos < 0: |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 631 | raise error, "%s not found.%s" \ |
| 632 | % (`endverbstr`, lle(lvl, buf, where)) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 633 | result.append(chunk(ENV, where, (envname, [chunk(PLAIN, newpos, (newpos, pos))]))) |
| 634 | newpos = pos + len(endverbstr) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 635 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 636 | elif s_buf_saveddata == 'begin': |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 637 | # start parsing recursively... If that parse returns |
| 638 | # from an '\end{...}', then should the last item of |
| 639 | # the returned data be a string containing the ended |
| 640 | # environment |
| 641 | newpos, data = parseit(buf, curpmode, newpos, lvl) |
| 642 | if not data or type(data[-1]) is not StringType: |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 643 | raise error, "missing 'end'" + lle(lvl, buf, where) \ |
| 644 | + epsilon(buf, newpos) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 645 | retenv = data[-1] |
| 646 | del data[-1] |
| 647 | if retenv != envname: |
| 648 | #[`retenv`, `envname`] |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 649 | raise error, 'environments do not match.%s%s' \ |
| 650 | % (lle(lvl, buf, where), epsilon(buf, newpos)) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 651 | result.append(chunk(ENV, where, (retenv, data))) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 652 | else: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 653 | # 'end'... append the environment name, as just |
| 654 | # pointed out, and order parsit to return... |
| 655 | result.append(envname) |
| 656 | ##print 'POINT of return: ' + epsilon(buf, newpos) |
| 657 | # the tuple will be returned by parseit |
| 658 | return (newpos, result), newpos |
| 659 | |
| 660 | # end of \begin ... \end handling |
| 661 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 662 | elif s_buf_data[0:2] == 'if': |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 663 | # another scary monster: the 'if' directive |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 664 | flag = s_buf_data[2:] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 665 | |
| 666 | # recursively call parseit, just like environment above.. |
| 667 | # the last item of data should contain the if-termination |
| 668 | # e.g., 'else' of 'fi' |
| 669 | newpos, data = parseit(buf, curpmode, newpos, lvl) |
| 670 | if not data or data[-1] not in ('else', 'fi'): |
| 671 | raise error, 'wrong if... termination' + \ |
| 672 | lle(lvl, buf, where) + epsilon(buf, newpos) |
| 673 | |
| 674 | ifterm = data[-1] |
| 675 | del data[-1] |
| 676 | # 0 means dont_negate flag |
| 677 | result.append(chunk(IF, where, (flag, 0, data))) |
| 678 | if ifterm == 'else': |
| 679 | # do the whole thing again, there is only one way |
| 680 | # to end this one, by 'fi' |
| 681 | newpos, data = parseit(buf, curpmode, newpos, lvl) |
| 682 | if not data or data[-1] not in ('fi', ): |
| 683 | raise error, 'wrong if...else... termination' \ |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 684 | + lle(lvl, buf, where) \ |
| 685 | + epsilon(buf, newpos) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 686 | |
| 687 | ifterm = data[-1] |
| 688 | del data[-1] |
| 689 | result.append(chunk(IF, where, (flag, 1, data))) |
| 690 | #done implicitely: return None, newpos |
| 691 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 692 | elif s_buf_data in ('else', 'fi'): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 693 | result.append(s(buf, data)) |
| 694 | # order calling party to return tuple |
| 695 | return (newpos, result), newpos |
| 696 | |
| 697 | # end of \if, \else, ... \fi handling |
| 698 | |
| 699 | elif s(buf, saveddata) == 'verb': |
| 700 | x2 = saveddata[1] |
| 701 | result.append(chunk(CSNAME, where, data)) |
| 702 | if x2 == end: |
| 703 | raise error, 'premature end of command.' + lle(lvl, buf, where) |
| 704 | delimchar = buf[x2] |
| 705 | ##print 'VERB: delimchar ' + `delimchar` |
| 706 | pos = regex.compile(un_re(delimchar)).search(buf, x2 + 1) |
| 707 | if pos < 0: |
| 708 | raise error, 'end of \'verb\' argument (' + \ |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 709 | `delimchar` + ') not found.' + \ |
| 710 | lle(lvl, buf, where) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 711 | result.append(chunk(GROUP, x2, [chunk(PLAIN, x2+1, (x2+1, pos))])) |
| 712 | newpos = pos + 1 |
| 713 | else: |
| 714 | result.append(chunk(CSNAME, where, data)) |
| 715 | return None, newpos |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 716 | |
| 717 | # this is just a function to get the string value if the possible data-tuple |
| 718 | def s(buf, data): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 719 | if type(data) is StringType: |
| 720 | return data |
| 721 | if len(data) != 2 or not (type(data[0]) is type(data[1]) is IntType): |
| 722 | raise TypeError, 'expected tuple of 2 integers' |
| 723 | x1, x2 = data |
| 724 | return buf[x1:x2] |
Guido van Rossum | 49604d3 | 1996-09-10 22:19:51 +0000 | [diff] [blame] | 725 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 726 | |
| 727 | ##length, data1, i = getnextarg(length, buf, pp, i + 1) |
| 728 | |
| 729 | # make a deep-copy of some chunks |
| 730 | def crcopy(r): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 731 | return map(chunkcopy, r) |
Guido van Rossum | 49604d3 | 1996-09-10 22:19:51 +0000 | [diff] [blame] | 732 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 733 | |
| 734 | # copy a chunk, would better be a method of class Chunk... |
| 735 | def chunkcopy(ch): |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 736 | if ch.chtype == chunk_type[GROUP]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 737 | return chunk(GROUP, ch.where, map(chunkcopy, ch.data)) |
| 738 | else: |
| 739 | return chunk(ch.chtype, ch.where, ch.data) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 740 | |
| 741 | |
| 742 | # get next argument for TeX-macro, flatten a group (insert between) |
| 743 | # or return Command Sequence token, or give back one character |
| 744 | def getnextarg(length, buf, pp, item): |
| 745 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 746 | ##wobj = Wobj() |
| 747 | ##dumpit(buf, wobj.write, pp[item:min(length, item + 5)]) |
| 748 | ##print 'GETNEXTARG, (len, item) =', `length, item` + ' ---> ' + wobj.data + ' <---' |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 749 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 750 | while item < length and pp[item].chtype == chunk_type[ENDLINE]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 751 | del pp[item] |
| 752 | length = length - 1 |
| 753 | if item >= length: |
| 754 | raise error, 'no next arg.' + epsilon(buf, pp[-1].where) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 755 | if pp[item].chtype == chunk_type[GROUP]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 756 | newpp = pp[item].data |
| 757 | del pp[item] |
| 758 | length = length - 1 |
| 759 | changeit(buf, newpp) |
| 760 | length = length + len(newpp) |
| 761 | pp[item:item] = newpp |
| 762 | item = item + len(newpp) |
| 763 | if len(newpp) < 10: |
| 764 | wobj = Wobj() |
| 765 | dumpit(buf, wobj.write, newpp) |
| 766 | ##print 'GETNEXTARG: inserted ' + `wobj.data` |
| 767 | return length, item |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 768 | elif pp[item].chtype == chunk_type[PLAIN]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 769 | #grab one char |
| 770 | print 'WARNING: grabbing one char' |
| 771 | if len(s(buf, pp[item].data)) > 1: |
| 772 | pp.insert(item, chunk(PLAIN, pp[item].where, s(buf, pp[item].data)[:1])) |
| 773 | item, length = item+1, length+1 |
| 774 | pp[item].data = s(buf, pp[item].data)[1:] |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 775 | else: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 776 | item = item+1 |
| 777 | return length, item |
| 778 | else: |
| 779 | ch = pp[item] |
| 780 | try: |
| 781 | str = `s(buf, ch.data)` |
| 782 | except TypeError: |
| 783 | str = `ch.data` |
| 784 | if len(str) > 400: |
| 785 | str = str[:400] + '...' |
| 786 | print 'GETNEXTARG:', ch.chtype, 'not handled, data ' + str |
| 787 | return length, item |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 788 | |
| 789 | |
| 790 | # this one is needed to find the end of LaTeX's optional argument, like |
| 791 | # item[...] |
| 792 | re_endopt = regex.compile(']') |
| 793 | |
| 794 | # get a LaTeX-optional argument, you know, the square braces '[' and ']' |
| 795 | def getoptarg(length, buf, pp, item): |
| 796 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 797 | wobj = Wobj() |
| 798 | dumpit(buf, wobj.write, pp[item:min(length, item + 5)]) |
| 799 | ##print 'GETOPTARG, (len, item) =', `length, item` + ' ---> ' + wobj.data + ' <---' |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 800 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 801 | if item >= length or \ |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 802 | pp[item].chtype != chunk_type[PLAIN] or \ |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 803 | s(buf, pp[item].data)[0] != '[': |
| 804 | return length, item |
| 805 | |
| 806 | pp[item].data = s(buf, pp[item].data)[1:] |
| 807 | if len(pp[item].data) == 0: |
| 808 | del pp[item] |
| 809 | length = length-1 |
| 810 | |
| 811 | while 1: |
| 812 | if item == length: |
| 813 | raise error, 'No end of optional arg found' |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 814 | if pp[item].chtype == chunk_type[PLAIN]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 815 | text = s(buf, pp[item].data) |
| 816 | pos = re_endopt.search(text) |
| 817 | if pos >= 0: |
| 818 | pp[item].data = text[:pos] |
| 819 | if pos == 0: |
| 820 | del pp[item] |
| 821 | length = length-1 |
| 822 | else: |
| 823 | item=item+1 |
| 824 | text = text[pos+1:] |
| 825 | |
| 826 | while text and text[0] in ' \t': |
| 827 | text = text[1:] |
| 828 | |
| 829 | if text: |
| 830 | pp.insert(item, chunk(PLAIN, 0, text)) |
| 831 | length = length + 1 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 832 | return length, item |
| 833 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 834 | item = item+1 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 835 | |
| 836 | |
| 837 | # Wobj just add write-requests to the ``data'' attribute |
| 838 | class Wobj: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 839 | data = '' |
Guido van Rossum | 49604d3 | 1996-09-10 22:19:51 +0000 | [diff] [blame] | 840 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 841 | def write(self, data): |
| 842 | self.data = self.data + data |
Guido van Rossum | b819bdf | 1995-03-15 11:26:26 +0000 | [diff] [blame] | 843 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 844 | # ignore these commands |
Fred Drake | 74a11e5 | 1998-02-26 05:52:37 +0000 | [diff] [blame] | 845 | ignoredcommands = ('hline', 'small', '/', 'tableofcontents', 'Large') |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 846 | # map commands like these to themselves as plaintext |
Fred Drake | 74a11e5 | 1998-02-26 05:52:37 +0000 | [diff] [blame] | 847 | wordsselves = ('UNIX', 'ABC', 'C', 'ASCII', 'EOF', 'LaTeX', 'POSIX', 'TeX', |
| 848 | 'SliTeX') |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 849 | # \{ --> {, \} --> }, etc |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 850 | themselves = ('{', '}', ',', '.', '@', ' ', '\n') + wordsselves |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 851 | # these ones also themselves (see argargs macro in myformat.sty) |
| 852 | inargsselves = (',', '[', ']', '(', ')') |
| 853 | # this is how *I* would show the difference between emph and strong |
| 854 | # code 1 means: fold to uppercase |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 855 | markcmds = {'code': ('', ''), 'var': 1, 'emph': ('_', '_'), |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 856 | 'strong': ('*', '*')} |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 857 | |
| 858 | # recognise patter {\FONTCHANGE-CMD TEXT} to \MAPPED-FC-CMD{TEXT} |
| 859 | fontchanges = {'rm': 'r', 'it': 'i', 'em': 'emph', 'bf': 'b', 'tt': 't'} |
| 860 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 861 | |
| 862 | # try to remove macros and return flat text |
| 863 | def flattext(buf, pp): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 864 | pp = crcopy(pp) |
| 865 | ##print '---> FLATTEXT ' + `pp` |
| 866 | wobj = Wobj() |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 867 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 868 | i, length = 0, len(pp) |
| 869 | while 1: |
| 870 | if len(pp) != length: |
| 871 | raise 'FATAL', 'inconsistent length' |
| 872 | if i >= length: |
| 873 | break |
| 874 | ch = pp[i] |
| 875 | i = i+1 |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 876 | if ch.chtype == chunk_type[PLAIN]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 877 | pass |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 878 | elif ch.chtype == chunk_type[CSNAME]: |
| 879 | s_buf_data = s(buf, ch.data) |
Fred Drake | 74a11e5 | 1998-02-26 05:52:37 +0000 | [diff] [blame] | 880 | if convertible_csname(s_buf_data): |
| 881 | ch.chtype, ch.data, nix = conversion(s_buf_data) |
| 882 | if hist.inargs and s_buf_data in inargsselves: |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 883 | ch.chtype = chunk_type[PLAIN] |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 884 | elif len(s_buf_data) == 1 \ |
| 885 | and s_buf_data in onlylatexspecial: |
| 886 | ch.chtype = chunk_type[PLAIN] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 887 | # if it is followed by an empty group, |
| 888 | # remove that group, it was needed for |
| 889 | # a true space |
| 890 | if i < length \ |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 891 | and pp[i].chtype==chunk_type[GROUP] \ |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 892 | and len(pp[i].data) == 0: |
| 893 | del pp[i] |
| 894 | length = length-1 |
| 895 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 896 | elif s_buf_data in markcmds.keys(): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 897 | length, newi = getnextarg(length, buf, pp, i) |
| 898 | str = flattext(buf, pp[i:newi]) |
| 899 | del pp[i:newi] |
| 900 | length = length - (newi - i) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 901 | ch.chtype = chunk_type[PLAIN] |
| 902 | markcmd = s_buf_data |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 903 | x = markcmds[markcmd] |
| 904 | if type(x) == TupleType: |
| 905 | pre, after = x |
| 906 | str = pre+str+after |
| 907 | elif x == 1: |
| 908 | str = string.upper(str) |
| 909 | else: |
| 910 | raise 'FATAL', 'corrupt markcmds' |
| 911 | ch.data = str |
| 912 | else: |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 913 | if s_buf_data not in ignoredcommands: |
| 914 | print 'WARNING: deleting command ' + s_buf_data |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 915 | print 'PP' + `pp[i-1]` |
| 916 | del pp[i-1] |
| 917 | i, length = i-1, length-1 |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 918 | elif ch.chtype == chunk_type[GROUP]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 919 | length, newi = getnextarg(length, buf, pp, i-1) |
| 920 | i = i-1 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 921 | ## str = flattext(buf, crcopy(pp[i-1:newi])) |
| 922 | ## del pp[i:newi] |
| 923 | ## length = length - (newi - i) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 924 | ## ch.chtype = chunk_type[PLAIN] |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 925 | ## ch.data = str |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 926 | else: |
| 927 | pass |
| 928 | |
| 929 | dumpit(buf, wobj.write, pp) |
| 930 | ##print 'FLATTEXT: RETURNING ' + `wobj.data` |
| 931 | return wobj.data |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 932 | |
| 933 | # try to generate node names (a bit shorter than the chapter title) |
| 934 | # note that the \nodename command (see elsewhere) overules these efforts |
| 935 | def invent_node_names(text): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 936 | words = string.split(text) |
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 | ##print 'WORDS ' + `words` |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 939 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 940 | if len(words) == 2 \ |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 941 | and string.lower(words[0]) == 'built-in' \ |
| 942 | and string.lower(words[1]) not in ('modules', 'functions'): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 943 | return words[1] |
| 944 | if len(words) == 3 and string.lower(words[1]) == 'module': |
| 945 | return words[2] |
| 946 | if len(words) == 3 and string.lower(words[1]) == 'object': |
| 947 | return string.join(words[0:2]) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 948 | if len(words) > 4 \ |
| 949 | and (string.lower(string.join(words[-4:])) \ |
| 950 | == 'methods and data attributes'): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 951 | return string.join(words[:2]) |
| 952 | return text |
| 953 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 954 | re_commas_etc = regex.compile('[,`\'@{}]') |
| 955 | |
| 956 | re_whitespace = regex.compile('[ \t]*') |
| 957 | |
| 958 | |
| 959 | ##nodenamecmd = next_command_p(length, buf, pp, newi, 'nodename') |
| 960 | |
| 961 | # look if the next non-white stuff is also a command, resulting in skipping |
| 962 | # double endlines (DENDLINE) too, and thus omitting \par's |
| 963 | # Sometimes this is too much, maybe consider DENDLINE's as stop |
| 964 | def next_command_p(length, buf, pp, i, cmdname): |
| 965 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 966 | while 1: |
| 967 | if i >= len(pp): |
| 968 | break |
| 969 | ch = pp[i] |
| 970 | i = i+1 |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 971 | if ch.chtype == chunk_type[ENDLINE]: |
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[DENDLINE]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 974 | continue |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 975 | if ch.chtype == chunk_type[PLAIN]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 976 | if re_whitespace.search(s(buf, ch.data)) == 0 and \ |
| 977 | re_whitespace.match(s(buf, ch.data)) == len(s(buf, ch.data)): |
| 978 | continue |
| 979 | return -1 |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 980 | if ch.chtype == chunk_type[CSNAME]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 981 | if s(buf, ch.data) == cmdname: |
| 982 | return i # _after_ the command |
| 983 | return -1 |
| 984 | return -1 |
| 985 | |
| 986 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 987 | # things that are special to LaTeX, but not to texi.. |
| 988 | onlylatexspecial = '_~^$#&%' |
| 989 | |
Guido van Rossum | 23301a9 | 1993-05-24 14:19:37 +0000 | [diff] [blame] | 990 | class Struct: pass |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 991 | |
| 992 | hist = Struct() |
| 993 | out = Struct() |
| 994 | |
| 995 | def startchange(): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 996 | global hist, out |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 997 | |
Fred Drake | 74a11e5 | 1998-02-26 05:52:37 +0000 | [diff] [blame] | 998 | hist.chaptertype = "chapter" |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 999 | hist.inenv = [] |
| 1000 | hist.nodenames = [] |
| 1001 | hist.cindex = [] |
| 1002 | hist.inargs = 0 |
| 1003 | hist.enumeratenesting, hist.itemizenesting = 0, 0 |
Fred Drake | 4f6d6e4 | 1998-04-17 15:28:09 +0000 | [diff] [blame] | 1004 | hist.this_module = None |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1005 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1006 | out.doublenodes = [] |
| 1007 | out.doublecindeces = [] |
| 1008 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1009 | |
| 1010 | spacech = [chunk(PLAIN, 0, ' ')] |
| 1011 | commach = [chunk(PLAIN, 0, ', ')] |
| 1012 | cindexch = [chunk(CSLINE, 0, 'cindex')] |
| 1013 | |
| 1014 | # the standard variation in symbols for itemize |
| 1015 | itemizesymbols = ['bullet', 'minus', 'dots'] |
| 1016 | |
| 1017 | # same for enumerate |
| 1018 | enumeratesymbols = ['1', 'A', 'a'] |
| 1019 | |
Fred Drake | 74a11e5 | 1998-02-26 05:52:37 +0000 | [diff] [blame] | 1020 | # Map of things that convert one-to-one. Each entry is a 3-tuple: |
| 1021 | # |
| 1022 | # new_chtype, new_data, nix_trailing_empty_group |
| 1023 | # |
Fred Drake | 9c7c6be | 1998-02-19 21:40:22 +0000 | [diff] [blame] | 1024 | d = {} |
Fred Drake | 74a11e5 | 1998-02-26 05:52:37 +0000 | [diff] [blame] | 1025 | # add stuff that converts from one name to another: |
Fred Drake | 9c7c6be | 1998-02-19 21:40:22 +0000 | [diff] [blame] | 1026 | for name in ('url', 'module', 'function', 'cfunction', |
| 1027 | 'keyword', 'method', 'exception', 'constant', |
| 1028 | 'email', 'class', 'member', 'cdata', 'ctype', |
Fred Drake | 4f6d6e4 | 1998-04-17 15:28:09 +0000 | [diff] [blame] | 1029 | 'member', 'sectcode', 'verb', |
| 1030 | 'cfunction', 'cdata', 'ctype', |
| 1031 | ): |
Fred Drake | 74a11e5 | 1998-02-26 05:52:37 +0000 | [diff] [blame] | 1032 | d[name] = chunk_type[CSNAME], 'code', 0 |
| 1033 | for name in ('emph', 'var', 'strong', 'code', 'kbd', 'key', |
| 1034 | 'dfn', 'samp', 'file', 'r', 'i', 't'): |
| 1035 | d[name] = chunk_type[CSNAME], name, 0 |
Fred Drake | 4f6d6e4 | 1998-04-17 15:28:09 +0000 | [diff] [blame] | 1036 | d['character'] = chunk_type[CSNAME], 'samp', 0 |
| 1037 | d['url'] = chunk_type[CSNAME], 'code', 0 |
| 1038 | d['email'] = chunk_type[CSNAME], 'code', 0 |
| 1039 | d['mimetype'] = chunk_type[CSNAME], 'code', 0 |
| 1040 | d['newsgroup'] = chunk_type[CSNAME], 'code', 0 |
Fred Drake | 74a11e5 | 1998-02-26 05:52:37 +0000 | [diff] [blame] | 1041 | d['program'] = chunk_type[CSNAME], 'strong', 0 |
| 1042 | d['\\'] = chunk_type[CSNAME], '*', 0 |
| 1043 | # add stuff that converts to text: |
| 1044 | for name in themselves: |
| 1045 | d[name] = chunk_type[PLAIN], name, 0 |
| 1046 | for name in wordsselves: |
| 1047 | d[name] = chunk_type[PLAIN], name, 1 |
| 1048 | for name in ',[]()': |
| 1049 | d[name] = chunk_type[PLAIN], name, 0 |
| 1050 | # a lot of these are LaTeX2e additions |
| 1051 | for name, value in [('quotedblbase', ',,'), ('quotesinglbase', ','), |
| 1052 | ('textquotedbl', '"'), ('LaTeXe', 'LaTeX2e'), |
| 1053 | ('e', '\\'), ('textquotedblleft', "``"), |
| 1054 | ('textquotedblright', "''"), ('textquoteleft', "`"), |
| 1055 | ('textquoteright', "'"), ('textbackslash', '\\'), |
| 1056 | ('textbar', '|'), ('textless', '<'), |
| 1057 | ('textgreater', '>'), ('textasciicircum', '^'), |
| 1058 | ('Cpp', 'C++'), ('copyright', '')]: |
| 1059 | d[name] = chunk_type[PLAIN], value, 1 |
Fred Drake | 9c7c6be | 1998-02-19 21:40:22 +0000 | [diff] [blame] | 1060 | convertible_csname = d.has_key |
| 1061 | conversion = d.get |
Fred Drake | 74a11e5 | 1998-02-26 05:52:37 +0000 | [diff] [blame] | 1062 | del d, name, value |
Fred Drake | 9c7c6be | 1998-02-19 21:40:22 +0000 | [diff] [blame] | 1063 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1064 | ## |
| 1065 | ## \begin{ {func,data,exc}desc }{name}... |
| 1066 | ## the resulting texi-code is dependent on the contents of indexsubitem |
| 1067 | ## |
| 1068 | |
| 1069 | # indexsubitem: `['XXX', 'function'] |
| 1070 | # funcdesc: |
| 1071 | # deffn {`idxsi`} NAME (FUNCARGS) |
| 1072 | |
| 1073 | # indexsubitem: `['XXX', 'method']` |
| 1074 | # funcdesc: |
| 1075 | # defmethod {`idxsi[0]`} NAME (FUNCARGS) |
| 1076 | |
| 1077 | # indexsubitem: `['in', 'module', 'MODNAME']' |
| 1078 | # datadesc: |
| 1079 | # defcv data {`idxsi[1:]`} NAME |
| 1080 | # excdesc: |
| 1081 | # defcv exception {`idxsi[1:]`} NAME |
| 1082 | # funcdesc: |
| 1083 | # deffn {function of `idxsi[1:]`} NAME (FUNCARGS) |
| 1084 | |
| 1085 | # indexsubitem: `['OBJECT', 'attribute']' |
| 1086 | # datadesc |
| 1087 | # defcv attribute {`OBJECT`} NAME |
| 1088 | |
| 1089 | |
| 1090 | ## this routine will be called on \begin{funcdesc}{NAME}{ARGS} |
| 1091 | ## or \funcline{NAME}{ARGS} |
| 1092 | ## |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1093 | def do_funcdesc(length, buf, pp, i, index=1): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1094 | startpoint = i-1 |
| 1095 | ch = pp[startpoint] |
| 1096 | wh = ch.where |
| 1097 | length, newi = getnextarg(length, buf, pp, i) |
| 1098 | funcname = chunk(GROUP, wh, pp[i:newi]) |
| 1099 | del pp[i:newi] |
| 1100 | length = length - (newi-i) |
| 1101 | save = hist.inargs |
| 1102 | hist.inargs = 1 |
| 1103 | length, newi = getnextarg(length, buf, pp, i) |
| 1104 | hist.inargs = save |
| 1105 | del save |
| 1106 | the_args = [chunk(PLAIN, wh, '()'[0])] + pp[i:newi] + \ |
Fred Drake | 7edd8d3 | 1996-10-09 16:11:26 +0000 | [diff] [blame] | 1107 | [chunk(PLAIN, wh, '()'[1])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1108 | del pp[i:newi] |
| 1109 | length = length - (newi-i) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1110 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1111 | idxsi = hist.indexsubitem # words |
Fred Drake | 4f6d6e4 | 1998-04-17 15:28:09 +0000 | [diff] [blame] | 1112 | command = 'deffn' |
| 1113 | if hist.this_module: |
| 1114 | cat_class = 'function of ' + hist.this_module |
| 1115 | else: |
| 1116 | cat_class = 'built-in function' |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1117 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1118 | ch.data = command |
| 1119 | |
| 1120 | cslinearg = [chunk(GROUP, wh, [chunk(PLAIN, wh, cat_class)])] |
| 1121 | cslinearg.append(chunk(PLAIN, wh, ' ')) |
| 1122 | cslinearg.append(funcname) |
| 1123 | cslinearg.append(chunk(PLAIN, wh, ' ')) |
| 1124 | l = len(cslinearg) |
| 1125 | cslinearg[l:l] = the_args |
| 1126 | |
| 1127 | pp.insert(i, chunk(GROUP, wh, cslinearg)) |
| 1128 | i, length = i+1, length+1 |
| 1129 | hist.command = command |
| 1130 | return length, i |
| 1131 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1132 | |
| 1133 | ## this routine will be called on \begin{excdesc}{NAME} |
| 1134 | ## or \excline{NAME} |
| 1135 | ## |
| 1136 | def do_excdesc(length, buf, pp, i): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1137 | startpoint = i-1 |
| 1138 | ch = pp[startpoint] |
| 1139 | wh = ch.where |
| 1140 | length, newi = getnextarg(length, buf, pp, i) |
| 1141 | excname = chunk(GROUP, wh, pp[i:newi]) |
| 1142 | del pp[i:newi] |
| 1143 | length = length - (newi-i) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1144 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1145 | idxsi = hist.indexsubitem # words |
| 1146 | command = '' |
| 1147 | cat_class = '' |
| 1148 | class_class = '' |
Fred Drake | 4f6d6e4 | 1998-04-17 15:28:09 +0000 | [diff] [blame] | 1149 | if idxsi == ['built-in', 'exception', 'base', 'class']: |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1150 | command = 'defvr' |
| 1151 | cat_class = 'exception base class' |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1152 | else: |
Fred Drake | 4f6d6e4 | 1998-04-17 15:28:09 +0000 | [diff] [blame] | 1153 | command = 'defcv' |
| 1154 | cat_class = 'exception' |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1155 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1156 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1157 | ch.data = command |
| 1158 | |
| 1159 | cslinearg = [chunk(GROUP, wh, [chunk(PLAIN, wh, cat_class)])] |
| 1160 | cslinearg.append(chunk(PLAIN, wh, ' ')) |
| 1161 | if class_class: |
| 1162 | cslinearg.append(chunk(GROUP, wh, [chunk(PLAIN, wh, class_class)])) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1163 | cslinearg.append(chunk(PLAIN, wh, ' ')) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1164 | cslinearg.append(excname) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1165 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1166 | pp.insert(i, chunk(GROUP, wh, cslinearg)) |
| 1167 | i, length = i+1, length+1 |
| 1168 | hist.command = command |
| 1169 | return length, i |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1170 | |
| 1171 | ## same for datadesc or dataline... |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1172 | def do_datadesc(length, buf, pp, i, index=1): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1173 | startpoint = i-1 |
| 1174 | ch = pp[startpoint] |
| 1175 | wh = ch.where |
| 1176 | length, newi = getnextarg(length, buf, pp, i) |
| 1177 | dataname = chunk(GROUP, wh, pp[i:newi]) |
| 1178 | del pp[i:newi] |
| 1179 | length = length - (newi-i) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1180 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1181 | idxsi = hist.indexsubitem # words |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1182 | command = 'defcv' |
| 1183 | cat_class = 'data' |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1184 | class_class = '' |
| 1185 | if idxsi[-1] in ('attribute', 'option'): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1186 | cat_class = idxsi[-1] |
| 1187 | class_class = string.join(idxsi[:-1]) |
| 1188 | elif len(idxsi) == 3 and idxsi[:2] == ['in', 'module']: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1189 | class_class = string.join(idxsi[1:]) |
| 1190 | elif len(idxsi) == 4 and idxsi[:3] == ['data', 'in', 'module']: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1191 | class_class = string.join(idxsi[2:]) |
Fred Drake | 11b6d24 | 1996-10-10 20:09:56 +0000 | [diff] [blame] | 1192 | else: |
Fred Drake | 11b6d24 | 1996-10-10 20:09:56 +0000 | [diff] [blame] | 1193 | class_class = string.join(idxsi) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1194 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1195 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1196 | ch.data = command |
| 1197 | |
| 1198 | cslinearg = [chunk(GROUP, wh, [chunk(PLAIN, wh, cat_class)])] |
| 1199 | cslinearg.append(chunk(PLAIN, wh, ' ')) |
| 1200 | if class_class: |
| 1201 | cslinearg.append(chunk(GROUP, wh, [chunk(PLAIN, wh, class_class)])) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1202 | cslinearg.append(chunk(PLAIN, wh, ' ')) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1203 | cslinearg.append(dataname) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1204 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1205 | pp.insert(i, chunk(GROUP, wh, cslinearg)) |
| 1206 | i, length = i+1, length+1 |
| 1207 | hist.command = command |
| 1208 | return length, i |
| 1209 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1210 | |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1211 | def do_opcodedesc(length, buf, pp, i): |
| 1212 | startpoint = i-1 |
| 1213 | ch = pp[startpoint] |
| 1214 | wh = ch.where |
| 1215 | length, newi = getnextarg(length, buf, pp, i) |
| 1216 | dataname = chunk(GROUP, wh, pp[i:newi]) |
| 1217 | del pp[i:newi] |
| 1218 | length = length - (newi-i) |
| 1219 | |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 1220 | ch.chtype = CSLINE |
| 1221 | ch.data = "deffn" |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1222 | |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1223 | cslinearg = [chunk(PLAIN, wh, 'byte\ code\ instruction'), |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1224 | chunk(GROUP, wh, [chunk(PLAIN, wh, "byte code instruction")]), |
| 1225 | chunk(PLAIN, wh, ' '), |
| 1226 | dataname, |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 1227 | chunk(PLAIN, wh, ' '), |
| 1228 | pp[i], |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1229 | ] |
| 1230 | |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 1231 | pp[i] = chunk(GROUP, wh, cslinearg) |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1232 | hist.command = ch.data |
| 1233 | return length, i |
| 1234 | |
| 1235 | |
Fred Drake | 4f6d6e4 | 1998-04-17 15:28:09 +0000 | [diff] [blame] | 1236 | def add_module_index(pp, length, i, buf, ch, extra, ref=1): |
| 1237 | ch.chtype = chunk_type[CSLINE] |
| 1238 | ch.data = 'pindex' |
| 1239 | length, newi = getnextarg(length, buf, pp, i) |
| 1240 | ingroupch = pp[i:newi] |
| 1241 | del pp[i:newi] |
| 1242 | length = length - (newi-i) |
| 1243 | if not ref: |
| 1244 | if len(ingroupch) == 1: |
| 1245 | hist.this_module = s(buf, ch.data) |
| 1246 | else: |
| 1247 | hist.this_module = None |
| 1248 | print 'add_module_index() error ==>', ingroupch |
| 1249 | |
| 1250 | if extra: |
| 1251 | ingroupch.append(chunk(PLAIN, ch.where, ' ')) |
| 1252 | ingroupch.append(chunk(CSNAME, ch.where, 'r')) |
| 1253 | ingroupch.append(chunk(GROUP, ch.where, [ |
| 1254 | chunk(PLAIN, ch.where, extra)])) |
| 1255 | |
| 1256 | pp.insert(i, chunk(GROUP, ch.where, ingroupch)) |
| 1257 | return length+1, i+1 |
| 1258 | |
| 1259 | |
| 1260 | def yank_indexsubitem(pp, length, i, buf, ch, cmdname): |
| 1261 | stuff = pp[i].data |
| 1262 | if len(stuff) != 1: |
| 1263 | raise error, "first parameter to \\%s too long" % cmdname |
| 1264 | if pp[i].chtype != chunk_type[GROUP]: |
| 1265 | raise error, "bad chunk type following \\%s" \ |
| 1266 | "\nexpected GROUP, got %s" + (cmdname, str(ch.chtype)) |
| 1267 | text = s(buf, stuff[0].data) |
| 1268 | if text[:1] != '(' or text[-1:] != ')': |
| 1269 | raise error, \ |
| 1270 | 'expected indexsubitem enclosed in parenteses' |
| 1271 | hist.indexsubitem = string.split(text[1:-1]) |
| 1272 | del pp[i-1:i+1] |
| 1273 | return length - 2, i - 1 |
| 1274 | |
| 1275 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1276 | # regular indices: those that are not set in tt font by default.... |
| 1277 | regindices = ('cindex', ) |
| 1278 | |
| 1279 | # remove illegal characters from node names |
| 1280 | def rm_commas_etc(text): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1281 | result = '' |
| 1282 | changed = 0 |
| 1283 | while 1: |
| 1284 | pos = re_commas_etc.search(text) |
| 1285 | if pos >= 0: |
| 1286 | changed = 1 |
| 1287 | result = result + text[:pos] |
| 1288 | text = text[pos+1:] |
| 1289 | else: |
| 1290 | result = result + text |
| 1291 | break |
| 1292 | if changed: |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 1293 | print 'Warning: nodename changed to ' + `result` |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1294 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1295 | return result |
| 1296 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1297 | # boolean flags |
| 1298 | flags = {'texi': 1} |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1299 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1300 | |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 1301 | # map of \label{} to node names |
| 1302 | label_nodes = {} |
| 1303 | |
| 1304 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1305 | ## |
| 1306 | ## changeit: the actual routine, that changes the contents of the parsed |
| 1307 | ## chunks |
| 1308 | ## |
| 1309 | |
| 1310 | def changeit(buf, pp): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1311 | global onlylatexspecial, hist, out |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1312 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1313 | i, length = 0, len(pp) |
| 1314 | while 1: |
| 1315 | # sanity check: length should always equal len(pp) |
| 1316 | if len(pp) != length: |
Fred Drake | 74a11e5 | 1998-02-26 05:52:37 +0000 | [diff] [blame] | 1317 | print i, pp[i] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1318 | raise 'FATAL', 'inconsistent length. thought ' + `length` + ', but should really be ' + `len(pp)` |
| 1319 | if i >= length: |
| 1320 | break |
| 1321 | ch = pp[i] |
| 1322 | i = i + 1 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1323 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1324 | if type(ch) is StringType: |
| 1325 | #normally, only chunks are present in pp, |
| 1326 | # but in some cases, some extra info |
| 1327 | # has been inserted, e.g., the \end{...} clauses |
| 1328 | raise 'FATAL', 'got string, probably too many ' + `end` |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1329 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1330 | if ch.chtype == chunk_type[GROUP]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1331 | # check for {\em ...} constructs |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1332 | data = ch.data |
| 1333 | if data and \ |
| 1334 | data[0].chtype == chunk_type[CSNAME] and \ |
| 1335 | fontchanges.has_key(s(buf, data[0].data)): |
| 1336 | k = s(buf, data[0].data) |
| 1337 | del data[0] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1338 | pp.insert(i-1, chunk(CSNAME, ch.where, fontchanges[k])) |
| 1339 | length, i = length+1, i+1 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1340 | |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1341 | elif data: |
| 1342 | if len(data) \ |
| 1343 | and data[0].chtype == chunk_type[GROUP] \ |
| 1344 | and len(data[0].data) \ |
| 1345 | and data[0].data[0].chtype == chunk_type[CSNAME] \ |
| 1346 | and s(buf, data[0].data[0].data) == 'e': |
| 1347 | data[0] = data[0].data[0] |
| 1348 | print "invoking \\e magic group transform..." |
| 1349 | else: |
| 1350 | ## print "GROUP -- ch.data[0].data =", ch.data[0].data |
| 1351 | k = s(buf, data[0].data) |
| 1352 | if k == "fulllineitems": |
| 1353 | del data[0] |
| 1354 | pp[i-1:i] = data |
| 1355 | i = i - 1 |
| 1356 | length = length + len(data) - 1 |
| 1357 | continue |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 1358 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1359 | # recursively parse the contents of the group |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1360 | changeit(buf, data) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1361 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1362 | elif ch.chtype == chunk_type[IF]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1363 | # \if... |
| 1364 | flag, negate, data = ch.data |
| 1365 | ##print 'IF: flag, negate = ' + `flag, negate` |
| 1366 | if flag not in flags.keys(): |
| 1367 | raise error, 'unknown flag ' + `flag` |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1368 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1369 | value = flags[flag] |
| 1370 | if negate: |
| 1371 | value = (not value) |
| 1372 | del pp[i-1] |
| 1373 | length, i = length-1, i-1 |
| 1374 | if value: |
| 1375 | pp[i:i] = data |
| 1376 | length = length + len(data) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1377 | |
| 1378 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1379 | elif ch.chtype == chunk_type[ENV]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1380 | # \begin{...} .... |
| 1381 | envname, data = ch.data |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1382 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1383 | #push this environment name on stack |
| 1384 | hist.inenv.insert(0, envname) |
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 | #append an endenv chunk after grouped data |
| 1387 | data.append(chunk(ENDENV, ch.where, envname)) |
| 1388 | ##[`data`] |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1389 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1390 | #delete this object |
| 1391 | del pp[i-1] |
| 1392 | i, length = i-1, length-1 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1393 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1394 | #insert found data |
| 1395 | pp[i:i] = data |
| 1396 | length = length + len(data) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1397 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1398 | if envname == 'verbatim': |
| 1399 | pp[i:i] = [chunk(CSLINE, ch.where, 'example'), |
| 1400 | chunk(GROUP, ch.where, [])] |
| 1401 | length, i = length+2, i+2 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1402 | |
Fred Drake | ef05803 | 1998-02-19 15:20:30 +0000 | [diff] [blame] | 1403 | elif envname in ('itemize', 'list', 'fulllineitems'): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1404 | if hist.itemizenesting > len(itemizesymbols): |
| 1405 | raise error, 'too deep itemize nesting' |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1406 | if envname == 'list': |
| 1407 | del pp[i:i+2] |
| 1408 | length = length - 2 |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1409 | ingroupch = [chunk(CSNAME, ch.where, |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1410 | itemizesymbols[hist.itemizenesting])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1411 | hist.itemizenesting = hist.itemizenesting + 1 |
| 1412 | pp[i:i] = [chunk(CSLINE, ch.where, 'itemize'), |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1413 | chunk(GROUP, ch.where, ingroupch)] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1414 | length, i = length+2, i+2 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1415 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1416 | elif envname == 'enumerate': |
| 1417 | if hist.enumeratenesting > len(enumeratesymbols): |
| 1418 | raise error, 'too deep enumerate nesting' |
| 1419 | ingroupch = [chunk(PLAIN, ch.where, |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1420 | enumeratesymbols[hist.enumeratenesting])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1421 | hist.enumeratenesting = hist.enumeratenesting + 1 |
| 1422 | pp[i:i] = [chunk(CSLINE, ch.where, 'enumerate'), |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1423 | chunk(GROUP, ch.where, ingroupch)] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1424 | length, i = length+2, i+2 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1425 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1426 | elif envname == 'description': |
| 1427 | ingroupch = [chunk(CSNAME, ch.where, 'b')] |
| 1428 | pp[i:i] = [chunk(CSLINE, ch.where, 'table'), |
| 1429 | chunk(GROUP, ch.where, ingroupch)] |
| 1430 | length, i = length+2, i+2 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1431 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1432 | elif (envname == 'tableiii') or (envname == 'tableii'): |
| 1433 | if (envname == 'tableii'): |
| 1434 | ltable = 2 |
| 1435 | else: |
| 1436 | ltable = 3 |
| 1437 | wh = ch.where |
| 1438 | newcode = [] |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1439 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1440 | #delete tabular format description |
| 1441 | # e.g., {|l|c|l|} |
| 1442 | length, newi = getnextarg(length, buf, pp, i) |
| 1443 | del pp[i:newi] |
| 1444 | length = length - (newi-i) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1445 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1446 | newcode.append(chunk(CSLINE, wh, 'table')) |
| 1447 | ingroupch = [chunk(CSNAME, wh, 'asis')] |
| 1448 | newcode.append(chunk(GROUP, wh, ingroupch)) |
| 1449 | newcode.append(chunk(CSLINE, wh, 'item')) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1450 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1451 | #get the name of macro for @item |
| 1452 | # e.g., {code} |
| 1453 | length, newi = getnextarg(length, buf, pp, i) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1454 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1455 | if newi-i != 1: |
| 1456 | raise error, 'Sorry, expected 1 chunk argument' |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1457 | if pp[i].chtype != chunk_type[PLAIN]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1458 | raise error, 'Sorry, expected plain text argument' |
| 1459 | hist.itemargmacro = s(buf, pp[i].data) |
Fred Drake | 9c7c6be | 1998-02-19 21:40:22 +0000 | [diff] [blame] | 1460 | if convertible_csname(hist.itemargmacro): |
Fred Drake | 74a11e5 | 1998-02-26 05:52:37 +0000 | [diff] [blame] | 1461 | hist.itemargmacro = conversion(hist.itemargmacro)[1] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1462 | del pp[i:newi] |
| 1463 | length = length - (newi-i) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1464 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1465 | itembody = [] |
| 1466 | for count in range(ltable): |
| 1467 | length, newi = getnextarg(length, buf, pp, i) |
| 1468 | emphgroup = [ |
| 1469 | chunk(CSNAME, wh, 'emph'), |
| 1470 | chunk(GROUP, 0, pp[i:newi])] |
| 1471 | del pp[i:newi] |
| 1472 | length = length - (newi-i) |
| 1473 | if count == 0: |
| 1474 | itemarg = emphgroup |
| 1475 | elif count == ltable-1: |
| 1476 | itembody = itembody + \ |
| 1477 | [chunk(PLAIN, wh, ' --- ')] + emphgroup |
| 1478 | else: |
| 1479 | itembody = emphgroup |
| 1480 | newcode.append(chunk(GROUP, wh, itemarg)) |
| 1481 | newcode = newcode + itembody + [chunk(DENDLINE, wh, '\n')] |
| 1482 | pp[i:i] = newcode |
| 1483 | l = len(newcode) |
| 1484 | length, i = length+l, i+l |
| 1485 | del newcode, l |
| 1486 | |
| 1487 | if length != len(pp): |
| 1488 | raise 'STILL, SOMETHING wrong', `i` |
| 1489 | |
Fred Drake | 4f6d6e4 | 1998-04-17 15:28:09 +0000 | [diff] [blame] | 1490 | elif envname in ('methoddesc', 'methoddescni'): |
| 1491 | length, newi = getoptarg(length, buf, pp, i) |
| 1492 | ingroupch = pp[i:newi] |
| 1493 | del pp[i:newi] |
| 1494 | length = length - (newi-i) |
| 1495 | # |
| 1496 | pp.insert(i, chunk(PLAIN, ch.where, '')) |
| 1497 | i, length = i+1, length+1 |
| 1498 | length, i = do_funcdesc(length, buf, pp, i, |
| 1499 | envname[-2:] != "ni") |
| 1500 | |
| 1501 | elif envname in ('memberdesc', 'memberdescni'): |
| 1502 | length, newi = getoptarg(length, buf, pp, i) |
| 1503 | ingroupch = pp[i:newi] |
| 1504 | del pp[i:newi] |
| 1505 | length = length - (newi-i) |
| 1506 | # |
| 1507 | pp.insert(i, chunk(PLAIN, ch.where, '')) |
| 1508 | i, length = i+1, length+1 |
| 1509 | length, i = do_datadesc(length, buf, pp, i, |
| 1510 | envname[-2:] != "ni") |
| 1511 | |
Fred Drake | ef05803 | 1998-02-19 15:20:30 +0000 | [diff] [blame] | 1512 | elif envname in ('funcdesc', 'funcdescni', 'classdesc'): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1513 | pp.insert(i, chunk(PLAIN, ch.where, '')) |
| 1514 | i, length = i+1, length+1 |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1515 | length, i = do_funcdesc(length, buf, pp, i, |
Fred Drake | ef05803 | 1998-02-19 15:20:30 +0000 | [diff] [blame] | 1516 | envname[-2:] != "ni") |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1517 | |
| 1518 | elif envname == 'excdesc': |
| 1519 | pp.insert(i, chunk(PLAIN, ch.where, '')) |
| 1520 | i, length = i+1, length+1 |
| 1521 | length, i = do_excdesc(length, buf, pp, i) |
| 1522 | |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1523 | elif envname in ('datadesc', 'datadescni'): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1524 | pp.insert(i, chunk(PLAIN, ch.where, '')) |
| 1525 | i, length = i+1, length+1 |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1526 | length, i = do_datadesc(length, buf, pp, i, |
Fred Drake | ef05803 | 1998-02-19 15:20:30 +0000 | [diff] [blame] | 1527 | envname[-2:] != "ni") |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1528 | |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1529 | elif envname == 'opcodedesc': |
| 1530 | pp.insert(i, chunk(PLAIN, ch.where, '')) |
| 1531 | i, length = i+1, length+1 |
| 1532 | length, i = do_opcodedesc(length, buf, pp, i) |
| 1533 | |
| 1534 | elif envname == 'seealso': |
| 1535 | chunks = [chunk(ENDLINE, ch.where, "\n"), |
| 1536 | chunk(CSNAME, ch.where, "b"), |
| 1537 | chunk(GROUP, ch.where, [ |
| 1538 | chunk(PLAIN, ch.where, "See also: ")]), |
| 1539 | chunk(ENDLINE, ch.where, "\n"), |
| 1540 | chunk(ENDLINE, ch.where, "\n")] |
| 1541 | pp[i-1:i] = chunks |
| 1542 | length = length + len(chunks) - 1 |
| 1543 | i = i + len(chunks) - 1 |
| 1544 | |
Fred Drake | 74a11e5 | 1998-02-26 05:52:37 +0000 | [diff] [blame] | 1545 | elif envname in ('sloppypar', 'flushleft', 'document'): |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 1546 | pass |
| 1547 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1548 | else: |
| 1549 | print 'WARNING: don\'t know what to do with env ' + `envname` |
| 1550 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1551 | elif ch.chtype == chunk_type[ENDENV]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1552 | envname = ch.data |
| 1553 | if envname != hist.inenv[0]: |
| 1554 | raise error, '\'end\' does not match. Name ' + `envname` + ', expected ' + `hist.inenv[0]` |
| 1555 | del hist.inenv[0] |
| 1556 | del pp[i-1] |
| 1557 | i, length = i-1, length-1 |
| 1558 | |
| 1559 | if envname == 'verbatim': |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 1560 | pp[i:i] = [chunk(CSLINE, ch.where, 'end'), |
| 1561 | chunk(GROUP, ch.where, [ |
| 1562 | chunk(PLAIN, ch.where, 'example')])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1563 | i, length = i+2, length+2 |
Fred Drake | ef05803 | 1998-02-19 15:20:30 +0000 | [diff] [blame] | 1564 | elif envname in ('itemize', 'list', 'fulllineitems'): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1565 | hist.itemizenesting = hist.itemizenesting - 1 |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 1566 | pp[i:i] = [chunk(CSLINE, ch.where, 'end'), |
| 1567 | chunk(GROUP, ch.where, [ |
| 1568 | chunk(PLAIN, ch.where, 'itemize')])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1569 | i, length = i+2, length+2 |
| 1570 | elif envname == 'enumerate': |
| 1571 | hist.enumeratenesting = hist.enumeratenesting-1 |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 1572 | pp[i:i] = [chunk(CSLINE, ch.where, 'end'), |
| 1573 | chunk(GROUP, ch.where, [ |
| 1574 | chunk(PLAIN, ch.where, 'enumerate')])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1575 | i, length = i+2, length+2 |
| 1576 | elif envname == 'description': |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 1577 | pp[i:i] = [chunk(CSLINE, ch.where, 'end'), |
| 1578 | chunk(GROUP, ch.where, [ |
| 1579 | chunk(PLAIN, ch.where, 'table')])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1580 | i, length = i+2, length+2 |
| 1581 | elif (envname == 'tableiii') or (envname == 'tableii'): |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 1582 | pp[i:i] = [chunk(CSLINE, ch.where, 'end'), |
| 1583 | chunk(GROUP, ch.where, [ |
| 1584 | chunk(PLAIN, ch.where, 'table')])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1585 | i, length = i+2, length + 2 |
| 1586 | pp.insert(i, chunk(DENDLINE, ch.where, '\n')) |
| 1587 | i, length = i+1, length+1 |
| 1588 | |
Fred Drake | ef05803 | 1998-02-19 15:20:30 +0000 | [diff] [blame] | 1589 | elif envname in ('funcdesc', 'excdesc', 'datadesc', 'classdesc', |
Fred Drake | 4f6d6e4 | 1998-04-17 15:28:09 +0000 | [diff] [blame] | 1590 | 'funcdescni', 'datadescni', |
| 1591 | 'methoddesc', 'memberdesc', |
| 1592 | 'methoddescni', 'memberdescni', |
| 1593 | ): |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 1594 | pp[i:i] = [chunk(CSLINE, ch.where, 'end'), |
| 1595 | chunk(GROUP, ch.where, [ |
| 1596 | chunk(PLAIN, ch.where, hist.command)])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1597 | i, length = i+2, length+2 |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1598 | |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 1599 | elif envname == 'opcodedesc': |
| 1600 | pp[i:i] = [chunk(CSLINE, ch.where, 'end'), |
| 1601 | chunk(GROUP, ch.where, [ |
| 1602 | chunk(PLAIN, ch.where, "deffn")])] |
| 1603 | i, length = i+2, length+2 |
| 1604 | |
Fred Drake | 74a11e5 | 1998-02-26 05:52:37 +0000 | [diff] [blame] | 1605 | elif envname in ('seealso', 'sloppypar', 'flushleft', 'document'): |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1606 | pass |
| 1607 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1608 | else: |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1609 | print 'WARNING: ending env %s has no actions' % `envname` |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1610 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1611 | elif ch.chtype == chunk_type[CSNAME]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1612 | # control name transformations |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1613 | s_buf_data = s(buf, ch.data) |
| 1614 | if s_buf_data == 'optional': |
| 1615 | pp[i-1].chtype = chunk_type[PLAIN] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1616 | pp[i-1].data = '[' |
| 1617 | if (i < length) and \ |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1618 | (pp[i].chtype == chunk_type[GROUP]): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1619 | cp=pp[i].data |
| 1620 | pp[i:i+1]=cp + [ |
| 1621 | chunk(PLAIN, ch.where, ']')] |
| 1622 | length = length+len(cp) |
Fred Drake | 74a11e5 | 1998-02-26 05:52:37 +0000 | [diff] [blame] | 1623 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1624 | elif s_buf_data in ignoredcommands: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1625 | del pp[i-1] |
| 1626 | i, length = i-1, length-1 |
Fred Drake | 74a11e5 | 1998-02-26 05:52:37 +0000 | [diff] [blame] | 1627 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1628 | elif s_buf_data == '@' and \ |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1629 | i != length and \ |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1630 | pp[i].chtype == chunk_type[PLAIN] and \ |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1631 | s(buf, pp[i].data)[0] == '.': |
| 1632 | # \@. --> \. --> @. |
| 1633 | ch.data = '.' |
| 1634 | del pp[i] |
Fred Drake | 74a11e5 | 1998-02-26 05:52:37 +0000 | [diff] [blame] | 1635 | length = length - 1 |
| 1636 | |
| 1637 | elif convertible_csname(s_buf_data): |
| 1638 | ch.chtype, ch.data, nix = conversion(s_buf_data) |
| 1639 | try: |
| 1640 | if nix and pp[i].chtype == chunk_type[GROUP] \ |
| 1641 | and len(pp[i].data) == 0: |
| 1642 | del pp[i] |
| 1643 | length = length - 1 |
| 1644 | except IndexError: |
| 1645 | pass |
| 1646 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1647 | elif s_buf_data == '\\': |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1648 | # \\ --> \* --> @* |
| 1649 | ch.data = '*' |
Fred Drake | 74a11e5 | 1998-02-26 05:52:37 +0000 | [diff] [blame] | 1650 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1651 | elif len(s_buf_data) == 1 and \ |
| 1652 | s_buf_data in onlylatexspecial: |
| 1653 | ch.chtype = chunk_type[PLAIN] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1654 | # check if such a command is followed by |
| 1655 | # an empty group: e.g., `\%{}'. If so, remove |
| 1656 | # this empty group too |
| 1657 | if i < length and \ |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1658 | pp[i].chtype == chunk_type[GROUP] \ |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1659 | and len(pp[i].data) == 0: |
| 1660 | del pp[i] |
| 1661 | length = length-1 |
| 1662 | |
Fred Drake | 74a11e5 | 1998-02-26 05:52:37 +0000 | [diff] [blame] | 1663 | elif s_buf_data == "appendix": |
| 1664 | hist.chaptertype = "appendix" |
| 1665 | del pp[i-1] |
| 1666 | i, length = i-1, length-1 |
| 1667 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1668 | elif hist.inargs and s_buf_data in inargsselves: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1669 | # This is the special processing of the |
| 1670 | # arguments of the \begin{funcdesc}... or |
| 1671 | # \funcline... arguments |
| 1672 | # \, --> , \[ --> [, \] --> ] |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1673 | ch.chtype = chunk_type[PLAIN] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1674 | |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1675 | elif s_buf_data == 'setindexsubitem': |
Fred Drake | 4f6d6e4 | 1998-04-17 15:28:09 +0000 | [diff] [blame] | 1676 | length, i = yank_indexsubitem(pp, length, i, buf, ch, |
| 1677 | 'setindexsubitem') |
| 1678 | |
| 1679 | elif s_buf_data == 'withsubitem': |
| 1680 | oldsubitem = hist.indexsubitem |
| 1681 | try: |
| 1682 | length, i = yank_indexsubitem(pp, length, i, buf, ch, |
| 1683 | 'withsubitem') |
| 1684 | stuff = pp[i].data |
| 1685 | del pp[i] |
| 1686 | length = length - 1 |
| 1687 | changeit(buf, stuff) |
| 1688 | stuff = None |
| 1689 | finally: |
| 1690 | hist.indexsubitem = oldsubitem |
| 1691 | |
| 1692 | elif s_buf_data in ('textrm', 'pytype'): |
| 1693 | stuff = pp[i].data |
| 1694 | pp[i-1:i+1] = stuff |
| 1695 | length = length - 2 + len(stuff) |
| 1696 | stuff = None |
| 1697 | i = i - 1 |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1698 | |
| 1699 | elif s_buf_data == 'newcommand': |
| 1700 | print "ignoring definition of \\" + s(buf, pp[i].data[0].data) |
| 1701 | del pp[i-1:i+2] |
| 1702 | i = i - 1 |
| 1703 | length = length - 3 |
| 1704 | |
Fred Drake | 74a11e5 | 1998-02-26 05:52:37 +0000 | [diff] [blame] | 1705 | elif s_buf_data == 'renewcommand': |
| 1706 | print "ignoring redefinition of \\" \ |
| 1707 | + s(buf, pp[i].data[0].data) |
| 1708 | del pp[i-1:i+2] |
| 1709 | i = i - 1 |
| 1710 | length = length - 3 |
| 1711 | |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1712 | elif s_buf_data == 'mbox': |
| 1713 | stuff = pp[i].data |
| 1714 | pp[i-1:i+1] = stuff |
| 1715 | i = i - 1 |
| 1716 | length = length + len(stuff) - 2 |
Fred Drake | 4f6d6e4 | 1998-04-17 15:28:09 +0000 | [diff] [blame] | 1717 | stuff = None |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1718 | |
| 1719 | elif s_buf_data == 'version': |
| 1720 | ch.chtype = chunk_type[PLAIN] |
| 1721 | ch.data = release_version |
| 1722 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1723 | elif s_buf_data == 'item': |
| 1724 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1725 | length, newi = getoptarg(length, buf, pp, i) |
| 1726 | ingroupch = pp[i:newi] |
| 1727 | del pp[i:newi] |
| 1728 | length = length - (newi-i) |
Fred Drake | 893e5e0 | 1996-10-25 22:13:10 +0000 | [diff] [blame] | 1729 | changeit(buf, ingroupch) # catch stuff inside the optional arg |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1730 | pp.insert(i, chunk(GROUP, ch.where, ingroupch)) |
| 1731 | i, length = i+1, length+1 |
| 1732 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1733 | elif s_buf_data == 'ttindex': |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1734 | idxsi = hist.indexsubitem |
| 1735 | |
| 1736 | cat_class = '' |
| 1737 | if len(idxsi) >= 2 and idxsi[1] in \ |
| 1738 | ('method', 'function', 'protocol'): |
| 1739 | command = 'findex' |
| 1740 | elif len(idxsi) >= 2 and idxsi[1] in \ |
| 1741 | ('exception', 'object'): |
| 1742 | command = 'vindex' |
Fred Drake | 7edd8d3 | 1996-10-09 16:11:26 +0000 | [diff] [blame] | 1743 | elif len(idxsi) == 3 and idxsi[:2] == ['in', 'module']: |
| 1744 | command = 'cindex' |
Fred Drake | 4f6d6e4 | 1998-04-17 15:28:09 +0000 | [diff] [blame] | 1745 | elif len(idxsi) == 3 and idxsi[:2] == ['class', 'in']: |
| 1746 | command = 'findex' |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1747 | else: |
Fred Drake | 7edd8d3 | 1996-10-09 16:11:26 +0000 | [diff] [blame] | 1748 | print 'WARNING: can\'t categorize ' + `idxsi` \ |
| 1749 | + ' for \'ttindex\' command' |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1750 | command = 'cindex' |
| 1751 | |
| 1752 | if not cat_class: |
Fred Drake | 4f6d6e4 | 1998-04-17 15:28:09 +0000 | [diff] [blame] | 1753 | cat_class = '(%s)' % string.join(idxsi) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1754 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1755 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1756 | ch.data = command |
| 1757 | |
| 1758 | length, newi = getnextarg(length, buf, pp, i) |
| 1759 | arg = pp[i:newi] |
| 1760 | del pp[i:newi] |
| 1761 | length = length - (newi-i) |
| 1762 | |
| 1763 | cat_arg = [chunk(PLAIN, ch.where, cat_class)] |
| 1764 | |
| 1765 | # determine what should be set in roman, and |
| 1766 | # what in tt-font |
| 1767 | if command in regindices: |
| 1768 | |
| 1769 | arg = [chunk(CSNAME, ch.where, 't'), |
Fred Drake | 9c7c6be | 1998-02-19 21:40:22 +0000 | [diff] [blame] | 1770 | chunk(GROUP, ch.where, arg)] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1771 | else: |
| 1772 | cat_arg = [chunk(CSNAME, ch.where, 'r'), |
Fred Drake | 9c7c6be | 1998-02-19 21:40:22 +0000 | [diff] [blame] | 1773 | chunk(GROUP, ch.where, cat_arg)] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1774 | |
| 1775 | ingroupch = arg + \ |
| 1776 | [chunk(PLAIN, ch.where, ' ')] + \ |
| 1777 | cat_arg |
| 1778 | |
| 1779 | pp.insert(i, chunk(GROUP, ch.where, ingroupch)) |
| 1780 | length, i = length+1, i+1 |
| 1781 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1782 | elif s_buf_data == 'ldots': |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1783 | # \ldots --> \dots{} --> @dots{} |
| 1784 | ch.data = 'dots' |
| 1785 | if i == length \ |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1786 | or pp[i].chtype != chunk_type[GROUP] \ |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1787 | or pp[i].data != []: |
| 1788 | pp.insert(i, chunk(GROUP, ch.where, [])) |
| 1789 | i, length = i+1, length+1 |
Fred Drake | 74a11e5 | 1998-02-26 05:52:37 +0000 | [diff] [blame] | 1790 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1791 | elif s_buf_data in themselves: |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1792 | # \UNIX --> &UNIX; |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1793 | ch.chtype = chunk_type[PLAIN] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1794 | if i != length \ |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1795 | and pp[i].chtype == chunk_type[GROUP] \ |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1796 | and pp[i].data == []: |
| 1797 | del pp[i] |
| 1798 | length = length-1 |
Fred Drake | 74a11e5 | 1998-02-26 05:52:37 +0000 | [diff] [blame] | 1799 | |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1800 | elif s_buf_data == 'manpage': |
| 1801 | ch.data = 'emph' |
| 1802 | sect = s(buf, pp[i+1].data[0].data) |
| 1803 | pp[i+1].data = "(%s)" % sect |
| 1804 | pp[i+1].chtype = chunk_type[PLAIN] |
| 1805 | |
Fred Drake | 4f6d6e4 | 1998-04-17 15:28:09 +0000 | [diff] [blame] | 1806 | elif s_buf_data == 'envvar': |
| 1807 | # this should do stuff in the index, too... |
| 1808 | ch.data = "$" |
| 1809 | ch.chtype = chunk_type[PLAIN] |
| 1810 | pp[i] = pp[i].data[0] |
| 1811 | |
| 1812 | elif s_buf_data == 'regexp': |
| 1813 | ch.data = 'code' |
| 1814 | pp.insert(i+1, chunk(PLAIN, ch.where, '"')) |
| 1815 | pp.insert(i-1, chunk(PLAIN, ch.where, '"')) |
| 1816 | length = length + 2 |
| 1817 | i = i + 1 |
| 1818 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1819 | elif s_buf_data in ('lineiii', 'lineii'): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1820 | # This is the most tricky one |
| 1821 | # \lineiii{a1}{a2}[{a3}] --> |
| 1822 | # @item @<cts. of itemargmacro>{a1} |
| 1823 | # a2 [ -- a3] |
| 1824 | # |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1825 | if not hist.inenv: |
| 1826 | raise error, 'no environment for lineiii' |
| 1827 | if (hist.inenv[0] != 'tableiii') and \ |
| 1828 | (hist.inenv[0] != 'tableii'): |
| 1829 | raise error, \ |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1830 | 'wrong command (%s) in wrong environment (%s)' \ |
| 1831 | % (s_buf_data, `hist.inenv[0]`) |
| 1832 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1833 | ch.data = 'item' |
| 1834 | length, newi = getnextarg(length, buf, pp, i) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1835 | ingroupch = [chunk(CSNAME, 0, hist.itemargmacro), |
| 1836 | chunk(GROUP, 0, pp[i:newi])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1837 | del pp[i:newi] |
| 1838 | length = length - (newi-i) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1839 | pp.insert(i, chunk(GROUP, ch.where, ingroupch)) |
| 1840 | grouppos = i |
| 1841 | i, length = i+1, length+1 |
| 1842 | length, i = getnextarg(length, buf, pp, i) |
| 1843 | length, newi = getnextarg(length, buf, pp, i) |
| 1844 | if newi > i: |
| 1845 | # we have a 3rd arg |
| 1846 | pp.insert(i, chunk(PLAIN, ch.where, ' --- ')) |
| 1847 | i = newi + 1 |
| 1848 | length = length + 1 |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1849 | if length != len(pp): |
| 1850 | raise 'IN LINEIII IS THE ERR', `i` |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1851 | |
Fred Drake | 74a11e5 | 1998-02-26 05:52:37 +0000 | [diff] [blame] | 1852 | elif s_buf_data in ('chapter', 'section', |
| 1853 | 'subsection', 'subsubsection'): |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1854 | #\xxxsection{A} ----> |
| 1855 | # @node A, , , |
| 1856 | # @xxxsection A |
| 1857 | ## also: remove commas and quotes |
Fred Drake | 4f6d6e4 | 1998-04-17 15:28:09 +0000 | [diff] [blame] | 1858 | hist.this_module = None |
Fred Drake | 74a11e5 | 1998-02-26 05:52:37 +0000 | [diff] [blame] | 1859 | if s_buf_data == "chapter": |
| 1860 | ch.data = hist.chaptertype |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1861 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1862 | length, newi = getnextarg(length, buf, pp, i) |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1863 | afternodenamecmd = next_command_p(length, buf, |
| 1864 | pp, newi, 'nodename') |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1865 | if afternodenamecmd < 0: |
| 1866 | cp1 = crcopy(pp[i:newi]) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1867 | pp[i:newi] = [chunk(GROUP, ch.where, pp[i:newi])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1868 | length, newi = length - (newi-i) + 1, i+1 |
| 1869 | text = flattext(buf, cp1) |
| 1870 | text = invent_node_names(text) |
| 1871 | else: |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 1872 | length, endarg = getnextarg(length, buf, |
| 1873 | pp, afternodenamecmd) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1874 | cp1 = crcopy(pp[afternodenamecmd:endarg]) |
| 1875 | del pp[newi:endarg] |
| 1876 | length = length - (endarg-newi) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1877 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1878 | pp[i:newi] = [chunk(GROUP, ch.where, pp[i:newi])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1879 | length, newi = length - (newi-i) + 1, i + 1 |
| 1880 | text = flattext(buf, cp1) |
| 1881 | if text[-1] == '.': |
| 1882 | text = text[:-1] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1883 | if text in hist.nodenames: |
| 1884 | print 'WARNING: node name ' + `text` + ' already used' |
| 1885 | out.doublenodes.append(text) |
| 1886 | else: |
| 1887 | hist.nodenames.append(text) |
| 1888 | text = rm_commas_etc(text) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1889 | pp[i-1:i-1] = [chunk(CSLINE, ch.where, 'node'), |
| 1890 | chunk(GROUP, ch.where, [ |
| 1891 | chunk(PLAIN, ch.where, text+', , ,') |
| 1892 | ])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1893 | i, length = newi+2, length+2 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1894 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1895 | elif s_buf_data == 'funcline': |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1896 | # fold it to a very short environment |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1897 | pp[i-1:i-1] = [chunk(CSLINE, ch.where, 'end'), |
| 1898 | chunk(GROUP, ch.where, [ |
| 1899 | chunk(PLAIN, ch.where, hist.command)])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1900 | i, length = i+2, length+2 |
| 1901 | length, i = do_funcdesc(length, buf, pp, i) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1902 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1903 | elif s_buf_data == 'dataline': |
| 1904 | pp[i-1:i-1] = [chunk(CSLINE, ch.where, 'end'), |
| 1905 | chunk(GROUP, ch.where, [ |
| 1906 | chunk(PLAIN, ch.where, hist.command)])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1907 | i, length = i+2, length+2 |
| 1908 | length, i = do_datadesc(length, buf, pp, i) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1909 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1910 | elif s_buf_data == 'excline': |
| 1911 | pp[i-1:i-1] = [chunk(CSLINE, ch.where, 'end'), |
| 1912 | chunk(GROUP, ch.where, [ |
| 1913 | chunk(PLAIN, ch.where, hist.command)])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1914 | i, length = i+2, length+2 |
| 1915 | length, i = do_excdesc(length, buf, pp, i) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1916 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1917 | elif s_buf_data == 'index': |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1918 | #\index{A} ---> |
| 1919 | # @cindex A |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1920 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1921 | ch.data = 'cindex' |
| 1922 | length, newi = getnextarg(length, buf, pp, i) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1923 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1924 | ingroupch = pp[i:newi] |
| 1925 | del pp[i:newi] |
| 1926 | length = length - (newi-i) |
| 1927 | pp.insert(i, chunk(GROUP, ch.where, ingroupch)) |
| 1928 | length, i = length+1, i+1 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1929 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1930 | elif s_buf_data == 'bifuncindex': |
| 1931 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1932 | ch.data = 'findex' |
| 1933 | length, newi = getnextarg(length, buf, pp, i) |
| 1934 | ingroupch = pp[i:newi] |
| 1935 | del pp[i:newi] |
| 1936 | length = length - (newi-i) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1937 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1938 | ingroupch.append(chunk(PLAIN, ch.where, ' ')) |
| 1939 | ingroupch.append(chunk(CSNAME, ch.where, 'r')) |
| 1940 | ingroupch.append(chunk(GROUP, ch.where, [ |
| 1941 | chunk(PLAIN, ch.where, |
| 1942 | '(built-in function)')])) |
| 1943 | |
| 1944 | pp.insert(i, chunk(GROUP, ch.where, ingroupch)) |
| 1945 | length, i = length+1, i+1 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1946 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1947 | elif s_buf_data == 'obindex': |
| 1948 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1949 | ch.data = 'findex' |
| 1950 | length, newi = getnextarg(length, buf, pp, i) |
| 1951 | ingroupch = pp[i:newi] |
| 1952 | del pp[i:newi] |
| 1953 | length = length - (newi-i) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1954 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1955 | ingroupch.append(chunk(PLAIN, ch.where, ' ')) |
| 1956 | ingroupch.append(chunk(CSNAME, ch.where, 'r')) |
| 1957 | ingroupch.append(chunk(GROUP, ch.where, [ |
| 1958 | chunk(PLAIN, ch.where, |
| 1959 | '(object)')])) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1960 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1961 | pp.insert(i, chunk(GROUP, ch.where, ingroupch)) |
| 1962 | length, i = length+1, i+1 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1963 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 1964 | elif s_buf_data == 'opindex': |
| 1965 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1966 | ch.data = 'findex' |
| 1967 | length, newi = getnextarg(length, buf, pp, i) |
| 1968 | ingroupch = pp[i:newi] |
| 1969 | del pp[i:newi] |
| 1970 | length = length - (newi-i) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1971 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1972 | ingroupch.append(chunk(PLAIN, ch.where, ' ')) |
| 1973 | ingroupch.append(chunk(CSNAME, ch.where, 'r')) |
| 1974 | ingroupch.append(chunk(GROUP, ch.where, [ |
| 1975 | chunk(PLAIN, ch.where, |
| 1976 | '(operator)')])) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1977 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1978 | pp.insert(i, chunk(GROUP, ch.where, ingroupch)) |
| 1979 | length, i = length+1, i+1 |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 1980 | |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1981 | elif s_buf_data in ('bimodindex', 'refbimodindex'): |
Fred Drake | 4f6d6e4 | 1998-04-17 15:28:09 +0000 | [diff] [blame] | 1982 | length, i = add_module_index( |
| 1983 | pp, length, i, buf, ch, '(built-in)', |
| 1984 | (s_buf_data[:3] == 'ref')) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1985 | |
Fred Drake | 4f6d6e4 | 1998-04-17 15:28:09 +0000 | [diff] [blame] | 1986 | elif s_buf_data in ('modindex', 'refmodindex'): |
| 1987 | length, i = add_module_index( |
| 1988 | pp, length, i, buf, ch, '', |
| 1989 | (s_buf_data[:3] == 'ref')) |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1990 | |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 1991 | elif s_buf_data in ('stmodindex', 'refstmodindex'): |
Fred Drake | 4f6d6e4 | 1998-04-17 15:28:09 +0000 | [diff] [blame] | 1992 | length, i = add_module_index( |
| 1993 | pp, length, i, buf, ch, '(standard)', |
| 1994 | (s_buf_data[:3] == 'ref')) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 1995 | |
Fred Drake | 4f6d6e4 | 1998-04-17 15:28:09 +0000 | [diff] [blame] | 1996 | elif s_buf_data in ('exmodindex', 'refexmodindex'): |
| 1997 | length, i = add_module_index( |
| 1998 | pp, length, i, buf, ch, '(extension)', |
| 1999 | (s_buf_data[:3] == 'ref')) |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 2000 | |
Fred Drake | 74a11e5 | 1998-02-26 05:52:37 +0000 | [diff] [blame] | 2001 | elif s_buf_data == 'stindex': |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2002 | # XXX must actually go to newindex st |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 2003 | what = (s_buf_data[:2] == "st") and "statement" or "keyword" |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2004 | wh = ch.where |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2005 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2006 | ch.data = 'cindex' |
| 2007 | length, newi = getnextarg(length, buf, pp, i) |
| 2008 | ingroupch = [chunk(CSNAME, wh, 'code'), |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 2009 | chunk(GROUP, wh, pp[i:newi])] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2010 | |
| 2011 | del pp[i:newi] |
| 2012 | length = length - (newi-i) |
| 2013 | |
| 2014 | t = ingroupch[:] |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 2015 | t.append(chunk(PLAIN, wh, ' ' + what)) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2016 | |
| 2017 | pp.insert(i, chunk(GROUP, wh, t)) |
| 2018 | i, length = i+1, length+1 |
| 2019 | |
| 2020 | pp.insert(i, chunk(CSLINE, wh, 'cindex')) |
| 2021 | i, length = i+1, length+1 |
| 2022 | |
| 2023 | t = ingroupch[:] |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 2024 | t.insert(0, chunk(PLAIN, wh, what + ', ')) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2025 | |
| 2026 | pp.insert(i, chunk(GROUP, wh, t)) |
| 2027 | i, length = i+1, length+1 |
| 2028 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2029 | elif s_buf_data == 'indexii': |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2030 | #\indexii{A}{B} ---> |
| 2031 | # @cindex A B |
| 2032 | # @cindex B, A |
| 2033 | length, newi = getnextarg(length, buf, pp, i) |
| 2034 | cp11 = pp[i:newi] |
| 2035 | cp21 = crcopy(pp[i:newi]) |
| 2036 | del pp[i:newi] |
| 2037 | length = length - (newi-i) |
| 2038 | length, newi = getnextarg(length, buf, pp, i) |
| 2039 | cp12 = pp[i:newi] |
| 2040 | cp22 = crcopy(pp[i:newi]) |
| 2041 | del pp[i:newi] |
| 2042 | length = length - (newi-i) |
| 2043 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2044 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2045 | ch.data = 'cindex' |
| 2046 | pp.insert(i, chunk(GROUP, ch.where, cp11 + [ |
| 2047 | chunk(PLAIN, ch.where, ' ')] + cp12)) |
| 2048 | i, length = i+1, length+1 |
| 2049 | pp[i:i] = [chunk(CSLINE, ch.where, 'cindex'), |
| 2050 | chunk(GROUP, ch.where, cp22 + [ |
| 2051 | chunk(PLAIN, ch.where, ', ')]+ cp21)] |
| 2052 | i, length = i+2, length+2 |
| 2053 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2054 | elif s_buf_data == 'indexiii': |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2055 | length, newi = getnextarg(length, buf, pp, i) |
| 2056 | cp11 = pp[i:newi] |
| 2057 | cp21 = crcopy(pp[i:newi]) |
| 2058 | cp31 = crcopy(pp[i:newi]) |
| 2059 | del pp[i:newi] |
| 2060 | length = length - (newi-i) |
| 2061 | length, newi = getnextarg(length, buf, pp, i) |
| 2062 | cp12 = pp[i:newi] |
| 2063 | cp22 = crcopy(pp[i:newi]) |
| 2064 | cp32 = crcopy(pp[i:newi]) |
| 2065 | del pp[i:newi] |
| 2066 | length = length - (newi-i) |
| 2067 | length, newi = getnextarg(length, buf, pp, i) |
| 2068 | cp13 = pp[i:newi] |
| 2069 | cp23 = crcopy(pp[i:newi]) |
| 2070 | cp33 = crcopy(pp[i:newi]) |
| 2071 | del pp[i:newi] |
| 2072 | length = length - (newi-i) |
| 2073 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2074 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2075 | ch.data = 'cindex' |
| 2076 | pp.insert(i, chunk(GROUP, ch.where, cp11 + [ |
| 2077 | chunk(PLAIN, ch.where, ' ')] + cp12 |
| 2078 | + [chunk(PLAIN, ch.where, ' ')] |
| 2079 | + cp13)) |
| 2080 | i, length = i+1, length+1 |
| 2081 | pp[i:i] = [chunk(CSLINE, ch.where, 'cindex'), |
| 2082 | chunk(GROUP, ch.where, cp22 + [ |
| 2083 | chunk(PLAIN, ch.where, ' ')]+ cp23 |
| 2084 | + [chunk(PLAIN, ch.where, ', ')] + |
| 2085 | cp21)] |
| 2086 | i, length = i+2, length+2 |
| 2087 | pp[i:i] = [chunk(CSLINE, ch.where, 'cindex'), |
| 2088 | chunk(GROUP, ch.where, cp33 + [ |
| 2089 | chunk(PLAIN, ch.where, ', ')]+ cp31 |
| 2090 | + [chunk(PLAIN, ch.where, ' ')] + |
| 2091 | cp32)] |
| 2092 | i, length = i+2, length+2 |
| 2093 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2094 | elif s_buf_data == 'indexiv': |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2095 | length, newi = getnextarg(length, buf, pp, i) |
| 2096 | cp11 = pp[i:newi] |
| 2097 | cp21 = crcopy(pp[i:newi]) |
| 2098 | cp31 = crcopy(pp[i:newi]) |
| 2099 | cp41 = crcopy(pp[i:newi]) |
| 2100 | del pp[i:newi] |
| 2101 | length = length - (newi-i) |
| 2102 | length, newi = getnextarg(length, buf, pp, i) |
| 2103 | cp12 = pp[i:newi] |
| 2104 | cp22 = crcopy(pp[i:newi]) |
| 2105 | cp32 = crcopy(pp[i:newi]) |
| 2106 | cp42 = crcopy(pp[i:newi]) |
| 2107 | del pp[i:newi] |
| 2108 | length = length - (newi-i) |
| 2109 | length, newi = getnextarg(length, buf, pp, i) |
| 2110 | cp13 = pp[i:newi] |
| 2111 | cp23 = crcopy(pp[i:newi]) |
| 2112 | cp33 = crcopy(pp[i:newi]) |
| 2113 | cp43 = crcopy(pp[i:newi]) |
| 2114 | del pp[i:newi] |
| 2115 | length = length - (newi-i) |
| 2116 | length, newi = getnextarg(length, buf, pp, i) |
| 2117 | cp14 = pp[i:newi] |
| 2118 | cp24 = crcopy(pp[i:newi]) |
| 2119 | cp34 = crcopy(pp[i:newi]) |
| 2120 | cp44 = crcopy(pp[i:newi]) |
| 2121 | del pp[i:newi] |
| 2122 | length = length - (newi-i) |
| 2123 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2124 | ch.chtype = chunk_type[CSLINE] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2125 | ch.data = 'cindex' |
| 2126 | ingroupch = cp11 + \ |
| 2127 | spacech + cp12 + \ |
| 2128 | spacech + cp13 + \ |
| 2129 | spacech + cp14 |
| 2130 | pp.insert(i, chunk(GROUP, ch.where, ingroupch)) |
| 2131 | i, length = i+1, length+1 |
| 2132 | ingroupch = cp22 + \ |
| 2133 | spacech + cp23 + \ |
| 2134 | spacech + cp24 + \ |
| 2135 | commach + cp21 |
| 2136 | pp[i:i] = cindexch + [ |
| 2137 | chunk(GROUP, ch.where, ingroupch)] |
| 2138 | i, length = i+2, length+2 |
| 2139 | ingroupch = cp33 + \ |
| 2140 | spacech + cp34 + \ |
| 2141 | commach + cp31 + \ |
| 2142 | spacech + cp32 |
| 2143 | pp[i:i] = cindexch + [ |
| 2144 | chunk(GROUP, ch.where, ingroupch)] |
| 2145 | i, length = i+2, length+2 |
| 2146 | ingroupch = cp44 + \ |
| 2147 | commach + cp41 + \ |
| 2148 | spacech + cp42 + \ |
| 2149 | spacech + cp43 |
| 2150 | pp[i:i] = cindexch + [ |
| 2151 | chunk(GROUP, ch.where, ingroupch)] |
| 2152 | i, length = i+2, length+2 |
| 2153 | |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 2154 | elif s_buf_data == 'seemodule': |
Fred Drake | 4f6d6e4 | 1998-04-17 15:28:09 +0000 | [diff] [blame] | 2155 | # discard optional arg first: |
| 2156 | length, newi = getoptarg(length, buf, pp, i) |
| 2157 | ingroupch = pp[i:newi] |
| 2158 | del pp[i:newi] |
| 2159 | length = length - (newi-i) |
| 2160 | # |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 2161 | ch.data = "code" |
| 2162 | data = pp[i+1].data |
Fred Drake | 4f6d6e4 | 1998-04-17 15:28:09 +0000 | [diff] [blame] | 2163 | data.insert(0, chunk(PLAIN, ch.where, " (")) |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 2164 | data.append(chunk(PLAIN, ch.where, ")")) |
| 2165 | pp[i+1:i+2] = data |
| 2166 | length = length + len(data) - 1 |
| 2167 | |
| 2168 | elif s_buf_data == 'seetext': |
| 2169 | data = pp[i].data |
| 2170 | data.insert(0, chunk(ENDLINE, ch.where, "\n")) |
| 2171 | pp[i-1:i+1] = data |
| 2172 | i = i - 1 |
| 2173 | length = length + len(data) - 2 |
| 2174 | |
Fred Drake | b98cd39 | 1998-03-04 06:33:43 +0000 | [diff] [blame] | 2175 | elif s_buf_data == 'deprecated': |
| 2176 | length, newi = getnextarg(length, buf, pp, i) |
| 2177 | version = pp[i:newi][0] |
Fred Drake | b98cd39 | 1998-03-04 06:33:43 +0000 | [diff] [blame] | 2178 | length, newi2 = getnextarg(length, buf, pp, newi) |
| 2179 | action = pp[newi:newi2] |
Fred Drake | b98cd39 | 1998-03-04 06:33:43 +0000 | [diff] [blame] | 2180 | del pp[i-1:newi2] |
| 2181 | length = length - (newi2 - i) - 1 |
| 2182 | stuff = [chunk(PLAIN, ch.where, 'Deprecated since release '), |
| 2183 | version, |
| 2184 | chunk(PLAIN, ch.where, '.')] |
| 2185 | chunks = [chunk(CSNAME, ch.where, 'strong'), |
| 2186 | chunk(GROUP, ch.where, stuff), |
| 2187 | chunk(PLAIN, ch.where, ' ')] + action \ |
| 2188 | + [chunk(DENDLINE, ch.where, '\n')] |
Fred Drake | 4f6d6e4 | 1998-04-17 15:28:09 +0000 | [diff] [blame] | 2189 | stuff = None |
Fred Drake | b98cd39 | 1998-03-04 06:33:43 +0000 | [diff] [blame] | 2190 | i = i - 1 |
| 2191 | pp[i:i] = chunks |
| 2192 | length = length + len(chunks) |
| 2193 | |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 2194 | elif s_buf_data == "quad": |
| 2195 | ch.chtype = PLAIN |
| 2196 | ch.data = " " |
| 2197 | |
Fred Drake | 74a11e5 | 1998-02-26 05:52:37 +0000 | [diff] [blame] | 2198 | elif s_buf_data in ('usepackage', 'input'): |
| 2199 | del pp[i-1:i+1] |
| 2200 | i, length = i-1, length-2 |
| 2201 | |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 2202 | elif s_buf_data in ('noindent', 'indexsubitem', 'footnote'): |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2203 | pass |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2204 | |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 2205 | elif s_buf_data == 'label': |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 2206 | name = s(buf, pp[i].data[0].data) |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 2207 | del pp[i-1:i+1] |
| 2208 | length = length - 2 |
| 2209 | i = i - 1 |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 2210 | label_nodes[name] = hist.nodenames[-1] |
| 2211 | |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 2212 | elif s_buf_data == 'rfc': |
| 2213 | ch.chtype = chunk_type[PLAIN] |
| 2214 | ch.data = "RFC " + s(buf, pp[i].data[0].data) |
| 2215 | del pp[i] |
| 2216 | length = length - 1 |
| 2217 | |
Fred Drake | 43c9350 | 1997-12-29 21:40:35 +0000 | [diff] [blame] | 2218 | elif s_buf_data == 'ref': |
| 2219 | name = s(buf, pp[i].data[0].data) |
| 2220 | if label_nodes.has_key(name): |
| 2221 | pp[i].data[0].data = label_nodes[name] |
| 2222 | else: |
| 2223 | pp[i-1:i+1] = [ |
| 2224 | chunk(PLAIN, ch.where, |
| 2225 | "(unknown node reference: %s)" % name)] |
| 2226 | length = length - 1 |
| 2227 | print "WARNING: unknown node label", `name` |
Fred Drake | a4541af | 1997-12-29 17:19:22 +0000 | [diff] [blame] | 2228 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2229 | else: |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2230 | 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] | 2231 | |
| 2232 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 2233 | re_atsign = regex.compile('[@{}]') |
| 2234 | re_newline = regex.compile('\n') |
| 2235 | |
| 2236 | def dumpit(buf, wm, pp): |
| 2237 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2238 | global out |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 2239 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2240 | i, length = 0, len(pp) |
| 2241 | |
| 2242 | addspace = 0 |
| 2243 | |
| 2244 | while 1: |
| 2245 | if len(pp) != length: |
| 2246 | raise 'FATAL', 'inconsistent length' |
| 2247 | if i == length: |
| 2248 | break |
| 2249 | ch = pp[i] |
| 2250 | i = i + 1 |
| 2251 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2252 | dospace = addspace |
| 2253 | addspace = 0 |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2254 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2255 | if ch.chtype == chunk_type[CSNAME]: |
| 2256 | s_buf_data = s(buf, ch.data) |
Fred Drake | 74a11e5 | 1998-02-26 05:52:37 +0000 | [diff] [blame] | 2257 | ## if s_buf_data == 'e': |
| 2258 | ## wm('\\') |
| 2259 | ## continue |
| 2260 | ## if s_buf_data == '$': |
| 2261 | ## wm('$') |
| 2262 | ## continue |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2263 | wm('@' + s_buf_data) |
| 2264 | if s_buf_data == 'node' and \ |
| 2265 | pp[i].chtype == chunk_type[PLAIN] and \ |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2266 | s(buf, pp[i].data) in out.doublenodes: |
| 2267 | ##XXX doesnt work yet?? |
| 2268 | wm(' ZZZ-' + zfill(`i`, 4)) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2269 | if s_buf_data[0] in string.letters: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2270 | addspace = 1 |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2271 | elif ch.chtype == chunk_type[PLAIN]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2272 | if dospace and s(buf, ch.data) not in (' ', '\t'): |
| 2273 | wm(' ') |
| 2274 | text = s(buf, ch.data) |
| 2275 | while 1: |
| 2276 | pos = re_atsign.search(text) |
| 2277 | if pos < 0: |
| 2278 | break |
| 2279 | wm(text[:pos] + '@' + text[pos]) |
| 2280 | text = text[pos+1:] |
| 2281 | wm(text) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2282 | elif ch.chtype == chunk_type[GROUP]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2283 | wm('{') |
| 2284 | dumpit(buf, wm, ch.data) |
| 2285 | wm('}') |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2286 | elif ch.chtype == chunk_type[DENDLINE]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2287 | wm('\n\n') |
| 2288 | while i != length and pp[i].chtype in \ |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2289 | (chunk_type[DENDLINE], chunk_type[ENDLINE]): |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 2290 | i = i + 1 |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2291 | elif ch.chtype == chunk_type[OTHER]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2292 | wm(s(buf, ch.data)) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2293 | elif ch.chtype == chunk_type[ACTIVE]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2294 | wm(s(buf, ch.data)) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2295 | elif ch.chtype == chunk_type[ENDLINE]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2296 | wm('\n') |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2297 | elif ch.chtype == chunk_type[CSLINE]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2298 | if i >= 2 and pp[i-2].chtype not in \ |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2299 | (chunk_type[ENDLINE], chunk_type[DENDLINE]) \ |
| 2300 | and (pp[i-2].chtype != chunk_type[PLAIN] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2301 | or s(buf, pp[i-2].data)[-1] != '\n'): |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 2302 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2303 | wm('\n') |
| 2304 | wm('@' + s(buf, ch.data)) |
| 2305 | if i == length: |
| 2306 | raise error, 'CSLINE expected another chunk' |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2307 | if pp[i].chtype != chunk_type[GROUP]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2308 | raise error, 'CSLINE expected GROUP' |
| 2309 | if type(pp[i].data) != ListType: |
| 2310 | raise error, 'GROUP chould contain []-data' |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 2311 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2312 | wobj = Wobj() |
| 2313 | dumpit(buf, wobj.write, pp[i].data) |
| 2314 | i = i + 1 |
| 2315 | text = wobj.data |
| 2316 | del wobj |
| 2317 | if text: |
| 2318 | wm(' ') |
| 2319 | while 1: |
| 2320 | pos = re_newline.search(text) |
| 2321 | if pos < 0: |
| 2322 | break |
Fred Drake | 4f6d6e4 | 1998-04-17 15:28:09 +0000 | [diff] [blame] | 2323 | # these seem to be completely harmless, so don't warn: |
| 2324 | ## print 'WARNING: found newline in csline arg (%s)' \ |
| 2325 | ## % s(buf, ch.data) |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2326 | wm(text[:pos] + ' ') |
| 2327 | text = text[pos+1:] |
| 2328 | wm(text) |
| 2329 | if i >= length or \ |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2330 | pp[i].chtype not in (chunk_type[CSLINE], |
| 2331 | chunk_type[ENDLINE], chunk_type[DENDLINE]) \ |
| 2332 | and (pp[i].chtype != chunk_type[PLAIN] |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2333 | or s(buf, pp[i].data)[0] != '\n'): |
| 2334 | wm('\n') |
Guido van Rossum | 49604d3 | 1996-09-10 22:19:51 +0000 | [diff] [blame] | 2335 | |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2336 | elif ch.chtype == chunk_type[COMMENT]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2337 | if s(buf, ch.data) and \ |
| 2338 | regex.match('^[ \t]*$', s(buf, ch.data)) < 0: |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2339 | if i >= 2 \ |
Fred Drake | 74a11e5 | 1998-02-26 05:52:37 +0000 | [diff] [blame] | 2340 | and pp[i-2].chtype not in (chunk_type[ENDLINE], |
| 2341 | chunk_type[DENDLINE]) \ |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2342 | and not (pp[i-2].chtype == chunk_type[PLAIN] |
| 2343 | 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] | 2344 | wm('\n') |
| 2345 | wm('@c ' + s(buf, ch.data)) |
Guido van Rossum | 36f219d | 1996-09-11 21:30:40 +0000 | [diff] [blame] | 2346 | elif ch.chtype == chunk_type[IGNORE]: |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2347 | pass |
| 2348 | else: |
| 2349 | try: |
| 2350 | str = `s(buf, ch.data)` |
| 2351 | except TypeError: |
| 2352 | str = `ch.data` |
| 2353 | if len(str) > 400: |
| 2354 | str = str[:400] + '...' |
| 2355 | print 'warning:', ch.chtype, 'not handled, data ' + str |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 2356 | |
| 2357 | |
| 2358 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 2359 | def main(): |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 2360 | global release_version |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2361 | outfile = None |
| 2362 | headerfile = 'texipre.dat' |
| 2363 | trailerfile = 'texipost.dat' |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 2364 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2365 | try: |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 2366 | opts, args = getopt.getopt(sys.argv[1:], 'o:h:t:v:') |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2367 | except getopt.error: |
| 2368 | args = [] |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 2369 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2370 | if not args: |
| 2371 | print 'usage: partparse [-o outfile] [-h headerfile]', |
| 2372 | print '[-t trailerfile] file ...' |
| 2373 | sys.exit(2) |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 2374 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2375 | for opt, arg in opts: |
| 2376 | if opt == '-o': outfile = arg |
| 2377 | if opt == '-h': headerfile = arg |
| 2378 | if opt == '-t': trailerfile = arg |
Fred Drake | e8b4613 | 1998-02-17 05:54:46 +0000 | [diff] [blame] | 2379 | if opt == '-v': release_version = arg |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 2380 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2381 | if not outfile: |
| 2382 | root, ext = os.path.splitext(args[0]) |
| 2383 | outfile = root + '.texi' |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 2384 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2385 | if outfile in args: |
| 2386 | print 'will not overwrite input file', outfile |
| 2387 | sys.exit(2) |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 2388 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2389 | outf = open(outfile, 'w') |
| 2390 | outf.write(open(headerfile, 'r').read()) |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 2391 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2392 | for file in args: |
| 2393 | if len(args) > 1: print '='*20, file, '='*20 |
| 2394 | buf = open(file, 'r').read() |
Fred Drake | b98cd39 | 1998-03-04 06:33:43 +0000 | [diff] [blame] | 2395 | chunk.buf = buf |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2396 | w, pp = parseit(buf) |
| 2397 | startchange() |
| 2398 | changeit(buf, pp) |
| 2399 | dumpit(buf, outf.write, pp) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 2400 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2401 | outf.write(open(trailerfile, 'r').read()) |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 2402 | |
Guido van Rossum | 5f18d6c | 1996-09-10 22:34:20 +0000 | [diff] [blame] | 2403 | outf.close() |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 2404 | |
Guido van Rossum | 49604d3 | 1996-09-10 22:19:51 +0000 | [diff] [blame] | 2405 | if __name__ == "__main__": |
| 2406 | main() |