Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOP-3424: "On this page" subnav incorrectly displays headings for all language tabs #625

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions snooty/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,15 +470,23 @@ class HeadingData(NamedTuple):
depth: int
id: str
title: Sequence[n.InlineNode]
selector_id: Optional[str]
selector_ids: Dict[Any, Any]
mayaraman19 marked this conversation as resolved.
Show resolved Hide resolved

def __init__(self, context: Context) -> None:
super().__init__(context)
self.contents_depth = sys.maxsize
self.current_depth = 0
self.has_contents_directive = False
self.headings: List[ContentsHandler.HeadingData] = []
self.scanned_pattern: List[str] = []
self.scanned_pattern: List[Tuple[str, str]] = []

def scan_pattern(self, arr: List[Tuple[str, str]]) -> Dict[Any, Any]:
if arr is []:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor - can use if not arr. also wondering if preference is to have empty object or None

return {}
if len(arr) == 1:
return {arr[0][0]: arr[0][1]}
scanned_pattern = {arr[0][0]: arr[0][1], "children": self.scan_pattern(arr[1:])}
return scanned_pattern

def enter_page(self, fileid_stack: FileIdStack, page: Page) -> None:
self.contents_depth = sys.maxsize
Expand All @@ -496,7 +504,7 @@ def exit_page(self, fileid_stack: FileIdStack, page: Page) -> None:
"depth": h.depth,
"id": h.id,
"title": [node.serialize() for node in h.title],
"selector_id": h.selector_id,
"selector_ids": h.selector_ids,
}
for h in self.headings
if h.depth - 1 <= self.contents_depth
Expand All @@ -509,8 +517,13 @@ def enter_node(self, fileid_stack: FileIdStack, node: n.Node) -> None:
self.current_depth += 1
return

if isinstance(node, n.Directive) and node.name == "method-option":
self.scanned_pattern.append(node.options["id"])
# add tab selectors here as well
mayaraman19 marked this conversation as resolved.
Show resolved Hide resolved

if isinstance(node, n.Directive):
if node.name == "method-option":
self.scanned_pattern.append((node.name, node.options["id"]))
elif node.name == "tab":
mayaraman19 marked this conversation as resolved.
Show resolved Hide resolved
self.scanned_pattern.append((node.name, node.options["tabid"]))

if isinstance(node, n.Directive) and node.name == "contents":
if self.has_contents_directive:
Expand All @@ -526,16 +539,15 @@ def enter_node(self, fileid_stack: FileIdStack, node: n.Node) -> None:
if self.current_depth - 1 > self.contents_depth:
return

selector_id = None
selector_ids = {}
if len(self.scanned_pattern) > 0:
for item in self.scanned_pattern:
selector_id = item
selector_ids = self.scan_pattern(self.scanned_pattern)

# Omit title headings (depth = 1) from heading list
if isinstance(node, n.Heading) and self.current_depth > 1:
self.headings.append(
ContentsHandler.HeadingData(
self.current_depth, node.id, node.children, selector_id
self.current_depth, node.id, node.children, selector_ids
)
)

Expand All @@ -546,12 +558,14 @@ def enter_node(self, fileid_stack: FileIdStack, node: n.Node) -> None:
self.current_depth + 1,
node.options["id"],
[n.Text(node.span, node.options["heading"])],
selector_id,
selector_ids,
)
)

def exit_node(self, fileid_stack: FileIdStack, node: n.Node) -> None:
if isinstance(node, n.Directive) and node.name == "method-option":
if isinstance(node, n.Directive) and (
node.name == "method-option" or node.name == "tab"
):
self.scanned_pattern.pop()
if isinstance(node, n.Section):
self.current_depth -= 1
Expand Down
79 changes: 71 additions & 8 deletions snooty/test_postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -2347,7 +2347,7 @@ def test_contents_directive() -> None:
check_ast_testing_string(
page.ast,
"""
<root fileid="page.txt" headings="[{'depth': 2, 'id': 'first-heading', 'title': [{'type': 'text', 'position': {'start': {'line': 9}}, 'value': 'First Heading'}], 'selector_id': None}, {'depth': 3, 'id': 'second-heading', 'title': [{'type': 'text', 'position': {'start': {'line': 12}}, 'value': 'Second Heading'}], 'selector_id': None}, {'depth': 2, 'id': 'third-heading', 'title': [{'type': 'text', 'position': {'start': {'line': 18}}, 'value': 'Third Heading'}], 'selector_id': None}]">
<root fileid="page.txt" headings="[{'depth': 2, 'id': 'first-heading', 'title': [{'type': 'text', 'position': {'start': {'line': 9}}, 'value': 'First Heading'}], 'selector_ids': {}}, {'depth': 3, 'id': 'second-heading', 'title': [{'type': 'text', 'position': {'start': {'line': 12}}, 'value': 'Second Heading'}], 'selector_ids': {}}, {'depth': 2, 'id': 'third-heading', 'title': [{'type': 'text', 'position': {'start': {'line': 18}}, 'value': 'Third Heading'}], 'selector_ids': {}}]">
<section>
<heading id="title"><text>Title</text></heading>
<directive name="contents" depth="2" />
Expand Down Expand Up @@ -3557,7 +3557,7 @@ def test_collapsible_headings() -> None:
"value": "Subsection heading",
}
],
"selector_id": None,
"selector_ids": {},
},
{
"depth": 3,
Expand All @@ -3569,7 +3569,7 @@ def test_collapsible_headings() -> None:
"value": "Subsubsection heading",
}
],
"selector_id": None,
"selector_ids": {},
},
]

Expand Down Expand Up @@ -3621,7 +3621,7 @@ def test_collapsible_headings() -> None:
"value": "Subsection heading",
}
],
"selector_id": None,
"selector_ids": {},
}
]

Expand Down Expand Up @@ -3659,7 +3659,7 @@ def test_collapsible_headings() -> None:
"value": "Collapsible heading",
}
],
"selector_id": None,
"selector_ids": {},
}
]

Expand Down Expand Up @@ -4070,12 +4070,12 @@ def test_method_selector_headings() -> None:
"value": "Subsection heading",
}
],
"selector_id": None,
"selector_ids": {},
},
{
"depth": 3,
"id": "what",
"selector_id": "driver",
"selector_ids": {"method-option": "driver"},
"title": [
{
"position": {"start": {"line": 17}},
Expand All @@ -4087,7 +4087,7 @@ def test_method_selector_headings() -> None:
{
"depth": 3,
"id": "this-is-a-heading",
"selector_id": "cli",
"selector_ids": {"method-option": "cli"},
"title": [
{
"position": {"start": {"line": 29}},
Expand All @@ -4099,6 +4099,69 @@ def test_method_selector_headings() -> None:
]


def test_tab_headings() -> None:
with make_test(
{
Path(
"source/index.txt"
): """
.. contents:: On this page
:depth: 3

Title here
==========

.. tabs::

.. tab:: tabs1
:tabid: tabs1

Heading here
------------
This is content in tab1.

.. tabs::

.. tab:: tabby
:tabid: tabby

This is another headinge!
~~~~~~~~~~~~~~~~~~~~~~~~~

Text ext text

""",
}
) as result:
page = result.pages[FileId("index.txt")]
assert (page.ast.options.get("headings")) == [
{
"depth": 2,
"id": "heading-here",
"title": [
{
"type": "text",
"position": {"start": {"line": 13}},
"value": "Heading here",
}
],
"selector_ids": {"tab": "tabs1"},
},
{
"depth": 3,
"id": "this-is-another-headinge-",
"title": [
{
"type": "text",
"position": {"start": {"line": 22}},
"value": "This is another headinge!",
}
],
"selector_ids": {"tab": "tabs1", "children": {"tab": "tabby"}},
},
]


def test_multi_page_tutorials() -> None:
test_page_template = """
.. multi-page-tutorial::
Expand Down