1.1 --- a/moinformat/__init__.py Thu May 04 17:11:52 2017 +0200
1.2 +++ b/moinformat/__init__.py Thu May 04 19:13:31 2017 +0200
1.3 @@ -189,7 +189,59 @@
1.4
1.5
1.6
1.7 -# Parser functions.
1.8 +# Parsing utilities.
1.9 +
1.10 +def parse_region_details(items, region, pattern_names):
1.11 +
1.12 + "Parse 'items' within 'region' searching using 'pattern_names'."
1.13 +
1.14 + try:
1.15 + while True:
1.16 +
1.17 + # Obtain text before any marker or the end of the input.
1.18 +
1.19 + preceding = items.read_until(pattern_names)
1.20 + if preceding:
1.21 + region.append_inline(Text(preceding))
1.22 +
1.23 + # End of input.
1.24 +
1.25 + if not items.matching:
1.26 + break
1.27 +
1.28 + # Obtain any feature.
1.29 +
1.30 + feature = items.read_match()
1.31 + handler = handlers.get(items.matching)
1.32 +
1.33 + # Handle each feature or add text to the region.
1.34 +
1.35 + if handler:
1.36 + handler(items, region)
1.37 + else:
1.38 + region.append_inline(Text(feature))
1.39 +
1.40 + except StopIteration:
1.41 + pass
1.42 +
1.43 + region.normalise()
1.44 +
1.45 +def end_region(items, region):
1.46 +
1.47 + "End the parsing of 'region', breaking out of the parsing loop."
1.48 +
1.49 + raise StopIteration
1.50 +
1.51 +def new_block(region):
1.52 +
1.53 + "Start a new block in 'region'."
1.54 +
1.55 + block = Block([])
1.56 + region.add(block)
1.57 +
1.58 +
1.59 +
1.60 +# Parser functions for different page features.
1.61
1.62 def parse_page(s):
1.63
1.64 @@ -251,47 +303,6 @@
1.65
1.66 parse_region_details(items, region, ["regionend"])
1.67
1.68 -def parse_region_details(items, region, pattern_names):
1.69 -
1.70 - "Parse 'items' within 'region' searching using 'pattern_names'."
1.71 -
1.72 - try:
1.73 - while True:
1.74 -
1.75 - # Obtain text before any marker or the end of the input.
1.76 -
1.77 - preceding = items.read_until(pattern_names)
1.78 - if preceding:
1.79 - region.append_inline(Text(preceding))
1.80 -
1.81 - # End of input.
1.82 -
1.83 - if not items.matching:
1.84 - break
1.85 -
1.86 - # Obtain any feature.
1.87 -
1.88 - feature = items.read_match()
1.89 - handler = handlers.get(items.matching)
1.90 -
1.91 - # Handle each feature or add text to the region.
1.92 -
1.93 - if handler:
1.94 - handler(items, region)
1.95 - else:
1.96 - region.append_inline(Text(feature))
1.97 -
1.98 - except StopIteration:
1.99 - pass
1.100 -
1.101 - region.normalise()
1.102 -
1.103 -def end_region(items, region):
1.104 -
1.105 - "End the parsing of 'region'."
1.106 -
1.107 - raise StopIteration
1.108 -
1.109 def parse_attrname(items, attrs):
1.110
1.111 "Handle an attribute name within 'attrs'."
1.112 @@ -539,6 +550,8 @@
1.113 attr = TableAttr("valign", value == "^" and "top" or "bottom", True)
1.114 attrs.append(attr)
1.115
1.116 +
1.117 +
1.118 # Inline formatting handlers.
1.119
1.120 def parse_inline(items, region, cls, pattern_name):
1.121 @@ -570,6 +583,8 @@
1.122 parse_rowspan = lambda items, cell: parse_table_attr(items, cell, "rowspan")
1.123 parse_width = lambda items, cell: parse_table_attr(items, cell, "width")
1.124
1.125 +
1.126 +
1.127 # Pattern handlers.
1.128
1.129 handlers = {
1.130 @@ -617,13 +632,6 @@
1.131 "width" : parse_width,
1.132 }
1.133
1.134 -def new_block(region):
1.135 -
1.136 - "Start a new block in 'region'."
1.137 -
1.138 - block = Block([])
1.139 - region.add(block)
1.140 -
1.141
1.142
1.143 # Top-level functions.