1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/MoinShare.py Sat Apr 14 00:42:16 2012 +0200
1.3 @@ -0,0 +1,75 @@
1.4 +# -*- coding: iso-8859-1 -*-
1.5 +"""
1.6 + MoinMoin - MoinShare library
1.7 +
1.8 + @copyright: 2011, 2012 by Paul Boddie <paul@boddie.org.uk>
1.9 + @license: GNU GPL (v2 or later), see COPYING.txt for details.
1.10 +"""
1.11 +
1.12 +from MoinSupport import *
1.13 +from MoinMoin import wikiutil
1.14 +import re
1.15 +
1.16 +escape = wikiutil.escape
1.17 +
1.18 +__version__ = "0.1"
1.19 +
1.20 +# More Moin 1.9 compatibility functions.
1.21 +
1.22 +def has_member(request, groupname, username):
1.23 + if hasattr(request.dicts, "has_member"):
1.24 + return request.dicts.has_member(groupname, username)
1.25 + else:
1.26 + return username in request.dicts.get(groupname, [])
1.27 +
1.28 +# Extraction of shared fragments.
1.29 +
1.30 +marker_regexp_str = r"([{]{3,}|[}]{3,})"
1.31 +marker_regexp = re.compile(marker_regexp_str, re.MULTILINE | re.DOTALL) # {{{... or }}}...
1.32 +
1.33 +def getRegions(s):
1.34 +
1.35 + "Parse the string 's', returning a list of shared regions."
1.36 +
1.37 + regions = []
1.38 + marker = None
1.39 + is_region = True
1.40 +
1.41 + for match_text in marker_regexp.split(s):
1.42 +
1.43 + # Capture section text.
1.44 +
1.45 + if is_region and marker:
1.46 + regions[-1] += match_text
1.47 +
1.48 + # Handle section markers.
1.49 +
1.50 + elif not is_region:
1.51 +
1.52 + # Close any open sections, returning to exposed text regions.
1.53 +
1.54 + if marker:
1.55 + if match_text.startswith("}") and len(marker) == len(match_text):
1.56 + marker = None
1.57 +
1.58 + # Without a current marker, start a section if an appropriate marker
1.59 + # is given.
1.60 +
1.61 + elif match_text.startswith("{"):
1.62 + marker = match_text
1.63 + regions.append("")
1.64 +
1.65 + # Markers and section text are added to the current region.
1.66 +
1.67 + regions[-1] += match_text
1.68 +
1.69 + # Exposed text is ignored.
1.70 +
1.71 + # The match text alternates between text between markers and the markers
1.72 + # themselves.
1.73 +
1.74 + is_region = not is_region
1.75 +
1.76 + return regions
1.77 +
1.78 +# vim: tabstop=4 expandtab shiftwidth=4
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/parsers/MoinShare.py Sat Apr 14 00:42:16 2012 +0200
2.3 @@ -0,0 +1,116 @@
2.4 +# -*- coding: iso-8859-1 -*-
2.5 +"""
2.6 + MoinMoin - MoinShare fragment parser
2.7 +
2.8 + @copyright: 2011, 2012 by Paul Boddie <paul@boddie.org.uk>
2.9 + @license: GNU GPL (v2 or later), see COPYING.txt for details.
2.10 +"""
2.11 +
2.12 +from MoinMoin import wikiutil
2.13 +
2.14 +try:
2.15 + from hashlib import sha1
2.16 +except ImportError:
2.17 + from sha import new as sha1
2.18 +
2.19 +Dependencies = ["pages"]
2.20 +
2.21 +# Parser support.
2.22 +
2.23 +class Parser:
2.24 +
2.25 + """
2.26 + Format shared content fragments of the form...
2.27 +
2.28 + {{{#!MoinShare ref=fragment format=content-type
2.29 +
2.30 + Content...
2.31 + }}}
2.32 + """
2.33 +
2.34 + def __init__(self, raw, request, **kw):
2.35 +
2.36 + """
2.37 + Initialise the parser with the given 'raw' data, 'request' and any
2.38 + keyword arguments that may have been supplied.
2.39 + """
2.40 +
2.41 + self.raw = raw
2.42 + self.request = request
2.43 +
2.44 + attrs, msg = wikiutil.parseAttributes(request, kw.get("format_args", ""))
2.45 +
2.46 + # The attributes returned from the formatting arguments are encoded like
2.47 + # strings.
2.48 +
2.49 + quotes = '"' + "'"
2.50 +
2.51 + # Get any reference to another fragment.
2.52 +
2.53 + self.ref = (attrs.get("ref") or '').strip(quotes)
2.54 + self.format_type = (attrs.get("format") or '').strip(quotes)
2.55 +
2.56 + def getFragmentIdentifier(self):
2.57 +
2.58 + "Make a unique identifier for the fragment."
2.59 +
2.60 + return sha1(self.raw).hexdigest()
2.61 +
2.62 + def format(self, fmt):
2.63 +
2.64 + "Format a fragment using the given formatter 'fmt'."
2.65 +
2.66 + request = self.request
2.67 + page = request.page
2.68 + _ = request.getText
2.69 +
2.70 + request.write(fmt.anchordef(self.getFragmentIdentifier()))
2.71 +
2.72 + request.write(fmt.div(1, css_class="moinsharefragment"))
2.73 + request.write(self.formatText(self.raw, fmt))
2.74 + request.write(fmt.div(0))
2.75 +
2.76 + def getFormat(self):
2.77 +
2.78 + "Get the format used on this page."
2.79 +
2.80 + return self.format_type
2.81 +
2.82 + def getParserClass(self, format):
2.83 +
2.84 + """
2.85 + Return a parser class for the given 'format', returning a plain text
2.86 + parser if no parser can be found for the specified 'format'.
2.87 + """
2.88 +
2.89 + cfg = self.request.cfg
2.90 +
2.91 + try:
2.92 + return wikiutil.searchAndImportPlugin(cfg, "parser", format or "wiki")
2.93 + except wikiutil.PluginMissingError:
2.94 + return wikiutil.searchAndImportPlugin(cfg, "parser", "plain")
2.95 +
2.96 + def formatText(self, text, fmt):
2.97 +
2.98 + """
2.99 + Format the given 'text' using the specified formatter 'fmt'.
2.100 + """
2.101 +
2.102 + request = self.request
2.103 + page = fmt.page = request.page
2.104 + format = self.getFormat()
2.105 +
2.106 + # Suppress line anchors.
2.107 +
2.108 + parser_cls = self.getParserClass(format)
2.109 + parser = parser_cls(text, request, line_anchors=False)
2.110 +
2.111 + # Fix lists by indicating that a paragraph is already started.
2.112 +
2.113 + kw = {}
2.114 + if not format or format == "wiki":
2.115 + kw["inhibit_p"] = True
2.116 +
2.117 + return request.redirectedOutput(parser.format, fmt, **kw)
2.118 +
2.119 +# vim: tabstop=4 expandtab shiftwidth=4
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/setup.py Sat Apr 14 00:42:16 2012 +0200
3.3 @@ -0,0 +1,13 @@
3.4 +#! /usr/bin/env python
3.5 +
3.6 +from distutils.core import setup
3.7 +
3.8 +setup(
3.9 + name = "MoinShare",
3.10 + description = "Share and receive content updates using MoinMoin",
3.11 + author = "Paul Boddie",
3.12 + author_email = "paul@boddie.org.uk",
3.13 + url = "http://moinmo.in/ParserMarket/MoinShare",
3.14 + version = "0.1",
3.15 + py_modules = ["MoinShare"]
3.16 + )
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/tests/test_regions.py Sat Apr 14 00:42:16 2012 +0200
4.3 @@ -0,0 +1,33 @@
4.4 +#!/usr/bin/env python
4.5 +
4.6 +from MoinShare import getRegions
4.7 +
4.8 +s = """
4.9 +Test some shared content...
4.10 +
4.11 +{{{#!MoinShare
4.12 +Hello!
4.13 +
4.14 +This is ''testing'' shared content.
4.15 +}}}
4.16 +
4.17 +{{{#!MoinShare format=html
4.18 +<table>
4.19 +<tr>
4.20 +<td>Test some <strong>HTML</strong> content.</td>
4.21 +<td>This is a table.</td>
4.22 +</tr>
4.23 +</table>
4.24 +}}}
4.25 +
4.26 +Some trailing content.
4.27 +"""
4.28 +
4.29 +regions = getRegions(s)
4.30 +expected = 2
4.31 +
4.32 +print regions
4.33 +print
4.34 +print len(regions) == expected, ": length is", len(regions), "==", expected
4.35 +
4.36 +# vim: tabstop=4 expandtab shiftwidth=4