PageRenderTime 105ms CodeModel.GetById 84ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/hyde/tests/ext/test_meta.py

http://github.com/hyde/hyde
Python | 264 lines | 250 code | 8 blank | 6 comment | 0 complexity | e798b1d8fc6376c6f87efad27b9186ec MD5 | raw file
  1# -*- coding: utf-8 -*-
  2"""
  3Use nose
  4`$ pip install nose`
  5`$ nosetests`
  6"""
  7from hyde.generator import Generator
  8from hyde.site import Site
  9
 10from fswrap import File, Folder
 11from pyquery import PyQuery
 12import yaml
 13
 14
 15TEST_SITE = File(__file__).parent.parent.child_folder('_test')
 16
 17
 18class TestMeta(object):
 19
 20    def setUp(self):
 21        TEST_SITE.make()
 22        TEST_SITE.parent.child_folder(
 23                    'sites/test_jinja').copy_contents_to(TEST_SITE)
 24
 25    def tearDown(self):
 26        TEST_SITE.delete()
 27
 28    def test_can_set_standard_attributes(self):
 29        text = """
 30---
 31is_processable: False
 32---
 33{% extends "base.html" %}
 34"""
 35        about2 = File(TEST_SITE.child('content/about2.html'))
 36        about2.write(text)
 37        s = Site(TEST_SITE)
 38        s.load()
 39        res = s.content.resource_from_path(about2.path)
 40        assert res.is_processable
 41
 42        s.config.plugins = ['hyde.ext.plugins.meta.MetaPlugin']
 43        gen = Generator(s)
 44        gen.generate_all()
 45        assert not res.meta.is_processable
 46        assert not res.is_processable
 47
 48    def test_ignores_pattern_in_content(self):
 49        text = """
 50{% markdown %}
 51
 52Heading 1
 53===
 54
 55Heading 2
 56===
 57
 58{% endmarkdown %}
 59"""
 60        about2 = File(TEST_SITE.child('content/about2.html'))
 61        about2.write(text)
 62        s = Site(TEST_SITE)
 63        s.load()
 64        res = s.content.resource_from_path(about2.path)
 65        assert res.is_processable
 66
 67        s.config.plugins = ['hyde.ext.plugins.meta.MetaPlugin']
 68        gen = Generator(s)
 69        gen.generate_all()
 70        target = File(Folder(s.config.deploy_root_path).child('about2.html'))
 71        text = target.read_all()
 72        q = PyQuery(text)
 73        assert q("h1").length == 2
 74        assert q("h1:nth-child(1)").text().strip() == "Heading 1"
 75        assert q("h1:nth-child(2)").text().strip() == "Heading 2"
 76
 77    def test_can_load_front_matter(self):
 78        d = {'title': 'A nice title',
 79            'author': 'Lakshmi Vyas',
 80            'twitter': 'lakshmivyas'}
 81        text = """
 82---
 83title: %(title)s
 84author: %(author)s
 85twitter: %(twitter)s
 86---
 87{%% extends "base.html" %%}
 88
 89{%% block main %%}
 90    Hi!
 91
 92    I am a test template to make sure jinja2 generation works well with hyde.
 93    <span class="title">{{resource.meta.title}}</span>
 94    <span class="author">{{resource.meta.author}}</span>
 95    <span class="twitter">{{resource.meta.twitter}}</span>
 96{%% endblock %%}
 97"""
 98        about2 = File(TEST_SITE.child('content/about2.html'))
 99        about2.write(text % d)
100        s = Site(TEST_SITE)
101        s.config.plugins = ['hyde.ext.plugins.meta.MetaPlugin']
102        gen = Generator(s)
103        gen.generate_all()
104        res = s.content.resource_from_path(about2.path)
105
106        assert hasattr(res, 'meta')
107        assert hasattr(res.meta, 'title')
108        assert hasattr(res.meta, 'author')
109        assert hasattr(res.meta, 'twitter')
110        assert res.meta.title == "A nice title"
111        assert res.meta.author == "Lakshmi Vyas"
112        assert res.meta.twitter == "lakshmivyas"
113        target = File(Folder(s.config.deploy_root_path).child('about2.html'))
114        text = target.read_all()
115        q = PyQuery(text)
116        for k, v in d.items():
117            assert v in q("span." + k).text()
118
119    def test_can_load_from_node_meta(self):
120        d = {'title': 'A nice title',
121            'author': 'Lakshmi Vyas',
122            'twitter': 'lakshmivyas'}
123        text = """
124===
125title: Even nicer title
126===
127{%% extends "base.html" %%}
128
129{%% block main %%}
130    Hi!
131
132    I am a test template to make sure jinja2 generation works well with hyde.
133    <span class="title">{{resource.meta.title}}</span>
134    <span class="author">{{resource.meta.author}}</span>
135    <span class="twitter">{{resource.meta.twitter}}</span>
136{%% endblock %%}
137"""
138        about2 = File(TEST_SITE.child('content/about2.html'))
139        about2.write(text % d)
140        meta = File(TEST_SITE.child('content/meta.yaml'))
141        meta.write(yaml.dump(d))
142        s = Site(TEST_SITE)
143        s.config.plugins = ['hyde.ext.plugins.meta.MetaPlugin']
144        gen = Generator(s)
145        gen.generate_all()
146        res = s.content.resource_from_path(about2.path)
147        assert hasattr(res, 'meta')
148        assert hasattr(res.meta, 'title')
149        assert hasattr(res.meta, 'author')
150        assert hasattr(res.meta, 'twitter')
151        assert res.meta.title == "Even nicer title"
152        assert res.meta.author == "Lakshmi Vyas"
153        assert res.meta.twitter == "lakshmivyas"
154        target = File(Folder(s.config.deploy_root_path).child('about2.html'))
155        text = target.read_all()
156        q = PyQuery(text)
157        for k, v in d.items():
158            if not k == 'title':
159                assert v in q("span." + k).text()
160        assert q("span.title").text() == "Even nicer title"
161
162    def test_can_load_from_site_meta(self):
163        d = {'title': 'A nice title',
164            'author': 'Lakshmi Vyas'}
165        text = """
166---
167title: Even nicer title
168---
169{%% extends "base.html" %%}
170
171{%% block main %%}
172    Hi!
173
174    I am a test template to make sure jinja2 generation works well with hyde.
175    <span class="title">{{resource.meta.title}}</span>
176    <span class="author">{{resource.meta.author}}</span>
177    <span class="twitter">{{resource.meta.twitter}}</span>
178{%% endblock %%}
179"""
180        about2 = File(TEST_SITE.child('content/about2.html'))
181        about2.write(text % d)
182        meta = File(TEST_SITE.child('content/nodemeta.yaml'))
183        meta.write(yaml.dump(d))
184        s = Site(TEST_SITE)
185        s.config.plugins = ['hyde.ext.plugins.meta.MetaPlugin']
186        s.config.meta = {
187            'author': 'Lakshmi',
188            'twitter': 'lakshmivyas'
189        }
190        gen = Generator(s)
191        gen.generate_all()
192        res = s.content.resource_from_path(about2.path)
193        assert hasattr(res, 'meta')
194        assert hasattr(res.meta, 'title')
195        assert hasattr(res.meta, 'author')
196        assert hasattr(res.meta, 'twitter')
197        assert res.meta.title == "Even nicer title"
198        assert res.meta.author == "Lakshmi Vyas"
199        assert res.meta.twitter == "lakshmivyas"
200        target = File(Folder(s.config.deploy_root_path).child('about2.html'))
201        text = target.read_all()
202        q = PyQuery(text)
203        for k, v in d.items():
204            if not k == 'title':
205                assert v in q("span." + k).text()
206        assert q("span.title").text() == "Even nicer title"
207
208
209    def test_multiple_levels(self):
210
211        page_d = {'title': 'An even nicer title'}
212
213        blog_d = {'author': 'Laks'}
214
215        content_d  = {'title': 'A nice title',
216                      'author': 'Lakshmi Vyas'}
217
218        site_d = {'author': 'Lakshmi',
219                  'twitter': 'lakshmivyas',
220                  'nodemeta': 'meta.yaml'}
221        text = """
222---
223title: %(title)s
224---
225{%% extends "base.html" %%}
226
227{%% block main %%}
228    Hi!
229
230    I am a test template to make sure jinja2 generation works well with hyde.
231    <span class="title">{{resource.meta.title}}</span>
232    <span class="author">{{resource.meta.author}}</span>
233    <span class="twitter">{{resource.meta.twitter}}</span>
234{%% endblock %%}
235"""
236        about2 = File(TEST_SITE.child('content/blog/about2.html'))
237        about2.write(text % page_d)
238        content_meta = File(TEST_SITE.child('content/nodemeta.yaml'))
239        content_meta.write(yaml.dump(content_d))
240        content_meta = File(TEST_SITE.child('content/blog/meta.yaml'))
241        content_meta.write(yaml.dump(blog_d))
242        s = Site(TEST_SITE)
243        s.config.plugins = ['hyde.ext.plugins.meta.MetaPlugin']
244        s.config.meta = site_d
245        gen = Generator(s)
246        gen.generate_all()
247        expected = {}
248
249        expected.update(site_d)
250        expected.update(content_d)
251        expected.update(blog_d)
252        expected.update(page_d)
253
254        res = s.content.resource_from_path(about2.path)
255        assert hasattr(res, 'meta')
256        for k, v in expected.items():
257            assert hasattr(res.meta, k)
258            assert getattr(res.meta, k) == v
259        target = File(Folder(s.config.deploy_root_path).child('blog/about2.html'))
260        text = target.read_all()
261        q = PyQuery(text)
262        for k, v in expected.items():
263            if k != 'nodemeta':
264                assert v in q("span." + k).text()