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:nth-child(1)").text().strip() == "Heading 1" assert q("h1:nth-child(2)").text().strip() == "Heading 2"
def test_textlinks(self): d = { 'objects': 'template/variables', 'plugins': 'plugins/metadata', 'sorter': 'plugins/sorter' } text = u""" {%% markdown %%} [[!!img/hyde-logo.png]] * [Rich object model][hyde objects] and [overridable hierarchical metadata]([[ %(plugins)s ]]) thats available for use in templates. * Configurable [sorting][], filtering and grouping support. [hyde objects]: [[ %(objects)s ]] [sorting]: [[%(sorter)s]] {%% endmarkdown %%} """ site = Site(TEST_SITE) site.config.plugins = ['hyde.ext.plugins.text.TextlinksPlugin'] site.config.base_url = 'http://example.com/' site.config.media_url = '/media' tlink = File(site.content.source_folder.child('tlink.html')) tlink.write(text % d) print tlink.read_all() gen = Generator(site) gen.generate_all() f = File(site.config.deploy_root_path.child(tlink.name)) assert f.exists html = f.read_all() assert html for name, path in d.items(): assert site.config.base_url + quote(path) in html assert '/media/img/hyde-logo.png' in html
def test_plugins(self): text = """ --- title: Hey author: Me twitter: @me --- {%% extends "base.html" %%} {%% block main %%} Hi! I am a test template to make sure jinja2 generation works well with hyde. <span class="title">{{resource.meta.title}}</span> <span class="author">{{resource.meta.author}}</span> <span class="twitter">{{resource.meta.twitter}}</span> {%% endblock %%} """ index = File(self.SITE_PATH.child('content/blog/index.html')) index.write(text) self.setup_config([ '**/*.html', 'media/css/*.css' ]) conf = {'plugins': ['hyde.ext.plugins.meta.MetaPlugin']} conf.update(self.config.to_dict()) s = Site(self.SITE_PATH, Config(sitepath=self.SITE_PATH, config_dict=conf)) g = Generator(s) g.generate_all() source = s.content.resource_from_relative_path('blog/index.html') target = File(s.config.deploy_root_path.child(source.relative_deploy_path)) left = source.source_file.read_all() right = target.read_all() assert left == right
def test_url_cleaner(self): s = Site(TEST_SITE) cfg = """ plugins: - hyde.ext.plugins.urls.UrlCleanerPlugin urlcleaner: index_file_names: - about.html strip_extensions: - html append_slash: true """ s.config = Config(TEST_SITE, config_dict=yaml.load(cfg, Loader=yaml.FullLoader)) text = """ {% extends "base.html" %} {% block main %} <a id="index" href="{{ content_url('about.html') }}"></a> <a id="blog" href= "{{ content_url('blog/2010/december/merry-christmas.html') }}"></a> {% endblock %} """ about2 = File(TEST_SITE.child('content/test.html')) about2.write(text) gen = Generator(s) gen.generate_all() from pyquery import PyQuery target = File(Folder(s.config.deploy_root_path).child('test.html')) text = target.read_all() q = PyQuery(text) assert q('a#index').attr("href") == '/' assert q('a#blog').attr( "href") == '/blog/2010/december/merry-christmas'
def test_textlinks(self): d = { 'objects': 'template/variables', 'plugins': 'plugins/metadata', 'sorter': 'plugins/sorter' } text = """ {%% markdown %%} [[!!img/hyde-logo.png]] * [Rich object model][hyde objects] and [overridable hierarchical metadata]([[ %(plugins)s ]]) thats available for use in templates. * Configurable [sorting][], filtering and grouping support. [hyde objects]: [[ %(objects)s ]] [sorting]: [[%(sorter)s]] {%% endmarkdown %%} """ site = Site(TEST_SITE) site.config.plugins = ['hyde.ext.plugins.text.TextlinksPlugin'] site.config.base_url = 'http://example.com/' site.config.media_url = '/media' tlink = File(site.content.source_folder.child('tlink.html')) tlink.write(text % d) print((tlink.read_all())) gen = Generator(site) gen.generate_all() f = File(site.config.deploy_root_path.child(tlink.name)) assert f.exists html = f.read_all() assert html for name, path in list(d.items()): assert site.config.base_url + quote(path) in html assert '/media/img/hyde-logo.png' in html
def test_url_cleaner(self): s = Site(TEST_SITE) cfg = """ plugins: - hyde.ext.plugins.urls.UrlCleanerPlugin urlcleaner: index_file_names: - about.html strip_extensions: - html append_slash: true """ s.config = Config(TEST_SITE, config_dict=yaml.load(cfg)) text = """ {% extends "base.html" %} {% block main %} <a id="index" href="{{ content_url('about.html') }}"></a> <a id="blog" href="{{ content_url('blog/2010/december/merry-christmas.html') }}"></a> {% endblock %} """ about2 = File(TEST_SITE.child('content/test.html')) about2.write(text) gen = Generator(s) gen.generate_all() from pyquery import PyQuery target = File(Folder(s.config.deploy_root_path).child('test.html')) text = target.read_all() q = PyQuery(text) assert q('a#index').attr("href") == '/' assert q('a#blog').attr("href") == '/blog/2010/december/merry-christmas'
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 === """ inc = File(TEST_SITE.child('content/inc.md')) inc.write(text) gen = Generator(s) gen.load_site_if_needed() gen.load_template_if_needed() def dateformat(x): return x.strftime('%Y-%m-%d') gen.template.env.filters['dateformat'] = dateformat gen.generate_resource_at_path(inc.name) res = s.content.resource_from_relative_path(inc.name) assert len(res.depends) == 1 assert 'index.html' in res.depends 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
def test_plugins(self): text = """ --- title: Hey author: Me twitter: @me --- {%% extends "base.html" %%} {%% block main %%} Hi! I am a test template to make sure jinja2 generation works well with hyde. <span class="title">{{resource.meta.title}}</span> <span class="author">{{resource.meta.author}}</span> <span class="twitter">{{resource.meta.twitter}}</span> {%% endblock %%} """ index = File(self.SITE_PATH.child('content/blog/index.html')) index.write(text) self.setup_config(['**/*.html', 'media/css/*.css']) conf = {'plugins': ['hyde.ext.plugins.meta.MetaPlugin']} conf.update(self.config.to_dict()) s = Site(self.SITE_PATH, Config(sitepath=self.SITE_PATH, config_dict=conf)) g = Generator(s) g.generate_all() source = s.content.resource_from_relative_path('blog/index.html') target = File( s.config.deploy_root_path.child(source.relative_deploy_path)) left = source.source_file.read_all() right = target.read_all() assert left == right
def poll(): running = File('/var/run/build') if running.exists: return queue_url = '{ "Ref" : "InputQueue" }' region = '{ "Ref" : "AWS::Region" }' aws_access_key = '{"Ref": "WorkerKeys"}' aws_secret_key = '{"Fn::GetAtt": ["WorkerKeys", "SecretAccessKey"]}' conn = sqsconnect(region, aws_access_key_id=aws_access_key, aws_secret_access_key=aws_secret_key) q = Queue(conn, queue_url) msg = q.read(600) if msg: body = msg.get_body() data = json.loads(body) status_url = data.get('status_url', None) running.write('.') try: run(data) q.delete_message(msg) except HandledException: raise except Exception, e: if status_url: post_status(status_url, dict( state='error', message=e.message )) raise else: print 'All done.' finally:
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 === """ inc = File(TEST_SITE.child('content/inc.md')) inc.write(text) gen = Generator(s) gen.load_site_if_needed() gen.load_template_if_needed() def dateformat(x): return x.strftime('%Y-%m-%d') gen.template.env.filters['dateformat'] = dateformat gen.generate_resource_at_path(inc.name) res = s.content.resource_from_relative_path(inc.name) assert len(res.depends) == 1 assert 'index.html' in res.depends 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
def _generate_site_with_meta(self, meta): self.site.config.mode = "production" self.site.config.plugins = ['hyde.ext.plugins.meta.MetaPlugin', 'hyde.ext.plugins.images.ImageThumbnailsPlugin'] mlink = File(self.image_folder.child('meta.yaml')) meta_text = yaml.dump(meta, default_flow_style=False) mlink.write(meta_text) gen = Generator(self.site) gen.generate_all()
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 assert_extended(self, s, txt, templ): content = (templ.strip() % txt).strip() bd = File(TEST_SITE.child('content/auto_extend.html')) bd.write(content) gen = Generator(s) gen.generate_resource_at_path(bd.path) res = s.content.resource_from_path(bd.path) target = File(s.config.deploy_root_path.child(res.relative_deploy_path)) assert target.exists text = target.read_all() q = PyQuery(text) assert q('title').text().strip() == txt.strip()
def _generic_test_image(self, text): self.site.config.mode = "production" self.site.config.plugins = ['hyde.ext.plugins.images.ImageSizerPlugin'] tlink = File(self.site.content.source_folder.child('timg.html')) tlink.write(text) gen = Generator(self.site) gen.generate_all() f = File(self.site.config.deploy_root_path.child(tlink.name)) assert f.exists html = f.read_all() assert html return html
def test_signed_url(): bucket = new_bucket() bucket.make() content = '<h1>A new html file on S3</h1>' data_folder.make() f = File(data_folder.child('index.html')) f.write(content) home = Folder('/home') full_path = bucket.add_file(f, target_folder=home.name) url = bucket.get_signed_url(full_path) response = urllib2.urlopen(url) html = response.read() assert html == content
def test_etag(): bucket = new_bucket() bucket.make() bucket.set_policy() bucket.serve() content = '<h1>A new html file on S3</h1>' data_folder.make() f = File(data_folder.child('index.html')) f.write(content) home = Folder('/home') bucket.add_file(f, target_folder=home.name) key = bucket.bucket.get_key('home/index.html') assert bucket.check_etag(key, f)
def assert_extended(self, s, txt, templ): content = (templ.strip() % txt).strip() bd = File(TEST_SITE.child('content/auto_extend.html')) bd.write(content) gen = Generator(s) gen.generate_resource_at_path(bd.path) res = s.content.resource_from_path(bd.path) target = File(s.config.deploy_root_path.child( res.relative_deploy_path)) assert target.exists text = target.read_all() q = PyQuery(text) assert q('title').text().strip() == txt.strip()
def test_expires(): bucket = new_bucket() bucket.make() bucket.set_policy() bucket.serve() content = '<h1>A new html file on S3</h1>' data_folder.make() f = File(data_folder.child('index.html')) f.write(content) home = Folder('/home') headers = {"Expires":get_expires()} bucket.add_file(f, target_folder=home.name, headers=headers) assert_headers_present(bucket.get_url() + '/home/index.html', headers)
def test_can_remove_file(): f = FS(__file__).parent c = f.child_folder("__test__") c.make() assert c.exists txt = "abc" abc = File(c.child("abc.txt")) abc.write(txt) assert abc.exists abc.delete() assert not abc.exists abc.delete() assert True # No Exception c.delete()
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_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 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 _create_tag_archive(self, config): """ Generates archives for each tag based on the given configuration. """ if not 'template' in config: raise HydeException( "No Template specified in tagger configuration.") content = self.site.content.source_folder source = Folder(config.get('source', '')) target = content.child_folder(config.get('target', 'tags')) if not target.exists: target.make() # Write meta data for the configuration meta = config.get('meta', {}) meta_text = u'' if meta: import yaml meta_text = yaml.dump(meta, default_flow_style=False) extension = config.get('extension', 'html') template = config['template'] archive_text = u""" --- extends: false %(meta)s --- {%% set tag = site.tagger.tags['%(tag)s'] %%} {%% set source = site.content.node_from_relative_path('%(node)s') %%} {%% set walker = source['walk_resources_tagged_with_%(tag)s'] %%} {%% extends "%(template)s" %%} """ for tagname, tag in self.site.tagger.tags.to_dict().iteritems(): tag_data = { "tag": tagname, "node": source.name, "template": template, "meta": meta_text } text = archive_text % tag_data archive_file = File(target.child("%s.%s" % (tagname, extension))) archive_file.delete() archive_file.write(text.strip()) self.site.content.add_resource(archive_file)
def _create_tag_archive(self, config): """ Generates archives for each tag based on the given configuration. """ if not 'template' in config: raise self.template.exception_class( "No Template specified in tagger configuration.") content = self.site.content.source_folder source = Folder(config.get('source', '')) target = content.child_folder(config.get('target', 'tags')) if not target.exists: target.make() # Write meta data for the configuration meta = config.get('meta', {}) meta_text = u'' if meta: import yaml meta_text = yaml.dump(meta, default_flow_style=False) extension = config.get('extension', 'html') template = config['template'] archive_text = u""" --- extends: false %(meta)s --- {%% set tag = site.tagger.tags['%(tag)s'] %%} {%% set source = site.content.node_from_relative_path('%(node)s') %%} {%% set walker = source['walk_resources_tagged_with_%(tag)s'] %%} {%% extends "%(template)s" %%} """ for tagname, tag in self.site.tagger.tags.to_dict().iteritems(): tag_data = { "tag": tagname, "node": source.name, "template": template, "meta": meta_text } text = archive_text % tag_data archive_file = File(target.child("%s.%s" % (tagname, extension))) archive_file.delete() archive_file.write(text.strip()) self.site.content.add_resource(archive_file)
def test_can_load_from_site_meta(self): d = {'title': 'A nice title', 'author': 'Lakshmi Vyas'} text = """ --- title: Even nicer title --- {%% extends "base.html" %%} {%% block main %%} Hi! I am a test template to make sure jinja2 generation works well with hyde. <span class="title">{{resource.meta.title}}</span> <span class="author">{{resource.meta.author}}</span> <span class="twitter">{{resource.meta.twitter}}</span> {%% endblock %%} """ about2 = File(TEST_SITE.child('content/about2.html')) about2.write(text % d) meta = File(TEST_SITE.child('content/nodemeta.yaml')) meta.write(yaml.dump(d)) s = Site(TEST_SITE) s.config.plugins = ['hyde.ext.plugins.meta.MetaPlugin'] s.config.meta = { 'author': 'Lakshmi', 'twitter': 'lakshmivyas' } gen = Generator(s) gen.generate_all() res = s.content.resource_from_path(about2.path) assert hasattr(res, 'meta') assert hasattr(res.meta, 'title') assert hasattr(res.meta, 'author') assert hasattr(res.meta, 'twitter') assert res.meta.title == "Even nicer title" assert res.meta.author == "Lakshmi Vyas" assert res.meta.twitter == "lakshmivyas" target = File(Folder(s.config.deploy_root_path).child('about2.html')) text = target.read_all() q = PyQuery(text) for k, v in d.items(): if not k == 'title': assert v in q("span." + k).text() assert q("span.title").text() == "Even nicer title"
def test_two_level_refer_with_var(self): text = """ === is_processable: False === <div class="fulltext"> {% filter markdown|typogrify %} {% mark heading %} This is a heading ================= {% endmark %} {% reference content %} Hyde & Jinja. {% endreference %} {% endfilter %} </div> """ text2 = """ {% set super = 'super.md' %} {% refer to super as sup %} <div class="justhead"> {% mark child %} {{ sup.heading }} {% endmark %} {% mark cont %} {{ sup.content }} {% endmark %} </div> """ text3 = """ {% set incu = 'inc.md' %} {% refer to incu as inc %} {% filter markdown|typogrify %} {{ inc.child }} {{ inc.cont }} {% endfilter %} """ superinc = File(TEST_SITE.child('content/super.md')) superinc.write(text) html = assert_markdown_typogrify_processed_well(text2, text3) assert "mark" not in html assert "reference" not in html
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() import time time.sleep(1) assert not gen.has_resource_changed(resource) 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_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
class Dependents(IterableUserDict): """ Represents the dependency graph for hyde. """ def __init__(self, sitepath, depends_file_name='.hyde_deps'): self.sitepath = Folder(sitepath) self.deps_file = File(self.sitepath.child(depends_file_name)) self.data = {} if self.deps_file.exists: self.data = yaml.load(self.deps_file.read_all()) import atexit atexit.register(self.save) def save(self): """ Saves the dependency graph (just a dict for now). """ if self.deps_file.parent.exists: self.deps_file.write(yaml.dump(self.data))
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() import time time.sleep(1) assert not gen.has_resource_changed(resource) 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 __generate_resource__(self, resource, incremental=False): self.refresh_config() if not resource.is_processable: logger.debug("Skipping [%s]", resource) return if incremental and not self.has_resource_changed(resource): logger.debug("No changes found. Skipping resource [%s]", resource) return logger.debug("Processing [%s]", resource) with self.context_for_resource(resource) as context: target = File(self.site.config.deploy_root_path.child( resource.relative_deploy_path)) target.parent.make() if resource.simple_copy: logger.debug("Simply Copying [%s]", resource) resource.source_file.copy_to(target) elif resource.source_file.is_text: self.update_deps(resource) if resource.uses_template: logger.debug("Rendering [%s]", resource) try: text = self.template.render_resource(resource, context) except Exception as e: HydeException.reraise("Error occurred when processing" "template: [%s]: %s" % (resource, repr(e)), sys.exc_info()) else: text = resource.source_file.read_all() text = self.events.begin_text_resource( resource, text) or text text = self.events.text_resource_complete( resource, text) or text target.write(text) copymode(resource.source_file.path, target.path) else: logger.debug("Copying binary file [%s]", resource) self.events.begin_binary_resource(resource) resource.source_file.copy_to(target) self.events.binary_resource_complete(resource)
def __generate_resource__(self, resource, incremental=False): self.refresh_config() if not resource.is_processable: logger.debug("Skipping [%s]", resource) return if incremental and not self.has_resource_changed(resource): logger.debug("No changes found. Skipping resource [%s]", resource) return logger.debug("Processing [%s]", resource) with self.context_for_resource(resource) as context: target = File( self.site.config.deploy_root_path.child( resource.relative_deploy_path)) target.parent.make() if resource.simple_copy: logger.debug("Simply Copying [%s]", resource) resource.source_file.copy_to(target) elif resource.source_file.is_text: self.update_deps(resource) if resource.uses_template: logger.debug("Rendering [%s]", resource) try: text = self.template.render_resource(resource, context) except Exception as e: HydeException.reraise( "Error occurred when processing" "template: [%s]: %s" % (resource, repr(e)), sys.exc_info()) else: text = resource.source_file.read_all() text = self.events.begin_text_resource(resource, text) or text text = self.events.text_resource_complete(resource, text) or text target.write(text) copymode(resource.source_file.path, target.path) else: logger.debug("Copying binary file [%s]", resource) self.events.begin_binary_resource(resource) resource.source_file.copy_to(target) self.events.binary_resource_complete(resource)
def test_reference(self): text = u""" === is_processable: False === {% filter markdown|typogrify %} §§ heading This is a heading ================= §§ /heading §§ content Hyde & Jinja §§ / {% endfilter %} """ text2 = u""" ※ inc.md as inc {% filter markdown|typogrify %} {{ inc.heading }} {{ inc.content }} {% endfilter %} """ site = Site(TEST_SITE) site.config.plugins = [ 'hyde.ext.plugins.meta.MetaPlugin', 'hyde.ext.plugins.text.MarkingsPlugin', 'hyde.ext.plugins.text.ReferencePlugin' ] inc = File(site.content.source_folder.child('inc.md')) inc.write(text.strip()) src = File(site.content.source_folder.child('src.html')) src.write(text2.strip()) gen = Generator(site) gen.generate_all() f = File(site.config.deploy_root_path.child(src.name)) assert f.exists html = f.read_all() assert_valid_conversion(html)
def test_can_parse_blockdown(self): s = Site(TEST_SITE) s.config.plugins = ['hyde.ext.plugins.blockdown.BlockdownPlugin'] txt ="This template tests to make sure blocks can be replaced with markdownish syntax." templ = """ {%% extends "base.html" %%} =====title======== %s ====/title========""" content = (templ.strip() % txt).strip() bd = File(TEST_SITE.child('content/blockdown.html')) bd.write(content) gen = Generator(s) gen.generate_resource_at_path(bd.path) res = s.content.resource_from_path(bd.path) target = File(s.config.deploy_root_path.child(res.relative_deploy_path)) assert target.exists text = target.read_all() q = PyQuery(text) assert q('title').text().strip() == txt.strip()
def test_can_parse_blockdown(self): s = Site(TEST_SITE) s.config.plugins = ['hyde.ext.plugins.text.BlockdownPlugin'] txt ="This template tests to make sure blocks can be replaced with markdownish syntax." templ = """ {%% extends "base.html" %%} =====title======== %s ====/title========""" content = (templ.strip() % txt).strip() bd = File(TEST_SITE.child('content/blockdown.html')) bd.write(content) gen = Generator(s) gen.generate_resource_at_path(bd.path) res = s.content.resource_from_path(bd.path) target = File(s.config.deploy_root_path.child(res.relative_deploy_path)) assert target.exists text = target.read_all() q = PyQuery(text) assert q('title').text().strip() == txt.strip()
def test_walk_resources_sorted_using_generator(self): s = Site(TEST_SITE) cfg = """ meta: time: !!timestamp 2010-10-23 title: NahNahNah plugins: - hyde.ext.plugins.meta.MetaPlugin - hyde.ext.plugins.meta.SorterPlugin sorter: time: attr: meta.time filters: source_file.kind: html """ s.config = Config(TEST_SITE, config_dict=yaml.load(cfg)) text = """ --- time: !!timestamp 2010-12-31 title: YayYayYay --- {% extends "base.html" %} {% block main %} {% set latest = site.content.walk_resources_sorted_by_time()|reverse|first %} <span class="latest">{{ latest.meta.title }}</span> {% endblock %} """ about2 = File(TEST_SITE.child('content/about2.html')) about2.write(text) gen = Generator(s) gen.generate_all() from pyquery import PyQuery target = File(Folder(s.config.deploy_root_path).child('about2.html')) text = target.read_all() q = PyQuery(text) assert q('span.latest').text() == 'YayYayYay'
def test_reference(self): text = u""" === is_processable: False === {% filter markdown|typogrify %} §§ heading This is a heading ================= §§ /heading §§ content Hyde & Jinja §§ / {% endfilter %} """ text2 = u""" ※ inc.md as inc {% filter markdown|typogrify %} {{ inc.heading }} {{ inc.content }} {% endfilter %} """ site = Site(TEST_SITE) site.config.plugins = [ 'hyde.ext.plugins.meta.MetaPlugin', 'hyde.ext.plugins.text.MarkingsPlugin', 'hyde.ext.plugins.text.ReferencePlugin'] inc = File(site.content.source_folder.child('inc.md')) inc.write(text.strip()) src = File(site.content.source_folder.child('src.html')) src.write(text2.strip()) gen = Generator(site) gen.generate_all() f = File(site.config.deploy_root_path.child(src.name)) assert f.exists html = f.read_all() assert_valid_conversion(html)
def test_syntext(self): text = u""" ~~~~~~~~css~~~~~~~ .body{ background-color: white; } ~~~~~~~~~~~~~~~~~~ """ site = Site(TEST_SITE) site.config.plugins = [ 'hyde.ext.plugins.meta.MetaPlugin', 'hyde.ext.plugins.text.SyntextPlugin'] syn = File(site.content.source_folder.child('syn.html')) syn.write(text) gen = Generator(site) gen.generate_all() f = File(site.config.deploy_root_path.child(syn.name)) assert f.exists html = f.read_all() assert html q = PyQuery(html) assert q('figure.code').length == 1
def test_mark(self): text = u""" === is_processable: False === {% filter markdown|typogrify %} §§ heading This is a heading ================= §§ /heading §§ content Hyde & Jinja §§ / {% endfilter %} """ text2 = """ {% refer to "inc.md" as inc %} {% filter markdown|typogrify %} {{ inc.heading }} {{ inc.content }} {% endfilter %} """ site = Site(TEST_SITE) site.config.plugins = [ 'hyde.ext.plugins.meta.MetaPlugin', 'hyde.ext.plugins.text.MarkingsPlugin' ] inc = File(TEST_SITE.child('content/inc.md')) inc.write(text) site.load() gen = Generator(site) gen.load_template_if_needed() template = gen.template html = template.render(text2, {}).strip() assert_valid_conversion(html)
def test_multiple_levels(self): page_d = {'title': 'An even nicer title'} blog_d = {'author': 'Laks'} content_d = {'title': 'A nice title', 'author': 'Lakshmi Vyas'} site_d = { 'author': 'Lakshmi', 'twitter': 'lakshmivyas', 'nodemeta': 'meta.yaml' } text = """ --- title: %(title)s --- {%% extends "base.html" %%} {%% block main %%} Hi! I am a test template to make sure jinja2 generation works well with hyde. <span class="title">{{resource.meta.title}}</span> <span class="author">{{resource.meta.author}}</span> <span class="twitter">{{resource.meta.twitter}}</span> {%% endblock %%} """ about2 = File(TEST_SITE.child('content/blog/about2.html')) about2.write(text % page_d) content_meta = File(TEST_SITE.child('content/nodemeta.yaml')) content_meta.write(yaml.dump(content_d)) content_meta = File(TEST_SITE.child('content/blog/meta.yaml')) content_meta.write(yaml.dump(blog_d)) s = Site(TEST_SITE) s.config.plugins = ['hyde.ext.plugins.meta.MetaPlugin'] s.config.meta = site_d gen = Generator(s) gen.generate_all() expected = {} expected.update(site_d) expected.update(content_d) expected.update(blog_d) expected.update(page_d) res = s.content.resource_from_path(about2.path) assert hasattr(res, 'meta') for k, v in list(expected.items()): assert hasattr(res.meta, k) assert getattr(res.meta, k) == v target = File( Folder(s.config.deploy_root_path).child('blog/about2.html')) text = target.read_all() q = PyQuery(text) for k, v in list(expected.items()): if k != 'nodemeta': assert v in q("span." + k).text()
def test_mark(self): text = u""" === is_processable: False === {% filter markdown|typogrify %} §§ heading This is a heading ================= §§ /heading §§ content Hyde & Jinja §§ / {% endfilter %} """ text2 = """ {% refer to "inc.md" as inc %} {% filter markdown|typogrify %} {{ inc.heading }} {{ inc.content }} {% endfilter %} """ site = Site(TEST_SITE) site.config.plugins = [ 'hyde.ext.plugins.meta.MetaPlugin', 'hyde.ext.plugins.text.MarkingsPlugin'] inc = File(TEST_SITE.child('content/inc.md')) inc.write(text) site.load() gen = Generator(site) gen.load_template_if_needed() template = gen.template html = template.render(text2, {}).strip() assert_valid_conversion(html)
def test_syntext(self): text = u""" ~~~~~~~~css~~~~~~~ .body{ background-color: white; } ~~~~~~~~~~~~~~~~~~ """ site = Site(TEST_SITE) site.config.plugins = [ 'hyde.ext.plugins.meta.MetaPlugin', 'hyde.ext.plugins.text.SyntextPlugin' ] syn = File(site.content.source_folder.child('syn.html')) syn.write(text) gen = Generator(site) gen.generate_all() f = File(site.config.deploy_root_path.child(syn.name)) assert f.exists html = f.read_all() assert html q = PyQuery(html) assert q('figure.code').length == 1
def test_multiple_levels(self): page_d = {'title': 'An even nicer title'} blog_d = {'author': 'Laks'} content_d = {'title': 'A nice title', 'author': 'Lakshmi Vyas'} site_d = {'author': 'Lakshmi', 'twitter': 'lakshmivyas', 'nodemeta': 'meta.yaml'} text = """ --- title: %(title)s --- {%% extends "base.html" %%} {%% block main %%} Hi! I am a test template to make sure jinja2 generation works well with hyde. <span class="title">{{resource.meta.title}}</span> <span class="author">{{resource.meta.author}}</span> <span class="twitter">{{resource.meta.twitter}}</span> {%% endblock %%} """ about2 = File(TEST_SITE.child('content/blog/about2.html')) about2.write(text % page_d) content_meta = File(TEST_SITE.child('content/nodemeta.yaml')) content_meta.write(yaml.dump(content_d)) content_meta = File(TEST_SITE.child('content/blog/meta.yaml')) content_meta.write(yaml.dump(blog_d)) s = Site(TEST_SITE) s.config.plugins = ['hyde.ext.plugins.meta.MetaPlugin'] s.config.meta = site_d gen = Generator(s) gen.generate_all() expected = {} expected.update(site_d) expected.update(content_d) expected.update(blog_d) expected.update(page_d) res = s.content.resource_from_path(about2.path) assert hasattr(res, 'meta') for k, v in list(expected.items()): assert hasattr(res.meta, k) assert getattr(res.meta, k) == v target = File( Folder(s.config.deploy_root_path).child('blog/about2.html')) text = target.read_all() q = PyQuery(text) for k, v in list(expected.items()): if k != 'nodemeta': assert v in q("span." + k).text()
def test_tagger_sorted(self): conf = { "tagger":{ "sorter": "time", "archives": { "blog": { "template": "emotions.j2", "source": "blog", "target": "blog/tags", "extension": "html", "meta": { "author": "Tagger Plugin" } } }, "tags": { "sad" : { "emotions": ["Dissappointed", "Lost"] }, "angry": { "emotions": ["Irritated", "Annoyed", "Disgusted"] } } } } text = """ <div id="author">{{ resource.meta.author }}</div> <h1>Posts tagged: {{ tag }} in {{ node.name|title }}</h1> Emotions: <ul> {% for emotion in tag.emotions %} <li class="emotion"> {{ emotion }} </li> {% endfor %} <ul> {% for resource in walker() -%} <li> <a href="{{ content_url(resource.url) }}">{{ resource.meta.title }}</a> </li> {%- endfor %} </ul> """ template = File(TEST_SITE.child('layout/emotions.j2')) template.write(text) s = Site(TEST_SITE) s.config.update(conf) gen = Generator(s) gen.load_site_if_needed() gen.generate_all() tags_folder = self.deploy.child_folder('blog/tags') assert tags_folder.exists tags = ['sad', 'happy', 'angry', 'thoughts', 'events'] archives = dict((tag, File(tags_folder.child("%s.html" % tag))) for tag in tags) for tag, archive in archives.items(): assert archive.exists from pyquery import PyQuery q = PyQuery(archives['sad'].read_all()) assert len(q("li.emotion")) == 2 assert q("#author").text() == "Tagger Plugin" q = PyQuery(archives['angry'].read_all()) assert len(q("li.emotion")) == 3 for tag, archive in archives.items(): if tag not in ["sad", "angry"]: q = PyQuery(archives[tag].read_all()) assert not len(q("li.emotion"))