# HG changeset patch # User Paul Boddie # Date 1493918011 -7200 # Node ID 90273d805859c419336727337cb6718006df7f24 # Parent 077c49a4d6701f626f8fbe5d2f770ef8c7b7d220 Reordered the functions and reformatted slightly. diff -r 077c49a4d670 -r 90273d805859 moinformat/__init__.py --- a/moinformat/__init__.py Thu May 04 17:11:52 2017 +0200 +++ b/moinformat/__init__.py Thu May 04 19:13:31 2017 +0200 @@ -189,7 +189,59 @@ -# Parser functions. +# Parsing utilities. + +def parse_region_details(items, region, pattern_names): + + "Parse 'items' within 'region' searching using 'pattern_names'." + + try: + while True: + + # Obtain text before any marker or the end of the input. + + preceding = items.read_until(pattern_names) + if preceding: + region.append_inline(Text(preceding)) + + # End of input. + + if not items.matching: + break + + # Obtain any feature. + + feature = items.read_match() + handler = handlers.get(items.matching) + + # Handle each feature or add text to the region. + + if handler: + handler(items, region) + else: + region.append_inline(Text(feature)) + + except StopIteration: + pass + + region.normalise() + +def end_region(items, region): + + "End the parsing of 'region', breaking out of the parsing loop." + + raise StopIteration + +def new_block(region): + + "Start a new block in 'region'." + + block = Block([]) + region.add(block) + + + +# Parser functions for different page features. def parse_page(s): @@ -251,47 +303,6 @@ parse_region_details(items, region, ["regionend"]) -def parse_region_details(items, region, pattern_names): - - "Parse 'items' within 'region' searching using 'pattern_names'." - - try: - while True: - - # Obtain text before any marker or the end of the input. - - preceding = items.read_until(pattern_names) - if preceding: - region.append_inline(Text(preceding)) - - # End of input. - - if not items.matching: - break - - # Obtain any feature. - - feature = items.read_match() - handler = handlers.get(items.matching) - - # Handle each feature or add text to the region. - - if handler: - handler(items, region) - else: - region.append_inline(Text(feature)) - - except StopIteration: - pass - - region.normalise() - -def end_region(items, region): - - "End the parsing of 'region'." - - raise StopIteration - def parse_attrname(items, attrs): "Handle an attribute name within 'attrs'." @@ -539,6 +550,8 @@ attr = TableAttr("valign", value == "^" and "top" or "bottom", True) attrs.append(attr) + + # Inline formatting handlers. def parse_inline(items, region, cls, pattern_name): @@ -570,6 +583,8 @@ parse_rowspan = lambda items, cell: parse_table_attr(items, cell, "rowspan") parse_width = lambda items, cell: parse_table_attr(items, cell, "width") + + # Pattern handlers. handlers = { @@ -617,13 +632,6 @@ "width" : parse_width, } -def new_block(region): - - "Start a new block in 'region'." - - block = Block([]) - region.add(block) - # Top-level functions.