Skip to content

Commit

Permalink
Allow passing extra args to code highlighter (#1683)
Browse files Browse the repository at this point in the history
I primarily want to allow for the possibility of line numbers
in HTML output. Pygments supports this natively, but we don't
have a way to pass arguments through to pygments.

A traitlet is added to allow passing arbitrary extra args
to the Pygments formatter, for Html and LaTeX. Should allow for
line number control and much more :)

This has been something folks have asked for for a while.

Ref ipython/ipython#5847
Ref #427
Ref yuvipanda/notebooksharing.space#34
  • Loading branch information
yuvipanda committed Dec 2, 2021
1 parent bc9802c commit 9c1dcfc
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions nbconvert/filters/highlight.py
Expand Up @@ -14,6 +14,7 @@
from warnings import warn

from traitlets import observe
from traitlets.config import Dict

MULTILINE_OUTPUTS = ['text', 'html', 'svg', 'latex', 'javascript', 'json']

Expand All @@ -23,10 +24,21 @@
]

class Highlight2HTML(NbConvertBase):
extra_formatter_options = Dict(
{},
help="""
Extra set of options to control how code is highlighted.
Passed through to the pygments' HtmlFormatter class.
See available list in https://pygments.org/docs/formatters/#HtmlFormatter
""",
config=True
)

def __init__(self, pygments_lexer=None, **kwargs):
self.pygments_lexer = pygments_lexer or 'ipython3'
super().__init__(**kwargs)

@observe('default_language')
def _default_language_changed(self, change):
warn('Setting default_language in config is deprecated as of 5.0, '
Expand All @@ -53,15 +65,26 @@ def __call__(self, source, language=None, metadata=None):

return _pygments_highlight(source if len(source) > 0 else ' ',
# needed to help post processors:
HtmlFormatter(cssclass=" highlight hl-"+language),
HtmlFormatter(cssclass=" highlight hl-" + language, **self.extra_formatter_options),
language, metadata)


class Highlight2Latex(NbConvertBase):
extra_formatter_options = Dict(
{},
help="""
Extra set of options to control how code is highlighted.
Passed through to the pygments' LatexFormatter class.
See available list in https://pygments.org/docs/formatters/#LatexFormatter
""",
config=True
)

def __init__(self, pygments_lexer=None, **kwargs):
self.pygments_lexer = pygments_lexer or 'ipython3'
super().__init__(**kwargs)

@observe('default_language')
def _default_language_changed(self, change):
warn('Setting default_language in config is deprecated as of 5.0, '
Expand All @@ -87,7 +110,7 @@ def __call__(self, source, language=None, metadata=None, strip_verbatim=False):
if not language:
language=self.pygments_lexer

latex = _pygments_highlight(source, LatexFormatter(), language, metadata)
latex = _pygments_highlight(source, LatexFormatter(**self.extra_formatter_options), language, metadata)
if strip_verbatim:
latex = latex.replace(r'\begin{Verbatim}[commandchars=\\\{\}]' + '\n', '')
return latex.replace('\n\\end{Verbatim}\n', '')
Expand Down

0 comments on commit 9c1dcfc

Please sign in to comment.