Skip to content

Commit

Permalink
Decl styling, update writers
Browse files Browse the repository at this point in the history
Add test objects so all builders gets exposed to the new nodes.
Make the fallback node post-transform change desc_inline as well.
Make the html4, latex, and text writers handle desc_inline.
  • Loading branch information
jakobandersen committed Mar 25, 2021
1 parent e8eba91 commit b16cb6c
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 40 deletions.
17 changes: 9 additions & 8 deletions sphinx/transforms/post_transforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def run(self, **kwargs: Any) -> None:


class SigElementFallbackTransform(SphinxPostTransform):
"""Fallback desc_sig_element nodes to inline if translator does not supported them."""
"""Fallback various desc_* nodes to inline if translator does not supported them."""
default_priority = 200

def run(self, **kwargs: Any) -> None:
Expand All @@ -218,14 +218,15 @@ def has_visitor(translator: Type[nodes.NodeVisitor], node: Type[Element]) -> boo
# subclass of SphinxTranslator supports desc_sig_element nodes automatically.
return

if all(has_visitor(translator, node) for node in addnodes.SIG_ELEMENTS):
# the translator supports all desc_sig_element nodes
return
else:
self.fallback()
# for the leaf elements (desc_sig_element), the translator should support _all_
if not all(has_visitor(translator, node) for node in addnodes.SIG_ELEMENTS):
self.fallback(addnodes.desc_sig_element)

if not has_visitor(translator, addnodes.desc_inline):
self.fallback(addnodes.desc_inline)

def fallback(self) -> None:
for node in self.document.traverse(addnodes.desc_sig_element):
def fallback(self, nodeType: Any) -> None:
for node in self.document.traverse(nodeType):
newnode = nodes.inline()
newnode.update_all_atts(node)
newnode.extend(node)
Expand Down
6 changes: 6 additions & 0 deletions sphinx/writers/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,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
48 changes: 33 additions & 15 deletions sphinx/writers/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,13 @@ def visit_subtitle(self, node: Element) -> None:
def depart_subtitle(self, node: Element) -> None:
self.body.append(self.context.pop())

#############################################################
# Domain-specific object descriptions
#############################################################

# Top-level nodes for descriptions
##################################

def visit_desc(self, node: Element) -> None:
if self.config.latex_show_urls == 'footnote':
self.body.append(BLANKLINE)
Expand Down Expand Up @@ -750,6 +757,31 @@ def visit_desc_signature_line(self, node: Element) -> None:
def depart_desc_signature_line(self, node: Element) -> None:
self._depart_signature_line(node)

def visit_desc_content(self, node: Element) -> None:
if node.children and not isinstance(node.children[0], nodes.paragraph):
# avoid empty desc environment which causes a formatting bug
self.body.append('~')

def depart_desc_content(self, node: Element) -> None:
pass

def visit_desc_inline(self, node: Element) -> None:
self.body.append(r'\sphinxcode{\sphinxupquote{')

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

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

def visit_desc_name(self, node: Element) -> None:
self.body.append(r'\sphinxbfcode{\sphinxupquote{')
self.literal_whitespace += 1

def depart_desc_name(self, node: Element) -> None:
self.body.append('}}')
self.literal_whitespace -= 1

def visit_desc_addname(self, node: Element) -> None:
self.body.append(r'\sphinxcode{\sphinxupquote{')
self.literal_whitespace += 1
Expand All @@ -770,14 +802,6 @@ def visit_desc_returns(self, node: Element) -> None:
def depart_desc_returns(self, node: Element) -> None:
self.body.append(r'}')

def visit_desc_name(self, node: Element) -> None:
self.body.append(r'\sphinxbfcode{\sphinxupquote{')
self.literal_whitespace += 1

def depart_desc_name(self, node: Element) -> None:
self.body.append('}}')
self.literal_whitespace -= 1

def visit_desc_parameterlist(self, node: Element) -> None:
# close name, open parameterlist
self.body.append('}{')
Expand Down Expand Up @@ -811,13 +835,7 @@ def visit_desc_annotation(self, node: Element) -> None:
def depart_desc_annotation(self, node: Element) -> None:
self.body.append('}}')

def visit_desc_content(self, node: Element) -> None:
if node.children and not isinstance(node.children[0], nodes.paragraph):
# avoid empty desc environment which causes a formatting bug
self.body.append('~')

def depart_desc_content(self, node: Element) -> None:
pass
##############################################

def visit_seealso(self, node: Element) -> None:
self.body.append(BLANKLINE)
Expand Down
34 changes: 23 additions & 11 deletions sphinx/writers/manpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ def visit_start_of_file(self, node: Element) -> None:
def depart_start_of_file(self, node: Element) -> None:
pass

#############################################################
# Domain-specific object descriptions
#############################################################

# Top-level nodes for descriptions
##################################

def visit_desc(self, node: Element) -> None:
self.visit_definition_list(node)

Expand All @@ -139,6 +146,21 @@ def visit_desc_signature_line(self, node: Element) -> None:
def depart_desc_signature_line(self, node: Element) -> None:
self.body.append(' ')

def visit_desc_content(self, node: Element) -> None:
self.visit_definition(node)

def depart_desc_content(self, node: Element) -> None:
self.depart_definition(node)

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

def visit_desc_name(self, node: Element) -> None:
pass

def depart_desc_name(self, node: Element) -> None:
pass

def visit_desc_addname(self, node: Element) -> None:
pass

Expand All @@ -157,12 +179,6 @@ def visit_desc_returns(self, node: Element) -> None:
def depart_desc_returns(self, node: Element) -> None:
pass

def visit_desc_name(self, node: Element) -> None:
pass

def depart_desc_name(self, node: Element) -> None:
pass

def visit_desc_parameterlist(self, node: Element) -> None:
self.body.append('(')
self.first_param = 1
Expand Down Expand Up @@ -191,11 +207,7 @@ def visit_desc_annotation(self, node: Element) -> None:
def depart_desc_annotation(self, node: Element) -> None:
pass

def visit_desc_content(self, node: Element) -> None:
self.visit_definition(node)

def depart_desc_content(self, node: Element) -> None:
self.depart_definition(node)
##############################################

def visit_versionmodified(self, node: Element) -> None:
self.visit_paragraph(node)
Expand Down
30 changes: 24 additions & 6 deletions sphinx/writers/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,13 @@ def visit_attribution(self, node: Element) -> None:
def depart_attribution(self, node: Element) -> None:
pass

#############################################################
# Domain-specific object descriptions
#############################################################

# Top-level nodes
#################

def visit_desc(self, node: Element) -> None:
pass

Expand All @@ -555,6 +562,22 @@ def visit_desc_signature_line(self, node: Element) -> None:
def depart_desc_signature_line(self, node: Element) -> None:
self.add_text('\n')

def visit_desc_content(self, node: Element) -> None:
self.new_state()
self.add_text(self.nl)

def depart_desc_content(self, node: Element) -> None:
self.end_state()

def visit_desc_inline(self, node: Element) -> None:
pass

def depart_desc_inline(self, node: Element) -> None:
pass

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

def visit_desc_name(self, node: Element) -> None:
pass

Expand Down Expand Up @@ -606,12 +629,7 @@ def visit_desc_annotation(self, node: Element) -> None:
def depart_desc_annotation(self, node: Element) -> None:
pass

def visit_desc_content(self, node: Element) -> None:
self.new_state()
self.add_text(self.nl)

def depart_desc_content(self, node: Element) -> None:
self.end_state()
##############################################

def visit_figure(self, node: Element) -> None:
self.new_state()
Expand Down
6 changes: 6 additions & 0 deletions tests/roots/test-root/objects.txt
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,9 @@ CPP domain

.. cpp:function:: T& operator[]( unsigned j )
const T& operator[]( unsigned j ) const

.. cpp:function:: template<typename T1, typename T2> \
requires A<T1, T2> \
void f()

- :cpp:expr:`a + b`

0 comments on commit b16cb6c

Please sign in to comment.