Skip to content

Commit

Permalink
Decl styling, make desc_inline node
Browse files Browse the repository at this point in the history
Use the new node for cpp:expr
  • Loading branch information
jakobandersen committed Mar 20, 2021
1 parent 7695871 commit 7bb2473
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Incompatible changes
* #8937: Use explicit title instead of <no title>
* #8487: The :file: option for csv-table directive now recognizes an absolute
path as a relative path from source directory
* #9023: Change the CSS classes on :rst:role:`cpp:expr` and
:rst:role:`cpp:texpr`.

Deprecated
----------
Expand Down
1 change: 1 addition & 0 deletions doc/extdev/nodes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ These nodes form the top-most levels of object descriptions.
.. autoclass:: desc_signature
.. autoclass:: desc_signature_line
.. autoclass:: desc_content
.. autoclass:: desc_inline

Nodes for high-level structure in signatures
............................................
Expand Down
30 changes: 27 additions & 3 deletions sphinx/addnodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,16 @@ class desc(nodes.Admonition, nodes.Element):
# that forces the specification of the domain and objtyp?


class desc_signature(nodes.Part, nodes.Inline, nodes.TextElement):
class desc_signature(_desc_classes_injector, nodes.Part, nodes.Inline, nodes.TextElement):
"""Node for a single object signature.
As default the signature is a single-line signature.
Set ``is_multiline = True`` to describe a multi-line signature.
In that case all child nodes must be :py:class:`desc_signature_line` nodes.
This node always has the classes ``sig`` and ``sig-object``.
"""
classes = ['sig', 'sig-object']

@property
def child_text_separator(self):
Expand All @@ -183,6 +186,22 @@ class desc_content(nodes.General, nodes.Element):
Must be the last child node in a :py:class:`desc` node.
"""


class desc_inline(_desc_classes_injector, nodes.Inline, nodes.TextElement):
"""Node for a signature fragment in inline text.
This is for example used for roles like :rst:role:`cpp:expr`.
This node always has the classes ``sig``, ``sig-inline``,
and the name of the domain it belongs to.
"""
classes = ['sig', 'sig-inline']

def __init__(self, domain: str, *args, **kwargs):
super().__init__(*args, **kwargs)
self['classes'].append(domain)


# Nodes for high-level structure in signatures
##############################################

Expand Down Expand Up @@ -507,20 +526,25 @@ class manpage(nodes.Inline, nodes.FixedTextElement):

def setup(app: "Sphinx") -> Dict[str, Any]:
app.add_node(toctree)

app.add_node(desc)
app.add_node(desc_signature)
app.add_node(desc_signature_line)
app.add_node(desc_content)
app.add_node(desc_inline)

app.add_node(desc_name)
app.add_node(desc_addname)
app.add_node(desc_type)
app.add_node(desc_returns)
app.add_node(desc_name)
app.add_node(desc_parameterlist)
app.add_node(desc_parameter)
app.add_node(desc_optional)
app.add_node(desc_annotation)
app.add_node(desc_content)

for n in SIG_ELEMENTS:
app.add_node(n)

app.add_node(versionmodified)
app.add_node(seealso)
app.add_node(productionlist)
Expand Down
7 changes: 2 additions & 5 deletions sphinx/domains/cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7394,32 +7394,29 @@ def __init__(self, asCode: bool) -> None:
if asCode:
# render the expression as inline code
self.class_type = 'cpp-expr'
self.node_type = nodes.literal # type: Type[TextElement]
else:
# render the expression as inline text
self.class_type = 'cpp-texpr'
self.node_type = nodes.inline

def run(self) -> Tuple[List[Node], List[system_message]]:
text = self.text.replace('\n', ' ')
parser = DefinitionParser(text,
location=self.get_source_info(),
config=self.config)
# attempt to mimic XRefRole classes, except that...
classes = ['xref', 'cpp', self.class_type]
try:
ast = parser.parse_expression()
except DefinitionError as ex:
logger.warning('Unparseable C++ expression: %r\n%s', text, ex,
location=self.get_source_info())
# see below
return [self.node_type(text, text, classes=classes)], []
return [addnodes.desc_inline('cpp', text, text, classes=[self.class_type])], []
parentSymbol = self.env.temp_data.get('cpp:parent_symbol', None)
if parentSymbol is None:
parentSymbol = self.env.domaindata['cpp']['root_symbol']
# ...most if not all of these classes should really apply to the individual references,
# not the container node
signode = self.node_type(classes=classes)
signode = addnodes.desc_inline('cpp', classes=[self.class_type])
ast.describe_signature(signode, 'markType', self.env, parentSymbol)
return [signode], []

Expand Down
6 changes: 6 additions & 0 deletions sphinx/themes/basic/static/basic.css_t
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,12 @@ table.hlist td {
font-style: italic;
}

/* C++ specific styling */

.sig-inline.cpp-expr {
font-family: monospace;
}


/* -- other body styles ----------------------------------------------------- */

Expand Down
6 changes: 6 additions & 0 deletions sphinx/writers/html5.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ def visit_desc_content(self, node: Element) -> None:
def depart_desc_content(self, node: Element) -> None:
self.body.append('</dd>')

def visit_desc_inline(self, node: Element) -> None:
self.body.append(self.starttag(node, 'span', ''))

def depart_desc_inline(self, node: Element) -> None:
self.body.append('</span>')

# Nodes for high-level structure in signatures
##############################################

Expand Down

0 comments on commit 7bb2473

Please sign in to comment.