Exemplo n.º 1
0
def test_renders():
    @renders(e.p()("{x}"), e.p()("{x} again"))
    def render_paras(data: dict) -> dict:
        return {"x": data["x"]}

    assert render_paras({"x": "y"}) == b"<p>y</p><p>y again</p>"

    dangerous_script = "<script>I'm dangerous</script>"
    assert render_paras({"x": dangerous_script}) == raw(
        f"<p>{escape(dangerous_script)}</p><p>{escape(dangerous_script)} again</p>"
    )
    assert render_paras({"x": raw(dangerous_script)}) == raw(
        f"<p>{dangerous_script}</p><p>{dangerous_script} again</p>"
    )

    @renders(e.li()("{item}"))
    def render_list_item(item):
        return {"item": item}

    @renders(e.p()("{foo}"), e.ul()("{list_items}"))
    def render_component():
        return {
            "foo": e.hr,
            "list_items": fn.foreach(["a", "b", "c"])(lambda x: render_list_item(x)),
        }

    assert (
        render(render_component())
        == "<p><hr /></p><ul><li>a</li><li>b</li><li>c</li></ul>"
    )
Exemplo n.º 2
0
def test_render():
    assert render("&nbsp;") == "&amp;nbsp;"
    assert render(txt("&nbsp;")) == "&amp;nbsp;"
    assert render(b"&nbsp;") == "&nbsp;"
    assert render(raw("&nbsp;")) == "&nbsp;"
    assert render(e.p(), e.p()) == "<p></p><p></p>"
    with pytest.raises(ValueError):
        render(1)
Exemplo n.º 3
0
import os
from collections import namedtuple
from types import MappingProxyType

from htmldoom.util import loadraw, loadtxt, render
from htmldoom.yaml_loader import loadyaml

EXTENSION_RENDERERS = MappingProxyType({
    "txt":
    lambda path: render(loadtxt(path)),
    "html":
    lambda path: render(loadraw(path)),
    "css":
    lambda path: render(loadraw(path)),
    "js":
    lambda path: render(loadraw(path)),
    "yml":
    lambda path: render(loadyaml(path)),
    "yaml":
    lambda path: render(loadyaml(path)),
})


def loadvalues(path, extension_renderers=None):
    """Scan a directory and load the values in a nested namedtuple.

    Arguments:
        path: Path to the directory of files containing values.
        extension_renderers: A map of file extensions and their renderers.

    Example: