3.1 --- a/moinformat/parsers/moin.py Mon Aug 06 15:55:46 2018 +0200
3.2 +++ b/moinformat/parsers/moin.py Tue Aug 07 17:07:03 2018 +0200
3.3 @@ -35,12 +35,13 @@
3.4
3.5 # Document tree nodes.
3.6
3.7 -from moinformat.tree.moin import Anchor, Break, DefItem, DefTerm, FontStyle, \
3.8 - Heading, Larger, LineBreak, Link, List, \
3.9 - ListItem, Macro, Monospace, Region, Rule, \
3.10 - Smaller, Strikethrough, Subscript, \
3.11 - Superscript, Table, TableAttr, TableAttrs, \
3.12 - TableCell, TableRow, Text, Underline
3.13 +from moinformat.tree.moin import Anchor, Break, Comment, DefItem, DefTerm, \
3.14 + Directive, FontStyle, Heading, Larger, \
3.15 + LineBreak, Link, List, ListItem, Macro, \
3.16 + Monospace, Region, Rule, Smaller, \
3.17 + Strikethrough, Subscript, Superscript, Table, \
3.18 + TableAttr, TableAttrs, TableCell, TableRow, \
3.19 + Text, Underline
3.20
3.21 join = "".join
3.22
3.23 @@ -85,9 +86,10 @@
3.24 self.items = self.get_items(s)
3.25 self.region = Region([], type="moin")
3.26
3.27 - # Parse page header.
3.28 + # Parse page header and directives.
3.29
3.30 self.parse_region_header(self.region)
3.31 + self.parse_region_directives(self.region)
3.32
3.33 # Handle pages directly with this parser. Pages do not need to use an
3.34 # explicit format indicator.
3.35 @@ -170,6 +172,15 @@
3.36 self.add_node(region, Break())
3.37 self.new_block(region)
3.38
3.39 + def parse_comment(self, region):
3.40 +
3.41 + "Handle a comment within 'region'."
3.42 +
3.43 + comment = self.match_group("comment")
3.44 + extra = self.match_group("extra")
3.45 + self.add_node(region, Comment(comment, extra))
3.46 + self.new_block(region)
3.47 +
3.48 def parse_defitem(self, region, extra=""):
3.49
3.50 "Handle a definition item within 'region'."
3.51 @@ -205,6 +216,15 @@
3.52 self.parse_region_details(region, ["deftermsep"])
3.53 self.parse_defitem(region, extra)
3.54
3.55 + def parse_directive(self, region):
3.56 +
3.57 + "Handle a processing directive within 'region'."
3.58 +
3.59 + directive = self.match_group("directive")
3.60 + extra = self.match_group("extra")
3.61 + self.add_node(region, Directive(directive, extra))
3.62 + self.new_block(region)
3.63 +
3.64 def parse_fontstyle(self, region):
3.65
3.66 "Handle emphasis and strong styles."
3.67 @@ -581,12 +601,17 @@
3.68 "regionend" : join((r"\N*", # ws... (optional)
3.69 group("feature", join((
3.70 group("level", repeat("[}]", 3)), # }}}...
3.71 - group("extra", r"\n"),
3.72 - "?"))))), # nl (optional)
3.73 + optional(group("extra", r"\n"))))))), # nl (optional)
3.74 +
3.75 + # Region header and directives:
3.76
3.77 "header" : join(("#!", # #!
3.78 group("args", ".*?"), "\n")), # text-excl-nl
3.79
3.80 + "directive" : join((r"^#", # #
3.81 + group("directive", r".*?$"), # rest of line
3.82 + optional(group("extra", r"\n")))), # nl (optional)
3.83 +
3.84 # Region contents:
3.85
3.86 # Line-oriented patterns support features which require their own
3.87 @@ -594,6 +619,10 @@
3.88
3.89 "break" : r"^(\s*?)\n", # blank line
3.90
3.91 + "comment" : join((r"^##", # ##
3.92 + group("comment", r".*?$"), # rest of line
3.93 + optional(group("extra", r"\n")))), # nl (optional)
3.94 +
3.95 "defterm" : join(("^",
3.96 group("pad", r"\N+"), # ws...
3.97 expect(".+?::"))), # text ::
3.98 @@ -703,7 +732,8 @@
3.99 # ws...
3.100
3.101 "listitemend" : join((r"^", # next line
3.102 - choice((excl(r"\N"), # without indent
3.103 + choice((expect(r"[^\s]"), # without indent
3.104 + expect(r"\Z"), # end of string
3.105 expect(r"\N+\*"), # or with ws... list-marker
3.106 expect(r"\N+\d\."), # or with ws... decimal-marker
3.107 expect(r"\N+[aA]\."), # or with ws... alpha-marker
3.108 @@ -771,7 +801,7 @@
3.109 listitem_pattern_names = inline_pattern_names + ["listitemend"]
3.110
3.111 region_without_table_pattern_names = inline_pattern_names + list_pattern_names + [
3.112 - "break", "heading", "defterm", "defterm_empty",
3.113 + "break", "comment", "heading", "defterm", "defterm_empty",
3.114 "regionend", "rule",
3.115 ]
3.116
3.117 @@ -798,10 +828,12 @@
3.118 "break" : parse_break,
3.119 "colour" : parse_colour,
3.120 "colspan" : parse_colspan,
3.121 + "comment" : parse_comment,
3.122 "defterm" : parse_defterm,
3.123 "defterm_empty" : parse_defterm_empty,
3.124 "deftermend" : end_region,
3.125 "deftermsep" : end_region,
3.126 + "directive" : parse_directive,
3.127 "fontstyle" : parse_fontstyle,
3.128 "halign" : parse_halign,
3.129 "heading" : parse_heading,
7.1 --- a/moinformat/tree/moin.py Mon Aug 06 15:55:46 2018 +0200
7.2 +++ b/moinformat/tree/moin.py Tue Aug 07 17:07:03 2018 +0200
7.3 @@ -621,6 +621,40 @@
7.4 def to_string(self, out):
7.5 out.break_()
7.6
7.7 +class Comment(Node):
7.8 +
7.9 + "A comment."
7.10 +
7.11 + def __init__(self, comment, extra):
7.12 + self.comment = comment
7.13 + self.extra = extra
7.14 +
7.15 + def __repr__(self):
7.16 + return "Comment(%r, %r)" % (self.comment, self.extra)
7.17 +
7.18 + def prettyprint(self, indent=""):
7.19 + return "%sComment: comment=%r extra=%r" % (indent, self.comment, self.extra)
7.20 +
7.21 + def to_string(self, out):
7.22 + out.comment(self.comment, self.extra)
7.23 +
7.24 +class Directive(Node):
7.25 +
7.26 + "A processing directive."
7.27 +
7.28 + def __init__(self, directive, extra):
7.29 + self.directive = directive
7.30 + self.extra = extra
7.31 +
7.32 + def __repr__(self):
7.33 + return "Directive(%r, %r)" % (self.directive, self.extra)
7.34 +
7.35 + def prettyprint(self, indent=""):
7.36 + return "%sDirective: directive=%r extra=%r" % (indent, self.directive, self.extra)
7.37 +
7.38 + def to_string(self, out):
7.39 + out.directive(self.directive, self.extra)
7.40 +
7.41 class LineBreak(Node):
7.42
7.43 "A line break within a block."