Skip to content

Commit

Permalink
fix: more nodes due to NumPy docs
Browse files Browse the repository at this point in the history
  • Loading branch information
agoose77 committed Aug 2, 2024
1 parent a784224 commit d32be83
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 20 deletions.
12 changes: 9 additions & 3 deletions src/sphinx_ext_mystmd/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ def write_doc(self, docname, doctree):

with open(self.env.doc2path(docname), "rb") as f:
sha256 = hashlib.sha256(f.read()).hexdigest()
title = to_text(next(find_by_type("heading", visitor.result)))

heading = next(find_by_type("heading", visitor.result), None)
if heading is not None:
title = to_text(heading)
else:
title = None

with open(json_xref_dst, "w") as f:
json.dump(
{
Expand All @@ -64,7 +70,7 @@ def write_doc(self, docname, doctree):
"slug": slug,
"location": f"/{docname}",
"dependencies": [],
"frontmatter": {"title": title, "content_includes_title": True},
"frontmatter": {"title": title, "content_includes_title": title is not None},
"mdast": visitor.result,
"references": {"cite": {"order": [], "data": {}}},
},
Expand Down Expand Up @@ -92,7 +98,7 @@ def _get_written_target_references(self, doc):
yield {
"identifier": node["identifier"],
"kind": self._xref_kind_for_node(node),
"data": f"/{slug}.json",
"data": self._get_xref_path(doc),
"url": f"/{slug}",
}

Expand Down
96 changes: 79 additions & 17 deletions src/sphinx_ext_mystmd/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@ def result(self):
def inherit_node_info(self, node, docutils_node):
ids = docutils_node.get("ids", [])
if ids:
longest_id = max(ids, key=len)
if len(ids) > 1:
print(f"Warning, found multiple ids: {ids}, using {ids[-1]}")
print(f"Warning, found multiple ids: {ids}, using {longest_id}")

identifier, label, _ = normalize_label(ids[-1])
identifier, label, _ = normalize_label(longest_id)
node["identifier"] = identifier
node["label"] = label

Expand All @@ -113,9 +114,16 @@ def enter_myst_node(self, node, docutils_node=None):
def visit_meta(self, node):
logger.warning("`meta` node not implemented")

def visit_container(self, node):
return self.enter_myst_node({"type": "container", "children": []})

def visit_comment(self, node):
with self.enter_myst_node(
{"type": "comment", "value": str(node.children[0])}, node # TODO: totext
{
"type": "comment",
"value": str(node.children[0]) if node.children else "",
},
node, # TODO: totext
):
yield SkipChildren

Expand Down Expand Up @@ -154,6 +162,20 @@ def visit_problematic(self, node):
{"type": "span", "class": "problematic", "children": []}, node
)

def visit_tgroup(self, node):
logger.warning("Encountered `tgroup` node, ignoring in favour of children")
return

def visit_colspec(self, node):
logger.warning("`colspec` node not implemented")
return SkipChildren

def visit_math(self, node):
return self.enter_myst_node({"type": "inlineMath", "children": []}, node)

def visit_math_block(self, node):
return self.enter_myst_node({"type": "math", "children": []}, node)

def visit_target(self, node):
logger.warning("`target` node not implemented")

Expand All @@ -172,6 +194,14 @@ def visit_title(self, node):
{"type": "heading", "depth": self._heading_depth, "children": []}, node
):
yield
elif parent_type == "table":
with self.enter_myst_node({"type": "caption", "children": []}, node):
yield
elif parent_type == "compact_paragraph":
with self.enter_myst_node(
{"type": "span", "class": "sphinx-caption-text", "children": []}, node
):
yield
elif parent_type in {"topic", "admonition", "sidebar"}:
with self.enter_myst_node(
{
Expand Down Expand Up @@ -205,8 +235,8 @@ def visit_bullet_list(self, node):
def visit_rubric(self, node):
with self.enter_myst_node({"type": "paragraph", "children": []}, node):
with self.enter_myst_node({"type": "strong", "children": []}):
self.push_myst_node({"type": "text", "value": str(node.children[0])})
yield SkipChildren
self.push_myst_node({"type": "text", "value": str(node.children[0])})
yield SkipChildren

def visit_transition(self, node):
return self.enter_myst_node({"type": "thematicBreak"})
Expand Down Expand Up @@ -245,7 +275,7 @@ def visit_literal_strong(self, node):

def visit_footnote_reference(self, node):
return self.enter_myst_node(
{"type": "link", "url": f"#", "children": []}, node
{"type": "link", "url": "#", "children": []}, node
) # TODO: fix url

def visit_index(self, node):
Expand All @@ -254,7 +284,7 @@ def visit_index(self, node):

def visit_title_reference(self, node):
return self.enter_myst_node( # TODO fix url
{"type": "link", "url": f"#", "children": []}, node
{"type": "link", "url": "#", "children": []}, node
)

def visit_sidebar(self, node):
Expand Down Expand Up @@ -321,14 +351,21 @@ def visit_doctest_block(self, node):
def visit_table(self, node):
return self.enter_myst_node({"type": "table", "children": []}, node)

def visit_tgroup(self, node):
logger.warning("`tgroup node not implemented")
return SkipChildren

def visit_tbody(self, node):
logger.warning("`tbody node not implemented")
logger.warning("Encountered `tbody` node, ignoring in favour of children")

def visit_autosummary_table(self, node):
logger.warning(
"Encountered `autosummary_table` node, ignoring in favor of children"
)

def visit_autosummary_toc(self, node):
logger.warning("Encountered `autosummary_table` node, skipping")
return SkipChildren

def visit_glossary(self, node):
return self.enter_myst_node({"type": "glossary", "children": []}, node)

def visit_row(self, node):
return self.enter_myst_node({"type": "tableRow", "children": []}, node)

Expand Down Expand Up @@ -379,10 +416,6 @@ def visit_thead(self, node):
for row_child in child["children"]:
row_child["header"] = True

def visit_colspec(self, node):
logger.warning("`colspec` node not implemented")
return SkipChildren

def visit_tabular_col_spec(self, node):
logger.warning("`tabular_colspec` node not implemented")
return SkipChildren
Expand All @@ -400,6 +433,35 @@ def visit_attribution(self, node):
def visit_admonition(self, node):
return self.enter_myst_node({"type": "admonition", "children": []}, node)

def visit_versionmodified(self, node):
logger.info(repr(node))
return self.enter_myst_node(
{
"type": "admonition",
"children": [
{
"type": "admonitionTitle",
"children": [{"type": "text", "value": "Version Modified"}],
}
],
},
node,
)

def visit_productionlist(self, node):
return self.enter_myst_node(
{
"type": "admonition",
"children": [
{
"type": "admonitionTitle",
"children": [{"type": "text", "value": "Version Modified"}],
}
],
},
node,
)

def visit_substitution_definition(self, node):
# TODO: cache this?

Expand Down Expand Up @@ -503,7 +565,6 @@ def visit_desc_parameter(self, node):
node,
)


def visit_desc_annotation(self, node):
return self.enter_myst_node(
{"type": "emphasis", "children": [], "class": "sphinx-desc-annotation"},
Expand Down Expand Up @@ -553,6 +614,7 @@ def _visit_span(self, node):
"note",
"tip",
"warning",
"seealso",
):

def visitor(self, node, name=name):
Expand Down

0 comments on commit d32be83

Please sign in to comment.