def test_top_filter(self): text = """ {% yaml test %} item_list: - A - B - C - D - E - F {% endyaml %} <ul class="top"> {% for value in test.item_list|top(3) %} <li>{{ value }}</li> {% endfor %} </ul> <ul class="mid"> {% for value in test.item_list|islice(3, 6) %} <li>{{ value }}</li> {% endfor %} </ul> """ t = Jinja2Template(JINJA2.path) t.configure(None) t.env.filters['dateformat'] = dateformat html = t.render(text, {}).strip() actual = PyQuery(html) assert actual("ul").length == 2 assert actual("li").length == 6 items = [item.text for item in actual("ul.top li")] assert items == ["A", "B", "C"] items = [item.text for item in actual("ul.mid li")] assert items == ["D", "E", "F"]
def test_asciidoc(): if PY3: # asciidoc is not supported under Python 3. Supporting it is out # of the scope of this project, so its tests are simply skipped # when run under Python 3. raise SkipTest source = """ {%asciidoc%} == Heading 2 == * test1 * test2 * test3 {%endasciidoc%} """ t = Jinja2Template(JINJA2.path) t.configure(None) html = t.render(source, {}).strip() assert html q = PyQuery(html) assert q assert q("li").length == 3 assert q("li:nth-child(1)").text().strip() == "test1" assert q("li:nth-child(2)").text().strip() == "test2" assert q("li:nth-child(3)").text().strip() == "test3"
def test_spaceless(): source = """ {%spaceless%} <html> <body> <ul> <li> One </li> <li> Two </li> <li> Three </li> </ul> </body> </html> {%endspaceless%} """ t = Jinja2Template(JINJA2.path) t.configure(None) t.env.filters['dateformat'] = dateformat html = t.render(source, {}).strip() expected = """ <html><body><ul><li> One </li><li> Two </li><li> Three </li></ul></body></html> """ assert html.strip() == expected.strip()
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 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() assert html.strip() == expected.strip()
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 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() assert html.strip() == expected.strip()
def test_depends(self): t = Jinja2Template(JINJA2.path) t.configure(None) deps = list(t.get_dependencies('index.html')) assert len(deps) == 2 assert 'helpers.html' in deps assert 'layout.html' in deps
def find_template(site): """ Reads the configuration to find the appropriate template. """ # TODO: Find the appropriate template environment from hyde.ext.templates.jinja import Jinja2Template template = Jinja2Template(site.sitepath) return template
def test_typogrify(): source = """ {%filter typogrify%} One & two {%endfilter%} """ t = Jinja2Template(JINJA2.path) t.configure(None) html = t.render(source, {}).strip() assert html == u'One <span class="amp">&</span> two'
def test_markdown(): source = """ {%markdown%} ### Heading 3 {%endmarkdown%} """ t = Jinja2Template(JINJA2.path) t.configure(None) html = t.render(source, {}).strip() assert html == '<h3>Heading 3</h3>'
def test_urldecode_filter(self): text= u""" <a href="{{ 'фотография.jpg'|urlencode }}">{{ "%D1%84%D0%BE%D1%82%D0%BE%D0%B3%D1%80%D0%B0%D1%84%D0%B8%D1%8F.jpg"|urldecode }}</a> """ expected = u""" <a href="%D1%84%D0%BE%D1%82%D0%BE%D0%B3%D1%80%D0%B0%D1%84%D0%B8%D1%8F.jpg">фотография.jpg</a> """ t = Jinja2Template(JINJA2.path) t.configure(None) html = t.render(text, {}).strip() assert html.strip() == expected.strip()
def test_urlencode_filter(self): text= u""" <a href="{{ 'фотография.jpg'|urlencode }}">фотография</a> <a href="{{ 'http://localhost:8080/"abc.jpg'|urlencode }}">quoted</a> """ expected = u""" <a href="%D1%84%D0%BE%D1%82%D0%BE%D0%B3%D1%80%D0%B0%D1%84%D0%B8%D1%8F.jpg">фотография</a> <a href="http%3A//localhost%3A8080/%22abc.jpg">quoted</a> """ t = Jinja2Template(JINJA2.path) t.configure(None) html = t.render(text, {}).strip() assert html.strip() == expected.strip()
def test_restructuredtext(): source = """ {% restructuredtext %} Hello ===== {% endrestructuredtext %} """ t = Jinja2Template(JINJA2.path) t.configure(None) html = t.render(source, {}).strip() assert html == """<div class="document" id="hello"> <h1 class="title">Hello</h1> </div>""", html
def test_markdown_with_extensions(): source = """ {%markdown%} ### Heading 3 {%endmarkdown%} """ t = Jinja2Template(JINJA2.path) s = Site(JINJA2.path) c = Config(JINJA2.path, config_dict=dict(markdown=dict(extensions=['headerid']))) s.config = c t.configure(s) html = t.render(source, {}).strip() assert html == u'<h3 id="heading_3">Heading 3</h3>'
def test_line_statements(): source = """ $$$ markdown ### Heading 3 $$$ endmarkdown """ t = Jinja2Template(JINJA2.path) s = Site(JINJA2.path) c = Config(JINJA2.path, config_dict=dict(markdown=dict(extensions=['headerid']))) s.config = c t.configure(s) html = t.render(source, {}).strip() assert html == u'<h3 id="heading_3">Heading 3</h3>'
def test_raw_tag(self): expected = """ <div class="template secondary"> <aside class="secondary"> <ul> {{#sections}} <li><a href="#{{id}}">{{textContent}}</a></li> {{/sections}} </ul> </aside> """ text = "{%% raw %%}%s{%% endraw %%}" % expected t = Jinja2Template(JINJA2.path) t.configure(None) html = t.render(text, {}).strip() assert html.strip() == expected.strip()
def test_render(): """ Uses pyquery to test the html structure for validity """ t = Jinja2Template(JINJA2.path) t.configure(None) t.env.filters['dateformat'] = dateformat source = File(JINJA2.child('index.html')).read_all() html = t.render(source, context) actual = PyQuery(html) assert actual(".navigation li").length == 30 assert actual("div.article").length == 20 assert actual("div.article h2").length == 20 assert actual("div.article h2 a").length == 20 assert actual("div.article p.meta").length == 20 assert actual("div.article div.text").length == 20
def test_asciidoc(): source = """ {%asciidoc%} == Heading 2 == * test1 * test2 * test3 {%endasciidoc%} """ t = Jinja2Template(JINJA2.path) t.configure(None) html = t.render(source, {}).strip() expected_output = """ <hr> <h2><a name="_heading_2"></a>Heading 2</h2> <ul> <li> <p> test1 </p> </li> <li> <p> test2 </p> </li> <li> <p> test3 </p> </li> </ul> """ assert html q = PyQuery(html) assert q assert q("li").length == 3 assert q("li:eq(0)").text().strip() == "test1" assert q("li:eq(1)").text().strip() == "test2" assert q("li:eq(2)").text().strip() == "test3"
def test_asciidoc(): source = """ {%asciidoc%} == Heading 2 == * test1 * test2 * test3 {%endasciidoc%} """ t = Jinja2Template(JINJA2.path) t.configure(None) html = t.render(source, {}).strip() assert html q = PyQuery(html) assert q assert q("li").length == 3 assert q("li:nth-child(1)").text().strip() == "test1" assert q("li:nth-child(2)").text().strip() == "test2" assert q("li:nth-child(3)").text().strip() == "test3"
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>'
def test_yaml_tag(self): text = """ {% yaml test %} one: - A - B - C two: - D - E - F {% endyaml %} {% for section, values in test.items() %} <ul class="{{ section }}"> {% for value in values %} <li>{{ value }}</li> {% endfor %} </ul> {% endfor %} """ t = Jinja2Template(JINJA2.path) t.configure(None) t.env.filters['dateformat'] = dateformat html = t.render(text, {}).strip() actual = PyQuery(html) assert actual("ul").length == 2 assert actual("ul.one").length == 1 assert actual("ul.two").length == 1 assert actual("li").length == 6 assert actual("ul.one li").length == 3 assert actual("ul.two li").length == 3 ones = [item.text for item in actual("ul.one li")] assert ones == ["A", "B", "C"] twos = [item.text for item in actual("ul.two li")] assert twos == ["D", "E", "F"]