Edgewall Software
Modify

Opened 3 months ago

Last modified 5 days ago

#13483 new defect

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

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

Description

Attachments (0)

Change History (2)

comment:2 by Jun Omae, 3 months 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',)

Modify Ticket

Change Properties
Set your email in Preferences
Action
as new The ticket will remain with no owner.
The ticket will be disowned.
as The resolution will be set. Next status will be 'closed'.
The owner will be changed from (none) to anonymous. Next status will be 'assigned'.

Add Comment


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