summaryrefslogtreecommitdiff
path: root/markdown/extensions
diff options
context:
space:
mode:
authorShrikant Sharat Kandula <shrikantsharat.k@gmail.com>2022-05-09 18:26:10 +0530
committerGitHub <noreply@github.com>2022-05-09 08:56:10 -0400
commit9d90d47157f4348ad70f1a070571de87efbc6cbc (patch)
tree6f412828fe645bda6b2557cc5c531ed40bc3ab6d /markdown/extensions
parent93d17b9cc4a3f53dc2059e44a691a28797422d21 (diff)
downloadpython-markdown-9d90d47157f4348ad70f1a070571de87efbc6cbc.tar.gz
Support for custom Pygments formatter
This adds configuration support for using a custom Pygments formatter, either by giving the string name, or a custom formatter class (or callable).
Diffstat (limited to 'markdown/extensions')
-rw-r--r--markdown/extensions/codehilite.py16
1 files changed, 14 insertions, 2 deletions
diff --git a/markdown/extensions/codehilite.py b/markdown/extensions/codehilite.py
index a768b73..b92ebdc 100644
--- a/markdown/extensions/codehilite.py
+++ b/markdown/extensions/codehilite.py
@@ -23,6 +23,7 @@ try: # pragma: no cover
from pygments import highlight
from pygments.lexers import get_lexer_by_name, guess_lexer
from pygments.formatters import get_formatter_by_name
+ from pygments.util import ClassNotFound
pygments = True
except ImportError: # pragma: no cover
pygments = False
@@ -99,6 +100,7 @@ class CodeHilite:
self.guess_lang = options.pop('guess_lang', True)
self.use_pygments = options.pop('use_pygments', True)
self.lang_prefix = options.pop('lang_prefix', 'language-')
+ self.pygments_formatter = options.pop('pygments_formatter', 'html')
if 'linenos' not in options:
options['linenos'] = options.pop('linenums', None)
@@ -139,7 +141,13 @@ class CodeHilite:
lexer = get_lexer_by_name('text', **self.options)
except ValueError: # pragma: no cover
lexer = get_lexer_by_name('text', **self.options)
- formatter = get_formatter_by_name('html', **self.options)
+ if isinstance(self.pygments_formatter, str):
+ try:
+ formatter = get_formatter_by_name(self.pygments_formatter, **self.options)
+ except ClassNotFound:
+ formatter = get_formatter_by_name('html', **self.options)
+ else:
+ formatter = self.pygments_formatter(**self.options)
return highlight(self.src, lexer, formatter)
else:
# just escape and build markup usable by JS highlighting libs
@@ -279,7 +287,11 @@ class CodeHiliteExtension(Extension):
'lang_prefix': [
'language-',
'Prefix prepended to the language when use_pygments is false. Default: "language-"'
- ]
+ ],
+ 'pygments_formatter': ['html',
+ 'Use a specific formatter for Pygments hilighting.'
+ 'Default: "html"',
+ ],
}
for key, value in kwargs.items():