Mercurial > genshi > mirror
changeset 448:1154f2aadb6c trunk
Add support for HTML5 doctype.
author | cmlenz |
---|---|
date | Fri, 13 Apr 2007 11:54:02 +0000 |
parents | 60ed07d60281 |
children | b07e65580175 |
files | doc/plugin.txt genshi/filters/i18n.py genshi/output.py genshi/template/plugin.py genshi/tests/output.py |
diffstat | 5 files changed, 39 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- 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
--- 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:
--- 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.
--- 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}
--- a/genshi/tests/output.py +++ b/genshi/tests/output.py @@ -316,6 +316,11 @@ <p></p> </div>""", output) + def test_html5_doctype(self): + stream = HTML('<html></html>') + output = stream.render(XHTMLSerializer, doctype=DocType.HTML5) + self.assertEqual('<!DOCTYPE html>\n<html></html>', output) + class HTMLSerializerTestCase(unittest.TestCase): @@ -371,6 +376,11 @@ html > body { display: none; } </style>""", output) + def test_html5_doctype(self): + stream = HTML('<html></html>') + output = stream.render(HTMLSerializer, doctype=DocType.HTML5) + self.assertEqual('<!DOCTYPE html>\n<html></html>', output) + class EmptyTagFilterTestCase(unittest.TestCase):