Пример #1
0
 def make_site(self, sitepath, config, deploy=None):
     """
     Creates a site object from the given sitepath and the config file.
     """
     config = Config(sitepath, config_file=config)
     if deploy:
         config.deploy_root = deploy
     return Site(sitepath, config)
Пример #2
0
 def make_site(self, sitepath, config, deploy):
     """
     Creates a site object from the given sitepath and the config file.
     """
     sitepath = Folder(Folder(sitepath).fully_expanded_path)
     config = Config(sitepath, config_file=config)
     if deploy:
         config.deploy_root = deploy
     return Site(sitepath, config)
Пример #3
0
    def make_site(self, sitepath, config, deploy=None, overrides=None):
        """
        Creates a site object from the given sitepath and the config file.
        """
        sitepath = Folder(Folder(sitepath).fully_expanded_path)
        config = Config(sitepath, config_file=config)
        if deploy:
            config.deploy_root = deploy
            
        if overrides:
            for override in overrides:
                key, value = (x.strip() for x in override.split("=", 1))
                logger.info("overriding %s to: %s" % (key, value))

                try:
                    target = config

                    path = key.split('.')

                    if len(path) == 1:
                        target.set_expando(key, value)
                    else:
                        for name in path[:-1]:
                            idx = None
                            if "#" in name:
                                name, idx = name.split("#", 1)
                            
                            target = getattr(target, name)

                            if idx:
                                target = target[int(idx)]
                        
                        target.set_expando(path[-1], value)
                except (AttributeError, TypeError, IndexError):
                    logger.error("unable to resolve override of %r" % (key,))
                    
        return Site(sitepath, config)
Пример #4
0
    def test_context_providers_no_data(self):
        site = Site(TEST_SITE, Config(TEST_SITE, config_dict={
            "context": {
                "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 "home" in out
        assert "articles" in out
        assert "projects" in out
Пример #5
0
def test_line_statements_with_config():
    source = """
    %% markdown
    ### Heading 3

    %% endmarkdown
    """
    config = """
    markdown:
        extensions:
            - headerid
    jinja2:
        line_statement_prefix: '%%'

    """
    t = Jinja2Template(JINJA2.path)
    s = Site(JINJA2.path)
    s.config = Config(JINJA2.path, config_dict=yaml.load(config))
    t.configure(s)
    html = t.render(source, {}).strip()
    assert html == u'<h3 id="heading_3">Heading 3</h3>'
Пример #6
0
    def test_can_flatten(self):
        s = Site(TEST_SITE)
        cfg = """
        plugins:
            - hyde.ext.plugins.structure.FlattenerPlugin
        flattener:
            items:
                -
                    source: blog
                    target: ''
        """
        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_folder('blog').exists
        assert File(
            s.config.deploy_root_path.child('merry-christmas.html')).exists
Пример #7
0
    def test_flattener_fixes_nodes(self):
        s = Site(TEST_SITE)
        cfg = """
        plugins:
            - hyde.ext.plugins.structure.FlattenerPlugin
        flattener:
            items:
                -
                    source: blog
                    target: ''
        """
        import yaml
        s.config = Config(TEST_SITE,
                          config_dict=yaml.load(cfg, Loader=yaml.FullLoader))
        s.load()
        gen = Generator(s)
        gen.generate_all()
        blog_node = s.content.node_from_relative_path('blog')

        assert blog_node
        assert blog_node.url == '/'
Пример #8
0
    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.sorter.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'
Пример #9
0
    def test_walk_resources_sorted_no_default_is_processable(self):
        s = Site(TEST_SITE)
        cfg = """
        plugins:
            - hyde.ext.sorter.SorterPlugin
        sorter:
            kind2:
                filters:
                    source_file.kind: html
        """
        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)
Пример #10
0
def test_restructuredtext_with_sourcecode():
    source = """
{% restructuredtext %}
Code
====
.. sourcecode:: python

    def add(a, b):
        return a + b

See `Example`_

.. _Example: example.html

{% endrestructuredtext %}
"""

    expected = ("""
<div class="document" id="code">
<h1 class="title">Code</h1>
<div class="highlight"><pre><span></span><span class="k">def</span> """
                """<span class="nf">add</span><span class="p">(</span>"""
                """<span class="n">a"""
                """</span><span class="p">,</span> <span class="n">b</span>"""
                """<span class="p">):</span>
    <span class="k">return</span> <span class="n">a</span> """
                """<span class="o">+</span> <span class="n">b</span>
</pre></div>
<p>See <a class="reference external" href="example.html">Example</a></p>
</div>
""")
    t = Jinja2Template(JINJA2.path)
    s = Site(JINJA2.path)
    c = Config(JINJA2.path,
               config_dict=dict(restructuredtext=dict(highlight_source=True)))
    s.config = c
    t.configure(s)
    html = t.render(source, {}).strip()
    eq_(html.strip(), expected.strip())
Пример #11
0
    def test_walk_resources_sorted_with_filters(self):
        s = Site(TEST_SITE)
        cfg = """
        plugins:
            - hyde.ext.meta.SorterPlugin
        sorter:
            kind2:
                filters:
                    source_file.kind: html
        """
        s.config = Config(TEST_SITE, config_dict=yaml.load(cfg))
        s.load()
        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)
Пример #12
0
    def test_walk_resources_sorted_with_multiple_attributes(self):
        s = Site(TEST_SITE)
        cfg = """
        plugins:
            - hyde.ext.sorter.SorterPlugin
        sorter:
            multi:
                attr:
                    - source_file.kind
                    - node.name
                    - name

        """
        s.config = Config(TEST_SITE, config_dict=yaml.load(cfg))
        s.load()
        SorterPlugin(s).begin_site()

        assert hasattr(s.content, 'walk_resources_sorted_by_multi')
        expected = ["content/404.html",
                    "content/about.html",
                    "content/apple-touch-icon.png",
                    "content/blog/2010/december/merry-christmas.html",
                    "content/crossdomain.xml",
                    "content/favicon.ico",
                    "content/robots.txt",
                    "content/site.css"
                    ]

        pages = [page.name for page in s.content.walk_resources_sorted_by_multi()]

        expected_sorted = [File(page).name
                                for page in
                                    sorted(expected,
                                        key=lambda p: tuple(
                                            [File(p).kind,
                                            File(p).parent.name, p]))]
        assert pages == expected_sorted
Пример #13
0
    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
Пример #14
0
    def test_prev_next_reversed(self):
          s = Site(TEST_SITE)
          cfg = """
          plugins:
              - hyde.ext.sorter.SorterPlugin
          sorter:
              folder_name:
                  attr:
                    - node.name
                  reverse: True
                  filters:
                      source_file.kind: html
          """
          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_mc, 'prev_by_folder_name')
          assert not p_mc.prev_by_folder_name
          assert hasattr(p_mc, 'next_by_folder_name')
          assert p_mc.next_by_folder_name == p_404

          assert hasattr(p_404, 'prev_by_folder_name')
          assert p_404.prev_by_folder_name == p_mc
          assert hasattr(p_404, 'next_by_folder_name')
          assert p_404.next_by_folder_name == p_about

          assert hasattr(p_about, 'prev_by_folder_name')
          assert p_about.prev_by_folder_name == p_404
          assert hasattr(p_about, 'next_by_folder_name')
          assert not p_about.next_by_folder_name
Пример #15
0
def test_markdown_with_sourcecode():
    source = """
{%markdown%}
# Code

    :::python
    def add(a, b):
        return a + b

See [Example][]
[Example]: example.html

{%endmarkdown%}
"""

    expected = ("""
    <h1>Code</h1>
<div class="codehilite"><pre><span></span><span class="k">def</span> """
                """<span class="nf">add</span><span class="p">(</span>"""
                """<span class="n">a</span><span class="p">,</span> """
                """<span class="n">b</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">a</span> <span class="o">+"""
                """</span> <span class="n">b</span>
</pre></div>


<p>See <a href="example.html">Example</a></p>
    """)
    t = Jinja2Template(JINJA2.path)
    s = Site(JINJA2.path)
    c = Config(JINJA2.path,
               config_dict=dict(markdown=dict(extensions=['codehilite'])))
    s.config = c
    t.configure(s)
    html = t.render(source, {}).strip()
    eq_(html.strip(), expected.strip())
Пример #16
0
    def test_prev_next_looped(self):
        s = Site(TEST_SITE)
        cfg = """
        plugins:
            - hyde.ext.meta.SorterPlugin
        sorter:
            kind2:
                circular: true
                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 p_404.prev_by_kind2 == p_mc
        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 p_mc.next_by_kind2 == p_404
Пример #17
0
 def test_conf2(self):
     c = Config(sitepath=TEST_SITE, config_dict=yaml.load(self.conf2))
     assert c.content_root_path == TEST_SITE.child_folder('site/stuff')
     assert c.media_root_path == c.content_root_path.child_folder('mmm')
     assert c.media_url == TEST_SITE.child_folder('/media')
     assert c.deploy_root_path == Folder('~/deploy_site')
Пример #18
0
 def test_conf1(self):
     c = Config(sitepath=TEST_SITE, config_dict=yaml.load(self.conf1))
     assert c.content_root_path == TEST_SITE.child_folder('stuff')
Пример #19
0
 def setup_config(self, passthru):
     self.config_file = File(self.SITE_PATH.child('site.yaml'))
     with open(self.config_file.path) as config:
         conf = yaml.load(config, Loader=yaml.FullLoader)
         conf['simple_copy'] = passthru
         self.config = Config(sitepath=self.SITE_PATH, config_dict=conf)
Пример #20
0
 def setup_config(self, passthru):
     self.config_file = File(self.SITE_PATH.child('site.yaml'))
     with open(self.config_file.path) as config:
         conf = yaml.load(config)
         conf['simple_copy'] = passthru
         self.config = Config(sitepath=self.SITE_PATH, config_dict=conf)
Пример #21
0
    def test_nav_with_grouper_sorted(self):

        cfg = """
        nodemeta: meta.yaml
        plugins:
          - hyde.ext.plugins.meta.MetaPlugin
          - hyde.ext.plugins.sorter.SorterPlugin
          - hyde.ext.plugins.grouper.GrouperPlugin
        sorter:
          kind:
              attr:
                  - source_file.kind
              filters:
                  is_processable: True
        grouper:
          section:
              description: Sections in the site
              sorter: kind
              groups:
                  -
                      name: start
                      description: Getting Started
                  -
                      name: awesome
                      description: Awesome
                  -
                      name: plugins
                      description: Plugins

        """
        self.s.config = Config(TEST_SITE, config_dict=yaml.load(cfg))
        self.s.load()
        MetaPlugin(self.s).begin_site()
        SorterPlugin(self.s).begin_site()
        GrouperPlugin(self.s).begin_site()

        text ="""
{% set sorted = site.grouper['section'].groups|sort(attribute='name') %}
{% for group in sorted %}
<ul>
    <li>
        <h2>{{ group.name|title }}</h2>
        <h3>{{ group.description }}</h3>
        <ul class="links">
            {% for resource in group.walk_resources_in_node(site.content) %}
            <li>{{resource.name}}</li>
            {% endfor %}
        </ul>
    </li>
</ul>
{% endfor %}

"""
        expected = """
<ul>
    <li>
        <h2>Awesome</h2>
        <h3>Awesome</h3>
        <ul class="links">
        </ul>
    </li>
</ul>
<ul>
    <li>
        <h2>Plugins</h2>
        <h3>Plugins</h3>
        <ul class="links">
            <li>plugins.html</li>
            <li>tags.html</li>
        </ul>
    </li>
</ul>
<ul>
    <li>
        <h2>Start</h2>
        <h3>Getting Started</h3>
        <ul class="links">
            <li>installation.html</li>
            <li>overview.html</li>
            <li>templating.html</li>
        </ul>
    </li>
</ul>


"""
        self.s.config.grouper.section.groups.append(Expando({"name": "awesome", "description": "Aweesoome"}));
        gen = Generator(self.s)
        gen.load_site_if_needed()
        gen.load_template_if_needed()
        out = gen.template.render(text, {'site':self.s})
        assert_html_equals(out, expected)
Пример #22
0
class TestSimpleCopy(object):
    @classmethod
    def setup_class(cls):
        cls.SITE_PATH =  File(__file__).parent.child_folder('sites/test_jinja_with_config')
        cls.SITE_PATH.make()
        TEST_SITE_ROOT.copy_contents_to(cls.SITE_PATH)

    @classmethod
    def teardown_class(cls):
        cls.SITE_PATH.delete()

    @nottest
    def setup_config(self, passthru):
        self.config_file = File(self.SITE_PATH.child('site.yaml'))
        with open(self.config_file.path) as config:
            conf = yaml.load(config)
            conf['simple_copy'] = passthru
            self.config = Config(sitepath=self.SITE_PATH, config_dict=conf)

    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_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_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_generator(self):
        self.setup_config([
            '**/*.html',
            'media/css/*.css'
        ])
        s = Site(self.SITE_PATH, self.config)
        g = Generator(s)
        g.generate_all()
        source = s.content.resource_from_relative_path('blog/2010/december/merry-christmas.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_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
Пример #23
0
class TestSimpleCopy(object):

    @classmethod
    def setup_class(cls):
        cls.SITE_PATH = File(__file__).parent.child_folder(
            'sites/test_jinja_with_config')
        cls.SITE_PATH.make()
        TEST_SITE_ROOT.copy_contents_to(cls.SITE_PATH)

    @classmethod
    def teardown_class(cls):
        cls.SITE_PATH.delete()

    @nottest
    def setup_config(self, passthru):
        self.config_file = File(self.SITE_PATH.child('site.yaml'))
        with open(self.config_file.path) as config:
            conf = yaml.load(config, Loader=yaml.FullLoader)
            conf['simple_copy'] = passthru
            self.config = Config(sitepath=self.SITE_PATH, config_dict=conf)

    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_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_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_generator(self):
        self.setup_config([
            '**/*.html',
            'media/css/*.css'
        ])
        s = Site(self.SITE_PATH, self.config)
        g = Generator(s)
        g.generate_all()
        source = s.content.resource_from_relative_path(
            'blog/2010/december/merry-christmas.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_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