Skip to content

Commit 5450e00

Browse files
committed
Add the ability to set pygments lexer options via traitlets
1 parent 2302544 commit 5450e00

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

Diff for: nbconvert/exporters/html.py

+10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from bs4 import BeautifulSoup
1616
from jupyter_core.paths import jupyter_path
1717
from traitlets import Bool, Unicode, default, validate
18+
from traitlets import Dict as TraitletsDict
1819
from traitlets.config import Config
1920

2021
if tuple(int(x) for x in jinja2.__version__.split(".")[:3]) < (3, 0, 0):
@@ -183,6 +184,14 @@ def _template_name_default(self):
183184

184185
output_mimetype = "text/html"
185186

187+
lexer_options = TraitletsDict(
188+
{},
189+
help=(
190+
"Options to be passed to the pygments lexer for highlighting markdown code blocks. "
191+
"See https://pygments.org/docs/lexers/#available-lexers for available options."
192+
),
193+
).tag(config=True)
194+
186195
@property
187196
def default_config(self):
188197
c = Config(
@@ -239,6 +248,7 @@ def markdown2html(self, context, source):
239248
path=path,
240249
anchor_link_text=self.anchor_link_text,
241250
exclude_anchor_links=self.exclude_anchor_links,
251+
**self.lexer_options,
242252
)
243253
return MarkdownWithMath(renderer=renderer).render(source)
244254

Diff for: nbconvert/filters/highlight.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ def __call__(self, source, language=None, metadata=None, strip_verbatim=False):
136136
return latex
137137

138138

139-
def _pygments_highlight(source, output_formatter, language="ipython", metadata=None):
139+
def _pygments_highlight(
140+
source, output_formatter, language="ipython", metadata=None, **lexer_options
141+
):
140142
"""
141143
Return a syntax-highlighted version of the input source
142144
@@ -149,6 +151,10 @@ def _pygments_highlight(source, output_formatter, language="ipython", metadata=N
149151
language to highlight the syntax of
150152
metadata : NotebookNode cell metadata
151153
metadata of the cell to highlight
154+
lexer_options : dict
155+
Options to pass to the pygments lexer. See
156+
https://pygments.org/docs/lexers/#available-lexers for more information about
157+
valid lexer options
152158
"""
153159
from pygments import highlight
154160
from pygments.lexers import get_lexer_by_name
@@ -179,7 +185,7 @@ def _pygments_highlight(source, output_formatter, language="ipython", metadata=N
179185

180186
if lexer is None:
181187
try:
182-
lexer = get_lexer_by_name(language, stripall=False)
188+
lexer = get_lexer_by_name(language, **lexer_options)
183189
except ClassNotFound:
184190
warn("No lexer found for language %r. Treating as plain text." % language, stacklevel=2)
185191
from pygments.lexers.special import TextLexer

Diff for: nbconvert/filters/markdown_mistune.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,15 @@ def __init__(
293293
anchor_link_text: str = "¶",
294294
path: str = "",
295295
attachments: Optional[Dict[str, Dict[str, str]]] = None,
296+
**lexer_options,
296297
):
297298
"""Initialize the renderer."""
298299
super().__init__(escape, allow_harmful_protocols)
299300
self.embed_images = embed_images
300301
self.exclude_anchor_links = exclude_anchor_links
301302
self.anchor_link_text = anchor_link_text
302303
self.path = path
304+
self.lexer_options = lexer_options
303305
if attachments is not None:
304306
self.attachments = attachments
305307
else:
@@ -317,7 +319,7 @@ def block_code(self, code: str, info: Optional[str] = None) -> str:
317319
try:
318320
if info.strip().split(None, 1):
319321
lang = info.strip().split(maxsplit=1)[0]
320-
lexer = get_lexer_by_name(lang, stripall=False)
322+
lexer = get_lexer_by_name(lang, **self.lexer_options)
321323
except ClassNotFound:
322324
code = f"{lang}\n{code}"
323325
lang = None

0 commit comments

Comments
 (0)