Modify ↓
Opened 9 months ago
Closed 9 months ago
#13866 closed defect (fixed)
Syntax hightlihging has unexpected styles after Pygments 2.7.0
| Reported by: | Jun Omae | Owned by: | Jun Omae |
|---|---|---|---|
| Priority: | normal | Milestone: | 1.6.1 |
| Component: | general | Version: | 1.6 |
| Severity: | normal | Keywords: | |
| Cc: | Branch: | ||
| Release Notes: |
Prevent uses of unnecessary lineno styles from Pygments 2.7.0 or later. |
||
| API Changes: | |||
| Internal Changes: | |||
Description
After Pygments 2.7.0, the syntax highlighting has unexpected styles (e.g. pre, td.linenoes pre).
$ script='import pygments; from pygments.formatters.html import HtmlFormatter as F; print(pygments.__version__); print(F().get_style_defs("div.code pre"))'
$ diff -u <(/venv/pygments-2.6.1/bin/python -c "$script") \
> <(/venv/pygments-2.7.0/bin/python -c "$script")
--- /dev/fd/63 2025-02-05 19:05:33.783629775 +0900
+++ /dev/fd/62 2025-02-05 19:05:33.783629775 +0900
@@ -1,6 +1,11 @@
-2.6.1
+2.7.0
+pre { line-height: 125%; margin: 0; }
+td.linenos pre { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
+span.linenos { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
+td.linenos pre.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
+span.linenos.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
div.code pre .hll { background-color: #ffffcc }
-div.code pre { background: #f8f8f8; }
+div.code pre { background: #f8f8f8; }
div.code pre .c { color: #408080; font-style: italic } /* Comment */
div.code pre .err { border: 1px solid #FF0000 } /* Error */
div.code pre .k { color: #008000; font-weight: bold } /* Keyword */
I think we could directly invoke get_background_style_defs and get_token_style_defs in the HtmlFormatter.
-
trac/mimeview/pygments.py
37 37 __all__ = ['PygmentsRenderer'] 38 38 39 39 40 if hasattr(HtmlFormatter, 'get_background_style_defs') and \ 41 hasattr(HtmlFormatter, 'get_token_style_defs'): 42 def _get_style_defs(style, arg): # Pygments 2.7.0+ 43 lines = [] 44 formatter = HtmlFormatter(style=style) 45 lines.extend(formatter.get_background_style_defs(arg)) 46 lines.extend(formatter.get_token_style_defs(arg)) 47 return '\n'.join(lines) 48 else: 49 def _get_style_defs(style, arg): 50 formatter = HtmlFormatter(style=style) 51 return formatter.get_style_defs(arg) 52 53 40 54 class PygmentsRenderer(Component): 41 55 """HTML renderer for syntax highlighting based on Pygments.""" 42 56 … … 186 200 req.end_headers() 187 201 return 188 202 189 formatter = HtmlFormatter(style=style_cls) 190 content = formatter.get_style_defs(['div.code pre', 'table.code td']) 203 content = _get_style_defs(style_cls, ['div.code pre', 'table.code td']) 191 204 content = content.encode('utf-8') 192 205 193 206 req.send_response(200)
See also: https://github.com/pygments/pygments/blob/2.19.1/pygments/formatters/html.py#L516
Attachments (0)
Change History (1)
comment:1 by , 9 months ago
| Release Notes: | modified (diff) |
|---|---|
| Resolution: | → fixed |
| Status: | assigned → closed |
Note:
See TracTickets
for help on using tickets.



Fixed in [17877] and merge [17879].