Edgewall Software
Modify

#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

     
    3737__all__ = ['PygmentsRenderer']
    3838
    3939
     40if 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)
     48else:
     49    def _get_style_defs(style, arg):
     50        formatter = HtmlFormatter(style=style)
     51        return formatter.get_style_defs(arg)
     52
     53
    4054class PygmentsRenderer(Component):
    4155    """HTML renderer for syntax highlighting based on Pygments."""
    4256
     
    186200            req.end_headers()
    187201            return
    188202
    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'])
    191204        content = content.encode('utf-8')
    192205
    193206        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 Jun Omae, 110 minutes ago

Release Notes: modified (diff)
Resolution: fixed
Status: assignedclosed

Fixed in [17877] and merge [17879].

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain Jun Omae.
The resolution will be deleted. Next status will be 'reopened'.
to The owner will be changed from Jun Omae to the specified user.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.