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