# HG changeset patch # User cmlenz # Date 1177580796 0 # Node ID 2f13c5fc4a4d66a845dde037cfaf208526a20d60 # Parent d5e2a7b581165876fb57c9b59ed0ab5cc1170f53 Move the mapping of doctype names to tuples out of the plugin into the `DocType` class. diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -9,6 +9,10 @@ * Added the functions `encode()` and `get_serializer()` to the `genshi.output` module, which provide a lower-level API to the functionality previously only available through `Stream.render()` and `Stream.serialize()`. + * The `DocType` class now has a `get(name)` function that returns a `DOCTYPE` + tuple for a given string. + * Added frameset variants to the `DocType` constants for HTML 4.01 and XHTML + 1.0. Version 0.4 diff --git a/genshi/output.py b/genshi/output.py --- a/genshi/output.py +++ b/genshi/output.py @@ -81,8 +81,14 @@ 'html', '-//W3C//DTD HTML 4.01 Transitional//EN', 'http://www.w3.org/TR/html4/loose.dtd' ) + HTML_FRAMESET = ( + 'html', '-//W3C//DTD HTML 4.01 Frameset//EN', + 'http://www.w3.org/TR/html4/frameset.dtd' + ) HTML = HTML_STRICT + HTML5 = ('html', None, None) + XHTML_STRICT = ( 'html', '-//W3C//DTD XHTML 1.0 Strict//EN', 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd' @@ -91,9 +97,40 @@ 'html', '-//W3C//DTD XHTML 1.0 Transitional//EN', 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd' ) + XHTML_FRAMESET = ( + 'html', '-//W3C//DTD XHTML 1.0 Frameset//EN', + 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd' + ) XHTML = XHTML_STRICT - HTML5 = ('html', None, None) + def get(cls, name): + """Return the ``(name, pubid, sysid)`` tuple of the ``DOCTYPE`` + declaration for the specified name. + + The following names are recognized in this version: + * "html" or "html-strict" for the HTML 4.01 strict DTD + * "html-transitional" for the HTML 4.01 transitional DTD + * "html-transitional" for the HTML 4.01 frameset DTD + * "html5" for the ``DOCTYPE`` proposed for HTML5 + * "xhtml" or "xhtml-strict" for the XHTML 1.0 strict DTD + * "xhtml-transitional" for the XHTML 1.0 transitional DTD + * "xhtml-frameset" for the XHTML 1.0 frameset DTD + + :param name: the name of the ``DOCTYPE`` + :return: the ``(name, pubid, sysid)`` tuple for the requested + ``DOCTYPE``, or ``None`` if the name is not recognized + :since: version 0.4.1 + """ + return { + 'html': cls.HTML, 'html-strict': cls.HTML_STRICT, + 'html-transitional': DocType.HTML_TRANSITIONAL, + 'html-frameset': DocType.HTML_FRAMESET, + 'html5': cls.HTML5, + 'xhtml': cls.XHTML, 'xhtml-strict': cls.XHTML_STRICT, + 'xhtml-transitional': cls.XHTML_TRANSITIONAL, + 'xhtml-frameset': cls.XHTML_FRAMESET, + }.get(name.lower()) + get = classmethod(get) class XMLSerializer(object): diff --git a/genshi/template/plugin.py b/genshi/template/plugin.py --- a/genshi/template/plugin.py +++ b/genshi/template/plugin.py @@ -30,7 +30,7 @@ __docformat__ = 'restructuredtext en' -class ConfigurationError(Exception): +class ConfigurationError(ValueError): """Exception raised when invalid plugin options are encountered.""" @@ -111,23 +111,21 @@ template_class = MarkupTemplate extension = '.html' - doctypes = {'html': DocType.HTML, 'html-strict': DocType.HTML_STRICT, - 'html-transitional': DocType.HTML_TRANSITIONAL, - 'html5': DocType.HTML5, - 'xhtml': DocType.XHTML, 'xhtml-strict': DocType.XHTML_STRICT, - 'xhtml-transitional': DocType.XHTML_TRANSITIONAL} - def __init__(self, extra_vars_func=None, options=None): AbstractTemplateEnginePlugin.__init__(self, extra_vars_func, options) - doctype = self.options.get('genshi.default_doctype') - if doctype and doctype not in self.doctypes: - raise ConfigurationError('Unknown doctype "%s"' % doctype) - self.default_doctype = self.doctypes.get(doctype) + 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') + format = self.options.get('genshi.default_format', 'html').lower() if format not in ('html', 'xhtml', 'xml', 'text'): - raise ConfigurationError('Unknown output format "%s"' % format) + raise ConfigurationError('Unknown output format %r' % format) self.default_format = format def _get_render_options(self, format=None):