def render(self, ctx): if ctx.response.status_code <> 200: return ctx.response.status template = self.template_for(ctx) stream = template.generate( ctx=ctx, url_for=lambda e, **v: ctx.adapter.build(e, v), resources=resources, **ctx.values ) stream = stream | HTMLFormFiller(data=ctx.values) return stream.render('html', doctype=DocType.get('html5'))
def __init__(self, extra_vars_func=None, options=None): AbstractTemplateEnginePlugin.__init__(self, extra_vars_func, options) default_doctype = self.options.get('genshi.default_doctype') if default_doctype: doctype = DocType.get(default_doctype) if doctype is None: raise ConfigurationError('Unknown doctype %r' % default_doctype) self.default_doctype = doctype else: self.default_doctype = None format = self.options.get('genshi.default_format', 'html').lower() if format not in ('html', 'xhtml', 'xml', 'text'): raise ConfigurationError('Unknown output format %r' % format) self.default_format = format
def _get_render_options(self, format=None, fragment=False, **options): """Return options dict for rendering the given format.""" # We do not call the methods of the base-classes here, because they # would screw up the options, so we have to implement their behaviour # ourselves. if format is None: format = self.default_format kwargs = {'method': format} if self.default_encoding: kwargs['encoding'] = self.default_encoding doctype = options.pop('doctype', self.default_doctype) kwargs.update(options) if doctype and not fragment: if isinstance(doctype, dict): doctype = doctype.get(format) if doctype: doctype = DocType.get(doctype) if doctype is None: raise ConfigurationError('Unknown doctype %r' % doctype) kwargs['doctype'] = doctype return kwargs
def get_doctype(name): capitalized_name = name.upper() if capitalized_name.startswith('HTML'): html = 'HTML' elif capitalized_name.startswith('XHTML'): html = 'html' else: raise ValueError( 'invalid doctype: only the following values are supported:' ' %s' % (', '.join(VALID_DOCTYPE_VALUES))) prefix = '<!DOCTYPE ' suffix = '>' returned_tuple = DocType.get(name) if returned_tuple is None: return None name, pubid, sysid = returned_tuple if not (pubid or sysid): return ''.join((prefix, html, suffix)), '' else: first_line = '%s %s "%s"' % (prefix, html, pubid) second_line = '"%s"%s' % (sysid, suffix) return first_line, second_line
def replace_doctype (stream): for kind, data, pos in stream: if kind == DOCTYPE: yield kind, DocType.get (doctype), pos else: yield kind, data, pos