Ticket #4346 (closed defect: fixed)

Opened 1 year ago

Last modified 1 year ago

php code does not seem to get highlighted

Reported by: izzy Assigned to: chrisheller
Priority: normal Component: IncludeSourcePartialPlugin
Severity: normal Keywords:
Cc: Trac Release: 0.11

Description

While e.g. Python code is highlighted ([[IncludeSource(file.py, start=30, end=38, rev=1)]]), PHP code seems to be handled like a simple plain text file - even if the mime type is explicitly specified. If including the entire file, it works - but as soon as there's no "<?php" string found in the quoted part, there's no highlighting. Could there be anything done about that?

Attachments

Change History

12/31/08 11:22:33 changed by chrisheller

  • status changed from new to assigned.

The macro just uses the mimeview code under the covers, so whatever it is invoking to render the php code is the likely issue.

I see that there is a mimeview/php.py file in Trac that uses the php executable for doing php highlighting. I don't know if that gets used in preference to something like Pygments though. Can you see if that is being used in your install? (the path to the php executable is set in the trac.ini file).

If so, try temporarily disabling that and see if pygments handles this properly.

12/31/08 11:26:29 changed by chrisheller

The other thing that might be possible is a bit of hack, but it might work. The plugin could be modified to check that php source actually begins with whatever string the renderer is looking for in order to make it happy.

03/31/09 19:33:51 changed by chrisheller

I finally did a little experiment with this last night. Adding

#!/usr/bin/php -f
<?

to the beginning of the source when the beginning is trimmed off did do the trick for getting the syntax highlighting back on.

Here's the diff if you want to experiment further.

Index: IncludeSource.py
===================================================================
--- IncludeSource.py	(revision 5438)
+++ IncludeSource.py	(working copy)
@@ -151,6 +151,12 @@
         start, end = kwargs.get('start', None), kwargs.get('end', None)
         if start or end:
             src, start, end = self._handle_partial(src, start, end)
+            
+            # fix up php - if there's no leading shebang line, then render
+            # does not identify it properly
+            if file_name.endswith('.php'):
+                src = '#!/usr/bin/php -f\n<?\n' + src
+
             context.startline = start
 
         mimetype = kwargs.get('mimetype', None)

The main issue with this is that the two added lines needed to get the rendering are included in the output. I think that a Genshi transformation could strip these two lines back out from the render() output, but I didn't have time to look at that.

Once that works, then this can be cleaned up a bit (e.g. handle other php extensions, etc.) and put into the plugin code.

04/04/09 03:44:23 changed by chrisheller

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

Fixed this in changeset:5481 by prepending php lead-in and then stripping the lead-in back out. Not the most efficient way of doing things, but it does the job.

It's also setup that adding other types of files that need this sort of "trick the renderer" behavior can be added relatively easily.


Add/Change #4346 (php code does not seem to get highlighted)




Change Properties
Action