diff --git a/dev-req.txt b/dev-req.txt new file mode 100644 index 0000000..e515f09 --- /dev/null +++ b/dev-req.txt @@ -0,0 +1,15 @@ +Django==1.2.3 +Genshi==0.6 +Jinja2==2.5.5 +Mako==0.3.6 +Markdown==2.0.3 +MarkupSafe==0.11 +PyYAML==3.09 +logilab-astng==0.21.0 +logilab-common==0.53.0 +lxml==2.3beta1 +nose==0.11.4 +pylint==0.22.0 +pyquery==0.6.1 +unittest2==0.5.1 +wsgiref==0.1.2 diff --git a/hyde/ext/templates/jinja2.py b/hyde/ext/templates/jinja2.py deleted file mode 100644 index 8a2fdb8..0000000 --- a/hyde/ext/templates/jinja2.py +++ /dev/null @@ -1,14 +0,0 @@ -""" -Hyde Template interface realization for Jinja2 -""" -from hyde.template import Template - -class Jinja2Template(Template): - def configure(self, config): - """ - Uses the config object to initialize the jinja environment. - """ - pass - - def render(self, template_name, context): - pass \ No newline at end of file diff --git a/hyde/ext/templates/jinja2Template.py b/hyde/ext/templates/jinja2Template.py new file mode 100644 index 0000000..af42aba --- /dev/null +++ b/hyde/ext/templates/jinja2Template.py @@ -0,0 +1,19 @@ +""" +Hyde Template interface realization for Jinja2 +""" +from hyde.template import Template +from jinja2 import Environment, FileSystemLoader + +class Jinja2Template(Template): + def configure(self, sitepath, config): + """ + Uses the config object to initialize the jinja environment. + """ + self.env = Environment(loader=FileSystemLoader(sitepath)) + + def render(self, template_name, context): + """ + Renders the given template using the context + """ + t = self.env.get_template(template_name) + return t.render(context) \ No newline at end of file diff --git a/hyde/fs.py b/hyde/fs.py index ce5e808..7f613a7 100644 --- a/hyde/fs.py +++ b/hyde/fs.py @@ -78,6 +78,12 @@ class File(FS): read_text = fin.read() return read_text + def write(self, text, encoding="utf-8"): + """ + Writes the given text to the file using the given encoding. + """ + with codecs.open(self.path, 'w', encoding) as fout: + fout.write(text) class Folder(FS): """ diff --git a/hyde/tests/test_fs.py b/hyde/tests/test_fs.py index 8845aec..08868a9 100644 --- a/hyde/tests/test_fs.py +++ b/hyde/tests/test_fs.py @@ -58,5 +58,12 @@ def test_read_all(): with codecs.open(path, 'w', 'utf-8') as f: f.write(utxt) + txt = File(path).read_all() + assert txt == utxt + +def test_write(): + utxt = u'åßcdeƒ' + path = DATA_ROOT.child('unicode.txt') + File(path).write(utxt) txt = File(path).read_all() assert txt == utxt \ No newline at end of file diff --git a/hyde/tests/test_jinja2template.py b/hyde/tests/test_jinja2template.py index 2346650..0922719 100644 --- a/hyde/tests/test_jinja2template.py +++ b/hyde/tests/test_jinja2template.py @@ -3,9 +3,11 @@ Use nose `$ pip install nose` `$ nosetests` + +Code borrowed from rwbench.py from the jinja2 examples """ from datetime import datetime -from hyde.ext.templates.jinja2 import Jinja2Template +from hyde.ext.templates.jinja2Template import Jinja2Template from hyde.fs import File, Folder from jinja2.utils import generate_lorem_ipsum from random import choice, randrange @@ -26,6 +28,8 @@ class Article(object): self.pub_date = datetime.utcfromtimestamp(randrange(10 ** 9, 2 * 10 ** 9)) self.published = True +def dateformat(x): + return x.strftime('%Y-%m-%d') class User(object): @@ -49,11 +53,17 @@ context = dict(users=users, articles=articles, page_navigation=navigation) def test_render(): """ - Uses the real world benchmark code from jinja. + Uses pyquery to test the html structure for validity """ t = Jinja2Template() - config = yaml.load(JINJA2.child('config.yaml')) - t.configure(None) + t.configure(JINJA2.path, None) + t.env.filters['dateformat'] = dateformat html = t.render('index.html', context) - expected = File(JINJA2.child('index_expected.html')).read_all() - assert_html_equals(expected, html) \ No newline at end of file + from pyquery import PyQuery + actual = PyQuery(html) + assert actual(".navigation li").length == 30 + assert actual("div.article").length == 20 + assert actual("div.article h2").length == 20 + assert actual("div.article h2 a").length == 20 + assert actual("div.article p.meta").length == 20 + assert actual("div.article div.text").length == 20 \ No newline at end of file diff --git a/hyde/tests/util.py b/hyde/tests/util.py index 369cdc2..423ee00 100644 --- a/hyde/tests/util.py +++ b/hyde/tests/util.py @@ -1,6 +1,9 @@ from django.utils.html import strip_spaces_between_tags -def assert_html_equals(expected, actual): +def assert_html_equals(expected, actual, sanitize=None): expected = strip_spaces_between_tags(expected.strip()) actual = strip_spaces_between_tags(actual.strip()) - assert expected == actual + if sanitize: + expected = sanitize(expected) + actual = sanitize(actual) + assert expected == actual \ No newline at end of file