Skip to content

Commit

Permalink
draft for allowing code blocks in nxdl.xsd file
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaspie committed Feb 12, 2025
1 parent 3e53905 commit 52d06be
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
28 changes: 27 additions & 1 deletion dev_tools/docs/xsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,29 @@ def print_docs(self, parent, indentLevel=0):
self._print(self._indent(indentLevel) + "\n")

def get_doc_from_node(self, node, retval=None):
def handle_code_and_literals(self, text):
"""
This function ensures inline code (``code``) and code blocks (.. code-block::) are correctly handled for reStructuredText.
"""
lines = text.splitlines()
self._print(text, lines)
result_lines = []

for line in lines:
# Check for inline code (e.g., ``code``)
if "``" in line:
line = self.handle_inline_code(line)

# Check for code blocks (i.e., .. code-block::)
if line.strip().startswith(".. code-block"):
# We assume the block is correctly formatted, so leave it as is
result_lines.append(f"{line.strip()}")

else:
result_lines.append(line)

return "\n".join(result_lines)

annotation_node = node.find("xs:annotation", self.ns)
if annotation_node is None:
return retval
Expand Down Expand Up @@ -279,7 +302,10 @@ def get_doc_from_node(self, node, retval=None):
htmlparser = HTMLParser.HTMLParser()
text = htmlparser.unescape(text)

return text.lstrip()
# Handle potential inline code and code blocks
text = self.handle_code_and_literals(text)

return text.lstrip()

def add_figure(self, name, indentLevel=0):
imageFile = f"img/nxdl/nxdl_{name}.png"
Expand Down
10 changes: 4 additions & 6 deletions nxdl.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -977,18 +977,16 @@ https://stackoverflow.com/a/48980995/1046449 -->
For example, consider the following NXDL snippet:

.. code-block:: xml
:linenos:
:linenos:

<definition xmlns="http://definition.nexusformat.org/nxdl/3.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
category="application"
name="NXexample"
extends="NXobject"
type="group"
xsi:schemaLocation="http://definition.nexusformat.org/nxdl/3.1 ../nxdl.xsd">
<doc>
Example application definition.
</doc>
<doc>Example application definition.</doc>
<group type="NXentry">
<group type="NXdata">
<doc>
Expand All @@ -1000,7 +998,7 @@ https://stackoverflow.com/a/48980995/1046449 -->
<field name="current" type="NX_NUMBER" extends="/NXdata/DATA"/>
</group>
</group>
</definition>
</definition>

Here, the ``extends`` attribute is used to specify that the ``temperature`` and ``voltage`` fields are specializations
of the ``AXISNAME`` field, whereas the ``current`` field is a specialization of the ``DATA`` field.
Expand Down

0 comments on commit 52d06be

Please sign in to comment.