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 highlight's linenothreshold for Sphinx >=1.8+ & <2.0 #783

Merged
merged 1 commit into from
Sep 5, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 34 additions & 3 deletions rst2pdf/pdfbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
from sphinx.environment import NoUri
from sphinx.environment.adapters.indexentries import IndexEntries
from sphinx.locale import admonitionlabels, versionlabels
from sphinx.transforms import SphinxTransform
if sphinx.__version__ >= '1.':
from sphinx.locale import _

Expand Down Expand Up @@ -302,11 +303,17 @@ def process_tree(docname, tree):
tree.append(appendix)
self.spinx_logger.info('done')

# Replace Sphinx's HighlightLanguageTransform with our own for Spinx version between 1.8.0 & less than 2.0.0 as
# Sphinx's HighlightLanguageTransform breaks linenothreshold setting in the highlight directive (See issue #721)
# This code can be removed when we drop support for Python 2
if sphinx.__version__ > '1.7.9' and sphinx.__version__ < '2.0.0':
for i in range(len(self.env.app.registry.post_transforms)):
if self.env.app.registry.post_transforms[i].__name__ == 'HighlightLanguageTransform':
self.env.app.registry.post_transforms[i] = HighlightLanguageTransform
break

self.spinx_logger.info("resolving references...")
#print tree
#print '--------------'
self.env.resolve_references(tree, docname, self)
#print tree

for pendingnode in tree.traverse(addnodes.pending_xref):
# This needs work, need to keep track of all targets
Expand Down Expand Up @@ -782,6 +789,30 @@ def visit_OddEvenNode(self, node):
def depart_OddEvenNode(self, node):
pass


class HighlightLanguageTransform(SphinxTransform):
"""
This is a copy of Sphinx's HighlightLanguageTransform for use with Sphinx versions between 1.8.0 & less than 2.0.0
as the Sphinx version of this class breaks the linenothreshold setting in the highlight directive (See issue #721).
This code can be removed when we drop support for Python 2

Apply highlight_language to all literal_block nodes.

This refers both :confval:`highlight_language` setting and
:rst:dir:`highlightlang` directive.

After processing, this overridden transform DOES NOT REMOVE ``highlightlang`` node from doctree in order to allow
pdfbuilder's visit_highlightlang to work as before.
"""
default_priority = 400

def apply(self):
from sphinx.transforms.post_transforms.code import HighlightLanguageVisitor
visitor = HighlightLanguageVisitor(self.document,
self.config.highlight_language)
self.document.walkabout(visitor)


# This is copied from sphinx.highlighting
def lang_for_block(source,lang):
if lang in ('py', 'python'):
Expand Down