Skip to content

Commit f9be348

Browse files
authored
DOP-5589: Composable tutorials dynamic headings (#662)
* insert headings under composable tutorials into page options * fix typing
1 parent ed6dc16 commit f9be348

File tree

2 files changed

+75
-4
lines changed

2 files changed

+75
-4
lines changed

snooty/postprocess.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ def enter_node(self, fileid_stack: FileIdStack, node: n.Node) -> None:
468468
node.refuri = refuri
469469

470470

471-
SelectorId = Dict[str, Union[str, "SelectorId"]]
471+
SelectorId = Dict[str, Union[str, Dict[str, str], "SelectorId"]]
472472

473473

474474
class ContentsHandler(Handler):
@@ -486,9 +486,11 @@ def __init__(self, context: Context) -> None:
486486
self.current_depth = 0
487487
self.has_contents_directive = False
488488
self.headings: List[ContentsHandler.HeadingData] = []
489-
self.scanned_pattern: List[Tuple[str, str]] = []
489+
self.scanned_pattern: List[Tuple[str, Union[str, Dict[str, str]]]] = []
490490

491-
def scan_pattern(self, arr: List[Tuple[str, str]]) -> SelectorId:
491+
def scan_pattern(
492+
self, arr: List[Tuple[str, Union[str, Dict[str, str]]]]
493+
) -> SelectorId:
492494
if not arr:
493495
return {}
494496
if len(arr) == 1:
@@ -533,6 +535,9 @@ def enter_node(self, fileid_stack: FileIdStack, node: n.Node) -> None:
533535
self.scanned_pattern.append((node.name, node.options["id"]))
534536
elif node.name == "tab":
535537
self.scanned_pattern.append((node.name, node.options["tabid"]))
538+
elif node.name == "selected-content":
539+
assert isinstance(node, n.ComposableContent)
540+
self.scanned_pattern.append((node.name, node.selections))
536541

537542
if isinstance(node, n.Directive) and node.name == "contents":
538543
if self.has_contents_directive:
@@ -573,7 +578,7 @@ def enter_node(self, fileid_stack: FileIdStack, node: n.Node) -> None:
573578

574579
def exit_node(self, fileid_stack: FileIdStack, node: n.Node) -> None:
575580
if isinstance(node, n.Directive) and (
576-
node.name == "method-option" or node.name == "tab"
581+
node.name in ["method-option", "tab", "selected-content"]
577582
):
578583
self.scanned_pattern.pop()
579584
if isinstance(node, n.Section):

snooty/test_postprocess.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4166,6 +4166,72 @@ def test_tab_headings() -> None:
41664166
]
41674167

41684168

4169+
def test_composable_headings() -> None:
4170+
with make_test(
4171+
{
4172+
Path(
4173+
"source/index.txt"
4174+
): """
4175+
.. contents:: On this page
4176+
:local:
4177+
:depth: 3
4178+
4179+
======================
4180+
This is the page title
4181+
======================
4182+
4183+
.. composable-tutorial::
4184+
:options: interface, language
4185+
:defaults: driver, nodejs
4186+
4187+
.. selected-content::
4188+
:selections: driver, nodejs
4189+
4190+
This is a title under selected content
4191+
--------------------------------------
4192+
4193+
This is another heading!
4194+
~~~~~~~~~~~~~~~~~~~~~~~~~
4195+
4196+
""",
4197+
}
4198+
) as result:
4199+
test_file_id = FileId("index.txt")
4200+
page = result.pages[test_file_id]
4201+
diagnostics = result.diagnostics[test_file_id]
4202+
assert len(diagnostics) == 0
4203+
assert page.ast.options.get("headings") == [
4204+
{
4205+
"depth": 2,
4206+
"id": "this-is-a-title-under-selected-content",
4207+
"title": [
4208+
{
4209+
"type": "text",
4210+
"position": {"start": {"line": 17}},
4211+
"value": "This is a title under selected content",
4212+
}
4213+
],
4214+
"selector_ids": {
4215+
"selected-content": {"interface": "driver", "language": "nodejs"}
4216+
},
4217+
},
4218+
{
4219+
"depth": 3,
4220+
"id": "this-is-another-heading-",
4221+
"title": [
4222+
{
4223+
"type": "text",
4224+
"position": {"start": {"line": 20}},
4225+
"value": "This is another heading!",
4226+
}
4227+
],
4228+
"selector_ids": {
4229+
"selected-content": {"interface": "driver", "language": "nodejs"}
4230+
},
4231+
},
4232+
]
4233+
4234+
41694235
def test_multi_page_tutorials() -> None:
41704236
test_page_template = """
41714237
.. multi-page-tutorial::

0 commit comments

Comments
 (0)