Skip to content

Commit 97f7166

Browse files
committed
Wrap Pygments lines with <span> to allow for better styling
Inline was always difficult to get line highlights to span the entire scrollable code block. By ensuring lines are wrapped with a span, code can be set to `display: grid` allowing for better line highlights. This should be a benign change and behaves like the `line_span` option, just without adding the IDs.
1 parent ad0d493 commit 97f7166

File tree

7 files changed

+60
-6
lines changed

7 files changed

+60
-6
lines changed

docs/src/markdown/about/changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ icon: lucide/scroll-text
1111

1212
In addition, Quotes also provides an optional feature to enable specifying callouts/alerts in the style used by
1313
GitHub and Obsidian.
14+
- **NEW**: Highlight: When using Pygments and not using the table format for line numbers, lines are now wrapped in
15+
`<span>` tags. This allows for easier and better styling of line highlights. Using `line_spans` will additionally
16+
add IDs as they always did.
1417

1518
## 10.19.1
1619

docs/src/scss/extensions/_highlight.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
}
5656

5757
pre > code {
58-
outline: none;
58+
display: grid;
5959
}
6060

6161
/* Don't always like code breaking in table cells */

docs/theme/assets/pymdownx-extras/extra-2395056a6a.css renamed to docs/theme/assets/pymdownx-extras/extra-54d451ef87.css

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/theme/assets/pymdownx-extras/extra-2395056a6a.css.map renamed to docs/theme/assets/pymdownx-extras/extra-54d451ef87.css.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pymdownx/__meta__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,5 +193,5 @@ def parse_version(ver: str) -> Version:
193193
return Version(major, minor, micro, release, pre, post, dev)
194194

195195

196-
__version_info__ = Version(10, 19, 1, "final")
196+
__version_info__ = Version(10, 20, 0, "final")
197197
__version__ = __version_info__._get_canonical()

pymdownx/highlight.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,57 @@ def _wrap_tablelinenos(self, inner):
229229
for t, line in HtmlFormatter._wrap_tablelinenos(self, inner):
230230
yield t, self.RE_TABLE_NUMS.sub(r'\1<span></span>', line)
231231

232+
def _wrap_lines(self, inner):
233+
"""Wrap lines."""
234+
235+
for t, line in inner:
236+
if t:
237+
yield 1, f'<span>{line}</span>'
238+
else:
239+
yield 0, line
240+
241+
def format_unencoded(self, tokensource, outfile):
242+
"""
243+
The formatting process uses several nested generators; which of
244+
them are used is determined by the user's options.
245+
246+
Each generator should take at least one argument, ``inner``,
247+
and wrap the pieces of text generated by this.
248+
249+
Always yield 2-tuples: (code, text). If "code" is 1, the text
250+
is part of the original tokensource being highlighted, if it's
251+
0, the text is some piece of wrapping. This makes it possible to
252+
use several different wrappers that process the original source
253+
linewise, e.g. line number generators.
254+
"""
255+
256+
source = self._format_lines(tokensource)
257+
258+
# As a special case, we wrap line numbers before line highlighting
259+
# so the line numbers get wrapped in the highlighting tag.
260+
if not self.nowrap and self.linenos == 2:
261+
source = self._wrap_inlinelinenos(source)
262+
263+
if self.hl_lines:
264+
source = self._highlight_lines(source)
265+
266+
if not self.nowrap:
267+
if self.lineanchors:
268+
source = self._wrap_lineanchors(source)
269+
if self.linespans:
270+
source = self._wrap_linespans(source)
271+
elif self.linenos in (0, 2):
272+
source = self._wrap_lines(source)
273+
source = self.wrap(source)
274+
if self.linenos == 1:
275+
source = self._wrap_tablelinenos(source)
276+
source = self._wrap_div(source)
277+
if self.full:
278+
source = self._wrap_full(source, outfile)
279+
280+
for _, piece in source:
281+
outfile.write(piece)
282+
232283

233284
class Highlight:
234285
"""Highlight class."""

zensical.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ extra:
268268

269269
extra_css:
270270
# - https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css
271-
- assets/pymdownx-extras/extra-2395056a6a.css
271+
- assets/pymdownx-extras/extra-54d451ef87.css
272272
extra_javascript:
273273
- assets/pymdownx-extras/extra-loader-Ccztcqfq.js
274274
- https://cdn.jsdelivr.net/npm/[email protected]/dist/mermaid.min.js

0 commit comments

Comments
 (0)