Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix code block highlighting #760

Merged
merged 20 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- 3.5.4
- 4.0.3
- 4.1.2
- git+https://github.com/sphinx-doc/sphinx.git@4.2.x
- git+https://github.com/sphinx-doc/sphinx.git@4.3.x
- git+https://github.com/sphinx-doc/sphinx.git@4.x
- git+https://github.com/sphinx-doc/sphinx.git@master

Expand Down
8 changes: 4 additions & 4 deletions breathe/parser/compound.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ def __init__(self, kind=None, prot=None, id=None, compoundname='', title='',
innerclass=None, innernamespace=None, innerpage=None, innergroup=None,
templateparamlist=None, sectiondef=None, briefdescription=None,
detaileddescription=None, inheritancegraph=None, collaborationgraph=None,
programlisting=None, location=None, listofallmembers=None):
programlisting=None, location=None, listofallmembers=None, language=None):

supermod.compounddefType.__init__(self, kind, prot, id, compoundname, title,
basecompoundref, derivedcompoundref, includes, includedby,
incdepgraph, invincdepgraph, innerdir, innerfile,
innerclass, innernamespace, innerpage, innergroup,
templateparamlist, sectiondef, briefdescription,
detaileddescription, inheritancegraph, collaborationgraph,
programlisting, location, listofallmembers)
programlisting, location, listofallmembers, language)


supermod.compounddefType.subclass = compounddefTypeSub
Expand Down Expand Up @@ -378,8 +378,8 @@ class listingTypeSub(supermod.listingType):

node_type = "listing"

def __init__(self, codeline=None):
supermod.listingType.__init__(self, codeline)
def __init__(self, codeline=None, domain=None):
supermod.listingType.__init__(self, codeline, domain)


supermod.listingType.subclass = listingTypeSub
Expand Down
26 changes: 20 additions & 6 deletions breathe/parser/compoundsuper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
#

import sys
import os
import getopt
from xml.dom import minidom
from xml.dom import Node
import pygments

#
# User methods
Expand Down Expand Up @@ -193,10 +195,11 @@ def buildChildren(self, child_, nodeName_):
class compounddefType(GeneratedsSuper):
subclass = None
superclass = None
def __init__(self, kind=None, prot=None, id=None, compoundname=None, title=None, basecompoundref=None, derivedcompoundref=None, includes=None, includedby=None, incdepgraph=None, invincdepgraph=None, innerdir=None, innerfile=None, innerclass=None, innernamespace=None, innerpage=None, innergroup=None, templateparamlist=None, sectiondef=None, briefdescription=None, detaileddescription=None, inheritancegraph=None, collaborationgraph=None, programlisting=None, location=None, listofallmembers=None):
def __init__(self, kind=None, prot=None, id=None, compoundname=None, title=None, basecompoundref=None, derivedcompoundref=None, includes=None, includedby=None, incdepgraph=None, invincdepgraph=None, innerdir=None, innerfile=None, innerclass=None, innernamespace=None, innerpage=None, innergroup=None, templateparamlist=None, sectiondef=None, briefdescription=None, detaileddescription=None, inheritancegraph=None, collaborationgraph=None, programlisting=None, location=None, listofallmembers=None, language=None):
self.kind = kind
self.prot = prot
self.id = id
self.language = language
self.compoundname = compoundname
self.title = title
if basecompoundref is None:
Expand Down Expand Up @@ -376,6 +379,8 @@ def buildAttributes(self, attrs):
self.prot = attrs.get('prot').value
if attrs.get('id'):
self.id = attrs.get('id').value
if attrs.get('language'):
self.language = attrs.get('language').value.lower()
def buildChildren(self, child_, nodeName_):
if child_.nodeType == Node.ELEMENT_NODE and \
nodeName_ == 'compoundname':
Expand Down Expand Up @@ -482,7 +487,7 @@ def buildChildren(self, child_, nodeName_):
self.set_collaborationgraph(obj_)
elif child_.nodeType == Node.ELEMENT_NODE and \
nodeName_ == 'programlisting':
obj_ = listingType.factory()
obj_ = listingType.factory(domain=self.language)
obj_.build(child_)
self.set_programlisting(obj_)
elif child_.nodeType == Node.ELEMENT_NODE and \
Expand Down Expand Up @@ -2398,7 +2403,8 @@ def buildChildren(self, child_, nodeName_):
class listingType(GeneratedsSuper):
subclass = None
superclass = None
def __init__(self, codeline=None):
def __init__(self, codeline=None, domain: str=None):
self.domain = domain
if codeline is None:
self.codeline = []
else:
Expand Down Expand Up @@ -2436,14 +2442,22 @@ def hasContent_(self):
return True
else:
return False
def build(self, node_):
def build(self, node_: minidom.Element):
attrs = node_.attributes
self.buildAttributes(attrs)
for child_ in node_.childNodes:
nodeName_ = child_.nodeName.split(':')[-1]
self.buildChildren(child_, nodeName_)
def buildAttributes(self, attrs):
pass
def buildAttributes(self, attrs: minidom.NamedNodeMap):
if "filename" in attrs.keys():
# extract the domain for this programlisting tag.
file_ext = list(os.path.splitext(attrs["filename"].value))
try:
lexer_cls = pygments.lexers.get_lexer_for_filename(attrs["filename"].value)
file_ext[1] = lexer_cls.name.lower()
2bndy5 marked this conversation as resolved.
Show resolved Hide resolved
except pygments.util.ClassNotFound:
pass # use file's extension as a fallback (file_ext from above)
self.domain = file_ext[0 if not file_ext[1] else 1].lstrip(".")
def buildChildren(self, child_, nodeName_):
if child_.nodeType == Node.ELEMENT_NODE and \
nodeName_ == 'codeline':
Expand Down
4 changes: 3 additions & 1 deletion breathe/renderer/sphinxrenderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1595,14 +1595,16 @@ def visit_docformula(self, node) -> List[Node]:
def visit_listing(self, node) -> List[Node]:
nodelist = [] # type: List[Node]
for i, item in enumerate(node.codeline):
# Put new lines between the lines. There must be a more pythonic way of doing this
# Put new lines between the lines
if i:
nodelist.append(nodes.Text("\n"))
nodelist.extend(self.render(item))

# Add blank string at the start otherwise for some reason it renders
# the pending_xref tags around the kind in plain text
block = nodes.literal_block("", "", *nodelist)
if node.domain:
block["language"] = node.domain
return [block]

def visit_codeline(self, node) -> List[Node]:
Expand Down