MCPcopy Create free account
hub / github.com/webpy/webpy / read_block_section

Method read_block_section

web/template.py:444–485  ·  view source on GitHub ↗

r""" >>> read_block_section = Parser().read_block_section >>> read_block_section('for i in range(10): hello $i\nfoo') ( ]>, 'foo') >>> read_block_section('for i in range(10):\n hello $

(self, text, begin_indent="")

Source from the content-addressed store, hash-verified

442 return text[: tok.index], text[tok.index :]
443
444 def read_block_section(self, text, begin_indent=""):
445 r"""
446 >>> read_block_section = Parser().read_block_section
447 >>> read_block_section('for i in range(10): hello $i\nfoo')
448 (<block: 'for i in range(10):', [<line: [t'hello ', $i, t'\n']>]>, 'foo')
449 >>> read_block_section('for i in range(10):\n hello $i\n foo', begin_indent=' ')
450 (<block: 'for i in range(10):', [<line: [t'hello ', $i, t'\n']>]>, ' foo')
451 >>> read_block_section('for i in range(10):\n hello $i\nfoo')
452 (<block: 'for i in range(10):', [<line: [t'hello ', $i, t'\n']>]>, 'foo')
453
454 With inline comment:
455
456 >>> read_block_section('for i in range(10): $# inline comment\n hello $i\nfoo')
457 (<block: 'for i in range(10):', []>, ' hello $i\nfoo')
458 """
459 line, text = splitline(text)
460 stmt, line = self.read_statement(line)
461 keyword = self.python_lookahead(stmt)
462
463 # if there is some thing left in the line
464 if line.strip() and not line.lstrip().startswith("$#"):
465 block = line.lstrip()
466 else:
467
468 def find_indent(text):
469 rx = re_compile(" +")
470 match = rx.match(text)
471 first_indent = match and match.group(0)
472 return first_indent or ""
473
474 # find the indentation of the block by looking at the first line
475 first_indent = find_indent(text)[len(begin_indent) :]
476
477 # TODO: fix this special case
478 if keyword == "code":
479 indent = begin_indent + first_indent
480 else:
481 indent = begin_indent + min(first_indent, INDENT)
482
483 block, text = self.read_indented_block(text, indent)
484
485 return self.create_block_node(keyword, stmt, block, begin_indent), text
486
487 def create_block_node(self, keyword, stmt, block, begin_indent):
488 if keyword in self.statement_nodes:

Callers 1

read_sectionMethod · 0.95

Calls 5

read_statementMethod · 0.95
python_lookaheadMethod · 0.95
read_indented_blockMethod · 0.95
create_block_nodeMethod · 0.95
splitlineFunction · 0.85

Tested by

no test coverage detected