Edgewall Software

Opened 18 years ago

Closed 18 years ago

#3691 closed enhancement (duplicate)

use Colorer instead of Enscript for syntax highlichting

Reported by: igor daniloff Owned by: Jonas Borgström
Priority: normal Milestone:
Component: version control/browser Version: 0.10b1
Severity: normal Keywords: colorer syntax mimeviewer
Cc: Branch:
Release Notes:
API Changes:
Internal Changes:


# -*- coding: utf-8 -*-
# Copyright (C) 2004-2006 Edgewall Software
# Copyright (C) 2004 Daniel Lundin <daniel@edgewall.com>
# Copyright (C) 2005 Christopher Lenz <cmlenz@gmx.de>
# All rights reserved.
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.org/wiki/TracLicense.
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/log/.
# Author: Daniel Lundin <daniel@edgewall.com>

from trac.config import Option, ListOption
from trac.core import *
from trac.mimeview.api import content_to_unicode, IHTMLPreviewRenderer, Mimeview
from trac.util import NaivePopen
import sys, os, random

__all__ = ['EnscriptRenderer']

types = {
    'application/xhtml+xml':    ('html', 2),
    'application/postscript':   ('ps', 2),
    'application/x-csh':        ('csh', 2),
    'application/x-javascript': ('js', 2),
    'application/x-troff':      ('nroff', 2),
    'text/plain':               ('txt', 2),
    'text/html':                ('html ', 2),
    'text/css':                 ('css  ', 2),
    'text/xml':                 ('xml  ', 2),
    'text/xsl':                 ('xsl  ', 2),
    'text/x-ada':               ('ada', 2),
    'text/x-asm':               ('asm', 2),
    'text/x-awk':               ('awk', 2),
    'text/x-c++src':            ('cpp', 2),
    'text/x-c++hdr':            ('cpp', 2),
    'text/x-chdr':              ('c', 2),
    'text/x-csh':               ('csh', 2),
    'text/x-csrc':              ('c', 2),
    'text/x-diff':              ('diff', 2), # Assume unified diff (works otherwise)
    'text/x-eiffel':            ('eiffel', 2),
    'text/x-elisp':             ('elisp', 2),
    'text/x-fortran':           ('fortran', 2),
    'text/x-haskell':           ('haskell', 2),
    'text/x-idl':               ('idl', 2),
    'text/x-inf':               ('inf', 2),
    'text/x-java':              ('java', 2),
    'text/x-javascript':        ('js', 2),
    'text/x-ksh':               ('ksh', 2),
    'text/x-lua':               ('lua', 2),
    'text/x-m4':                ('m4', 2),
    'text/x-makefile':          ('mak', 2),
    'text/x-mail':              ('mail', 2),
    'text/x-matlab':            ('matlab', 2),
    'text/x-objc':              ('objc', 2),
    'text/x-pascal':            ('pas', 2),
    'text/x-perl':              ('pl', 2),
    'text/x-php':               ('php', 2),
    'text/x-pyrex':             ('pyrex', 2),
    'text/x-python':            ('python', 2),
    'text/x-rfc':               ('rfc', 2),
    'text/x-ruby':              ('ruby', 2),
    'text/x-sh':                ('sh', 2),
    'text/x-scheme':            ('scheme', 2),
    'text/x-sql':               ('sql', 2),
    'text/x-tcl':               ('tcl', 2),
    'text/x-tex':               ('tex', 2),
    'text/x-vba':               ('vba', 2),
    'text/x-verilog':           ('verilog', 2),
    'text/x-vhdl':              ('vhdl', 2),
    'model/vrml':               ('vrml', 2),
    'application/x-sh':         ('sh', 2),
    'text/x-zsh':               ('zsh', 2),
    'text/vnd.wap.wmlscript':   ('wmlscript', 2),

class EnscriptRenderer(Component):
    """Syntax highlighting using Colorer 
       Tested with Colorer-take5.beta4 for Windows


    expand_tabs = True

    # ()
    path = "C:\\Program Files\\Colorer-take5.beta4\\bin\\colorer.exe"

#   path = Option('mimeviewer', 'enscript_path', 'enscript',
#       """Path to the Enscript executable.""")

    def __init__(self):
        self._types = None

    # IHTMLPreviewRenderer methods

    def get_quality_ratio(self, mimetype):
        # Extend default MIME type to mode mappings with configured ones
        if not self._types:
            self._types = {}
        return self._types.get(mimetype, (None, 0))[1]

    def render(self, req, mimetype, content, filename=None, rev=None):
        mimetype = mimetype.split(';', 1)[0] # strip off charset
           charset = mimetype.split(';', 1)[1][8:]
           charset = 'utf-8'
        if type(content)==type(u''):
           content = content.encode('utf-8')
           charset = 'utf-8'
           charset = charset

        mode = self._types[mimetype][0]

        if not filename:
            filename = 'xxx.' + mode

        tmpdir = os.environ.get("TEMP", "C:")
        while True:
            tmpfname = '%s/%08X.%s' % (tmpdir, random.randint(0,sys.maxint), os.path.basename(filename))
            if not os.path.exists(tmpfname):

        f = file(tmpfname, 'wb')

        cmdline = '"%s" -h -dc -db -dh -ei%s -eo%s "%s"' % (self.path, charset, 'utf-8', tmpfname)
        self.env.log.debug("Colorer command line: %s" % cmdline)

        np = NaivePopen(cmdline, '', capturestderr=1)

        if np.errorlevel!=0:
            err = 'Running (%s) failed: %s, %s.' % (cmdline, np.errorlevel,
            raise Exception, err

        odata = np.out.decode('utf-8')
        return odata.splitlines()

Attachments (0)

Change History (8)

comment:1 by Jonas Borgström, 18 years ago

Please motivate why we should switch from enscript to colorer. And please attach your proposal as a unified diff (svn diff) instead of adding the full source code into the description field.

comment:2 by Matthew Good, 18 years ago

Enscript is more readily available since it's packaged for most Linux distributions. Colorer at least has no Debian packages, I don't know about others.

If we would support Colorer, I think it should be through bindings or using ctypes to access the C API, rather than the command line. Someone can start a plugin on http://trac-hacks.org if they'd like to use the command line version, but I don't think there's sufficient reason to replace enscript.

comment:3 by Christian Boos, 18 years ago

Well, I see several reasons: enscript is a hell to install on Windows, and doesn't appear to be supported anymore on that platform… anyway, it's aging and has no UTF-8 support.

Colorer is available on Windows, is more recent, supports UTF-8, covers a wider range of languages (e.g. C#), and is actively maintained. So I think it's worth considering to be (one of) the default choice.

Check for example http://colorer.sourceforge.net/php/demos.php?filename=Unicode.html&hrd_color=neo

comment:4 by trac@…, 18 years ago

FWIW, some of the guys where I work were glad to see support for enscript because it supports VHDL. However, it's true, it was a pain to install on Windows, and personally I think the point about it not being actively maintained is the biggest issue. That doesn't inspire confidence.

Fortuantely, colorer appears to support VHDL and Verilog, so I'd be happy with it that way. C# is starting to show up here as well, so support for that would be a bonus. And if it's easier to install, that would help to lower the "barrier to entry" so to speak (we're using Windows). When we were evaluating Trac, one of the guys tried to get Trac running on his personal (Linux-based) system, but never succeeded. Our IT person couldn't manage to do it either, so I had to do it myself <sigh>.

in reply to:  1 comment:5 by igor daniloff, 18 years ago

Replying to jonas:

Please motivate why we should switch from enscript to colorer. And please attach your proposal as a unified diff (svn diff) instead of adding the full source code into the description field.

You should not switch to Colorer. I think, it would be better if you support both and allow user to choose. I use Colorer module named enscript.py and placed instead of original enscript.py, because it was just a simplest solution for me, and maybe it helps somebody who use Trac on Windows.

comment:6 by Matthew Good, 18 years ago

Resolution: duplicate
Status: newclosed

It appears that #366 was already opened regarding the use of Colorer. You can compare your implementation to the one in that ticket and attach yours over there.

comment:7 by anonymous, 18 years ago

Resolution: duplicate
Status: closedreopened

#366 does not work with modern Trac distribution, because mimeviewer api was changed

comment:8 by Christian Boos, 18 years ago

Resolution: duplicate
Status: reopenedclosed

In #366, there's a reference to this ticket, and I'll add there an indication that there's an updated patch in this ticket that interested people can use in the interim.

The policy we use here is to close as duplicate the newer tickets in favor of the eldest one, as we don't like to have multiple tickets opened for tracking the same bug or feature. That's better for focusing the discussion in one place.

Modify Ticket

Change Properties
Set your email in Preferences
as closed The owner will remain Jonas Borgström.
The resolution will be deleted. Next status will be 'reopened'.
to The owner will be changed from Jonas Borgström 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.