1. Andriy Kornatskyy
  2. wheezy.html

Commits

Andriy Kornatskyy  committed 1490613

Added support for wheezy.template.

  • Participants
  • Parent commits 3caf605
  • Branches default

Comments (0)

Files changed (2)

File src/wheezy/html/ext/template.py

View file
  • Ignore whitespace
+
+
+""" ``wheezy.template`` extension module.
+"""
+
+import re
+
+
+from wheezy.html.ext.lexer import InlinePreprocessor
+from wheezy.html.ext.lexer import Preprocessor
+from wheezy.html.ext.lexer import WhitespacePreprocessor
+
+
+class WheezyPreprocessor(Preprocessor):
+
+    def __init__(self):
+        super(WheezyPreprocessor, self).__init__(
+            r'@((?P<expr>.+?)\.'
+            r'(?P<widget>%(widgets)s){1}\((?P<params>.*?)\)\s*'
+            r'(?P<expr_filter>((?<!!)!\w+(!\w+)*|\s*)))(\s|$)')
+
+    EXPRESSION = '@%(expr)s%(expr_filter)s'
+
+    ERROR_CLASS0 = """\\
+@if '%(name)s' in errors:
+ class="error"\\
+@end
+"""
+
+    ERROR_CLASS1 = """\\
+@if '%(name)s' in errors:
+ class="error %(class)s"\\
+@else:
+ class="%(class)s"\\
+@end
+"""
+
+    MULTIPLE_HIDDEN = """\\
+@for item in %(value)s:
+<input type="hidden" name="%(name)s" value="@item%(expr_filter)s" />\
+@end
+"""
+
+    INPUT = """\\
+<input id="%(id)s" name="%(name)s" type="%(type)s"%(attrs)s%(class)s\\
+@if %(value)s%(condition)s:
+ value="@%(func)s%(expr_filter)s" />\\
+@else:
+ />\\
+@end
+"""
+
+    CHECKBOX = """\\
+<input id="%(id)s" name="%(name)s" type="checkbox" \
+value="1"%(attrs)s%(class)s\\
+@if %(value)s:
+ checked="checked"\\
+@end
+ />"""
+
+    MULTIPLE_CHECKBOX = """\\
+@for key, text in %(choices)s:
+<label%(attrs)s%(class)s><input id="%(id)s" name="%(name)s" type="checkbox" \
+value="1"%(attrs)s%(class)s\\
+@if key in %(value)s:
+ checked="checked"\\
+@end
+ />@text%(expr_filter)s</label>\\
+@end
+"""
+
+    RADIO = """\\
+@for key, text in %(choices)s:
+<label%(attrs)s%(class)s>\
+<input type="radio" name="%(name)s"%(attrs)s \
+value="@key%(expr_filter)s"%(class)s\\
+@if key == %(value)s:
+ checked="checked"\\
+@end
+ />@text%(expr_filter)s</label>\\
+@end
+"""
+
+    SELECT = """\\
+<select id="%(id)s" name="%(name)s"%(select_type)s%(attrs)s%(class)s>\\
+@for key, text in %(choices)s:
+<option value="@key%(expr_filter)s"\\
+@if key == %(value)s:
+ selected="selected"\\
+@end
+>@text%(expr_filter)s</option>\\
+@end
+</select>"""
+
+    ERROR = """\\
+@if '%(name)s' in errors:
+<span%(attrs)s>@errors['%(name)s'][-1]%(expr_filter)s</span>\\
+@end
+"""
+
+    MESSAGE = """\\
+@if %(value)s:
+<span%(attrs)s>%(info)s</span>\\
+@end
+"""
+
+
+class WidgetExtension(object):
+
+    preprocessors = [WheezyPreprocessor()]
+
+
+class WhitespaceExtension(object):
+
+    preprocessors = [WhitespacePreprocessor(rules=[
+        (re.compile(r'>\s+<'),
+            r'><'),
+        (re.compile(r'^\s*$', re.MULTILINE),
+            r''),
+        (re.compile(r'^ \s+', re.MULTILINE),
+            r''),
+        (re.compile(r'>\s+$', re.MULTILINE),
+            r'>'),
+        (re.compile(r'>\n@def'),
+            r'>\\\n@def'),
+        (re.compile(r'(?<!:|\\)\n@end'),
+            r'\\\n@end'),
+    ])]

File src/wheezy/html/ext/tests/test_template.py

View file
  • Ignore whitespace
+
+""" Unit tests for ``wheezy.html.ext.mako``.
+"""
+
+import unittest
+
+from wheezy.html.ext.tests.test_lexer import PreprocessorMixin
+
+
+class TemplatePreprocessorTestCase(PreprocessorMixin, unittest.TestCase):
+    """ Test the ``MakoPreprocessor``.
+    """
+
+    def assert_render_equal(self, template, expected, **kwargs):
+        assert_template_equal(template, expected, **kwargs)
+
+    HIDDEN = '@model.pref.hidden()!h'
+    MULTIPLE_HIDDEN = '@model.prefs.multiple_hidden()'
+    LABEL = "@model.username.label('<i>*</i>Username:')"
+    EMPTYBOX = "@model.amount.emptybox(class_='x')!s"
+    TEXTBOX = "@model.username.textbox(autocomplete='off')"
+    PASSWORD = "@model.pwd.password()"
+    TEXTAREA = "@model.comment.textarea()"
+    CHECKBOX = "@model.remember_me.checkbox()"
+    MULTIPLE_CHECKBOX = "@model.scm.multiple_checkbox(choices=scm)"
+    RADIO = "@model.scm.radio(choices=scm)"
+    DROPDOWN = "@model.scm.dropdown(choices=scm)"
+    LISTBOX = "@model.scm.listbox(choices=scm, class_='x')"
+    ERROR = "@model.username.error()"
+    GENERAL_ERROR = "@model.error()"
+    INFO = "@model.user_info.info()"
+    GENERAL_INFO = "@message.info()"
+    WARNING = "@model.user_info.warning()"
+    GENERAL_WARNING = "@message.warning()"
+
+
+class WheezyWhitespaceExtensionTestCase(unittest.TestCase):
+    """ Test the ``WhitespaceExtension``.
+    """
+
+    def setUp(self):
+        from wheezy.html.ext.template import WhitespaceExtension
+        self.preprocess = WhitespaceExtension.preprocessors[0]
+
+    def test_whitespace(self):
+        """
+        """
+        assert '><' == self.preprocess('  >  <')
+        assert '' == self.preprocess('  ')
+        assert 'x' == self.preprocess('  x')
+        assert '>' == self.preprocess('>  ')
+        assert '>\\\n@def' == self.preprocess('>\n@def')
+        assert '>\\\n@end' == self.preprocess('>\n@end')
+        assert ':\n@end' == self.preprocess(':\n@end')
+        assert 'x\\\n@end' == self.preprocess('x\\\n@end')
+
+try:
+    from wheezy.html.ext.template import WidgetExtension
+    from wheezy.html.utils import html_escape
+    from wheezy.template.engine import Engine
+    from wheezy.template.ext.core import CoreExtension
+    from wheezy.template.loader import DictLoader
+
+    def assert_template_equal(text, expected, **kwargs):
+        engine = Engine(
+                loader=DictLoader({
+                    'x': "@require(model, errors, message, scm)\n" + text}),
+                extensions=[CoreExtension, WidgetExtension])
+        engine.global_vars.update({'h': html_escape})
+        template = engine.get_template('x')
+        value = template.render(kwargs)
+        assert expected == value
+
+except ImportError:  # pragma: nocover
+
+    def assert_template_equal(text, expected, **kwargs):
+        pass