Edgewall Software

Ticket #366 (closed enhancement: wontfix)

Opened 5 years ago

Last modified 2 years ago

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@…

Description (last modified by jonas) (diff)

A mimeviewer plugin for Colorer.

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

Attachments

Change History

  Changed 5 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

  Changed 4 years ago by cmlenz

  • milestone 0.8 deleted

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

  Changed 4 years ago by anonymous

  • priority changed from lowest to normal
  • milestone set to 0.8

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

  Changed 4 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

  Changed 4 years ago by jonas

  • description modified (diff)
  • milestone changed from 0.8 to 0.9

This can wait until 0.9.

  Changed 4 years ago by anonymous

  • owner changed from jonas to anonymous
  • status changed from new to assigned

  Changed 4 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>'

  Changed 4 years ago by anonymous

  • owner changed from anonymous to jonas
  • status changed from assigned to new

  Changed 4 years ago by cmlenz

  • keywords patch added

  Changed 4 years ago by cmlenz

  • milestone 0.9 deleted

follow-up: ↓ 14   Changed 2 years ago by mgood

#3691 has been marked as a duplicate.

  Changed 2 years ago by cboos

  • keywords syntaxcoloring mimeview processor added; patch removed
  • owner changed from jonas to cboos
  • component changed from general to wiki
  • severity changed from normal to major
  • milestone set to 0.11

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.

  Changed 2 years ago by Russell Hind <rhind@…>

  • cc rhind@… added

in reply to: ↑ 11 ; follow-up: ↓ 15   Changed 2 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.

in reply to: ↑ 14   Changed 2 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.

  Changed 2 years ago by cmlenz

  • status changed from new to closed
  • resolution set to wontfix
  • severity changed from major to minor
  • milestone 0.11 deleted

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).

follow-up: ↓ 18   Changed 2 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.

in reply to: ↑ 17   Changed 2 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.

  Changed 2 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)

Add/Change #366 (Syntax highlighting with Colorer)

Author



Change Properties
<Author field>
Action
as closed
Next status will be 'reopened'
to The owner will change from cboos. Next status will be 'closed'
 
Note: See TracTickets for help on using tickets.