1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/parsers/MoinShare.py Sat Apr 14 00:42:16 2012 +0200
1.3 @@ -0,0 +1,116 @@
1.4 +# -*- coding: iso-8859-1 -*-
1.5 +"""
1.6 + MoinMoin - MoinShare fragment parser
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 MoinMoin import wikiutil
1.13 +
1.14 +try:
1.15 + from hashlib import sha1
1.16 +except ImportError:
1.17 + from sha import new as sha1
1.18 +
1.19 +Dependencies = ["pages"]
1.20 +
1.21 +# Parser support.
1.22 +
1.23 +class Parser:
1.24 +
1.25 + """
1.26 + Format shared content fragments of the form...
1.27 +
1.28 + {{{#!MoinShare ref=fragment format=content-type
1.29 +
1.30 + Content...
1.31 + }}}
1.32 + """
1.33 +
1.34 + def __init__(self, raw, request, **kw):
1.35 +
1.36 + """
1.37 + Initialise the parser with the given 'raw' data, 'request' and any
1.38 + keyword arguments that may have been supplied.
1.39 + """
1.40 +
1.41 + self.raw = raw
1.42 + self.request = request
1.43 +
1.44 + attrs, msg = wikiutil.parseAttributes(request, kw.get("format_args", ""))
1.45 +
1.46 + # The attributes returned from the formatting arguments are encoded like
1.47 + # strings.
1.48 +
1.49 + quotes = '"' + "'"
1.50 +
1.51 + # Get any reference to another fragment.
1.52 +
1.53 + self.ref = (attrs.get("ref") or '').strip(quotes)
1.54 + self.format_type = (attrs.get("format") or '').strip(quotes)
1.55 +
1.56 + def getFragmentIdentifier(self):
1.57 +
1.58 + "Make a unique identifier for the fragment."
1.59 +
1.60 + return sha1(self.raw).hexdigest()
1.61 +
1.62 + def format(self, fmt):
1.63 +
1.64 + "Format a fragment using the given formatter 'fmt'."
1.65 +
1.66 + request = self.request
1.67 + page = request.page
1.68 + _ = request.getText
1.69 +
1.70 + request.write(fmt.anchordef(self.getFragmentIdentifier()))
1.71 +
1.72 + request.write(fmt.div(1, css_class="moinsharefragment"))
1.73 + request.write(self.formatText(self.raw, fmt))
1.74 + request.write(fmt.div(0))
1.75 +
1.76 + def getFormat(self):
1.77 +
1.78 + "Get the format used on this page."
1.79 +
1.80 + return self.format_type
1.81 +
1.82 + def getParserClass(self, format):
1.83 +
1.84 + """
1.85 + Return a parser class for the given 'format', returning a plain text
1.86 + parser if no parser can be found for the specified 'format'.
1.87 + """
1.88 +
1.89 + cfg = self.request.cfg
1.90 +
1.91 + try:
1.92 + return wikiutil.searchAndImportPlugin(cfg, "parser", format or "wiki")
1.93 + except wikiutil.PluginMissingError:
1.94 + return wikiutil.searchAndImportPlugin(cfg, "parser", "plain")
1.95 +
1.96 + def formatText(self, text, fmt):
1.97 +
1.98 + """
1.99 + Format the given 'text' using the specified formatter 'fmt'.
1.100 + """
1.101 +
1.102 + request = self.request
1.103 + page = fmt.page = request.page
1.104 + format = self.getFormat()
1.105 +
1.106 + # Suppress line anchors.
1.107 +
1.108 + parser_cls = self.getParserClass(format)
1.109 + parser = parser_cls(text, request, line_anchors=False)
1.110 +
1.111 + # Fix lists by indicating that a paragraph is already started.
1.112 +
1.113 + kw = {}
1.114 + if not format or format == "wiki":
1.115 + kw["inhibit_p"] = True
1.116 +
1.117 + return request.redirectedOutput(parser.format, fmt, **kw)
1.118 +
1.119 +# vim: tabstop=4 expandtab shiftwidth=4