1.1 --- a/moinformat/__init__.py Mon May 01 22:51:12 2017 +0200
1.2 +++ b/moinformat/__init__.py Mon May 01 23:36:31 2017 +0200
1.3 @@ -20,8 +20,8 @@
1.4 """
1.5
1.6 from moinformat.tree import Block, Break, DefItem, DefTerm, FontStyle, Heading, \
1.7 - ListItem, Monospace, Region, Rule, Subscript, \
1.8 - Superscript, Text, Underline
1.9 + Larger, ListItem, Monospace, Region, Rule, Smaller, \
1.10 + Subscript, Superscript, Text, Underline
1.11 import re
1.12
1.13 # Regular expressions.
1.14 @@ -56,14 +56,18 @@
1.15 # Region contents:
1.16 # Inline patterns:
1.17 "fontstyle" : r"('{2,6})",
1.18 + "larger" : r"~\+",
1.19 "monospace" : r"`",
1.20 "rule" : r"(-----*)", # ----...
1.21 + "smaller" : r"~-",
1.22 "sub" : r",,",
1.23 "super" : r"\^",
1.24 "underline" : r"__",
1.25
1.26 # Inline contents:
1.27 + "largerend" : r"\+~",
1.28 "monospaceend" : r"`",
1.29 + "smallerend" : r"-~",
1.30 "subend" : r",,",
1.31 "superend" : r"\^",
1.32 "underlineend" : r"__",
1.33 @@ -77,19 +81,21 @@
1.34 "listitemend" : r"^", # next line
1.35 }
1.36
1.37 +# Define inline pattern details.
1.38 +
1.39 +inline_pattern_names = ["fontstyle", "larger", "monospace", "smaller", "sub", "super", "underline"]
1.40 +
1.41 +def inline_patterns_for(name):
1.42 + names = inline_pattern_names[:]
1.43 + names[names.index(name)] = "%send" % name
1.44 + return names
1.45 +
1.46 # Define patterns for the regular expressions.
1.47
1.48 patterns = {}
1.49 for name, value in syntax.items():
1.50 patterns[name] = re.compile(value, re.UNICODE | re.MULTILINE)
1.51
1.52 -inline_pattern_names = ["fontstyle", "monospace", "sub", "super", "underline"]
1.53 -
1.54 -def inline_patterns_for(name):
1.55 - names = inline_pattern_names[:]
1.56 - names[names.index(name)] = "%send" % name
1.57 - return names
1.58 -
1.59
1.60
1.61 # Tokenising functions.
1.62 @@ -372,14 +378,6 @@
1.63 region.add(item)
1.64 new_block(region)
1.65
1.66 -def parse_monospace(items, region):
1.67 -
1.68 - "Handle monospace."
1.69 -
1.70 - span = Monospace([])
1.71 - parse_region_details(items, span, inline_patterns_for("monospace"))
1.72 - region.append_inline(span)
1.73 -
1.74 def parse_rule(items, region):
1.75
1.76 "Handle a horizontal rule within 'region'."
1.77 @@ -410,29 +408,22 @@
1.78 else:
1.79 region.append_inline(Text(feature))
1.80
1.81 -def parse_sub(items, region):
1.82 +# Inline formatting handlers.
1.83 +
1.84 +def parse_inline(items, region, cls, pattern_name):
1.85
1.86 - "Handle subscript."
1.87 + "Handle an inline region."
1.88
1.89 - span = Subscript([])
1.90 - parse_region_details(items, span, inline_patterns_for("sub"))
1.91 + span = cls([])
1.92 + parse_region_details(items, span, inline_patterns_for(pattern_name))
1.93 region.append_inline(span)
1.94
1.95 -def parse_super(items, region):
1.96 -
1.97 - "Handle superscript."
1.98 -
1.99 - span = Superscript([])
1.100 - parse_region_details(items, span, inline_patterns_for("super"))
1.101 - region.append_inline(span)
1.102 -
1.103 -def parse_underline(items, region):
1.104 -
1.105 - "Handle underline."
1.106 -
1.107 - span = Underline([])
1.108 - parse_region_details(items, span, inline_patterns_for("underline"))
1.109 - region.append_inline(span)
1.110 +parse_larger = lambda items, region: parse_inline(items, region, Larger, "larger")
1.111 +parse_monospace = lambda items, region: parse_inline(items, region, Monospace, "monospace")
1.112 +parse_smaller = lambda items, region: parse_inline(items, region, Smaller, "smaller")
1.113 +parse_sub = lambda items, region: parse_inline(items, region, Subscript, "sub")
1.114 +parse_super = lambda items, region: parse_inline(items, region, Superscript, "super")
1.115 +parse_underline = lambda items, region: parse_inline(items, region, Underline, "underline")
1.116
1.117 # Pattern handlers.
1.118
1.119 @@ -446,6 +437,8 @@
1.120 "fontstyle" : parse_fontstyle,
1.121 "heading" : parse_heading,
1.122 "headingend" : parse_heading_end,
1.123 + "larger" : parse_larger,
1.124 + "largerend" : end_region,
1.125 "listitemend" : end_region,
1.126 "listitem" : parse_listitem,
1.127 "listitem_alpha" : parse_listitem,
1.128 @@ -457,6 +450,8 @@
1.129 "regionstart" : parse_section,
1.130 "regionend" : parse_section_end,
1.131 "rule" : parse_rule,
1.132 + "smaller" : parse_smaller,
1.133 + "smallerend" : end_region,
1.134 "sub" : parse_sub,
1.135 "subend" : end_region,
1.136 "super" : parse_super,
2.1 --- a/moinformat/serialisers.py Mon May 01 22:51:12 2017 +0200
2.2 +++ b/moinformat/serialisers.py Mon May 01 23:36:31 2017 +0200
2.3 @@ -74,6 +74,12 @@
2.4 def end_heading(self, level, pad, extra):
2.5 self.out(pad + "=" * level + extra)
2.6
2.7 + def start_larger(self):
2.8 + self.out("~+")
2.9 +
2.10 + def end_larger(self):
2.11 + self.out("+~")
2.12 +
2.13 def start_listitem(self, indent, marker, space):
2.14 self.out("%s%s%s" % (indent * " ", marker, space))
2.15
2.16 @@ -86,6 +92,12 @@
2.17 def end_monospace(self):
2.18 self.out("`")
2.19
2.20 + def start_smaller(self):
2.21 + self.out("~-")
2.22 +
2.23 + def end_smaller(self):
2.24 + self.out("-~")
2.25 +
2.26 def start_strong(self):
2.27 self.out("'''")
2.28
2.29 @@ -172,6 +184,12 @@
2.30 def end_heading(self, level, pad, extra):
2.31 self.out("</h%d>" % level)
2.32
2.33 + def start_larger(self):
2.34 + self.out("<big>")
2.35 +
2.36 + def end_larger(self):
2.37 + self.out("</big>")
2.38 +
2.39 def start_listitem(self, indent, marker, space):
2.40 self.out("<li>")
2.41
2.42 @@ -184,6 +202,12 @@
2.43 def end_monospace(self):
2.44 self.out("</tt>")
2.45
2.46 + def start_smaller(self):
2.47 + self.out("<small>")
2.48 +
2.49 + def end_smaller(self):
2.50 + self.out("</small>")
2.51 +
2.52 def start_strong(self):
2.53 self.out("<strong>")
2.54
3.1 --- a/moinformat/tree.py Mon May 01 22:51:12 2017 +0200
3.2 +++ b/moinformat/tree.py Mon May 01 23:36:31 2017 +0200
3.3 @@ -276,65 +276,68 @@
3.4 self._to_string(out)
3.5 out.end_listitem(self.indent, self.marker)
3.6
3.7 -class Monospace(Container):
3.8 +
3.9
3.10 - "Monospaced text."
3.11 +class Inline(Container):
3.12 +
3.13 + "Generic inline formatting."
3.14
3.15 def __repr__(self):
3.16 - return "Monospace(%r)" % self.nodes
3.17 + return "%s(%r)" % (self.__class__.__name__, self.nodes)
3.18
3.19 def prettyprint(self, indent=""):
3.20 - l = ["%sMonospace" % indent]
3.21 + l = ["%s%s" % (indent, self.__class__.__name__)]
3.22 return self._prettyprint(l, indent)
3.23
3.24 +class Larger(Inline):
3.25 +
3.26 + "Larger text."
3.27 +
3.28 + def to_string(self, out):
3.29 + out.start_larger()
3.30 + self._to_string(out)
3.31 + out.end_larger()
3.32 +
3.33 +class Monospace(Inline):
3.34 +
3.35 + "Monospaced text."
3.36 +
3.37 def to_string(self, out):
3.38 out.start_monospace()
3.39 self._to_string(out)
3.40 out.end_monospace()
3.41
3.42 -class Subscript(Container):
3.43 +class Smaller(Inline):
3.44 +
3.45 + "Smaller text."
3.46 +
3.47 + def to_string(self, out):
3.48 + out.start_smaller()
3.49 + self._to_string(out)
3.50 + out.end_smaller()
3.51 +
3.52 +class Subscript(Inline):
3.53
3.54 "Subscripted text."
3.55
3.56 - def __repr__(self):
3.57 - return "Subscript(%r)" % self.nodes
3.58 -
3.59 - def prettyprint(self, indent=""):
3.60 - l = ["%sSubscript" % indent]
3.61 - return self._prettyprint(l, indent)
3.62 -
3.63 def to_string(self, out):
3.64 out.start_subscript()
3.65 self._to_string(out)
3.66 out.end_subscript()
3.67
3.68 -class Superscript(Container):
3.69 +class Superscript(Inline):
3.70
3.71 "Superscripted text."
3.72
3.73 - def __repr__(self):
3.74 - return "Superscript(%r)" % self.nodes
3.75 -
3.76 - def prettyprint(self, indent=""):
3.77 - l = ["%sSuperscript" % indent]
3.78 - return self._prettyprint(l, indent)
3.79 -
3.80 def to_string(self, out):
3.81 out.start_superscript()
3.82 self._to_string(out)
3.83 out.end_superscript()
3.84
3.85 -class Underline(Container):
3.86 +class Underline(Inline):
3.87
3.88 "Underlined text."
3.89
3.90 - def __repr__(self):
3.91 - return "Underline(%r)" % self.nodes
3.92 -
3.93 - def prettyprint(self, indent=""):
3.94 - l = ["%sUnderline" % indent]
3.95 - return self._prettyprint(l, indent)
3.96 -
3.97 def to_string(self, out):
3.98 out.start_underline()
3.99 self._to_string(out)