Browse Source

Move the AutoExtend plugin into the meta module.

main
Elliott Sales de Andrade 12 years ago
committed by Lakshmi Vyasarajan
parent
commit
3cc29f9fad
8 changed files with 58 additions and 63 deletions
  1. +0
    -55
      hyde/ext/plugins/auto_extend.py
  2. +50
    -0
      hyde/ext/plugins/meta.py
  3. +2
    -2
      hyde/layouts/basic/site.yaml
  4. +1
    -1
      hyde/layouts/starter/site.yaml
  5. +2
    -2
      hyde/tests/ext/test_auto_extend.py
  6. +1
    -1
      hyde/tests/sites/test_sorter/site.yaml
  7. +1
    -1
      hyde/tests/sites/test_tagger/site.yaml
  8. +1
    -1
      hyde/tests/ssp/site.yaml

+ 0
- 55
hyde/ext/plugins/auto_extend.py View File

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

+ 50
- 0
hyde/ext/plugins/meta.py View File

@@ -142,3 +142,53 @@ class MetaPlugin(Plugin):
has changed. Return text without meta data.
"""
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


+ 2
- 2
hyde/layouts/basic/site.yaml View File

@@ -4,7 +4,7 @@ media_url: /media # URL where the media files are served from.
base_url: / # The base url for autogenerated links.
plugins:
- hyde.ext.plugins.meta.MetaPlugin
- hyde.ext.plugins.auto_extend.AutoExtendPlugin
- hyde.ext.plugins.meta.AutoExtendPlugin
- hyde.ext.plugins.sorter.SorterPlugin
- hyde.ext.plugins.tagger.TaggerPlugin
- hyde.ext.plugins.syntext.SyntextPlugin
@@ -58,4 +58,4 @@ tagger:
template: tagged_posts.j2
archive_extension: html
meta:
listable: false
listable: false

+ 1
- 1
hyde/layouts/starter/site.yaml View File

@@ -10,7 +10,7 @@ base_url: /
template: hyde.ext.templates.jinja.Jinja2Template
plugins:
- hyde.ext.plugins.meta.MetaPlugin
- hyde.ext.plugins.auto_extend.AutoExtendPlugin
- hyde.ext.plugins.meta.AutoExtendPlugin
# Plugins needed for the advances section.
- hyde.ext.plugins.sorter.SorterPlugin
- hyde.ext.plugins.grouper.GrouperPlugin


+ 2
- 2
hyde/tests/ext/test_auto_extend.py View File

@@ -42,7 +42,7 @@ class TestAutoExtend(object):
def test_can_auto_extend(self):
s = Site(TEST_SITE)
s.config.plugins = ['hyde.ext.plugins.meta.MetaPlugin',
'hyde.ext.plugins.auto_extend.AutoExtendPlugin',
'hyde.ext.plugins.meta.AutoExtendPlugin',
'hyde.ext.plugins.text.BlockdownPlugin']
txt ="This template tests to make sure blocks can be replaced with markdownish syntax."
templ = """
@@ -59,7 +59,7 @@ extends: base.html
def test_can_auto_extend_with_default_blocks(self):
s = Site(TEST_SITE)
s.config.plugins = ['hyde.ext.plugins.meta.MetaPlugin',
'hyde.ext.plugins.auto_extend.AutoExtendPlugin',
'hyde.ext.plugins.meta.AutoExtendPlugin',
'hyde.ext.plugins.text.BlockdownPlugin']
txt ="This template tests to make sure blocks can be replaced with markdownish syntax."
templ = """


+ 1
- 1
hyde/tests/sites/test_sorter/site.yaml View File

@@ -4,7 +4,7 @@ media_url: /media # URL where the media files are served from.
base_url: / # The base url for autogenerated links.
plugins:
- hyde.ext.plugins.meta.MetaPlugin
- hyde.ext.plugins.auto_extend.AutoExtendPlugin
- hyde.ext.plugins.meta.AutoExtendPlugin
- hyde.ext.plugins.sorter.SorterPlugin
- hyde.ext.plugins.text.TextlinksPlugin
meta:


+ 1
- 1
hyde/tests/sites/test_tagger/site.yaml View File

@@ -4,7 +4,7 @@ media_url: /media # URL where the media files are served from.
base_url: / # The base url for autogenerated links.
plugins:
- hyde.ext.plugins.meta.MetaPlugin
- hyde.ext.plugins.auto_extend.AutoExtendPlugin
- hyde.ext.plugins.meta.AutoExtendPlugin
- hyde.ext.plugins.sorter.SorterPlugin
- hyde.ext.plugins.tagger.TaggerPlugin
- hyde.ext.plugins.text.TextlinksPlugin


+ 1
- 1
hyde/tests/ssp/site.yaml View File

@@ -4,7 +4,7 @@ media_url: /media # URL where the media files are served from.
base_url: / # The base url for autogenerated links.
plugins:
- hyde.ext.plugins.meta.MetaPlugin
- hyde.ext.plugins.auto_extend.AutoExtendPlugin
- hyde.ext.plugins.meta.AutoExtendPlugin
- hyde.ext.plugins.sorter.SorterPlugin
- hyde.ext.plugins.tagger.TaggerPlugin
- hyde.ext.plugins.text.SyntextPlugin


Loading…
Cancel
Save