# HG changeset patch # User cmlenz # Date 1176465242 0 # Node ID 0407937b28531e758bf66449e1c54d3eb932afc8 # Parent 71522d48f2cb252bc70f1054ccd993bd8d10c64e Add support for HTML5 doctype. diff --git a/doc/plugin.txt b/doc/plugin.txt --- a/doc/plugin.txt +++ b/doc/plugin.txt @@ -125,6 +125,10 @@ XHTML 1.0 Strict **xhtml-transitional** XHTML 1.0 Transitional +**html5** + HTML5 (as `proposed`_ by the WHAT-WG) + +.. _proposed: http://www.whatwg.org/specs/web-apps/current-work/ .. note:: While using the Genshi API directly allows you to specify document types not in that list, the *dictionary-of-strings* based diff --git a/genshi/filters/i18n.py b/genshi/filters/i18n.py --- a/genshi/filters/i18n.py +++ b/genshi/filters/i18n.py @@ -12,10 +12,10 @@ from genshi.template.base import Template, EXPR, SUB from genshi.template.markup import EXEC -LOAD_NAME = chr(opmap['LOAD_NAME']) -LOAD_CONST = chr(opmap['LOAD_CONST']) -CALL_FUNCTION = chr(opmap['CALL_FUNCTION']) -BINARY_ADD = chr(opmap['BINARY_ADD']) +_LOAD_NAME = chr(opmap['LOAD_NAME']) +_LOAD_CONST = chr(opmap['LOAD_CONST']) +_CALL_FUNCTION = chr(opmap['CALL_FUNCTION']) +_BINARY_ADD = chr(opmap['BINARY_ADD']) class Translator(object): @@ -82,6 +82,20 @@ self.include_attrs = include_attrs def __call__(self, stream, ctxt=None, search_text=True): + """Translate any localizable strings in the given stream. + + This function shouldn't be called directly. Instead, an instance of + the `Translator` class should be registered as a filter with the + `Template` or the `TemplateLoader`, or applied as a regular stream + filter. If used as a template filter, it should be inserted in front of + all the default filters. + + :param stream: the markup event stream + :param ctxt: the template context (not used) + :param search_text: whether text nodes should be translated (used + internally) + :return: the localized stream + """ skip = 0 for kind, data, pos in stream: @@ -205,15 +219,15 @@ gettext_locs = [consts[n] for n in gettext_functions if n in consts] ops = [ - LOAD_CONST, '(', '|'.join(gettext_locs), ')', - CALL_FUNCTION, '.\x00', - '((?:', BINARY_ADD, '|', LOAD_CONST, '.\x00)+)' + _LOAD_CONST, '(', '|'.join(gettext_locs), ')', + _CALL_FUNCTION, '.\x00', + '((?:', _BINARY_ADD, '|', _LOAD_CONST, '.\x00)+)' ] for _, opcodes in re.findall(''.join(ops), data.code.co_code): strings = [] opcodes = iter(opcodes) for opcode in opcodes: - if opcode == BINARY_ADD: + if opcode == _BINARY_ADD: arg = strings.pop() strings[-1] += arg else: diff --git a/genshi/output.py b/genshi/output.py --- a/genshi/output.py +++ b/genshi/output.py @@ -54,6 +54,8 @@ ) XHTML = XHTML_STRICT + HTML5 = ('html', None, None) + class XMLSerializer(object): """Produces XML text from an event stream. diff --git a/genshi/template/plugin.py b/genshi/template/plugin.py --- a/genshi/template/plugin.py +++ b/genshi/template/plugin.py @@ -113,6 +113,7 @@ 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} diff --git a/genshi/tests/output.py b/genshi/tests/output.py --- a/genshi/tests/output.py +++ b/genshi/tests/output.py @@ -316,6 +316,11 @@

""", output) + def test_html5_doctype(self): + stream = HTML('') + output = stream.render(XHTMLSerializer, doctype=DocType.HTML5) + self.assertEqual('\n', output) + class HTMLSerializerTestCase(unittest.TestCase): @@ -371,6 +376,11 @@ html > body { display: none; } """, output) + def test_html5_doctype(self): + stream = HTML('') + output = stream.render(HTMLSerializer, doctype=DocType.HTML5) + self.assertEqual('\n', output) + class EmptyTagFilterTestCase(unittest.TestCase):