Edgewall Software

Ticket #984 (closed enhancement: fixed)

Opened 4 years ago

Last modified 2 years ago

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

Reported by: k-fish@… Owned by: cboos
Priority: normal Milestone: 0.9
Component: version control/browser Version: 0.9.6
Severity: normal Keywords: highlight enscript php
Cc: quinn@…

Description

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

2. Change display to look like this:

if lang == 'php':

enscript = 'php -s'

else:

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

php.py (0.6 KB) - added by cboos@… 4 years ago.
The php mimeviewer (uses php -s to highlight PHP code)

Change History

Changed 4 years ago by cmlenz

  • status changed from new to closed
  • resolution set to worksforme
  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.

Changed 4 years ago by cboos@…

  • keywords highlight enscript added; hightlight, enscript, 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.

Changed 4 years ago by cboos@…

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

Changed 4 years ago by anonymous

  • status changed from closed to reopened
  • resolution worksforme deleted

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.

Changed 4 years ago by david@…

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

Changed 4 years ago by david@…

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

Changed 4 years ago by quinn@…

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.

Changed 4 years ago by cboos

  • status changed from reopened to closed
  • resolution set to fixed

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

Changed 4 years ago by dna

  • status changed from closed to reopened
  • resolution fixed deleted
  • version changed from 0.8 to devel
  • component changed from general to browser
  • milestone set to 0.9

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
    module.render(req)
  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

Changed 4 years ago by cboos

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?

Changed 4 years ago by dna

  • status changed from reopened to closed
  • resolution set to worksforme

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.

Changed 4 years ago by quinn@…

  • cc quinn@… added
  • status changed from closed to reopened
  • resolution worksforme deleted

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?

Changed 4 years ago by cboos

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'),
    ]
...

Changed 4 years ago by dna

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

Changed 4 years ago by anonymous

  • owner changed from jonas to cboos
  • status changed from reopened to new

Changed 4 years ago by cboos

  • status changed from new to assigned

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

Changed 4 years ago by dna

  • status changed from assigned to closed
  • resolution set to fixed

fixed in [1592]

add to trac.ini

[mimeviewer]
php_path = /path/to/php

Changed 2 years ago by pcook@…

  • version changed from devel to 0.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

Add/Change #984 (Allow PHP files to be syntax highlighted (possible patch included))

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.