Skip to content

Commit fffb882

Browse files
gjchong25Grace Chong
andauthored
(DOP-2320) Add tocicon page-level option to toctree_node (#367)
* (DOP-2320) add tocicon page-level option to toctree_node * alter ast attribute definition * reformat code * fix tests * Make tocicon option a string, add tests Co-authored-by: Grace Chong <[email protected]>
1 parent 165dbe8 commit fffb882

File tree

5 files changed

+87
-6
lines changed

5 files changed

+87
-6
lines changed

snooty/page.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Page:
2828
source_path: Path
2929
output_filename: str
3030
source: str
31-
ast: n.Parent[n.Node]
31+
ast: n.Root
3232
static_assets: Set[StaticAsset] = field(default_factory=set)
3333
pending_tasks: List[PendingTask] = field(default_factory=list)
3434
category: Optional[str] = None
@@ -40,7 +40,7 @@ def create(
4040
source_path: Path,
4141
output_filename: Optional[str],
4242
source: str,
43-
ast: Optional[n.Parent[n.Node]] = None,
43+
ast: Optional[n.Root] = None,
4444
) -> "Page":
4545
if output_filename is None:
4646
output_filename = source_path.name

snooty/parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ def parse_rst(
997997
visitor, text = parser.parse(path, text)
998998

999999
top_of_state = visitor.state[-1]
1000-
assert isinstance(top_of_state, n.Parent)
1000+
assert isinstance(top_of_state, n.Root)
10011001
page = Page.create(path, None, text, top_of_state)
10021002
page.static_assets = visitor.static_assets
10031003
page.pending_tasks = visitor.pending

snooty/postprocess.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1688,11 +1688,22 @@ def find_toctree_nodes(
16881688
else None
16891689
)
16901690

1691+
toctree_node_options: Dict[str, Any] = {
1692+
"drawer": slug not in toc_landing_pages
1693+
}
1694+
1695+
# Check if tocicon is a page level option
1696+
if context.pages[FileId(slug_fileid)].ast.options:
1697+
if "tocicon" in context.pages[FileId(slug_fileid)].ast.options:
1698+
toctree_node_options["tocicon"] = context.pages[
1699+
FileId(slug_fileid)
1700+
].ast.options["tocicon"]
1701+
16911702
toctree_node = {
16921703
"title": title,
16931704
"slug": "/" if slug == "index" else slug,
16941705
"children": [],
1695-
"options": {"drawer": slug not in toc_landing_pages},
1706+
"options": toctree_node_options,
16961707
}
16971708

16981709
# Don't recurse on the index page

snooty/test_postprocess.py

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,76 @@ def test_broken_link() -> None:
453453
)
454454

455455

456+
# Test for toctree icon
457+
def test_tocicon() -> None:
458+
with make_test(
459+
{
460+
Path(
461+
"source/index.txt"
462+
): """
463+
:tocicon: sync
464+
465+
=============================
466+
Collections :icon:`sync-pill`
467+
=============================
468+
"""
469+
}
470+
) as result:
471+
page = result.pages[FileId("index.txt")]
472+
check_ast_testing_string(
473+
page.ast,
474+
"""
475+
<root fileid="index.txt" tocicon="sync">
476+
<section>
477+
<heading id="collections">
478+
<text>Collections </text>
479+
<role name="icon" target="sync-pill"></role>
480+
</heading>
481+
</section>
482+
</root>
483+
""",
484+
)
485+
486+
487+
def test_toctree_tocicon() -> None:
488+
with make_test(
489+
{
490+
Path(
491+
"source/install.txt"
492+
): """
493+
:tocicon: sync
494+
495+
=============================
496+
Collections :icon:`sync-pill`
497+
=============================
498+
""",
499+
Path(
500+
"source/index.txt"
501+
): """
502+
.. toctree::
503+
:titlesonly:
504+
505+
Overview </index>
506+
/install
507+
""",
508+
}
509+
) as result:
510+
check_toctree_testing_string(
511+
result.metadata["toctree"],
512+
"""
513+
<toctree slug="/">
514+
<title><text>untitled</text></title>
515+
<toctree slug="/" drawer="True">
516+
<title><text>Overview</text></title>
517+
</toctree>
518+
<toctree slug="install" drawer="True" tocicon="sync">
519+
<title><text>Collections </text><role name="icon" target="sync-pill"></role></title>
520+
</toctree>
521+
</toctree>
522+
""",
523+
)
524+
525+
456526
# Ensure that "index.txt" can add itself to the toctree
457527
def test_toctree_self_add() -> None:
458528
with make_test(
@@ -479,7 +549,7 @@ def test_toctree_self_add() -> None:
479549
<toctree slug="/">
480550
<title><text>untitled</text></title>
481551
<toctree slug="page1" drawer="True" />
482-
<toctree slug="/" drawer="True">
552+
<toctree slug="/" drawer="True" >
483553
<title><text>Overview</text></title>
484554
</toctree>
485555
<toctree slug="page2" drawer="True" />

snooty/test_postprocess_old_and_monolithic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def test_toctree(backend: Backend) -> None:
234234
<title><text>MongoDB Connector for Business Intelligence</text></title>
235235
</toctree>
236236
<toctree slug="page3" drawer="False" />
237-
<toctree slug="page4" drawer="True">
237+
<toctree slug="page4" drawer="True" >
238238
<title><text>Page Four</text></title>
239239
</toctree>
240240
</toctree>

0 commit comments

Comments
 (0)