part is a string of ipython text, comprised of at most one input, one output, comments, and blank lines. The block parser parses the text into a list of:: blocks = [ (TOKEN0, data0), (TOKEN1, data1), ...] where TOKEN is one of [COMMENT | INPUT | OUTPUT ] and data is, de
(part, rgxin, rgxout, fmtin, fmtout)
| 229 | #----------------------------------------------------------------------------- |
| 230 | |
| 231 | def block_parser(part, rgxin, rgxout, fmtin, fmtout): |
| 232 | """ |
| 233 | part is a string of ipython text, comprised of at most one |
| 234 | input, one output, comments, and blank lines. The block parser |
| 235 | parses the text into a list of:: |
| 236 | |
| 237 | blocks = [ (TOKEN0, data0), (TOKEN1, data1), ...] |
| 238 | |
| 239 | where TOKEN is one of [COMMENT | INPUT | OUTPUT ] and |
| 240 | data is, depending on the type of token:: |
| 241 | |
| 242 | COMMENT : the comment string |
| 243 | |
| 244 | INPUT: the (DECORATOR, INPUT_LINE, REST) where |
| 245 | DECORATOR: the input decorator (or None) |
| 246 | INPUT_LINE: the input as string (possibly multi-line) |
| 247 | REST : any stdout generated by the input line (not OUTPUT) |
| 248 | |
| 249 | OUTPUT: the output string, possibly multi-line |
| 250 | |
| 251 | """ |
| 252 | block = [] |
| 253 | lines = part.split('\n') |
| 254 | N = len(lines) |
| 255 | i = 0 |
| 256 | decorator = None |
| 257 | while 1: |
| 258 | |
| 259 | if i==N: |
| 260 | # nothing left to parse -- the last line |
| 261 | break |
| 262 | |
| 263 | line = lines[i] |
| 264 | i += 1 |
| 265 | line_stripped = line.strip() |
| 266 | if line_stripped.startswith('#'): |
| 267 | block.append((COMMENT, line)) |
| 268 | continue |
| 269 | |
| 270 | if any( |
| 271 | line_stripped.startswith("@" + pseudo_decorator) |
| 272 | for pseudo_decorator in PSEUDO_DECORATORS |
| 273 | ): |
| 274 | if decorator: |
| 275 | raise RuntimeError( |
| 276 | "Applying multiple pseudo-decorators on one line is not supported" |
| 277 | ) |
| 278 | else: |
| 279 | decorator = line_stripped |
| 280 | continue |
| 281 | |
| 282 | # does this look like an input line? |
| 283 | matchin = rgxin.match(line) |
| 284 | if matchin: |
| 285 | lineno, inputline = int(matchin.group(1)), matchin.group(2) |
| 286 | |
| 287 | # the ....: continuation string |
| 288 | continuation = ' %s:'%''.join(['.']*(len(str(lineno))+2)) |