| @@ -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 | |||||
| @@ -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() | |||||