def test_simple_copy_basic(self): self.setup_config(['about.html']) s = Site(self.SITE_PATH, config=self.config) s.load() res = s.content.resource_from_relative_path('about.html') assert res assert res.simple_copy
def test_prev_next(self): s = Site(TEST_SITE) cfg = """ plugins: - hyde.ext.meta.SorterPlugin sorter: kind2: filters: source_file.kind: html attr: - name """ s.config = Config(TEST_SITE, config_dict=yaml.load(cfg)) s.load() SorterPlugin(s).begin_site() p_404 = s.content.resource_from_relative_path('404.html') p_about = s.content.resource_from_relative_path('about.html') p_mc = s.content.resource_from_relative_path( 'blog/2010/december/merry-christmas.html') assert hasattr(p_404, 'prev_by_kind2') assert not p_404.prev_by_kind2 assert hasattr(p_404, 'next_by_kind2') assert p_404.next_by_kind2 == p_about assert hasattr(p_about, 'prev_by_kind2') assert p_about.prev_by_kind2 == p_404 assert hasattr(p_about, 'next_by_kind2') assert p_about.next_by_kind2 == p_mc assert hasattr(p_mc, 'prev_by_kind2') assert p_mc.prev_by_kind2 == p_about assert hasattr(p_mc, 'next_by_kind2') assert not p_mc.next_by_kind2
def test_context(self): site = Site(TEST_SITE, Config(TEST_SITE, config_dict={ "context": { "data": { "abc": "def" } } })) text = """ {% extends "base.html" %} {% block main %} abc = {{ abc }} Hi! I am a test template to make sure jinja2 generation works well with hyde. {{resource.name}} {% endblock %} """ site.load() resource = site.content.resource_from_path( TEST_SITE.child('content/about.html')) gen = Generator(site) resource.source_file.write(text) gen.generate_all() target = File(site.config.deploy_root_path.child(resource.name)) assert "abc = def" in target.read_all()
def test_ignores_pattern_in_content(self): text = """ {% markdown %} Heading 1 === Heading 2 === {% endmarkdown %} """ about2 = File(TEST_SITE.child("content/about2.html")) about2.write(text) s = Site(TEST_SITE) s.load() res = s.content.resource_from_path(about2.path) assert res.is_processable s.config.plugins = ["hyde.ext.plugins.meta.MetaPlugin"] gen = Generator(s) gen.generate_all() target = File(Folder(s.config.deploy_root_path).child("about2.html")) text = target.read_all() q = PyQuery(text) assert q("h1").length == 2 assert q("h1:eq(0)").text().strip() == "Heading 1" assert q("h1:eq(1)").text().strip() == "Heading 2"
def test_get_resource(): s = Site(TEST_SITE_ROOT) s.load() path = 'blog/2010/december' node = s.content.node_from_relative_path(path) resource = node.get_resource('merry-christmas.html') assert resource == s.content.resource_from_relative_path(Folder(path).child('merry-christmas.html'))
def test_resource_slug(): s = Site(TEST_SITE_ROOT) s.load() path = 'blog/2010/december' node = s.content.node_from_relative_path(path) resource = node.get_resource('merry-christmas.html') assert resource.slug == 'merry-christmas'
def test_ignores_pattern_in_content(self): text = """ {% markdown %} Heading 1 === Heading 2 === {% endmarkdown %} """ about2 = File(TEST_SITE.child('content/about2.html')) about2.write(text) s = Site(TEST_SITE) s.load() res = s.content.resource_from_path(about2.path) assert res.is_processable s.config.plugins = ['hyde.ext.plugins.meta.MetaPlugin'] gen = Generator(s) gen.generate_all() target = File(Folder(s.config.deploy_root_path).child('about2.html')) text = target.read_all() q = PyQuery(text) assert q("h1").length == 2 assert q("h1:eq(0)").text().strip() == "Heading 1" assert q("h1:eq(1)").text().strip() == "Heading 2"
def test_context(self): site = Site(TEST_SITE, Config(TEST_SITE, config_dict={ "context": { "data": { "abc": "def" } } })) text = """ {% extends "base.html" %} {% block main %} abc = {{ abc }} Hi! I am a test template to make sure jinja2 generation works well with hyde. {{resource.name}} {% endblock %} """ site.load() resource = site.content.resource_from_path(TEST_SITE.child('content/about.html')) gen = Generator(site) resource.source_file.write(text) gen.generate_all() target = File(site.config.deploy_root_path.child(resource.name)) assert "abc = def" in target.read_all()
def test_content_url_encoding_safe(self): s = Site(self.SITE_PATH, config=self.config) s.load() path = '".jpg/abc' print(s.content_url(path, "")) print("/" + quote(path, "")) assert s.content_url(path, "") == "/" + quote(path, "")
def test_simple_copy_basic(self): self.setup_config(["about.html"]) s = Site(self.SITE_PATH, config=self.config) s.load() res = s.content.resource_from_relative_path("about.html") assert res assert res.simple_copy
def test_full_url_for_media(self): s = Site(self.SITE_PATH, config=self.config) s.load() path = 'media/css/site.css' assert s.is_media(path) full_url = s.full_url(path) assert full_url == "/" + path
def test_walk_resources_sorted_no_default_is_processable(self): s = Site(TEST_SITE) cfg = """ plugins: - hyde.ext.meta.SorterPlugin sorter: kind2: filters: source_file.kind: html attr: - name """ s.config = Config(TEST_SITE, config_dict=yaml.load(cfg)) s.load() p_404 = s.content.resource_from_relative_path('404.html') p_404.is_processable = False SorterPlugin(s).begin_site() assert hasattr(s.content, 'walk_resources_sorted_by_kind2') expected = ["404.html", "about.html", "merry-christmas.html"] pages = [ page.name for page in s.content.walk_resources_sorted_by_kind2() ] assert pages == sorted(expected)
def test_prev_next(self): s = Site(TEST_SITE) cfg = """ plugins: - hyde.ext.sorter.SorterPlugin sorter: kind2: filters: source_file.kind: html attr: - name """ s.config = Config(TEST_SITE, config_dict=yaml.load(cfg)) s.load() SorterPlugin(s).begin_site() p_404 = s.content.resource_from_relative_path('404.html') p_about = s.content.resource_from_relative_path('about.html') p_mc = s.content.resource_from_relative_path( 'blog/2010/december/merry-christmas.html') assert hasattr(p_404, 'prev_by_kind2') assert not p_404.prev_by_kind2 assert hasattr(p_404, 'next_by_kind2') assert p_404.next_by_kind2 == p_about assert hasattr(p_about, 'prev_by_kind2') assert p_about.prev_by_kind2 == p_404 assert hasattr(p_about, 'next_by_kind2') assert p_about.next_by_kind2 == p_mc assert hasattr(p_mc, 'prev_by_kind2') assert p_mc.prev_by_kind2 == p_about assert hasattr(p_mc, 'next_by_kind2') assert not p_mc.next_by_kind2
def test_content_url_encoding_safe(self): s = Site(self.SITE_PATH, config=self.config) s.load() path = '".jpg/abc' print((s.content_url(path, ""))) print(("/" + quote(path, ""))) assert s.content_url(path, "") == "/" + quote(path, "")
def test_media_url_from_resource(self): s = Site(self.SITE_PATH, config=self.config) s.load() path = 'css/site.css' resource = s.content.resource_from_relative_path( Folder("media").child(path)) assert resource assert resource.full_url == "/media/" + path
def test_is_media(self): s = Site(self.SITE_PATH, config=self.config) s.load() assert s.is_media('media/css/site.css') s.config.media_root = 'monkey' assert not s.is_media('media/css/site.css') assert s.is_media('monkey/css/site.css')
def test_generate_resource_from_path_with_is_processable_false(self): site = Site(TEST_SITE) site.load() resource = site.content.resource_from_path(TEST_SITE.child('content/about.html')) resource.is_processable = False gen = Generator(site) gen.generate_resource_at_path(TEST_SITE.child('content/about.html')) about = File(Folder(site.config.deploy_root_path).child('about.html')) assert not about.exists
def test_generate_resource_from_path(self): site = Site(TEST_SITE) site.load() gen = Generator(site) gen.generate_resource_at_path(TEST_SITE.child('content/about.html')) about = File(Folder(site.config.deploy_root_path).child('about.html')) assert about.exists text = about.read_all() q = PyQuery(text) assert about.name in q("div#main").text()
def test_relative_deploy_path_override(): s = Site(TEST_SITE_ROOT) s.load() res = s.content.resource_from_relative_path('blog/2010/december/merry-christmas.html') res.relative_deploy_path = 'blog/2010/december/happy-holidays.html' for page in s.content.walk_resources(): if res.source_file == page.source_file: assert page.relative_deploy_path == 'blog/2010/december/happy-holidays.html' else: assert page.relative_deploy_path == Folder(page.relative_path)
def test_generate_resource_from_path_with_is_processable_false(self): site = Site(TEST_SITE) site.load() resource = site.content.resource_from_path( TEST_SITE.child('content/about.html')) resource.is_processable = False gen = Generator(site) gen.generate_resource_at_path(TEST_SITE.child('content/about.html')) about = File(Folder(site.config.deploy_root_path).child('about.html')) assert not about.exists
def test_simple_copy_directory(self): self.setup_config(["**/*.html"]) s = Site(self.SITE_PATH, config=self.config) s.load() res = s.content.resource_from_relative_path("about.html") assert res assert not res.simple_copy res = s.content.resource_from_relative_path("blog/2010/december/merry-christmas.html") assert res assert res.simple_copy
def test_context_providers_equivalence(self): import yaml events = """ 2011: - title: "one event" location: "a city" - title: "one event" location: "a city" 2010: - title: "one event" location: "a city" - title: "one event" location: "a city" """ events_dict = yaml.load(events) config_dict = dict(context=dict( data=dict(events1=events_dict), providers=dict(events2="events.yaml") )) text = """ {%% extends "base.html" %%} {%% block main %%} <ul> {%% for year, eventlist in %s %%} <li> <h1>{{ year }}</h1> <ul> {%% for event in eventlist %%} <li>{{ event.title }}-{{ event.location }}</li> {%% endfor %%} </ul> </li> {%% endfor %%} </ul> {%% endblock %%} """ File(TEST_SITE.child('events.yaml')).write(events) f1 = File(TEST_SITE.child('content/text1.html')) f2 = File(TEST_SITE.child('content/text2.html')) f1.write(text % "events1") f2.write(text % "events2") site = Site(TEST_SITE, Config(TEST_SITE, config_dict=config_dict)) site.load() gen = Generator(site) gen.generate_all() left = File(site.config.deploy_root_path.child(f1.name)).read_all() right = File(site.config.deploy_root_path.child(f2.name)).read_all() assert left == right
def test_context_providers_equivalence(self): import yaml events = """ 2011: - title: "one event" location: "a city" - title: "one event" location: "a city" 2010: - title: "one event" location: "a city" - title: "one event" location: "a city" """ events_dict = yaml.load(events, Loader=yaml.FullLoader) config_dict = dict(context=dict( data=dict(events1=events_dict), providers=dict(events2="events.yaml") )) text = """ {%% extends "base.html" %%} {%% block main %%} <ul> {%% for year, eventlist in %s %%} <li> <h1>{{ year }}</h1> <ul> {%% for event in eventlist %%} <li>{{ event.title }}-{{ event.location }}</li> {%% endfor %%} </ul> </li> {%% endfor %%} </ul> {%% endblock %%} """ File(TEST_SITE.child('events.yaml')).write(events) f1 = File(TEST_SITE.child('content/text1.html')) f2 = File(TEST_SITE.child('content/text2.html')) f1.write(text % "events1") f2.write(text % "events2") site = Site(TEST_SITE, Config(TEST_SITE, config_dict=config_dict)) site.load() gen = Generator(site) gen.generate_all() left = File(site.config.deploy_root_path.child(f1.name)).read_all() right = File(site.config.deploy_root_path.child(f2.name)).read_all() assert left == right
def test_config_ignore(self): s = Site(self.SITE_PATH, config=self.config) s.load() path = 'apple-touch-icon.png' resource = s.content.resource_from_relative_path(path) assert resource assert resource.full_url == "/" + path s = Site(self.SITE_PATH, config=self.config) s.config.ignore.append('*.png') resource = s.content.resource_from_relative_path(path) assert not resource
def test_attribute_checker_with_meta(self): s = Site(TEST_SITE) s.load() MetaPlugin(s).begin_site() from hyde.ext.plugins.meta import attributes_checker have_index = ["angry-post.html", "another-sad-post.html", "happy-post.html"] for r in s.content.walk_resources(): expected = r.name in have_index assert attributes_checker(r, ['meta.index']) == expected
def test_attribute_checker_with_meta(self): s = Site(TEST_SITE) s.load() MetaPlugin(s).begin_site() from hyde.ext.plugins.meta import attributes_checker have_index = [ "angry-post.html", "another-sad-post.html", "happy-post.html" ] for r in s.content.walk_resources(): expected = r.name in have_index assert attributes_checker(r, ['meta.index']) == expected
def test_simple_copy_directory(self): self.setup_config(['**/*.html']) s = Site(self.SITE_PATH, config=self.config) s.load() res = s.content.resource_from_relative_path('about.html') assert res assert not res.simple_copy res = s.content.resource_from_relative_path( 'blog/2010/december/merry-christmas.html') assert res assert res.simple_copy
def test_load_with_config(self): s = Site(self.SITE_PATH, config=self.config) s.load() path = 'blog/2010/december' node = s.content.node_from_relative_path(path) assert node assert Folder(node.relative_path) == Folder(path) path += '/merry-christmas.html' resource = s.content.resource_from_relative_path(path) assert resource assert resource.relative_path == path assert not s.content.resource_from_relative_path('/happy-festivus.html')
def test_walk_resources(): s = Site(TEST_SITE_ROOT) s.load() pages = [page.name for page in s.content.walk_resources()] expected = [ "404.html", "about.html", "apple-touch-icon.png", "merry-christmas.html", "crossdomain.xml", "favicon.ico", "robots.txt", "site.css" ] pages.sort() expected.sort() assert pages == expected
def test_load(): s = Site(TEST_SITE_ROOT) s.load() path = 'blog/2010/december' node = s.content.node_from_relative_path(path) assert node assert Folder(node.relative_path) == Folder(path) path += '/merry-christmas.html' resource = s.content.resource_from_relative_path(path) assert resource assert resource.relative_path == path assert not s.content.resource_from_relative_path('/happy-festivus.html')
def test_generate_resource_from_path_with_uses_template_false(self): site = Site(TEST_SITE) site.load() resource = site.content.resource_from_path(TEST_SITE.child('content/about.html')) resource.uses_template = False gen = Generator(site) gen.generate_resource_at_path(TEST_SITE.child('content/about.html')) about = File(Folder(site.config.deploy_root_path).child('about.html')) assert about.exists text = about.read_all() expected = resource.source_file.read_all() assert text == expected
def test_generate_resource_from_path_with_uses_template_false(self): site = Site(TEST_SITE) site.load() resource = site.content.resource_from_path( TEST_SITE.child('content/about.html')) resource.uses_template = False gen = Generator(site) gen.generate_resource_at_path(TEST_SITE.child('content/about.html')) about = File(Folder(site.config.deploy_root_path).child('about.html')) assert about.exists text = about.read_all() expected = resource.source_file.read_all() assert text == expected
def test_cant_find_depends_with_reference_tag_var(self): site = Site(TEST_SITE) JINJA2.copy_contents_to(site.content.source) inc = File(TEST_SITE.child('content/inc.md')) inc.write("{% set ind = 'index.html' %}{% refer to ind as index %}") site.load() gen = Generator(site) gen.load_template_if_needed() t = gen.template deps = list(t.get_dependencies('inc.md')) assert len(deps) == 1 assert not deps[0]
def test_drafts_are_published_in_development(self): s = Site(TEST_SITE) cfg = """ mode: development plugins: - hyde.ext.plugins.meta.MetaPlugin - hyde.ext.plugins.blog.DraftsPlugin """ import yaml s.config = Config(TEST_SITE, config_dict=yaml.load(cfg)) s.load() gen = Generator(s) gen.generate_all() assert s.config.deploy_root_path.child_file( 'blog/2013/may/draft-post.html').exists
def test_context_providers(self): site = Site( TEST_SITE, Config(TEST_SITE, config_dict={ "context": { "data": { "abc": "def" }, "providers": { "nav": "nav.yaml" } } })) nav = """ main: - home - articles - projects """ text = """ {% extends "base.html" %} {% block main %} {{nav}} {% for item in nav.main %} {{item}} {% endfor %} abc = {{ abc }} Hi! I am a test template to make sure jinja2 generation works well with hyde. {{resource.name}} {% endblock %} """ File(TEST_SITE.child('nav.yaml')).write(nav) site.load() resource = site.content.resource_from_path( TEST_SITE.child('content/about.html')) gen = Generator(site) resource.source_file.write(text) gen.generate_all() target = File(site.config.deploy_root_path.child(resource.name)) out = target.read_all() assert "abc = def" in out assert "home" in out assert "articles" in out assert "projects" in out
def test_depends_with_reference_tag(self): site = Site(TEST_SITE) JINJA2.copy_contents_to(site.content.source) inc = File(TEST_SITE.child('content/inc.md')) inc.write("{% refer to 'index.html' as index%}") site.load() gen = Generator(site) gen.load_template_if_needed() t = gen.template deps = list(t.get_dependencies('inc.md')) assert len(deps) == 3 assert 'helpers.html' in deps assert 'layout.html' in deps assert 'index.html' in deps
def test_walk_resources(): s = Site(TEST_SITE_ROOT) s.load() pages = [page.name for page in s.content.walk_resources()] expected = ["404.html", "about.html", "apple-touch-icon.png", "merry-christmas.html", "crossdomain.xml", "favicon.ico", "robots.txt", "site.css" ] pages.sort() expected.sort() assert pages == expected
def test_simple_copy_multiple(self): self.setup_config([ '**/*.html', 'media/css/*.css' ]) s = Site(self.SITE_PATH, config=self.config) s.load() res = s.content.resource_from_relative_path('about.html') assert res assert not res.simple_copy res = s.content.resource_from_relative_path('blog/2010/december/merry-christmas.html') assert res assert res.simple_copy res = s.content.resource_from_relative_path('media/css/site.css') assert res assert res.simple_copy
def test_drafts_are_skipped_in_production(self): s = Site(TEST_SITE) cfg = """ mode: production plugins: - hyde.ext.plugins.meta.MetaPlugin - hyde.ext.plugins.blog.DraftsPlugin """ import yaml s.config = Config(TEST_SITE, config_dict=yaml.load(cfg, Loader=yaml.FullLoader)) s.load() gen = Generator(s) gen.generate_all() assert not s.config.deploy_root_path.child_file( 'blog/2013/may/draft-post.html').exists
def assert_markdown_typogrify_processed_well(include_text, includer_text): site = Site(TEST_SITE) site.config.plugins = ['hyde.ext.plugins.meta.MetaPlugin'] inc = File(TEST_SITE.child('content/inc.md')) inc.write(include_text) site.load() gen = Generator(site) gen.load_template_if_needed() template = gen.template html = template.render(includer_text, {}).strip() assert html q = PyQuery(html) assert "is_processable" not in html assert "This is a" in q("h1").text() assert "heading" in q("h1").text() assert q(".amp").length == 1 return html
def test_config_ignore_nodes(self): c = Config(self.SITE_PATH, config_dict=self.config.to_dict()) git = self.SITE_PATH.child_folder('.git') git.make() s = Site(self.SITE_PATH, config=c) s.load() git_node = s.content.node_from_relative_path('.git') assert not git_node blog_node = s.content.node_from_relative_path('blog') assert blog_node assert blog_node.full_url == "/blog" s = Site(self.SITE_PATH, config=c) s.config.ignore.append('blog') blog_node = s.content.node_from_relative_path('blog') assert not blog_node git_node = s.content.node_from_relative_path('.git') assert not git_node
def test_has_resource_changed(self): site = Site(TEST_SITE) site.load() resource = site.content.resource_from_path(TEST_SITE.child('content/about.html')) gen = Generator(site) gen.generate_all() assert not gen.has_resource_changed(resource) import time time.sleep(1) text = resource.source_file.read_all() resource.source_file.write(text) assert gen.has_resource_changed(resource) gen.generate_all() assert not gen.has_resource_changed(resource) time.sleep(1) l = File(TEST_SITE.child('layout/root.html')) l.write(l.read_all()) assert gen.has_resource_changed(resource)
def test_walk_resources_sorted_by_index(self): s = Site(TEST_SITE) s.load() config = {"index": {"attr": ['meta.index', 'name']}} s.config.sorter = Expando(config) MetaPlugin(s).begin_site() SorterPlugin(s).begin_site() assert hasattr(s.content, 'walk_resources_sorted_by_index') expected = [ "angry-post.html", "another-sad-post.html", "happy-post.html" ] pages = [ page.name for page in s.content.walk_resources_sorted_by_index() ] assert pages == sorted(expected, key=lambda f: (File(f).kind, f))
def test_context_providers(self): site = Site(TEST_SITE, Config(TEST_SITE, config_dict={ "context": { "data": { "abc": "def" }, "providers": { "nav": "nav.yaml" } } })) nav = """ main: - home - articles - projects """ text = """ {% extends "base.html" %} {% block main %} {{nav}} {% for item in nav.main %} {{item}} {% endfor %} abc = {{ abc }} Hi! I am a test template to make sure jinja2 generation works well with hyde. {{resource.name}} {% endblock %} """ File(TEST_SITE.child('nav.yaml')).write(nav) site.load() resource = site.content.resource_from_path(TEST_SITE.child('content/about.html')) gen = Generator(site) resource.source_file.write(text) gen.generate_all() target = File(site.config.deploy_root_path.child(resource.name)) out = target.read_all() assert "abc = def" in out assert "home" in out assert "articles" in out assert "projects" in out
def test_can_set_standard_attributes(self): text = """ --- is_processable: False --- {% extends "base.html" %} """ about2 = File(TEST_SITE.child("content/about2.html")) about2.write(text) s = Site(TEST_SITE) s.load() res = s.content.resource_from_path(about2.path) assert res.is_processable s.config.plugins = ["hyde.ext.plugins.meta.MetaPlugin"] gen = Generator(s) gen.generate_all() assert not res.meta.is_processable assert not res.is_processable