A fork of hyde, the static site generation. Some patches will be pushed upstream.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

265 lines
8.1 KiB

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