2018-07-31 | Paul Boddie | raw files shortlog changelog graph | Added anchor syntax and Moin-compatible macro. | |
moinformat/macros/anchor.py (file) moinformat/parsers/moin.py (file) moinformat/serialisers/html/moin.py (file) moinformat/serialisers/moin/moin.py (file) moinformat/tree/moin.py (file) tests/test_anchors.tree (file) tests/test_anchors.txt (file) |
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/moinformat/macros/anchor.py Tue Jul 31 19:15:48 2018 +0200 1.3 @@ -0,0 +1,46 @@ 1.4 +#!/usr/bin/env python 1.5 + 1.6 +""" 1.7 +Anchor macro for Moin compatibility. 1.8 + 1.9 +Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk> 1.10 + 1.11 +This program is free software; you can redistribute it and/or modify it under 1.12 +the terms of the GNU General Public License as published by the Free Software 1.13 +Foundation; either version 3 of the License, or (at your option) any later 1.14 +version. 1.15 + 1.16 +This program is distributed in the hope that it will be useful, but WITHOUT 1.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 1.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 1.19 +details. 1.20 + 1.21 +You should have received a copy of the GNU General Public License along with 1.22 +this program. If not, see <http://www.gnu.org/licenses/>. 1.23 +""" 1.24 + 1.25 +from moinformat.macros.common import Macro 1.26 +from moinformat.tree.moin import Anchor 1.27 + 1.28 +class AnchorMacro(Macro): 1.29 + 1.30 + "An anchor macro." 1.31 + 1.32 + name = "Anchor" 1.33 + 1.34 + def evaluate(self): 1.35 + 1.36 + "Evaluate the macro, producing an anchor node." 1.37 + 1.38 + # Obtain the concatenated arguments since they might contain commas. 1.39 + 1.40 + argstr = ",".join(self.node.args) 1.41 + 1.42 + # Replace the macro node with the anchor. 1.43 + 1.44 + macro = self.node 1.45 + macro.parent.replace(macro, Anchor(argstr)) 1.46 + 1.47 +macro = AnchorMacro 1.48 + 1.49 +# vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/moinformat/parsers/moin.py Tue Jul 31 19:14:38 2018 +0200 2.2 +++ b/moinformat/parsers/moin.py Tue Jul 31 19:15:48 2018 +0200 2.3 @@ -35,12 +35,12 @@ 2.4 2.5 # Document tree nodes. 2.6 2.7 -from moinformat.tree.moin import Break, DefItem, DefTerm, FontStyle, Heading, \ 2.8 - Larger, LineBreak, Link, List, ListItem, \ 2.9 - Macro, Monospace, Region, Rule, Smaller, \ 2.10 - Strikethrough, Subscript, Superscript, Table, \ 2.11 - TableAttr, TableAttrs, TableCell, TableRow, \ 2.12 - Text, Underline 2.13 +from moinformat.tree.moin import Anchor, Break, DefItem, DefTerm, FontStyle, \ 2.14 + Heading, Larger, LineBreak, Link, List, \ 2.15 + ListItem, Macro, Monospace, Region, Rule, \ 2.16 + Smaller, Strikethrough, Subscript, \ 2.17 + Superscript, Table, TableAttr, TableAttrs, \ 2.18 + TableCell, TableRow, Text, Underline 2.19 2.20 join = "".join 2.21 2.22 @@ -481,6 +481,11 @@ 2.23 2.24 # Complete inline pattern handlers. 2.25 2.26 + def parse_anchor(self, region): 2.27 + target = self.match_group("target") 2.28 + anchor = Anchor(target) 2.29 + region.append_inline(anchor) 2.30 + 2.31 def parse_linebreak(self, region): 2.32 region.append_inline(LineBreak()) 2.33 2.34 @@ -619,6 +624,10 @@ 2.35 # Complete inline patterns are for markup features that do not support 2.36 # arbitrary content within them: 2.37 2.38 + "anchor" : join((r"\(\(", # (( 2.39 + group("target", ".*?"), # target 2.40 + r"\)\)")), # )) 2.41 + 2.42 "linebreak" : r"\\\\", # \\ 2.43 2.44 "link" : join((r"\[\[", # [[ 2.45 @@ -706,8 +715,9 @@ 2.46 ] 2.47 2.48 inline_pattern_names = [ 2.49 - "fontstyle", "larger", "linebreak", "link", "macro", "monospace", 2.50 - "regionstart", "smaller", "strike", "sub", "super", "underline", 2.51 + "anchor", "fontstyle", "larger", "linebreak", "link", "macro", 2.52 + "monospace", "regionstart", "smaller", "strike", "sub", "super", 2.53 + "underline", 2.54 ] 2.55 2.56 list_pattern_names = [ 2.57 @@ -740,6 +750,7 @@ 2.58 2.59 handlers = { 2.60 None : end_region, 2.61 + "anchor" : parse_anchor, 2.62 "attrname" : parse_attrname, 2.63 "break" : parse_break, 2.64 "colour" : parse_colour,
3.1 --- a/moinformat/serialisers/html/moin.py Tue Jul 31 19:14:38 2018 +0200 3.2 +++ b/moinformat/serialisers/html/moin.py Tue Jul 31 19:15:48 2018 +0200 3.3 @@ -247,6 +247,9 @@ 3.4 def end_underline(self): 3.5 self.out("</span>") 3.6 3.7 + def anchor(self, target): 3.8 + self.out("<a name='%s' />" % escape_attr(make_id(target))) 3.9 + 3.10 def break_(self): 3.11 pass 3.12
4.1 --- a/moinformat/serialisers/moin/moin.py Tue Jul 31 19:14:38 2018 +0200 4.2 +++ b/moinformat/serialisers/moin/moin.py Tue Jul 31 19:15:48 2018 +0200 4.3 @@ -179,6 +179,9 @@ 4.4 def end_underline(self): 4.5 self.out("__") 4.6 4.7 + def anchor(self, target): 4.8 + self.out("((%s))" % target) 4.9 + 4.10 def break_(self): 4.11 self.out("\n") 4.12
5.1 --- a/moinformat/tree/moin.py Tue Jul 31 19:14:38 2018 +0200 5.2 +++ b/moinformat/tree/moin.py Tue Jul 31 19:15:48 2018 +0200 5.3 @@ -584,6 +584,22 @@ 5.4 def empty(self): 5.5 return False 5.6 5.7 +class Anchor(Node): 5.8 + 5.9 + "Anchor details." 5.10 + 5.11 + def __init__(self, target): 5.12 + self.target = target 5.13 + 5.14 + def __repr__(self): 5.15 + return "Anchor(%r)" % self.target 5.16 + 5.17 + def prettyprint(self, indent=""): 5.18 + return "%sAnchor: target=%r" % (indent, self.target) 5.19 + 5.20 + def to_string(self, out): 5.21 + out.anchor(self.target) 5.22 + 5.23 class Break(Node): 5.24 5.25 "A paragraph break."
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/tests/test_anchors.tree Tue Jul 31 19:15:48 2018 +0200 6.3 @@ -0,0 +1,43 @@ 6.4 +Region 6.5 + Heading 6.6 + Text 6.7 + Break 6.8 + Block 6.9 + Anchor 6.10 + Text 6.11 + Break 6.12 + Block 6.13 + Anchor 6.14 + Text 6.15 + Break 6.16 + Heading 6.17 + Text 6.18 + Underline 6.19 + Text 6.20 + Break 6.21 + Block 6.22 + Text 6.23 + Break 6.24 + Block 6.25 + Anchor 6.26 + Text 6.27 + Break 6.28 + Block 6.29 + Text 6.30 + Link 6.31 + Text 6.32 + Text 6.33 + Break 6.34 + Block 6.35 + Macro 6.36 + Text 6.37 + Heading 6.38 + Text 6.39 + Break 6.40 + Block 6.41 + Text 6.42 + Break 6.43 + Block 6.44 + Text 6.45 + Link 6.46 + Text
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/tests/test_anchors.txt Tue Jul 31 19:15:48 2018 +0200 7.3 @@ -0,0 +1,22 @@ 7.4 += Level 1 = 7.5 + 7.6 +((Number #1 anchor)) 7.7 +Some text. 7.8 + 7.9 +((Text, anchored.)) Some text referenced elsewhere. 7.10 + 7.11 += Heading __Underlined__ = 7.12 + 7.13 +Some more text. 7.14 + 7.15 +((Anchored!)) 7.16 +More anchored text. 7.17 + 7.18 +A [[#Heading Underlined|link]] to the above heading. 7.19 + 7.20 +<<Anchor(Anchored, using macro)>> 7.21 += Macro-Anchored Heading = 7.22 + 7.23 +Yet more text. 7.24 + 7.25 +Link to the [[#Number #1 anchor]].