Edgewall Software
Modify

Opened 10 years ago

Closed 8 years ago

Last modified 4 years ago

#366 closed enhancement (wontfix)

Syntax highlighting with Colorer

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

Description (last modified by jonas)

A mimeviewer plugin for Colorer.

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

Attachments (0)

Change History (20)

comment:1 Changed 10 years ago by cmlenz@…

Depends on Colorer implementing reading its input from stdin:

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

comment:2 Changed 10 years ago by cmlenz

  • Milestone 0.8 deleted

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

comment:3 Changed 10 years ago by anonymous

  • Milestone set to 0.8
  • Priority changed from lowest to normal

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

comment:4 Changed 10 years ago by anonymous

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 Changed 9 years ago by jonas

  • Description modified (diff)
  • Milestone changed from 0.8 to 0.9

This can wait until 0.9.

comment:6 Changed 9 years ago by anonymous

  • Owner changed from jonas to anonymous
  • Status changed from new to assigned

comment:7 Changed 9 years ago by Nathah Miller <nxmiller@…>

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 Changed 9 years ago by anonymous

  • Owner changed from anonymous to jonas
  • Status changed from assigned to new

comment:9 Changed 9 years ago by cmlenz

  • Keywords patch added

comment:10 Changed 9 years ago by cmlenz

  • Milestone 0.9 deleted

comment:11 follow-up: Changed 8 years ago by mgood

#3691 has been marked as a duplicate.

comment:12 Changed 8 years ago by cboos

  • Component changed from general to wiki
  • Keywords syntaxcoloring mimeview processor added; patch removed
  • Milestone set to 0.11
  • Owner changed from jonas to cboos
  • Severity changed from normal to major

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 Changed 8 years ago by Russell Hind <rhind@…>

  • Cc rhind@… added

comment:14 in reply to: ↑ 11 ; follow-up: Changed 8 years ago by 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.

comment:15 in reply to: ↑ 14 Changed 8 years ago by mgood

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 Changed 8 years ago by cmlenz

  • Milestone 0.11 deleted
  • Resolution set to wontfix
  • Severity changed from major to minor
  • Status changed from new to closed

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 follow-up: Changed 8 years ago by 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.

comment:18 in reply to: ↑ 17 Changed 8 years ago by cmlenz

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 Changed 8 years ago by cboos

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 Changed 4 years ago by anatoly techtonik <techtonik@…>

  • Cc techtonik@… added

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed The owner will remain cboos.
The resolution will be deleted. Next status will be 'reopened'.
to The owner will be changed from cboos to the specified user.
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.