Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/link_first_reference' into nyaml
Browse files Browse the repository at this point in the history
  • Loading branch information
sanbrock committed Jun 19, 2023
2 parents 9462a1d + 90ff26b commit 49fc2dc
Show file tree
Hide file tree
Showing 6 changed files with 1,721 additions and 18 deletions.
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,23 @@ makelog.txt
# Unknown
/python/
__github_creds__.txt

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
4 changes: 4 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
recursive-include applications/ *.nxdl.xml
recursive-include contributed_definitions/ *.nxdl.xml
recursive-include base_classes/ *.nxdl.xml
include ./ *.xsd
99 changes: 82 additions & 17 deletions dev_tools/docs/nxdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from ..globals.errors import NXDLParseError
from ..globals.nxdl import NXDL_NAMESPACE
from ..globals.urls import REPO_URL
from ..utils import nexus as pynxtools_nxlib
from ..utils.types import PathLike
from .anchor_list import AnchorRegistry

Expand Down Expand Up @@ -109,7 +110,7 @@ def _parse_nxdl_file(self, nxdl_file: Path):
# print official description of this class
self._print("")
self._print("**Description**:\n")
self._print_doc(self._INDENTATION_UNIT, ns, root, required=True)
self._print_doc_enum("", ns, root, required=True)

# print symbol list
node_list = root.xpath("nx:symbols", namespaces=ns)
Expand All @@ -119,7 +120,7 @@ def _parse_nxdl_file(self, nxdl_file: Path):
elif len(node_list) > 1:
raise Exception(f"Invalid symbol table in {nxclass_name}")
else:
self._print_doc(self._INDENTATION_UNIT, ns, node_list[0])
self._print_doc_enum("", ns, node_list[0])
for node in node_list[0].xpath("nx:symbol", namespaces=ns):
doc = self._get_doc_line(ns, node)
self._print(f" **{node.get('name')}**", end="")
Expand Down Expand Up @@ -498,6 +499,35 @@ def _print_doc(self, indent, ns, node, required=False):
self._print(f"{indent}{line}")
self._print()

def long_doc(self, ns, node):
length = 0
line = "documentation"
fnd = False
blocks = self._get_doc_blocks(ns, node)
for block in blocks:
lines = block.splitlines()
length += len(lines)
for single_line in lines:
if len(single_line) > 2 and single_line[0] != "." and not fnd:
fnd = True
line = single_line
return (length, line, blocks)

def _print_doc_enum(self, indent, ns, node, required=False):
collapse_indent = indent
node_list = node.xpath("nx:enumeration", namespaces=ns)
(doclen, line, blocks) = self.long_doc(ns, node)
if len(node_list) + doclen > 1:
collapse_indent = f"{indent} "
self._print(f"{indent}{self._INDENTATION_UNIT}.. collapse:: {line} ...\n")
self._print_doc(
collapse_indent + self._INDENTATION_UNIT, ns, node, required=required
)
if len(node_list) == 1:
self._print_enumeration(
collapse_indent + self._INDENTATION_UNIT, ns, node_list[0]
)

def _print_attribute(self, ns, kind, node, optional, indent, parent_path):
name = node.get("name")
index_name = name
Expand All @@ -506,12 +536,9 @@ def _print_attribute(self, ns, kind, node, optional, indent, parent_path):
)
self._print(f"{indent}.. index:: {index_name} ({kind} attribute)\n")
self._print(
f"{indent}**@{name}**: {optional}{self._format_type(node)}{self._format_units(node)}\n"
f"{indent}**@{name}**: {optional}{self._format_type(node)}{self._format_units(node)} {self.get_first_parent_ref(f'{parent_path}/{name}', 'attribute')}\n"
)
self._print_doc(indent + self._INDENTATION_UNIT, ns, node)
node_list = node.xpath("nx:enumeration", namespaces=ns)
if len(node_list) == 1:
self._print_enumeration(indent + self._INDENTATION_UNIT, ns, node_list[0])
self._print_doc_enum(indent, ns, node)

def _print_if_deprecated(self, ns, node, indent):
deprecated = node.get("deprecated", None)
Expand Down Expand Up @@ -549,17 +576,12 @@ def _print_full_tree(self, ns, parent, name, indent, parent_path):
f"{self._format_type(node)}"
f"{dims}"
f"{self._format_units(node)}"
f" {self.get_first_parent_ref(f'{parent_path}/{name}', 'field')}"
"\n"
)

self._print_if_deprecated(ns, node, indent + self._INDENTATION_UNIT)
self._print_doc(indent + self._INDENTATION_UNIT, ns, node)

node_list = node.xpath("nx:enumeration", namespaces=ns)
if len(node_list) == 1:
self._print_enumeration(
indent + self._INDENTATION_UNIT, ns, node_list[0]
)
self._print_doc_enum(indent, ns, node)

for subnode in node.xpath("nx:attribute", namespaces=ns):
optional = self._get_required_or_optional_text(subnode)
Expand All @@ -585,10 +607,12 @@ def _print_full_tree(self, ns, parent, name, indent, parent_path):
# target = hTarget.replace(".. _", "").replace(":\n", "")
# TODO: https://github.com/nexusformat/definitions/issues/1057
self._print(f"{indent}{hTarget}")
self._print(f"{indent}**{name}**: {optional_text}{typ}\n")
self._print(
f"{indent}**{name}**: {optional_text}{typ} {self.get_first_parent_ref(f'{parent_path}/{name}', 'group')}\n"
)

self._print_if_deprecated(ns, node, indent + self._INDENTATION_UNIT)
self._print_doc(indent + self._INDENTATION_UNIT, ns, node)
self._print_doc_enum(indent, ns, node)

for subnode in node.xpath("nx:attribute", namespaces=ns):
optional = self._get_required_or_optional_text(subnode)
Expand Down Expand Up @@ -619,8 +643,49 @@ def _print_full_tree(self, ns, parent, name, indent, parent_path):
f"(suggested target: ``{node.get('target')}``)"
"\n"
)
self._print_doc(indent + self._INDENTATION_UNIT, ns, node)
self._print_doc_enum(indent, ns, node)

def _print(self, *args, end="\n"):
# TODO: change instances of \t to proper indentation
self._rst_lines.append(" ".join(args) + end)

def get_first_parent_ref(self, path, tag):
nx_name = path[1 : path.find("/", 1)]
path = path[path.find("/", 1) :]

try:
parents = pynxtools_nxlib.get_inherited_nodes(path, nx_name)[2]
except FileNotFoundError:
return ""
if len(parents) > 1:
parent = parents[1]
parent_path = parent_display_name = parent.attrib["nxdlpath"]
parent_path_segments = parent_path[1:].split("/")
parent_def_name = parent.attrib["nxdlbase"][
parent.attrib["nxdlbase"]
.rfind("/") : parent.attrib["nxdlbase"]
.rfind(".nxdl")
]

# Case where the first parent is a base_class
if parent_path_segments[0] == "":
return ""

# special treatment for NXnote@type
if (
tag == "attribute"
and parent_def_name == "/NXnote"
and parent_path == "/type"
):
return ""

if tag == "attribute":
pos_of_right_slash = parent_path.rfind("/")
parent_path = (
parent_path[:pos_of_right_slash]
+ "@"
+ parent_path[pos_of_right_slash + 1 :]
)
parent_display_name = f"{parent_def_name[1:]}{parent_path}"
return f":ref:`⤆ </{parent_display_name}-{tag}>`"
return ""
Loading

0 comments on commit 49fc2dc

Please sign in to comment.