示例#1
0
def test_activate_nav():
    files = types.Files(
        [
            types.File(
                input_path="index.md",
                output_path="index.html",
                input_dir="docs",
                output_dir="build",
                convertor=MarkdownPages(),
            ),
            types.File(
                input_path=os.path.join("topics", "a.md"),
                output_path=os.path.join("topics", "a", "index.html"),
                input_dir="docs",
                output_dir="build",
                convertor=MarkdownPages(),
            ),
            types.File(
                input_path=os.path.join("topics", "b.md"),
                output_path=os.path.join("topics", "b", "index.html"),
                input_dir="docs",
                output_dir="build",
                convertor=MarkdownPages(),
            ),
        ]
    )

    nav = types.Nav(
        [
            types.NavPage(title="Home", file=files[0]),
            types.NavGroup(
                title="Topics",
                children=[
                    types.NavPage(title="Topic A", file=files[1]),
                    types.NavPage(title="Topic B", file=files[2]),
                ],
            ),
        ]
    )

    nav.activate(files[0])
    assert nav[0].is_active
    assert not nav[1].is_active
    assert not nav[1].children[0].is_active
    assert not nav[1].children[0].is_active
    nav.deactivate()

    nav.activate(files[1])
    assert not nav[0].is_active
    assert nav[1].is_active
    assert nav[1].children[0].is_active
    assert not nav[1].children[1].is_active
    nav.deactivate()

    nav.activate(files[2])
    assert not nav[0].is_active
    assert nav[1].is_active
    assert not nav[1].children[0].is_active
    assert nav[1].children[1].is_active
    nav.deactivate()
示例#2
0
def test_nav_relative_urls():
    input_dir = "input"
    output_dir = "output"

    files = types.Files(
        [
            types.File(
                input_path="index.md",
                output_path="index.html",
                input_dir=input_dir,
                output_dir=output_dir,
                convertor=MarkdownPages(),
            ),
            types.File(
                input_path=os.path.join("topics", "a.md"),
                output_path=os.path.join("topics", "a", "index.html"),
                input_dir=input_dir,
                output_dir=output_dir,
                convertor=MarkdownPages(),
            ),
            types.File(
                input_path=os.path.join("topics", "b.md"),
                output_path=os.path.join("topics", "b", "index.html"),
                input_dir=input_dir,
                output_dir=output_dir,
                convertor=MarkdownPages(),
            ),
        ]
    )

    nav = types.Nav(
        items=[
            types.NavPage(title="Home", file=files[0]),
            types.NavGroup(
                title="Topics",
                children=[
                    types.NavPage(title="Topic A", file=files[1]),
                    types.NavPage(title="Topic B", file=files[2]),
                ],
            ),
        ],
        base_url=None,
    )

    nav.activate(files[1])
    assert nav[0].url == "../../"
    assert nav[1].children[0].url == "."
    assert nav[1].children[1].url == "../b/"
    nav.deactivate()
示例#3
0
def test_urls_for_files():
    file = types.File(
        input_path="index.md",
        output_path="index.html",
        input_dir="input",
        output_dir="output",
        convertor=MarkdownPages(),
    )
    assert file.url == "/"

    file = types.File(
        input_path="page.html",
        output_path="page.html",
        input_dir="input",
        output_dir="output",
        convertor=MarkdownPages(),
    )
    assert file.url == "/page.html"
示例#4
0
def test_gather_files(tmpdir):
    """
    Ensure that `gather_files` collects all the files in the input directory.
    """
    input_dir = os.path.join(tmpdir, "input")
    output_dir = os.path.join(tmpdir, "output")
    convertors = [MarkdownPages()]

    index_md = os.path.join(input_dir, "index.md")
    a_md = os.path.join(input_dir, "a.md")
    b_md = os.path.join(input_dir, "b", "b.md")
    write_file(index_md, "index")
    write_file(a_md, "aaa")
    write_file(b_md, "bbb")

    files = core.gather_files(
        input_dir=input_dir, output_dir=output_dir, convertors=convertors
    )
    assert files == types.Files(
        [
            types.File(
                input_path="a.md",
                output_path=os.path.join("a", "index.html"),
                input_dir=input_dir,
                output_dir=output_dir,
                convertor=convertors[0],
            ),
            types.File(
                input_path=os.path.join("b", "b.md"),
                output_path=os.path.join("b", "b", "index.html"),
                input_dir=input_dir,
                output_dir=output_dir,
                convertor=convertors[0],
            ),
            types.File(
                input_path="index.md",
                output_path="index.html",
                input_dir=input_dir,
                output_dir=output_dir,
                convertor=convertors[0],
            ),
        ]
    )
示例#5
0
def test_url_function():
    input_dir = "input"
    output_dir = "output"

    files = types.Files(
        [
            types.File(
                input_path="index.md",
                output_path="index.html",
                input_dir=input_dir,
                output_dir=output_dir,
                convertor=MarkdownPages(),
            ),
            types.File(
                input_path=os.path.join("topics", "a.md"),
                output_path=os.path.join("topics", "a", "index.html"),
                input_dir=input_dir,
                output_dir=output_dir,
                convertor=MarkdownPages(),
            ),
            types.File(
                input_path=os.path.join("topics", "b.md"),
                output_path=os.path.join("topics", "b", "index.html"),
                input_dir=input_dir,
                output_dir=output_dir,
                convertor=MarkdownPages(),
            ),
        ]
    )

    nav = types.Nav(
        [
            types.NavPage(title="Home", file=files[0]),
            types.NavGroup(
                title="Topics",
                children=[
                    types.NavPage(title="Topic A", file=files[1]),
                    types.NavPage(title="Topic B", file=files[2]),
                ],
            ),
        ]
    )

    assert files[0].url == "/"
    assert files[1].url == "/topics/a/"
    assert files[2].url == "/topics/b/"
    assert nav[0].url == "/"
    assert nav[1].children[0].url == "/topics/a/"
    assert nav[1].children[1].url == "/topics/b/"

    # No base URL.
    env = types.Env(files=files, nav=nav, template_dir="templates")
    url = functools.partial(env.get_url, from_file=files[0])
    assert url("#anchor") == "#anchor"
    assert url("https://www.example.com") == "https://www.example.com"
    assert url("topics/a.md") == "topics/a/"
    assert url("topics/b.md") == "topics/b/"
    assert url("/topics/a/") == "topics/a/"
    assert url("/topics/b/") == "topics/b/"

    url = functools.partial(env.get_url, from_file=files[1])
    assert url("../index.md") == "../../"
    assert url("b.md") == "../b/"
    assert url("/") == "../../"
    assert url("/topics/b/") == "../b/"

    url = functools.partial(env.get_url, from_file=files[2])
    assert url("../index.md") == "../../"
    assert url("a.md#anchor") == "../a/#anchor"
    assert url("/") == "../../"
    assert url("/topics/a/#anchor") == "../a/#anchor"

    # Host relative base URL.
    env = types.Env(files=files, nav=nav, template_dir="templates", base_url="/")

    url = functools.partial(env.get_url, from_file=files[0])
    assert url("#anchor") == "#anchor"
    assert url("https://www.example.com") == "https://www.example.com"
    assert url("topics/a.md") == "/topics/a/"
    assert url("topics/b.md") == "/topics/b/"
    assert url("/topics/a/") == "/topics/a/"
    assert url("/topics/b/") == "/topics/b/"

    url = functools.partial(env.get_url, from_file=files[1])
    assert url("../index.md") == "/"
    assert url("b.md") == "/topics/b/"
    assert url("/") == "/"
    assert url("/topics/b/") == "/topics/b/"

    url = functools.partial(env.get_url, from_file=files[2])
    assert url("../index.md") == "/"
    assert url("a.md#anchor") == "/topics/a/#anchor"
    assert url("/") == "/"
    assert url("/topics/a/#anchor") == "/topics/a/#anchor"

    # Absolute base URL.
    env = types.Env(
        files=files, nav=nav, template_dir="templates", base_url="https://example.com"
    )

    url = functools.partial(env.get_url, from_file=files[0])
    assert url("#anchor") == "#anchor"
    assert url("https://www.example.com") == "https://www.example.com"
    assert url("topics/a.md") == "https://example.com/topics/a/"
    assert url("topics/b.md") == "https://example.com/topics/b/"
    assert url("/topics/a/") == "https://example.com/topics/a/"
    assert url("/topics/b/") == "https://example.com/topics/b/"

    url = functools.partial(env.get_url, from_file=files[1])
    assert url("../index.md") == "https://example.com/"
    assert url("b.md") == "https://example.com/topics/b/"
    assert url("/") == "https://example.com/"
    assert url("/topics/b/") == "https://example.com/topics/b/"

    url = functools.partial(env.get_url, from_file=files[2])
    assert url("../index.md") == "https://example.com/"
    assert url("a.md#anchor") == "https://example.com/topics/a/#anchor"
    assert url("/") == "https://example.com/"
    assert url("/topics/a/#anchor") == "https://example.com/topics/a/#anchor"
示例#6
0
def test_load_nav():
    input_dir = "input"
    output_dir = "output"
    markdown_convertor = MarkdownPages()

    files = types.Files(
        [
            types.File(
                input_path="index.md",
                output_path="index.html",
                input_dir=input_dir,
                output_dir=output_dir,
                convertor=markdown_convertor,
            ),
            types.File(
                input_path=os.path.join("topics", "a.md"),
                output_path=os.path.join("topics", "a", "index.html"),
                input_dir=input_dir,
                output_dir=output_dir,
                convertor=markdown_convertor,
            ),
            types.File(
                input_path=os.path.join("topics", "b.md"),
                output_path=os.path.join("topics", "b", "index.html"),
                input_dir=input_dir,
                output_dir=output_dir,
                convertor=markdown_convertor,
            ),
        ]
    )

    nav_info = {
        "Home": "index.md",
        "Topics": {
            "Topic A": os.path.join("topics", "a.md"),
            "Topic B": os.path.join("topics", "b.md"),
        },
    }

    nav = core.load_nav(nav_info, files)
    assert nav == types.Nav(
        [
            types.NavPage(title="Home", file=files[0]),
            types.NavGroup(
                title="Topics",
                children=[
                    types.NavPage(title="Topic A", file=files[1]),
                    types.NavPage(title="Topic B", file=files[2]),
                ],
            ),
        ]
    )

    home = nav[0]
    topics = nav[1]
    topic_a = topics.children[0]
    topic_b = topics.children[1]
    assert home.parent is None
    assert topics.parent is None
    assert topic_a.parent is topics
    assert topic_b.parent is topics

    # Ensure basic `__bool__`, `__len__`, `__iter__` interfaces are supported.
    assert nav
    assert len(nav) == 2
    assert len(list(nav)) == 2