From a296f77d5af3605cab9f46deb971d6b1e03854f0 Mon Sep 17 00:00:00 2001 From: Lakshmi Vyasarajan Date: Wed, 5 Jan 2011 10:25:07 +0530 Subject: [PATCH] Added blockdown plugin --- hyde/ext/plugins/blockdown.py | 46 ++++++++++++++++++++++++++++++++ hyde/tests/ext/test_blockdown.py | 46 ++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 hyde/ext/plugins/blockdown.py create mode 100644 hyde/tests/ext/test_blockdown.py diff --git a/hyde/ext/plugins/blockdown.py b/hyde/ext/plugins/blockdown.py new file mode 100644 index 0000000..9b735d1 --- /dev/null +++ b/hyde/ext/plugins/blockdown.py @@ -0,0 +1,46 @@ +""" +Less css plugin +""" + +from hyde.plugin import Plugin +from hyde.fs import File, Folder + +import re +from functools import partial + +class BlockdownPlugin(Plugin): + """ + The plugin class for less css + """ + + def __init__(self, site): + super(BlockdownPlugin, self).__init__(site) + + def template_loaded(self, template): + self.template = template + + def begin_text_resource(self, resource, text): + """ + Replace =======////blockname\\\\=========== + with + {% block blockname %} or equivalent and + Replace =======\\\\blockname////=========== + with + {% block blockname %} or equivalent + """ + blocktag_open = re.compile( + '^\s*=+/+\s*([A-Za-z0-9_\-.]+)\s*\\\\+=+$', + re.MULTILINE) + blocktag_close = re.compile( + '^\s*=+\\\\+\s*([A-Za-z0-9_\-.]+)\s*/+=+$', + re.MULTILINE) + def blockdown_to_block(match, start_block=True): + if not match.lastindex: + return '' + block_name = match.groups(1)[0] + return (self.template.get_block_open_statement(block_name) + if start_block + else self.template.get_block_close_statement(block_name)) + text = blocktag_open.sub(blockdown_to_block, text) + text = blocktag_close.sub(partial(blockdown_to_block, start_block=False), text) + return text \ No newline at end of file diff --git a/hyde/tests/ext/test_blockdown.py b/hyde/tests/ext/test_blockdown.py new file mode 100644 index 0000000..bc25986 --- /dev/null +++ b/hyde/tests/ext/test_blockdown.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +""" +Use nose +`$ pip install nose` +`$ nosetests` +""" +from hyde.fs import File, Folder +from hyde.generator import Generator +from hyde.site import Site + +from pyquery import PyQuery + +TEST_SITE = File(__file__).parent.parent.child_folder('_test') + + +class TestBlockdown(object): + + def setUp(self): + TEST_SITE.make() + TEST_SITE.parent.child_folder( + 'sites/test_jinja').copy_contents_to(TEST_SITE) + + def tearDown(self): + TEST_SITE.delete() + + def test_can_execute_less(self): + s = Site(TEST_SITE) + s.config.plugins = ['hyde.ext.plugins.blockdown.BlockdownPlugin'] + txt ="This template tests to make sure blocks can be replaced with markdownish syntax." + templ = """ +{%% extends "base.html" %%} +===========================////title\\\\============================ +%s +===========================\\\\title////============================""" + + content = (templ.strip() % txt).strip() + bd = File(TEST_SITE.child('content/blockdown.html')) + bd.write(content) + gen = Generator(s) + gen.generate_resource_at_path(bd.path) + res = s.content.resource_from_path(bd.path) + target = File(s.config.deploy_root_path.child(res.relative_deploy_path)) + assert target.exists + text = target.read_all() + q = PyQuery(text) + assert q('title').text().strip() == txt.strip() \ No newline at end of file