1.1 --- a/moinformat.py Thu Apr 27 19:05:27 2017 +0200
1.2 +++ b/moinformat.py Thu Apr 27 23:02:45 2017 +0200
1.3 @@ -26,12 +26,12 @@
1.4
1.5 syntax = {
1.6 # Page regions:
1.7 - "regionstart" : (r"^\s*([{]{3,})", re.MULTILINE | re.DOTALL), # {{{...
1.8 - "regionend" : (r"^\s*([}]{3,})", re.MULTILINE | re.DOTALL), # }}}...
1.9 + "regionstart" : (r"^\s*([{]{3,})", re.MULTILINE | re.DOTALL), # {{{...
1.10 + "regionend" : (r"^\s*([}]{3,})", re.MULTILINE | re.DOTALL), # }}}...
1.11 + "header" : (r"#!(.*?)\n", 0), # #! char-excl-nl
1.12
1.13 # Region contents:
1.14 - "header" : (r"#!(.*?)\n", 0), # #! char-excl-nl
1.15 - "break" : (r"^\s*?\n", re.MULTILINE), # blank line
1.16 + "break" : (r"^(\s*?)\n", re.MULTILINE), # blank line
1.17 }
1.18
1.19 # Define patterns for the regular expressions.
1.20 @@ -282,7 +282,6 @@
1.21 if self.match:
1.22 _start, self.pos = self.match.span()
1.23 s = self.match.group(1)
1.24 - self.match = None
1.25 return s
1.26 else:
1.27 self.pos = len(self.s)
1.28 @@ -298,17 +297,16 @@
1.29 Parse page text 's'. Pages consist of regions delimited by markers.
1.30 """
1.31
1.32 - items = TokenStream(s)
1.33 + return parse_region(TokenStream(s))
1.34
1.35 - # Define a region for the page and parse it.
1.36 +def parse_region(items, level=0):
1.37
1.38 - region = Region([])
1.39 - parse_region(items, region)
1.40 - return region
1.41 + """
1.42 + Parse the data provided by 'items' to populate a region at the given
1.43 + 'level'.
1.44 + """
1.45
1.46 -def parse_region(items, region):
1.47 -
1.48 - "Parse the data provided by 'items' to populate 'region'."
1.49 + region = Region([], level)
1.50
1.51 # Parse section headers.
1.52
1.53 @@ -319,14 +317,24 @@
1.54 else:
1.55 parse_region_opaque(items, region)
1.56
1.57 + return region
1.58 +
1.59 +def parse_region_header(items, region):
1.60 +
1.61 + """
1.62 + Parse the region header from the 'items', setting it for the given 'region'.
1.63 + """
1.64 +
1.65 + if items.read_until(["header"], False) == "": # None means no header
1.66 + region.type = items.read_match()
1.67 +
1.68 def parse_region_wiki(items, region):
1.69
1.70 "Parse the data provided by 'items' to populate a wiki 'region'."
1.71
1.72 # Process exposed text and sections.
1.73
1.74 - block = Block([])
1.75 - region.append(block)
1.76 + block = new_block(region)
1.77
1.78 while True:
1.79
1.80 @@ -349,16 +357,10 @@
1.81
1.82 if items.matching == "regionstart":
1.83
1.84 - # Define the section and parse it.
1.85 + # Parse the section and start a new block after the section.
1.86
1.87 - _region = Region([], len(feature))
1.88 - region.append(_region)
1.89 - parse_region(items, _region)
1.90 -
1.91 - # Start a new block after the section.
1.92 -
1.93 - block = Block([])
1.94 - region.append(block)
1.95 + region.append(parse_region(items, len(feature)))
1.96 + block = new_block(region)
1.97
1.98 # Interpret the given marker, closing the current section if the
1.99 # given marker is the corresponding end marker for the current
1.100 @@ -371,8 +373,7 @@
1.101
1.102 elif items.matching == "break":
1.103 block.final = False
1.104 - block = Block([])
1.105 - region.append(block)
1.106 + block = new_block(region)
1.107
1.108 # Add any inappropriate marker to the text.
1.109
1.110 @@ -385,7 +386,7 @@
1.111
1.112 "Parse the data provided by 'items' to populate an opaque 'region'."
1.113
1.114 - # Process exposed text and sections.
1.115 + # Process exposed text and the section end.
1.116
1.117 while True:
1.118
1.119 @@ -418,14 +419,13 @@
1.120
1.121 region.normalise()
1.122
1.123 -def parse_region_header(items, region):
1.124 +def new_block(region):
1.125 +
1.126 + "Start a new block in 'region'."
1.127
1.128 - """
1.129 - Parse the region header from the 'items', setting it for the given 'region'.
1.130 - """
1.131 -
1.132 - if items.read_until(["header"], False) == "": # None means no header
1.133 - region.type = items.read_match()
1.134 + block = Block([])
1.135 + region.append(block)
1.136 + return block
1.137
1.138
1.139