1 # -*- coding: iso-8859-1 -*- 2 """ 3 MoinMoin - MoinShare fragment parser 4 5 @copyright: 2011, 2012 by Paul Boddie <paul@boddie.org.uk> 6 @license: GNU GPL (v2 or later), see COPYING.txt for details. 7 """ 8 9 from MoinSupport import parseAttributes 10 from MoinMoin import wikiutil 11 12 try: 13 from hashlib import sha1 14 except ImportError: 15 from sha import new as sha1 16 17 Dependencies = ["pages"] 18 19 # Parser support. 20 21 class Parser: 22 23 """ 24 Format shared content fragments of the form... 25 26 {{{#!MoinShare fragment=fragment-id ref=ref-fragment-id format=content-format 27 28 Content... 29 }}} 30 """ 31 32 def __init__(self, raw, request, **kw): 33 34 """ 35 Initialise the parser with the given 'raw' data, 'request' and any 36 keyword arguments that may have been supplied. 37 """ 38 39 self.raw = raw 40 self.request = request 41 42 attrs = parseAttributes(request, False) 43 44 # Get any reference to this fragment, another fragment and the content 45 # format. 46 47 self.fragment = attrs.get("fragment", '') 48 self.ref = attrs.get("ref", '') 49 self.format_type = attrs.get("format", '') 50 51 def getDigest(self): 52 53 "Make a message digest for the fragment's content." 54 55 return sha1(self.raw).hexdigest() 56 57 def format(self, fmt): 58 59 "Format a fragment using the given formatter 'fmt'." 60 61 request = self.request 62 63 if self.fragment: 64 request.write(fmt.anchordef(self.fragment)) 65 66 request.write(fmt.div(1, css_class="moinsharefragment")) 67 request.write(self.formatText(self.raw, fmt)) 68 request.write(fmt.div(0)) 69 70 def getFormat(self): 71 72 "Get the format used on this page." 73 74 return self.format_type 75 76 def getParserClass(self, format): 77 78 """ 79 Return a parser class for the given 'format', returning a plain text 80 parser if no parser can be found for the specified 'format'. 81 """ 82 83 cfg = self.request.cfg 84 85 try: 86 return wikiutil.searchAndImportPlugin(cfg, "parser", format or "wiki") 87 except wikiutil.PluginMissingError: 88 return wikiutil.searchAndImportPlugin(cfg, "parser", "plain") 89 90 def formatText(self, text, fmt): 91 92 """ 93 Format the given 'text' using the specified formatter 'fmt'. 94 """ 95 96 request = self.request 97 page = fmt.page = request.page 98 format = self.getFormat() 99 100 # Suppress line anchors. 101 102 parser_cls = self.getParserClass(format) 103 parser = parser_cls(text, request, line_anchors=False) 104 105 # Fix lists by indicating that a paragraph is already started. 106 107 kw = {} 108 if not format or format == "wiki": 109 kw["inhibit_p"] = True 110 111 return request.redirectedOutput(parser.format, fmt, **kw) 112 113 # vim: tabstop=4 expandtab shiftwidth=4