Skip to content

Commit

Permalink
DOP-5329: Osiris TOC (#646)
Browse files Browse the repository at this point in the history
* Deserialize toctrees

* bughunting

* clean

* PR feedback, lint, format
  • Loading branch information
mmeigs authored Feb 14, 2025
1 parent 49c32b6 commit c0d1856
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
7 changes: 6 additions & 1 deletion snooty/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -2547,6 +2547,11 @@ def find_toctree_nodes(
"drawer": slug not in toc_landing_pages
}

# Ensure Osiris-built TOC parent nodes are functional
if ast.options:
if ast.options.get("osiris_parent"):
toctree_node_options = {"drawer": False}

# Check if the cleaned slug corresponds to an associated project name, indicating an external node
if slug in associated_project_names:
toctree_node_options["project"] = slug
Expand Down Expand Up @@ -2709,7 +2714,7 @@ def clean_slug(slug: str) -> str:
# TODO: remove file extensions in initial parse layer
# https://jira.mongodb.org/browse/DOCSP-7595
root, ext = os.path.splitext(slug)
if ext in SOURCE_FILE_EXTENSIONS:
if ext in SOURCE_FILE_EXTENSIONS or ext == ".ast":
return root

return slug
24 changes: 22 additions & 2 deletions snooty/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
import tomli

from snooty.diagnostics import Diagnostic, NestedProject, UnexpectedNodeType
from snooty.n import FileId
from snooty.n import FileId, TocTreeDirectiveEntry

from . import n, tinydocutils

Expand Down Expand Up @@ -525,15 +525,17 @@ def deserialize(
deserialized_children: List[n.Node] = []

for child in node_value:

if not isinstance(child, dict):
continue

child_type: str = child.get("type", "")
child_node_type = cls.node_classes.get(child_type)

if child_node_type:
if (
child_node_type == n.Directive
and node.get("name") == "toctree"
and child.get("name") == "toctree"
):
child_node_type = n.TocTreeDirective

Expand All @@ -547,6 +549,24 @@ def deserialize(
filtered_fields[field.name] = deserialized_children
elif field.type == FileId and isinstance(node_value, str):
filtered_fields[field.name] = field.type(node_value)
elif field.name == "entries":
deserialized_entries: List[n.TocTreeDirectiveEntry] = []

if isinstance(node_value, List):
for entry in node_value:
if not isinstance(entry, dict):
continue

title = entry.get("title")
slug = entry.get("slug")

if not title or not slug:
continue

entryNode = TocTreeDirectiveEntry(title, None, slug, None)
deserialized_entries.append(entryNode)

filtered_fields[field.name] = deserialized_entries
else:
# Ideally, we validate that the data types of the fields match the data types of the JSON node,
# but that requires a more verbose and time-consuming process. For now, we assume data types are correct.
Expand Down

0 comments on commit c0d1856

Please sign in to comment.