def process_request(self, req): # Allow all POST requests (with a valid __FORM_TOKEN, ensuring that # the client has at least some permission). Additionally, allow GET # requests from TRAC_ADMIN for testing purposes. if req.method != 'POST': req.perm.require('TRAC_ADMIN') # @todo: Embed "tips" within the rendered output for the editor # (recognize TracLinks, table-stuff, macros, processors) # @todo: Save the content in server-side user-specific field for recovery realm = req.args.get('realm', 'wiki') id = req.args.get('id') version = req.args.get('version') if version is not None: try: version = int(version) except ValueError: version = None text = req.args.get('text', '') flavor = req.args.get('flavor') options = {} if 'escape_newlines' in req.args: options['escape_newlines'] = bool(int(req.args['escape_newlines'] or 0)) if 'shorten' in req.args: options['shorten'] = bool(int(req.args['shorten'] or 0)) resource = Resource(realm, id=id, version=version) context = Context.from_request(req, resource) rendered = format_to_cke_html(self.env, context, text, self.code_styles, **options) # since Trac renders underlined text as `<span class="underlined">text</span> # instead of u-tag, we need to adjust it for compatibility's sake # see also discussion at Google Groups: # https://groups.google.com/group/trac-dev/browse_thread/thread/833206a932d1f918 html = HTML(rendered) html |= Transformer('//span[@class="underline"]').rename('u').attr('class', None) # CKEditor renders indentation by using p style="margin-left: 40px" # instead of blockquote-tag html |= Transformer('//blockquote/p').attr('style', 'margin-left: 40px') html |= Transformer('//blockquote').unwrap() buffer = StringIO() html.render(out=buffer, encoding='utf-8') req.send( buffer.getvalue() )
def process_request(self, req): # Allow all POST requests (with a valid __FORM_TOKEN, ensuring that # the client has at least some permission). Additionally, allow GET # requests from TRAC_ADMIN for testing purposes. if req.method != "POST": req.perm.require("TRAC_ADMIN") # @todo: Embed "tips" within the rendered output for the editor # (recognize TracLinks, table-stuff, macros, processors) # @todo: Save the content in server-side user-specific field for recovery realm = req.args.get("realm", "wiki") id = req.args.get("id") version = req.args.get("version") if version is not None: try: version = int(version) except ValueError: version = None text = req.args.get("text", "") flavor = req.args.get("flavor") options = {} if "escape_newlines" in req.args: options["escape_newlines"] = bool(int(req.args["escape_newlines"] or 0)) if "shorten" in req.args: options["shorten"] = bool(int(req.args["shorten"] or 0)) resource = Resource(realm, id=id, version=version) context = Context.from_request(req, resource) rendered = format_to_cke_html(self.env, context, text, self.code_styles, **options) # since Trac renders underlined text as `<span class="underlined">text</span> # instead of u-tag, we need to adjust it for compatibility's sake # see also discussion at Google Groups: # https://groups.google.com/group/trac-dev/browse_thread/thread/833206a932d1f918 html = HTML(rendered) html |= Transformer('//span[@class="underline"]').rename("u").attr("class", None) # CKEditor renders indentation by using p style="margin-left: 40px" # instead of blockquote-tag html |= Transformer("//blockquote/p").attr("style", "margin-left: 40px") html |= Transformer("//blockquote").unwrap() buffer = StringIO() html.render(out=buffer, encoding="utf-8") req.send(buffer.getvalue())
def postprocess(self, html): stream = HTML(html) stream |= lambda s: wrapper(self.canon, s) # rewrite_canonical_urls(self.canon, s) return stream.render('html')