Edgewall Software
Modify

Opened 21 years ago

Closed 18 years ago

Last modified 15 years ago

#366 closed enhancement (wontfix)

Syntax highlighting with Colorer

Reported by: daniel Owned by: Christian Boos
Priority: normal Milestone:
Component: wiki system Version:
Severity: minor Keywords: syntaxcoloring mimeview processor
Cc: rhind@…, techtonik@… Branch:
Release Notes:
API Changes:
Internal Changes:

Description (last modified by Jonas Borgström)

A mimeviewer plugin for Colorer.

See: http://colorer.sourceforge.net/

Attachments (0)

Change History (20)

comment:1 by cmlenz@…, 21 years ago

Depends on Colorer implementing reading its input from stdin:

http://sourceforge.net/forum/forum.php?thread_id=1073629&forum_id=110043

comment:2 by Christopher Lenz, 21 years ago

Milestone: 0.8

Can't target this until the colorer package has incorporated the required features.

comment:3 by anonymous, 20 years ago

Milestone: 0.8
Priority: lowestnormal

why do not use temp file ?
write to temp file then run colorer then delete temp file

comment:4 by anonymous, 20 years ago

Colorer-take5.beta3 supports reading from stdin
command line is

  colorer.exe -h -dc -tphp

where php is sort of file

http://sourceforge.net/project/showfiles.php?group_id=34855&package_id=27292

comment:5 by Jonas Borgström, 20 years ago

Description: modified (diff)
Milestone: 0.80.9

This can wait until 0.9.

comment:6 by anonymous, 20 years ago

Owner: changed from Jonas Borgström to anonymous
Status: newassigned

comment:7 by Nathah Miller <nxmiller@…>, 20 years ago

windows version:

# -*- coding: iso8859-1 -*-
#
# Copyright (C) 2004 Edgewall Software
# Copyright (C) 2004 Nathan Miller <nxmiller@yahoo.com>
#
# Trac is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# Trac is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Author: Nathan Miller <nxmiller@yahoo.com>
#
# Syntax highlighting module, using colorer
#

import re
import sys
import os
import random

from trac.util import NaivePopen

supported_types = [
    (1, 'text/plain',         'txt'),
    (1, 'text/html',          'html'),
    (1, 'text/css',           'css'),
    (1, 'text/xml',           'xml'),
    (1, 'text/xsl',           'xls'),
    (1, 'text/x-perl',        'perl'),
    (1, 'text/x-php',         'php'),
    (1, 'text/x-python',      'py'),
    (1, 'text/x-diff',        'diff'),
    (1, 'text/x-javascript',  'js'),
    (1, 'text/x-csrc',        'c'),
    (1, 'text/x-chdr',        'h'),
    (1, 'text/x-c++src',      'cpp'),
    (1, 'text/x-c++hdr',      'hpp'),
    (1, 'text/x-java',        'java'),
    (1, 'text/x-idl',         'idl'),
    (1, 'text/x-asm',         'asm'),
    (1, 'text/x-sql',         'sql'),
    (1, 'application/x-sh',   'sh'),
    # much more rare languages....
    ]

types = {}
for p,t,s in supported_types:
    types[t] = s


def display(data, mimetype, filename, env):

    try:
        ext = types[mimetype]
    except KeyError:
        raise Exception, "Colorer doesn't support %s" % mimetype

    # if filename is defined, highlight according to filename, not to mimetype 
    if filename is None:
        filename = 'tmp.' + ext

    tmpfname = 'c:/temp/tmp%08X.%s' % (random.randint(0,sys.maxint), filename)

    f = open(tmpfname, 'wb')
    f.write(data)
    f.close()

    cmdline = env.get_config('mimeviewer', 'colorer_path', 'C:\\bin\\Colorer\\bin\\colorer.exe')
    cmdline += ' -h -dc -eiUTF-8 -eoUTF-8 ' + tmpfname
    np = NaivePopen(cmdline)

    os.unlink(tmpfname)

    if np.errorlevel:
        err = 'Running (%s) failed: %s, %s.' % (cmdline, np.errorlevel, np.err)
        raise Exception, err
    odata = re.sub("^\xEF\xBB\xBF","",np.out)

    return '<div class="code-block">' + odata + '</div>'

comment:8 by anonymous, 20 years ago

Owner: changed from anonymous to Jonas Borgström
Status: assignednew

comment:9 by Christopher Lenz, 20 years ago

Keywords: patch added

comment:10 by Christopher Lenz, 20 years ago

Milestone: 0.9

comment:11 by Matthew Good, 18 years ago

#3691 has been marked as a duplicate.

comment:12 by Christian Boos, 18 years ago

Component: generalwiki
Keywords: syntaxcoloring mimeview processor added; patch removed
Milestone: 0.11
Owner: changed from Jonas Borgström to Christian Boos
Severity: normalmajor

I think we should have Colorer support out of the box, as we have for enscript and SilverCity. It's not as if we currently had a bright solution for TracSyntaxColoring… for example we advise to downgrade the SilverCity packages to 0.9.5, and enscript is aging and not practical on Windows.

Ideally, there should be Python bindings for Colorer, as invoking an .exe for each code snippet to be rendered can become costly (as that's currently the case for enscript).

There's also the possibility to provide a cache for code block rendering, that will ideally benefit any renderer, like a md5 hash of (code block content + rendering options) → cached html fragment.

comment:13 by Russell Hind <rhind@…>, 18 years ago

Cc: rhind@… added

in reply to:  11 ; comment:14 by Christian Boos, 18 years ago

Replying to mgood:

#3691 has been marked as a duplicate.

And there's an updated patch there that people can use with 0.10, in the interim.

in reply to:  14 comment:15 by Matthew Good, 18 years ago

Replying to cboos:

Replying to mgood:

#3691 has been marked as a duplicate.

And there's an updated patch there that people can use with 0.10, in the interim.

Well, the "patch" requires some work for people to use since it hardcodes paths to "colorer.exe" and a temp directory, and the creation of temp files is not particularly safe.

comment:16 by Christopher Lenz, 18 years ago

Milestone: 0.11
Resolution: wontfix
Severity: majorminor
Status: newclosed

I see no reason why this shouldn't be implemented using a separate plugin. Ideally, at some point we could even move out the other highlighters into plugins (IMHO).

comment:17 by Christian Boos, 18 years ago

Well, I don't see why the buggy and unsupported enscript should be prefered to the more capable Colorer, so yes, we should remove support for all highlighters altogether.

in reply to:  17 comment:18 by Christopher Lenz, 18 years ago

Replying to cboos:

Well, I don't see why the buggy and unsupported enscript should be prefered to the more capable Colorer, so yes, we should remove support for all highlighters altogether.

I'm not sure whether that's supposed to be sarcastic or something. “Removing support for highlighters" is not what I said, nor what I meant. Note that colorer is a somewhat “exotic” tool that (very?) few people have installed. There are no Python bindings for it, so you need to call it over the command-line (or you could use ctypes I guess). So colorer really isn't the perfect solution for all things syntax highlighting in Trac.

Also note that there's a good reason why we have the enscript, silvercity, and php mimeviewers in the core: history. Those modules were added before we had a plugin system. There was no other way to add highlighters without adding those modules to Trac directly.

comment:19 by Christian Boos, 18 years ago

My suggestion was to move the enscript, silvercity and php mimeviewers out of the core trac.mimeview package. We could possibly still bundle them in a set of "default" plugins (e.g. in this case /tracext/mimeview)

comment:20 by anatoly techtonik <techtonik@…>, 15 years ago

Cc: techtonik@… added

Modify Ticket

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