From 005da79c4e47f4608292e80d8f463564740f271f Mon Sep 17 00:00:00 2001 From: Lakshmi Vyasarajan Date: Wed, 26 Jan 2011 04:45:30 +0530 Subject: [PATCH] Added text links --- doc/1. Overview.html | 0 doc/2. Commandline.html | 0 doc/3. Templates.html | 0 doc/4. Extending.html | 0 doc/4.1. Layouts.html | 0 doc/4.2. Widgets.html | 0 doc/4.3. Plugins.html | 0 doc/4.4. Aggregators.html | 0 doc/4.5. Importers.html | 0 doc/5. Moving from Hyde 0.5.html | 0 hyde/ext/plugins/textlinks.py | 40 ++++++++++++++++++++++ hyde/ext/templates/jinja.py | 57 +++++++++++++++++++------------- hyde/template.py | 14 ++++++++ hyde/tests/ext/test_sorter.py | 49 +++++++++++++++++++++++++++ hyde/tests/ext/test_syntext.py | 2 +- hyde/tests/ext/test_textlinks.py | 57 ++++++++++++++++++++++++++++++++ 16 files changed, 195 insertions(+), 24 deletions(-) delete mode 100644 doc/1. Overview.html delete mode 100644 doc/2. Commandline.html delete mode 100644 doc/3. Templates.html delete mode 100644 doc/4. Extending.html delete mode 100644 doc/4.1. Layouts.html delete mode 100644 doc/4.2. Widgets.html delete mode 100644 doc/4.3. Plugins.html delete mode 100644 doc/4.4. Aggregators.html delete mode 100644 doc/4.5. Importers.html delete mode 100644 doc/5. Moving from Hyde 0.5.html create mode 100644 hyde/ext/plugins/textlinks.py create mode 100644 hyde/tests/ext/test_textlinks.py diff --git a/doc/1. Overview.html b/doc/1. Overview.html deleted file mode 100644 index e69de29..0000000 diff --git a/doc/2. Commandline.html b/doc/2. Commandline.html deleted file mode 100644 index e69de29..0000000 diff --git a/doc/3. Templates.html b/doc/3. Templates.html deleted file mode 100644 index e69de29..0000000 diff --git a/doc/4. Extending.html b/doc/4. Extending.html deleted file mode 100644 index e69de29..0000000 diff --git a/doc/4.1. Layouts.html b/doc/4.1. Layouts.html deleted file mode 100644 index e69de29..0000000 diff --git a/doc/4.2. Widgets.html b/doc/4.2. Widgets.html deleted file mode 100644 index e69de29..0000000 diff --git a/doc/4.3. Plugins.html b/doc/4.3. Plugins.html deleted file mode 100644 index e69de29..0000000 diff --git a/doc/4.4. Aggregators.html b/doc/4.4. Aggregators.html deleted file mode 100644 index e69de29..0000000 diff --git a/doc/4.5. Importers.html b/doc/4.5. Importers.html deleted file mode 100644 index e69de29..0000000 diff --git a/doc/5. Moving from Hyde 0.5.html b/doc/5. Moving from Hyde 0.5.html deleted file mode 100644 index e69de29..0000000 diff --git a/hyde/ext/plugins/textlinks.py b/hyde/ext/plugins/textlinks.py new file mode 100644 index 0000000..023788f --- /dev/null +++ b/hyde/ext/plugins/textlinks.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- +""" +Textlinks plugin +""" +import re + +from hyde.plugin import Plugin + +class TextlinksPlugin(Plugin): + """ + The plugin class for syntax text replacement. + """ + def __init__(self, site): + super(TextlinksPlugin, self).__init__(site) + + def template_loaded(self, template): + """ + Handles the template loaded event to keep + a reference to the template object. + """ + self.template = template + + def begin_text_resource(self, resource, text): + """ + Replace content url pattern [[/abc/def]]) + with + {{ content_url('/abc/def') }} or equivalent and + Replace media url pattern [[!!/abc/def]] + with + {{ media_url('/abc/def') }} or equivalent. + """ + content_link = re.compile('\[\[([^\]]*)\]\]', re.UNICODE|re.MULTILINE) + media_link = re.compile('\[\[\!\!([^\]]*)\]\]', re.UNICODE|re.MULTILINE) + def replace_content(match): + return self.template.get_content_url_statement(match.groups(1)[0]) + def replace_media(match): + return self.template.get_media_url_statement(match.groups(1)[0]) + text = content_link.sub(replace_content, text) + text = media_link.sub(replace_media, text) + return text \ No newline at end of file diff --git a/hyde/ext/templates/jinja.py b/hyde/ext/templates/jinja.py index 1b84694..60e35be 100644 --- a/hyde/ext/templates/jinja.py +++ b/hyde/ext/templates/jinja.py @@ -383,42 +383,53 @@ class Jinja2Template(Template): @property def patterns(self): - """ - The pattern for matching selected template statements. - """ - return { + """ + The pattern for matching selected template statements. + """ + return { "block_open": '\s*\{\%\s*block\s*([^\s]+)\s*\%\}', "block_close": '\s*\{\%\s*endblock\s*([^\s]*)\s*\%\}', "include": '\s*\{\%\s*include\s*(?:\'|\")(.+?\.[^.]*)(?:\'|\")\s*\%\}', "extends": '\s*\{\%\s*extends\s*(?:\'|\")(.+?\.[^.]*)(?:\'|\")\s*\%\}' - } + } def get_include_statement(self, path_to_include): - """ - Returns an include statement for the current template, - given the path to include. - """ - return '{%% include \'%s\' %%}' % path_to_include + """ + Returns an include statement for the current template, + given the path to include. + """ + return '{%% include \'%s\' %%}' % path_to_include def get_extends_statement(self, path_to_extend): - """ - Returns an extends statement for the current template, - given the path to extend. - """ - return '{%% extends \'%s\' %%}' % path_to_extend + """ + Returns an extends statement for the current template, + given the path to extend. + """ + return '{%% extends \'%s\' %%}' % path_to_extend def get_open_tag(self, tag, params): - """ - Returns an open tag statement. - """ - return '{%% %s %s -%%}' % (tag, params) + """ + Returns an open tag statement. + """ + return '{%% %s %s -%%}' % (tag, params) def get_close_tag(self, tag, params): - """ - Returns an open tag statement. - """ - return '{%%- end%s %%}' % tag + """ + Returns an open tag statement. + """ + return '{%%- end%s %%}' % tag + def get_content_url_statement(self, url): + """ + Returns the content url statement. + """ + return '{{ content_url(\'%s\') }}' % url + + def get_media_url_statement(self, url): + """ + Returns the media url statement. + """ + return '{{ media_url(\'%s\') }}' % url def render(self, text, context): """ diff --git a/hyde/template.py b/hyde/template.py index b11d5c8..78f9693 100644 --- a/hyde/template.py +++ b/hyde/template.py @@ -128,6 +128,20 @@ class Template(object): """ return '{%% end%s %%}' % tag + @abc.abstractmethod + def get_content_url_statement(self, url): + """ + Returns the content url statement. + """ + return '/' + url + + @abc.abstractmethod + def get_media_url_statement(self, url): + """ + Returns the media url statement. + """ + return '/media/' + url + @staticmethod def find_template(site): """ diff --git a/hyde/tests/ext/test_sorter.py b/hyde/tests/ext/test_sorter.py index 329379f..04fb7bb 100644 --- a/hyde/tests/ext/test_sorter.py +++ b/hyde/tests/ext/test_sorter.py @@ -136,6 +136,55 @@ class TestSorter(object): File(p).parent.name]))] assert pages == expected_sorted + # def test_walk_resources_sorted_with_grouping_one_level(self): + # s = Site(TEST_SITE) + # cfg = """ + # plugins: + # - hyde.ext.sorter.SorterPlugin + # sorter: + # multi: + # groups: sections.yaml + # attr: + # - source_file.kind + # - node.name + # + # """ + # sections = """ + # group_name: section + # groups: + # - + # name: support + # description: All site support pages + # - + # name: media + # description: Media files + # """ + # s.config = Config(TEST_SITE, config_dict=yaml.load(cfg)) + # s.load() + # SorterPlugin(s).begin_site() + # + # assert hasattr(s.content, 'walk_resources_sorted_by_multi') + # expected = ["content/404.html", + # "content/about.html", + # "content/apple-touch-icon.png", + # "content/blog/2010/december/merry-christmas.html", + # "content/crossdomain.xml", + # "content/favicon.ico", + # "content/robots.txt", + # "content/site.css" + # ] + # + # pages = [page.name for page in s.content.walk_resources_sorted_by_multi()] + # + # expected_sorted = [File(page).name + # for page in + # sorted(expected, + # key=lambda p: tuple( + # [File(p).kind, + # File(p).parent.name]))] + # assert pages == expected_sorted + + def test_walk_resources_sorted_no_default_is_processable(self): s = Site(TEST_SITE) cfg = """ diff --git a/hyde/tests/ext/test_syntext.py b/hyde/tests/ext/test_syntext.py index 0db61d9..9581776 100644 --- a/hyde/tests/ext/test_syntext.py +++ b/hyde/tests/ext/test_syntext.py @@ -25,7 +25,7 @@ class TestSyntext(object): - def test_mark(self): + def test_syntext(self): text = u""" ~~~~~~~~css~~~~~~~ .body{ diff --git a/hyde/tests/ext/test_textlinks.py b/hyde/tests/ext/test_textlinks.py new file mode 100644 index 0000000..f9bf27a --- /dev/null +++ b/hyde/tests/ext/test_textlinks.py @@ -0,0 +1,57 @@ +# -*- 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 TestTextlinks(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_textlinks(self): + d = { + 'objects': 'template/variables', + 'plugins': 'plugins/metadata', + 'sorter': 'plugins/sorter' + } + text = u""" +{%% markdown %%} +* [Rich object model][hyde objects] and + [overridable hierarchical metadata]([[%(plugins)s]]) thats available for use in + templates. +* Configurable [sorting][], filtering and grouping support. + +[hyde objects]: [[%(objects)s]] +[sorting]: [[%(sorter)s]] +{%% endmarkdown %%} +""" + site = Site(TEST_SITE) + site.config.plugins = ['hyde.ext.plugins.textlinks.TextlinksPlugin'] + site.config.base_url = 'http://example.com/' + tlink = File(site.content.source_folder.child('tlink.html')) + tlink.write(text % d) + gen = Generator(site) + gen.generate_all() + f = File(site.config.deploy_root_path.child(tlink.name)) + assert f.exists + html = f.read_all() + assert html + for name, path in d.items(): + assert site.config.base_url + path in html \ No newline at end of file