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-5110: Convert nested list-tables into nested rows #643

Closed
wants to merge 8 commits into from
Closed
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
32 changes: 16 additions & 16 deletions snooty/n.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,29 @@ class DefinitionList(Parent[DefinitionListItem]):
type = "definitionList"


@dataclass
class Directive(Parent[Node]):
__slots__ = ("domain", "name", "argument", "options")
type = "directive"
domain: str
name: str
argument: MutableSequence["Text"]
options: Dict[str, str]

def verify(self) -> None:
super().verify()
for arg in self.argument:
arg.verify()


@dataclass
class ListNodeItem(Parent[Node]):
__slots__ = ()
type = "listItem"


@dataclass
class ListNode(Parent[ListNodeItem]):
class ListNode(Parent[Union[ListNodeItem, Directive]]):
__slots__ = ("enumtype", "startat")
type = "list"
enumtype: ListEnumType
Expand All @@ -349,21 +364,6 @@ class LineBlock(Parent[Line]):
type = "line_block"


@dataclass
class Directive(Parent[Node]):
__slots__ = ("domain", "name", "argument", "options")
type = "directive"
domain: str
name: str
argument: MutableSequence["Text"]
options: Dict[str, str]

def verify(self) -> None:
super().verify()
for arg in self.argument:
arg.verify()


class TocTreeDirectiveEntry(NamedTuple):
title: Optional[str]
url: Optional[str]
Expand Down
67 changes: 67 additions & 0 deletions snooty/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -2143,6 +2143,72 @@ def exit_page(self, fileid_stack: FileIdStack, page: Page) -> None:
page.ast.children.remove(node)


class ListTableHandler(Handler):
def __init__(self, context: Context) -> None:
super().__init__(context)

def __identify_expandable_content(self, node: n.Directive) -> None:
# List tables have nested list nodes and list items, so we attempt to destructure them
# .. table::
# .. list:: <row list>
# .. list-item:: <row>
# .. list:: <cell list>
# .. list-item:: <cell>

list_node = node.children[0]
rayangler marked this conversation as resolved.
Show resolved Hide resolved
assert isinstance(list_node, n.ListNode)
rows = list_node.children

# Need to determine if any cell in a row has a nested table. If that's the case, any list-table, and any sibling
# after it, will be in a nested row directive
for row in rows:
list_node = row.children[0]
assert isinstance(list_node, n.ListNode)
cells = list_node.children
nested_row_cells: MutableSequence[n.Node] = []
found_nested_list_table = False

for cell in cells:
nested_row_cell_content: MutableSequence[n.Node] = []
nested_list_table_index = next(
(
i
for i, cell_content_node in enumerate(cell.children)
if isinstance(cell_content_node, n.Directive)
and cell_content_node.name == "list-table"
),
-1,
)
if cell.children and nested_list_table_index > -1:
nested_row_cell_content = cell.children[nested_list_table_index:]
cell.children = cell.children[:nested_list_table_index]
found_nested_list_table = True

nested_row_cell = n.Directive(
(0,), nested_row_cell_content, "mongodb", "cell", [], {}
)
nested_row_cells.append(nested_row_cell)

if found_nested_list_table:
# We append a nested row to the end of the cells to simulate the rST structure:
# .. table::
# .. row::
# .. cell::
# Content
# .. row:: (nested)
# ...
# .. row::
nested_row = n.Directive(
(0,), nested_row_cells, "mongodb", "row", [], {}
)
cells.append(nested_row)

def enter_node(self, fileid_stack: FileIdStack, node: n.Node) -> None:
if not isinstance(node, n.Directive) or node.name != "list-table":
return
self.__identify_expandable_content(node)


class PostprocessorResult(NamedTuple):
pages: Dict[FileId, Page]
metadata: Dict[str, SerializableType]
Expand Down Expand Up @@ -2213,6 +2279,7 @@ class Postprocessor:
WayfindingHandler,
MethodSelectorHandler,
MultiPageTutorialHandler,
ListTableHandler,
],
[TargetHandler, IAHandler, NamedReferenceHandlerPass1],
[RefsHandler, NamedReferenceHandlerPass2],
Expand Down
Loading