Skip to content

Commit

Permalink
Merge pull request getnikola#3619 from getnikola/fix-pygments-2.12.0
Browse files Browse the repository at this point in the history
Fix getnikola#3617, fix getnikola#3618 — compatibility with Pygments 2.12.0
  • Loading branch information
Kwpolska authored Apr 24, 2022
2 parents 4e76a51 + 7e2fd4f commit 097fe28
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
11 changes: 11 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
New in master
=============

Features
--------

Bugfixes
--------

* Compatibility with Pygments 2.12.0 (Issue #3617, #3618)

New in v8.2.1
=============

Expand Down
9 changes: 8 additions & 1 deletion nikola/plugins/compile/markdown/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,20 @@

from nikola import shortcodes as sc
from nikola.plugin_categories import PageCompiler
from nikola.utils import makedirs, req_missing, write_metadata, LocaleBorg, map_metadata
from nikola.utils import makedirs, req_missing, write_metadata, LocaleBorg, map_metadata, NikolaPygmentsHTML

try:
from markdown import Markdown
except ImportError:
Markdown = None

# Override Pygments formatter for Markdown.
try:
import markdown.extensions.codehilite
markdown.extensions.codehilite.get_formatter_by_name = lambda _, **args: NikolaPygmentsHTML(**args)
except ImportError:
pass


class ThreadLocalMarkdown(threading.local):
"""Convert Markdown to HTML using per-thread Markdown objects.
Expand Down
14 changes: 13 additions & 1 deletion nikola/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1690,7 +1690,7 @@ def __init__(self, anchor_ref=None, classes=None, **kwargs):
kwargs['nowrap'] = False
super().__init__(**kwargs)

def wrap(self, source, outfile):
def wrap(self, source, *args):
"""Wrap the ``source``, which is a generator yielding individual lines, in custom generators."""
style = []
if self.prestyles:
Expand All @@ -1708,6 +1708,18 @@ def wrap(self, source, outfile):

# For consistency, override the default formatter.
pygments.formatters._formatter_cache['HTML'] = NikolaPygmentsHTML
pygments.formatters._formatter_cache['html'] = NikolaPygmentsHTML
_original_find_formatter_class = pygments.formatters.find_formatter_class


def nikola_find_formatter_class(alias):
"""A Nikola-specific version of find_formatter_class."""
if alias.lower().contains('html'):
return NikolaPygmentsHTML
return _original_find_formatter_class(alias)


pygments.formatters.find_formatter_class = nikola_find_formatter_class


def get_displayed_page_number(i, num_pages, site):
Expand Down
10 changes: 4 additions & 6 deletions tests/test_compile_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@
from this
""",
"""\
<table class="codehilitetable"><tr><td class="linenos">\
<div class="linenodiv"><pre><span class="normal">1</span></pre></div>\
</td><td class="code"><pre class="code literal-block"><span></span>\
<code><span class="kn">from</span> <span class="nn">this</span>
</code></pre>
</td></tr></table>
<div class="code"><table class="codetable"><tr><td class="linenos linenodiv">\
<a href="#-1"><code data-line-number="1"></code></a></td>\
<td class="code"><code><span class="kn">from</span> <span class="nn">this</span>
</code></td></tr></table></div>
""",
id="hilite",
),
Expand Down

0 comments on commit 097fe28

Please sign in to comment.