Exemplo n.º 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()
Exemplo n.º 2
0
def test_load_nav():
    input_dir = "input"
    output_dir = "output"
    template_dir = "templates"

    config = {"site": {"url": "/"}, "build": {"template_dir": template_dir}}
    markdown_convertor = convertors.MarkdownConvertor(config=config)

    files = types.Files([
        types.File(
            input_rel_path="index.md",
            output_rel_path="index.html",
            input_dir=input_dir,
            output_dir=output_dir,
            convertor=markdown_convertor,
        ),
        types.File(
            input_rel_path=os.path.join("topics", "a.md"),
            output_rel_path=os.path.join("topics", "a", "index.html"),
            input_dir=input_dir,
            output_dir=output_dir,
            convertor=markdown_convertor,
        ),
        types.File(
            input_rel_path=os.path.join("topics", "b.md"),
            output_rel_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_info(nav_info, files)
    assert 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
Exemplo n.º 3
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()
Exemplo n.º 4
0
def load_nav_items(
    nav_info: dict, files: types.Files
) -> typing.List[typing.Union[types.NavGroup, types.NavPage]]:
    nav_items = [
    ]  # type: typing.List[typing.Union[types.NavGroup, types.NavPage]]
    for title, child in nav_info.items():
        if isinstance(child, str):
            file = files.get_by_input_path(child)
            nav_page = types.NavPage(title=title, file=file)
            nav_items.append(nav_page)
        else:
            children = load_nav_items(child, files)
            nav_group = types.NavGroup(title=title, children=children)
            nav_items.append(nav_group)
    return nav_items
Exemplo n.º 5
0
def load_nav_info(nav_info: dict,
                  files: types.Files) -> typing.List[types.NavItem]:
    """
    Determine the navigation info.
    """
    nav_items = []  # type: typing.List[types.NavItem]
    for title, child in nav_info.items():
        if isinstance(child, str):
            file = files.get_by_input_path(child)
            nav_page = types.NavPage(title=title, file=file)
            nav_items.append(nav_page)
        else:
            children = load_nav_info(child, files)
            nav_group = types.NavGroup(title=title, children=children)
            nav_items.append(nav_group)
    return nav_items
Exemplo n.º 6
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"
Exemplo n.º 7
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