Skip to content

Commit

Permalink
Decl styling, move static classes to addnodes from HTML5 writer
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobandersen committed Mar 20, 2021
1 parent 95aa7cc commit 7695871
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
25 changes: 22 additions & 3 deletions sphinx/addnodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ def extract_original_messages(self) -> List[str]:
# Domain-specific object descriptions (class, function etc.)
#############################################################

class _desc_classes_injector:
"""Helper base class for injecting a fixes list of classes.
Use as the first base class.
"""

classes = [] # type: List[str]

def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self['classes'].extend(self.classes)


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

Expand Down Expand Up @@ -175,20 +188,26 @@ class desc_content(nodes.General, nodes.Element):

# nodes to use within a desc_signature or desc_signature_line

class desc_name(nodes.Part, nodes.Inline, nodes.FixedTextElement):
class desc_name(_desc_classes_injector, nodes.Part, nodes.Inline, nodes.FixedTextElement):
"""Node for the main object name.
For example, in the declaration of a Python class ``MyModule.MyClass``,
the main name is ``MyClass``.
This node always has the class ``sig-name``.
"""
classes = ['sig-name', 'descname'] # 'descname' is for backwards compatibility


class desc_addname(nodes.Part, nodes.Inline, nodes.FixedTextElement):
class desc_addname(_desc_classes_injector, nodes.Part, nodes.Inline, nodes.FixedTextElement):
"""Node for additional name parts for an object.
For example, in the declaration of a Python class ``MyModule.MyClass``,
the additional name part is ``MyModule.``.
This node always has the class ``sig-prename``.
"""
classes = ['sig-prename', 'descclassname'] # 'descclassname' is for backwards compatibility


# compatibility alias
Expand Down Expand Up @@ -237,7 +256,7 @@ class desc_annotation(nodes.Part, nodes.Inline, nodes.FixedTextElement):
# in SigElementFallbackTransform.
# When adding a new one, add it to SIG_ELEMENTS.

class desc_sig_element(nodes.inline):
class desc_sig_element(nodes.inline, _desc_classes_injector):
"""Common parent class of nodes for inline text of a signature."""
classes = [] # type: List[str]

Expand Down
14 changes: 7 additions & 7 deletions sphinx/writers/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,14 @@ def depart_desc_content(self, node: Element) -> None:
# Nodes for high-level structure in signatures
##############################################

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

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

def visit_desc_addname(self, node: Element) -> None:
self.body.append(self.starttag(node, 'code', '', CLASS='descclassname'))
self.body.append(self.starttag(node, 'code', ''))

def depart_desc_addname(self, node: Element) -> None:
self.body.append('</code>')
Expand All @@ -167,12 +173,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:
self.body.append(self.starttag(node, 'code', '', CLASS='descname'))

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

def visit_desc_parameterlist(self, node: Element) -> None:
self.body.append('<span class="sig-paren">(</span>')
self.first_param = 1
Expand Down
4 changes: 2 additions & 2 deletions sphinx/writers/html5.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ def depart_desc_content(self, node: Element) -> None:
##############################################

def visit_desc_name(self, node: Element) -> None:
self.body.append(self.starttag(node, 'code', '', CLASS='sig-name descname'))
self.body.append(self.starttag(node, 'code', ''))

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

def visit_desc_addname(self, node: Element) -> None:
self.body.append(self.starttag(node, 'code', '', CLASS='sig-prename descclassname'))
self.body.append(self.starttag(node, 'code', ''))

def depart_desc_addname(self, node: Element) -> None:
self.body.append('</code>')
Expand Down

0 comments on commit 7695871

Please sign in to comment.