Edgewall Software

Opened 14 years ago

Closed 14 years ago

Last modified 5 years ago

#984 closed enhancement (fixed)

Allow PHP files to be syntax highlighted (possible patch included)

Reported by: k-fish@… Owned by: Christian Boos
Priority: normal Milestone: 0.9
Component: version control/browser Version: 0.9.6
Severity: normal Keywords: highlight enscript php
Cc: quinn@… Branch:
Release Notes:
API Changes:


I just started using trac, after having looked at it for a while. The current project is in PHP, but enscript can't highlight PHP source. So I came up with a pretty lame solution for this :) trac/mimeviewers/enscript.py needs to be changed for this:

  1. (2, 'text/x-php', 'php'), has to be added to supported_types
  1. Change display to look like this:

if lang == 'php':

enscript = 'php -s'


enscript = env.get_config('mimeviewer', 'enscript_path', 'enscript') enscript += ' —color -h -q —language=html -p - -E' if lang:

enscript += lang

Then PHP files get highlighted by PHP itself. This isn't very clean, but… it works here.

Attachments (1)

php.py (627 bytes ) - added by cboos@… 14 years ago.
The php mimeviewer (uses php -s to highlight PHP code)

Download all attachments as: .zip

Change History (18)

comment:1 by Christopher Lenz, 14 years ago

Resolution: worksforme
Status: newclosed
  1. SilverCity supports PHP, so you could just go with that.
  2. If you want to use PHP itself to highlight PHP files, you really shouldn't messing with the enscript mimeviewer, but rather make a separate mimeviewer that only supports PHP files.

comment:2 by cboos@…, 14 years ago

Keywords: highlight added; hightlight removed

The attachment:php.py is an attempt to do what you did a little bit more cleanly. Simply drop it in the trac/mimeviewers/ folder, and reinstall.

by cboos@…, 14 years ago

Attachment: php.py added

The php mimeviewer (uses php -s to highlight PHP code)

comment:3 by (none), 14 years ago

Resolution: worksforme
Status: closedreopened

TracSyntaxColoring says enscript does PHP as well. For me it does not do it. Mistake on the Wiki page?

Should the patch by cboos go to MacroBazaar ? I think this would be useful.

comment:4 by david@…, 14 years ago

For me 'php -s' don't work inside trac; from shell it is ok, however from trac it ignore -s switch and wants to interpret trac cgi like php source. Any hints? Thanks

comment:5 by david@…, 14 years ago

Update: My problem was in environment variables, php thought it should execute trac.cgi. What helped me was enscript hack with

enscript = '/usr/bin/env -i /usr/bin/php -s -q'

The attached file php.py didn't work, it seems it's not even called from upside…

comment:6 by quinn@…, 14 years ago

I've tried the attached php.py script in my installation but it doesn't do anything. I'm not familiar with adding mimeviewers, or with python at all for that matter. Here were my steps.

  1. copy php.py to trac/mimeviewers/ in my source directory.
  2. run 'python ./setup.py install -f

Then nothing! Please help.

comment:7 by Christian Boos, 14 years ago

Resolution: fixed
Status: reopenedclosed

Sorry, my fault: the Simply drop it in the trac/mimeviewers/ folder, and reinstall. comment was misleading: the source:trunk/trac/mimeviewers/__init__.py file should be modified too. Anyway, now it's done in [1500].

comment:8 by dna, 14 years ago

Component: generalbrowser
Milestone: 0.9
Resolution: fixed
Status: closedreopened
Version: 0.8devel

Just updated to changeset 1501, but trac uses SilverCity

19:11:35 Trac[cache] DEBUG: Checking whether sync with repository is needed
19:11:35 Trac[silvercity] DEBUG: type: text/x-php

And a new bug is inside. Try downloading a php file in original format (browser/trunk/file.php?rev=&format=raw) then following error is detected:

Traceback (most recent call last):
  File "/usr/lib/python2.3/site-packages/trac/web/modpython_frontend.py", line 237, in handler
    dispatch_request(mpr.path_info, mpr, env)
  File "/usr/lib/python2.3/site-packages/trac/web/main.py", line 311, in dispatch_request
  File "/usr/lib/python2.3/site-packages/trac/Browser.py", line 83, in render
    self.render_file(req, repos, node, rev)
  File "/usr/lib/python2.3/site-packages/trac/Browser.py", line 153, in render_file
    req.send_header('Content-Type', node.content_type)
  File "/usr/lib/python2.3/site-packages/trac/web/modpython_frontend.py", line 101, in send_header
    self.req.content_type = value
TypeError: content_type must be a string

comment:9 by Christian Boos, 14 years ago

trac uses SilverCity over PHP highlighting, yes. That's not a big deal to change (there's a comment in the source that shows what to do), but I have no idea what the PHP people would prefer. The PHP highlighting doesn't highlight the HTML at all, while SilverCity does, so for one, I would prefer SilverCity.

Maybe the right thing to do would be to provide links to alternate highlighting, besides the links to alternate formats?

As for the other problem you report, I can't reproduce it (I'm exclusively using tracd).

Does the problem happen exclusively with PHP files when the PHP highlighting is performed, or is it more general?

comment:10 by dna, 14 years ago

Resolution: worksforme
Status: reopenedclosed

Ok, PHP highlighting works for me now. I think you should add the path to the php interpreter and a switch for php highlighting in the trac.ini

The bug is more general, tested on txt and html files too. I'll open a new ticket for that.

comment:11 by quinn@…, 14 years ago

Cc: quinn@… added
Resolution: worksforme
Status: closedreopened

php.py still doesn't work for me. I've updated init.py as cboos suggests. I checked [1500] and noticed php.py has evolved further than the file attached to this ticket. But when I install the one from [1500] an error occurs because I don't have trac.util.

This is getting all very complicated and I sure I'm missing something important. I'm only using php files in my project and don't need something as complex as enscrypt (which doesn't appear to support php anyways) or silvercity. Will php.py will work without these?

comment:12 by Christian Boos, 14 years ago

You can't just take the newest php.py without also upgrading the rest… I would suggest that you grab the latest trunk version and retry from that point.

Note that if you have SilverCity installed, it will take precedence over PHP highlighting. Either uninstall SilverCity, or change the precedence in the php.py file:

# Hint: Use '0' precedence if you want to override SilverCity
supported_types = [
    (4, 'text/x-php', 'php'),
    (4, 'application/x-httpd-php'),
    (4, 'application/x-httpd-php4'),
    (4, 'application/x-httpd-php1'),

comment:13 by dna, 14 years ago

you should add 2 new settings to the trac.ini

  • php_path = /usr/local/bin/php
  • php_highlight_enable = true|false

then it's usable for DAU's

comment:14 by anonymous, 14 years ago

Owner: changed from Jonas Borgström to Christian Boos
Status: reopenednew

comment:15 by Christian Boos, 14 years ago

Status: newassigned

Configuring the highlighting is currently quite complex, I agree. But that's not only an issue for PHP, it's a generic one.

comment:16 by dna, 14 years ago

Resolution: fixed
Status: assignedclosed

fixed in [1592]

add to trac.ini

php_path = /path/to/php

comment:17 by pcook@…, 13 years ago

Version: devel0.9.6

One comment for people having problems: in version 9.6, I found that adding the switch "-q" after my php_path ini setting made syntax highlighting work. The hint was a line logged on DEBUG log level that said: "TracError: You appear to be using the PHP CGI binary. Trac requires the CLI version for syntax highlighting."

So in trac.ini, I use:

php_path = /usr/bin/php -q

Modify Ticket

Change Properties
Set your email in Preferences
as closed The owner will remain Christian Boos.
The resolution will be deleted.
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.