Edgewall Software
Modify

Opened 3 years ago

Closed 2 years ago

Last modified 2 years ago

#13483 closed defect (fixed)

File viewer renders *.c file as an image with Pygments 2.11 - 2.12

Reported by: Jun Omae Owned by: Jun Omae
Priority: normal Milestone: 1.5.4
Component: rendering Version: 1.5.3
Severity: normal Keywords: pygments
Cc: Branch:
Release Notes:

Perfer text/* type in order to prevent rendering as an image for *.c filename with Pygments 2.11+.

API Changes:
Internal Changes:

Description

Attachments (0)

Change History (4)

comment:2 by Jun Omae, 3 years ago

PygmentsRenderer.get_extra_mimetypes() with Pygments 2.11+ returns multiple mimetypes for c.

>>> from trac.test import EnvironmentStub
>>> from trac.mimeview.api import Mimeview
>>> from trac.mimeview.pygments import PygmentsRenderer
>>> env = EnvironmentStub()
>>> mimeview = Mimeview(env)
>>> mimeview.mime_map['c']
'image/x-xpixmap'
>>> mimeview.get_mimetype('filename.c')
'image/x-xpixmap; charset=utf-8'
>>> renderer = PygmentsRenderer(env)
>>> list(renderer.get_system_info())
[('Pygments', '2.12.0')]
>>> for mimetype, kwds in renderer.get_extra_mimetypes():
...   if 'c' in kwds: print(mimetype, kwds)
...
text/x-chdr ('c',)
text/x-csrc ('c',)
image/x-xbitmap ('c',)
image/x-xpixmap ('c',)

When Pygments 2.10.0 is used:

>>> mimeview = Mimeview(env)
>>> renderer = PygmentsRenderer(env)
>>> list(renderer.get_system_info())
[('Pygments', '2.10.0')]
>>> mimeview.mime_map['c']
'text/x-csrc'
>>> mimeview.get_mimetype('filename.c')
'text/x-csrc; charset=utf-8'
>>> for mimetype, kwds in renderer.get_extra_mimetypes():
...   if 'c' in kwds: print(mimetype, kwds)
...
text/x-chdr ('c',)
text/x-csrc ('c',)

Work around is to avoid one alias having multiple mimetypes like this:

  • trac/mimeview/pygments.py

    diff --git a/trac/mimeview/pygments.py b/trac/mimeview/pygments.py
    index 3be2d9c7c..cd963fab5 100644
    a b class PygmentsRenderer(Component):  
    109109        for _, aliases, _, mimetypes in get_all_lexers():
    110110            for mimetype in mimetypes:
    111111                yield mimetype, aliases
     112                aliases = ()  # avoid one alias having multiple mimetypes
    112113
    113114    def get_quality_ratio(self, mimetype):
    114115        # Extend default MIME type to mode mappings with configured ones

After the changes:

>>> from trac.test import EnvironmentStub
>>> from trac.mimeview.api import Mimeview
>>> from trac.mimeview.pygments import PygmentsRenderer
>>> env = EnvironmentStub()
>>> mimeview = Mimeview(env)
>>> mimeview.mime_map['c']
'text/x-chdr'
>>> mimeview.get_mimetype('filename.c')
'text/x-chdr; charset=utf-8'
>>> for mimetype, kwds in renderer.get_extra_mimetypes():
...   if 'c' in kwds: print(mimetype, kwds)
...
text/x-chdr ('c',)

comment:3 by Jun Omae, 2 years ago

Keywords: pygments added
Owner: set to Jun Omae
Status: newassigned

Proposed changes in [a5d3519b1/jomae.git] (jomae.git@t13483). After the changes, text/* type will be preferred in order to prevent rendering as an image for *.c filename.

comment:4 by Jun Omae, 2 years ago

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

Pushed in [17623].

Last edited 2 years ago by Jun Omae (previous) (diff)

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.