@@ -1,55 +0,0 @@ | |||||
# -*- coding: utf-8 -*- | |||||
""" | |||||
Autoextend plugin | |||||
""" | |||||
from hyde.plugin import Plugin | |||||
import re | |||||
class AutoExtendPlugin(Plugin): | |||||
""" | |||||
The plugin class for extending templates using metadata. | |||||
""" | |||||
def __init__(self, site): | |||||
super(AutoExtendPlugin, self).__init__(site) | |||||
def begin_text_resource(self, resource, text): | |||||
""" | |||||
If the meta data for the resource contains a layout attribute, | |||||
and there is no extends statement, this plugin automatically adds | |||||
an extends statement to the top of the file. | |||||
""" | |||||
if not resource.uses_template: | |||||
return text | |||||
layout = None | |||||
block = None | |||||
try: | |||||
layout = resource.meta.extends | |||||
except AttributeError: | |||||
pass | |||||
try: | |||||
block = resource.meta.default_block | |||||
except AttributeError: | |||||
pass | |||||
if layout: | |||||
self.logger.debug("Autoextending %s with %s" % ( | |||||
resource.relative_path, layout)) | |||||
extends_pattern = self.template.patterns['extends'] | |||||
if not re.search(extends_pattern, text): | |||||
extended_text = self.template.get_extends_statement(layout) | |||||
extended_text += '\n' | |||||
if block: | |||||
extended_text += ('%s\n%s\n%s' % | |||||
(self.t_block_open_tag(block), | |||||
text, | |||||
self.t_block_close_tag(block))) | |||||
else: | |||||
extended_text += text | |||||
return extended_text | |||||
return text |
@@ -142,3 +142,53 @@ class MetaPlugin(Plugin): | |||||
has changed. Return text without meta data. | has changed. Return text without meta data. | ||||
""" | """ | ||||
return self.__read_resource__(resource, text) | return self.__read_resource__(resource, text) | ||||
class AutoExtendPlugin(Plugin): | |||||
""" | |||||
The plugin class for extending templates using metadata. | |||||
""" | |||||
def __init__(self, site): | |||||
super(AutoExtendPlugin, self).__init__(site) | |||||
def begin_text_resource(self, resource, text): | |||||
""" | |||||
If the meta data for the resource contains a layout attribute, | |||||
and there is no extends statement, this plugin automatically adds | |||||
an extends statement to the top of the file. | |||||
""" | |||||
if not resource.uses_template: | |||||
return text | |||||
layout = None | |||||
block = None | |||||
try: | |||||
layout = resource.meta.extends | |||||
except AttributeError: | |||||
pass | |||||
try: | |||||
block = resource.meta.default_block | |||||
except AttributeError: | |||||
pass | |||||
if layout: | |||||
self.logger.debug("Autoextending %s with %s" % ( | |||||
resource.relative_path, layout)) | |||||
extends_pattern = self.template.patterns['extends'] | |||||
if not re.search(extends_pattern, text): | |||||
extended_text = self.template.get_extends_statement(layout) | |||||
extended_text += '\n' | |||||
if block: | |||||
extended_text += ('%s\n%s\n%s' % | |||||
(self.t_block_open_tag(block), | |||||
text, | |||||
self.t_block_close_tag(block))) | |||||
else: | |||||
extended_text += text | |||||
return extended_text | |||||
return text | |||||
@@ -4,7 +4,7 @@ media_url: /media # URL where the media files are served from. | |||||
base_url: / # The base url for autogenerated links. | base_url: / # The base url for autogenerated links. | ||||
plugins: | plugins: | ||||
- hyde.ext.plugins.meta.MetaPlugin | - hyde.ext.plugins.meta.MetaPlugin | ||||
- hyde.ext.plugins.auto_extend.AutoExtendPlugin | - hyde.ext.plugins.meta.AutoExtendPlugin | ||||
- hyde.ext.plugins.sorter.SorterPlugin | - hyde.ext.plugins.sorter.SorterPlugin | ||||
- hyde.ext.plugins.tagger.TaggerPlugin | - hyde.ext.plugins.tagger.TaggerPlugin | ||||
- hyde.ext.plugins.syntext.SyntextPlugin | - hyde.ext.plugins.syntext.SyntextPlugin | ||||
@@ -58,4 +58,4 @@ tagger: | |||||
template: tagged_posts.j2 | template: tagged_posts.j2 | ||||
archive_extension: html | archive_extension: html | ||||
meta: | meta: | ||||
listable: false | listable: false |
@@ -10,7 +10,7 @@ base_url: / | |||||
template: hyde.ext.templates.jinja.Jinja2Template | template: hyde.ext.templates.jinja.Jinja2Template | ||||
plugins: | plugins: | ||||
- hyde.ext.plugins.meta.MetaPlugin | - hyde.ext.plugins.meta.MetaPlugin | ||||
- hyde.ext.plugins.auto_extend.AutoExtendPlugin | - hyde.ext.plugins.meta.AutoExtendPlugin | ||||
# Plugins needed for the advances section. | # Plugins needed for the advances section. | ||||
- hyde.ext.plugins.sorter.SorterPlugin | - hyde.ext.plugins.sorter.SorterPlugin | ||||
- hyde.ext.plugins.grouper.GrouperPlugin | - hyde.ext.plugins.grouper.GrouperPlugin | ||||
@@ -42,7 +42,7 @@ class TestAutoExtend(object): | |||||
def test_can_auto_extend(self): | def test_can_auto_extend(self): | ||||
s = Site(TEST_SITE) | s = Site(TEST_SITE) | ||||
s.config.plugins = ['hyde.ext.plugins.meta.MetaPlugin', | s.config.plugins = ['hyde.ext.plugins.meta.MetaPlugin', | ||||
'hyde.ext.plugins.auto_extend.AutoExtendPlugin', | 'hyde.ext.plugins.meta.AutoExtendPlugin', | ||||
'hyde.ext.plugins.text.BlockdownPlugin'] | 'hyde.ext.plugins.text.BlockdownPlugin'] | ||||
txt ="This template tests to make sure blocks can be replaced with markdownish syntax." | txt ="This template tests to make sure blocks can be replaced with markdownish syntax." | ||||
templ = """ | templ = """ | ||||
@@ -59,7 +59,7 @@ extends: base.html | |||||
def test_can_auto_extend_with_default_blocks(self): | def test_can_auto_extend_with_default_blocks(self): | ||||
s = Site(TEST_SITE) | s = Site(TEST_SITE) | ||||
s.config.plugins = ['hyde.ext.plugins.meta.MetaPlugin', | s.config.plugins = ['hyde.ext.plugins.meta.MetaPlugin', | ||||
'hyde.ext.plugins.auto_extend.AutoExtendPlugin', | 'hyde.ext.plugins.meta.AutoExtendPlugin', | ||||
'hyde.ext.plugins.text.BlockdownPlugin'] | 'hyde.ext.plugins.text.BlockdownPlugin'] | ||||
txt ="This template tests to make sure blocks can be replaced with markdownish syntax." | txt ="This template tests to make sure blocks can be replaced with markdownish syntax." | ||||
templ = """ | templ = """ | ||||
@@ -4,7 +4,7 @@ media_url: /media # URL where the media files are served from. | |||||
base_url: / # The base url for autogenerated links. | base_url: / # The base url for autogenerated links. | ||||
plugins: | plugins: | ||||
- hyde.ext.plugins.meta.MetaPlugin | - hyde.ext.plugins.meta.MetaPlugin | ||||
- hyde.ext.plugins.auto_extend.AutoExtendPlugin | - hyde.ext.plugins.meta.AutoExtendPlugin | ||||
- hyde.ext.plugins.sorter.SorterPlugin | - hyde.ext.plugins.sorter.SorterPlugin | ||||
- hyde.ext.plugins.text.TextlinksPlugin | - hyde.ext.plugins.text.TextlinksPlugin | ||||
meta: | meta: | ||||
@@ -4,7 +4,7 @@ media_url: /media # URL where the media files are served from. | |||||
base_url: / # The base url for autogenerated links. | base_url: / # The base url for autogenerated links. | ||||
plugins: | plugins: | ||||
- hyde.ext.plugins.meta.MetaPlugin | - hyde.ext.plugins.meta.MetaPlugin | ||||
- hyde.ext.plugins.auto_extend.AutoExtendPlugin | - hyde.ext.plugins.meta.AutoExtendPlugin | ||||
- hyde.ext.plugins.sorter.SorterPlugin | - hyde.ext.plugins.sorter.SorterPlugin | ||||
- hyde.ext.plugins.tagger.TaggerPlugin | - hyde.ext.plugins.tagger.TaggerPlugin | ||||
- hyde.ext.plugins.text.TextlinksPlugin | - hyde.ext.plugins.text.TextlinksPlugin | ||||
@@ -4,7 +4,7 @@ media_url: /media # URL where the media files are served from. | |||||
base_url: / # The base url for autogenerated links. | base_url: / # The base url for autogenerated links. | ||||
plugins: | plugins: | ||||
- hyde.ext.plugins.meta.MetaPlugin | - hyde.ext.plugins.meta.MetaPlugin | ||||
- hyde.ext.plugins.auto_extend.AutoExtendPlugin | - hyde.ext.plugins.meta.AutoExtendPlugin | ||||
- hyde.ext.plugins.sorter.SorterPlugin | - hyde.ext.plugins.sorter.SorterPlugin | ||||
- hyde.ext.plugins.tagger.TaggerPlugin | - hyde.ext.plugins.tagger.TaggerPlugin | ||||
- hyde.ext.plugins.text.SyntextPlugin | - hyde.ext.plugins.text.SyntextPlugin | ||||