Browse Source

Added a basic version of depends plugin

main
Lakshmi Vyasarajan 14 years ago
parent
commit
d41ca56ebf
2 changed files with 98 additions and 0 deletions
  1. +47
    -0
      hyde/ext/plugins/depends.py
  2. +51
    -0
      hyde/tests/ext/test_depends.py

+ 47
- 0
hyde/ext/plugins/depends.py View File

@@ -0,0 +1,47 @@
# -*- coding: utf-8 -*-
"""
Depends plugin

/// Experimental: Not working yet.
"""

from hyde.plugin import Plugin
import re

class DependsPlugin(Plugin):
"""
The plugin class setting explicit dependencies.
"""

def __init__(self, site):
super(DependsPlugin, self).__init__(site)

def begin_text_resource(self, resource, text):
"""
If the meta data for the resource contains a depends attribute,
this plugin adds an entry to the depends property of the
resource.

The dependency can contain the following template variables:
node, resource, site, context.

The folloing strings are valid:
'{node.module}/dependencies/{resource.source.name_without_extension}.inc'
'{context.dependency_folder}/{resource.source.name_without_extension}.{site.meta.depext}'
"""
depends = []
try:
depends = resource.meta.depends
except AttributeError:
pass

if not hasattr(resource, 'depends'):
resource.depends = []
resource.depends = resource.depends or []

for dep in depends:
resource.depends.append(dep.format(node=resource.node,
resource=resource,
site=self.site,
context=self.site.context))
return text

+ 51
- 0
hyde/tests/ext/test_depends.py View File

@@ -0,0 +1,51 @@
# -*- 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

from nose.tools import nottest

TEST_SITE = File(__file__).parent.parent.child_folder('_test')


class TestDepends(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_depends(self):
s = Site(TEST_SITE)
s.config.plugins = ['hyde.ext.plugins.meta.MetaPlugin',
'hyde.ext.plugins.depends.DependsPlugin']
text = """
===
depends: index.html
===
{% set ind = 'index.html' %}
{% refer to ind as index %}
"""
inc = File(TEST_SITE.child('content/inc.md'))
inc.write(text)
gen = Generator(s)
gen.load_site_if_needed()
gen.load_template_if_needed()
res = s.content.resource_from_relative_path('inc.md')
deps = list(gen.get_dependencies(res))

assert len(deps) == 3

assert 'helpers.html' in deps
assert 'layout.html' in deps
assert 'index.html' in deps

Loading…
Cancel
Save