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.
 
 
 

272 lines
8.8 KiB

  1. # -*- coding: utf-8 -*-
  2. """
  3. Use nose
  4. `$ pip install nose`
  5. `$ nosetests`
  6. """
  7. from hyde.ext.plugins.meta import MetaPlugin
  8. from hyde.ext.plugins.sorter import SorterPlugin
  9. from hyde.fs import File, Folder
  10. from hyde.generator import Generator
  11. from hyde.site import Site
  12. from hyde.model import Config, Expando
  13. import yaml
  14. from operator import attrgetter
  15. TEST_SITE = File(__file__).parent.parent.child_folder('_test')
  16. class TestSorter(object):
  17. def setUp(self):
  18. TEST_SITE.make()
  19. TEST_SITE.parent.child_folder(
  20. 'sites/test_jinja').copy_contents_to(TEST_SITE)
  21. def tearDown(self):
  22. TEST_SITE.delete()
  23. def test_walk_resources_sorted(self):
  24. s = Site(TEST_SITE)
  25. s.load()
  26. s.config.plugins = ['hyde.ext.sorter.SorterPlugin']
  27. s.config.sorter = Expando(dict(kind=dict(attr='source_file.kind')))
  28. SorterPlugin(s).begin_site()
  29. assert hasattr(s.content, 'walk_resources_sorted_by_kind')
  30. expected = ["404.html",
  31. "about.html",
  32. "apple-touch-icon.png",
  33. "merry-christmas.html",
  34. "crossdomain.xml",
  35. "favicon.ico",
  36. "robots.txt",
  37. "site.css"
  38. ]
  39. pages = [page.name for page in
  40. s.content.walk_resources_sorted_by_kind()]
  41. assert pages == sorted(expected, key=lambda f: File(f).kind)
  42. def test_walk_resources_sorted_reverse(self):
  43. s = Site(TEST_SITE)
  44. s.load()
  45. s.config.plugins = ['hyde.ext.sorter.SorterPlugin']
  46. s.config.sorter = Expando(dict(kind=dict(attr='source_file.kind', reverse=True)))
  47. SorterPlugin(s).begin_site()
  48. assert hasattr(s.content, 'walk_resources_sorted_by_kind')
  49. expected = ["404.html",
  50. "about.html",
  51. "apple-touch-icon.png",
  52. "merry-christmas.html",
  53. "crossdomain.xml",
  54. "favicon.ico",
  55. "robots.txt",
  56. "site.css"
  57. ]
  58. pages = [page.name for page in
  59. s.content.walk_resources_sorted_by_kind()]
  60. assert pages == sorted(expected, key=lambda f: File(f).kind, reverse=True)
  61. def test_walk_resources_sorted_with_filters(self):
  62. s = Site(TEST_SITE)
  63. cfg = """
  64. plugins:
  65. - hyde.ext.sorter.SorterPlugin
  66. sorter:
  67. kind2:
  68. filters:
  69. source_file.kind: html
  70. """
  71. s.config = Config(TEST_SITE, config_dict=yaml.load(cfg))
  72. s.load()
  73. SorterPlugin(s).begin_site()
  74. assert hasattr(s.content, 'walk_resources_sorted_by_kind2')
  75. expected = ["404.html",
  76. "about.html",
  77. "merry-christmas.html"
  78. ]
  79. pages = [page.name for page in s.content.walk_resources_sorted_by_kind2()]
  80. assert pages == sorted(expected)
  81. def test_walk_resources_sorted_with_multiple_attributes(self):
  82. s = Site(TEST_SITE)
  83. cfg = """
  84. plugins:
  85. - hyde.ext.sorter.SorterPlugin
  86. sorter:
  87. multi:
  88. attr:
  89. - source_file.kind
  90. - node.name
  91. """
  92. s.config = Config(TEST_SITE, config_dict=yaml.load(cfg))
  93. s.load()
  94. SorterPlugin(s).begin_site()
  95. assert hasattr(s.content, 'walk_resources_sorted_by_multi')
  96. expected = ["content/404.html",
  97. "content/about.html",
  98. "content/apple-touch-icon.png",
  99. "content/blog/2010/december/merry-christmas.html",
  100. "content/crossdomain.xml",
  101. "content/favicon.ico",
  102. "content/robots.txt",
  103. "content/site.css"
  104. ]
  105. pages = [page.name for page in s.content.walk_resources_sorted_by_multi()]
  106. expected_sorted = [File(page).name
  107. for page in
  108. sorted(expected,
  109. key=lambda p: tuple(
  110. [File(p).kind,
  111. File(p).parent.name]))]
  112. assert pages == expected_sorted
  113. def test_walk_resources_sorted_no_default_is_processable(self):
  114. s = Site(TEST_SITE)
  115. cfg = """
  116. plugins:
  117. - hyde.ext.sorter.SorterPlugin
  118. sorter:
  119. kind2:
  120. filters:
  121. source_file.kind: html
  122. """
  123. s.config = Config(TEST_SITE, config_dict=yaml.load(cfg))
  124. s.load()
  125. p_404 = s.content.resource_from_relative_path('404.html')
  126. p_404.is_processable = False
  127. SorterPlugin(s).begin_site()
  128. assert hasattr(s.content, 'walk_resources_sorted_by_kind2')
  129. expected = ["404.html", "about.html", "merry-christmas.html"]
  130. pages = [page.name for page in s.content.walk_resources_sorted_by_kind2()]
  131. assert pages == sorted(expected)
  132. def test_prev_next(self):
  133. s = Site(TEST_SITE)
  134. cfg = """
  135. plugins:
  136. - hyde.ext.sorter.SorterPlugin
  137. sorter:
  138. kind2:
  139. filters:
  140. source_file.kind: html
  141. """
  142. s.config = Config(TEST_SITE, config_dict=yaml.load(cfg))
  143. s.load()
  144. SorterPlugin(s).begin_site()
  145. p_404 = s.content.resource_from_relative_path('404.html')
  146. p_about = s.content.resource_from_relative_path('about.html')
  147. p_mc = s.content.resource_from_relative_path(
  148. 'blog/2010/december/merry-christmas.html')
  149. assert hasattr(p_404, 'prev_by_kind2')
  150. assert not p_404.prev_by_kind2
  151. assert hasattr(p_404, 'next_by_kind2')
  152. assert p_404.next_by_kind2 == p_about
  153. assert hasattr(p_about, 'prev_by_kind2')
  154. assert p_about.prev_by_kind2 == p_404
  155. assert hasattr(p_about, 'next_by_kind2')
  156. assert p_about.next_by_kind2 == p_mc
  157. assert hasattr(p_mc, 'prev_by_kind2')
  158. assert p_mc.prev_by_kind2 == p_about
  159. assert hasattr(p_mc, 'next_by_kind2')
  160. assert not p_mc.next_by_kind2
  161. def test_prev_next_reversed(self):
  162. s = Site(TEST_SITE)
  163. cfg = """
  164. plugins:
  165. - hyde.ext.sorter.SorterPlugin
  166. sorter:
  167. folder_name:
  168. attr: node.name
  169. reverse: True
  170. filters:
  171. source_file.kind: html
  172. """
  173. s.config = Config(TEST_SITE, config_dict=yaml.load(cfg))
  174. s.load()
  175. SorterPlugin(s).begin_site()
  176. p_404 = s.content.resource_from_relative_path('404.html')
  177. p_about = s.content.resource_from_relative_path('about.html')
  178. p_mc = s.content.resource_from_relative_path(
  179. 'blog/2010/december/merry-christmas.html')
  180. assert hasattr(p_mc, 'prev_by_folder_name')
  181. assert not p_mc.prev_by_folder_name
  182. assert hasattr(p_mc, 'next_by_folder_name')
  183. assert p_mc.next_by_folder_name == p_404
  184. assert hasattr(p_404, 'prev_by_folder_name')
  185. assert p_404.prev_by_folder_name == p_mc
  186. assert hasattr(p_404, 'next_by_folder_name')
  187. assert p_404.next_by_folder_name == p_about
  188. assert hasattr(p_about, 'prev_by_folder_name')
  189. assert p_about.prev_by_folder_name == p_404
  190. assert hasattr(p_about, 'next_by_folder_name')
  191. assert not p_about.next_by_folder_name
  192. def test_walk_resources_sorted_using_generator(self):
  193. s = Site(TEST_SITE)
  194. cfg = """
  195. meta:
  196. time: !!timestamp 2010-10-23
  197. title: NahNahNah
  198. plugins:
  199. - hyde.ext.plugins.meta.MetaPlugin
  200. - hyde.ext.plugins.sorter.SorterPlugin
  201. sorter:
  202. time:
  203. attr: meta.time
  204. filters:
  205. source_file.kind: html
  206. """
  207. s.config = Config(TEST_SITE, config_dict=yaml.load(cfg))
  208. text = """
  209. ---
  210. time: !!timestamp 2010-12-31
  211. title: YayYayYay
  212. ---
  213. {% extends "base.html" %}
  214. {% block main %}
  215. {% set latest = site.content.walk_resources_sorted_by_time()|reverse|first %}
  216. <span class="latest">{{ latest.meta.title }}</span>
  217. {% endblock %}
  218. """
  219. about2 = File(TEST_SITE.child('content/about2.html'))
  220. about2.write(text)
  221. gen = Generator(s)
  222. gen.generate_all()
  223. from pyquery import PyQuery
  224. target = File(Folder(s.config.deploy_root_path).child('about2.html'))
  225. text = target.read_all()
  226. q = PyQuery(text)
  227. assert q('span.latest').text() == 'YayYayYay'