PHP syntax highlight filter

PHP syntax highlight filter

by Grigory Rubtsov -
Number of replies: 33
Dear developers!

I have written simple php syntax highlight filter, using built-in php function highlight_string(). The filter is useful for PHP language teaching.

The filter highlights php code, embraced by <?php and ?>
The example of highlighted code is:
<?php
if(isset($_GET["a"]) && isset($_GET["a"])) {
echo intval($_GET["a"]) + intval($_GET["b"]);
}
else {
echo "Error";
}
?>


You can download filter from:
http://onlineuniversity.ru/filter/php_filter.zip

For my purpose I need also HTML and SQL syntax highlighter. For this and for many other programming languages I have an idea to embed GeSHi
Then it is possible to create filter interpreting <span syntax="c++"> </span>

If you are interested, please write your opinion
Average of ratings: -
In reply to Grigory Rubtsov

Re: PHP syntax highlight filter

by Grigory Rubtsov -
I finally embedded GeSHi in a simple way.
You can download test version of a filter from:
http://onlineuniversity.ru/filter/geshi_filter.zip

To Use it:
- Enclose your code in <span syntax="lang_name"> </span>
- It can highlight c,asm,bash,cpp,css,lisp,matlab,html4strict,php,pascal,xml and many other languages

This filter is helpful for programming languages teaching.
In reply to Grigory Rubtsov

Re: PHP syntax highlight filter

by David Scotson -

That's very nice.

Is there an option to use the more CSS enabled version that uses classes rather than embedded styles. I think I prefer geshi's numbered list option (using CSS to hide the numbers if you wish) rather than large numbers of br tags, though it appears that my copy of Firefox also cut and pastes the numbers, even though the Geshi docs claim this shouldn't happen.

The blocks, modules and various other parts of Moodle have their own CSS in Moodle 1.5 that can then be overriden by users, is it possible for filters to do the same? It would really cut down the page size (50% less code acording to the geshi docs) when using this filter as well as making it easier to choose your own color scheme to fit with your favourite editor or Moodle theme.

I'd vote for including this on Moodle.org once it's done.

In reply to David Scotson

Re: PHP syntax highlight filter

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
Filters can't (yet) unfortunately, but I will certainly add this to moodle.org once it's a bit more tested - great stuff Grigory!
In reply to Grigory Rubtsov

Re: PHP syntax highlight filter

by Julian Ridden -
This is great work. I am sure it will see good use in our senior computer classes.
In reply to Grigory Rubtsov

An update

by Grigory Rubtsov -
I have updated geshi filter.
http://onlineuniversity.ru/filter/geshi_filter.zip

1. I have utilized line numbers feature of geshi. Now if you put underscore before language name it make lines numbered. For example use <span syntax="_html"> </span> for html syntax with line numbers

2. There is a way of customizing colors (but not supporting some advanced features yet)
- Open /filter/geshi/geshi/contrib/cssgen.php with a web browser
- Select languages and colors you wish and generate stylesheet
- Save stylesheet in you theme directory as geshi.css
- In theme config.php add 'geshi' element to $THEME->sheets array
- In the beginning of filter.php set $CFG->geshifilterexternalcss=true;

Stylesheet for all supported languages in less than 8 kb.
In reply to Grigory Rubtsov

Re: An update

by Manel Lopez -
Hi again

Sorry but i'm wrong. This file don't works

i get some errors when i unselect all and i get another errors when i select something but css is generated.

Best regards
In reply to Grigory Rubtsov

Re: An update on the Geshi filter

by Wouter De Backer -
Yours is a great initiative! Why is your filter not directly available for download from the Moodle.org Download section?
In reply to Wouter De Backer

Re: An update on the Geshi filter

by Grigory Rubtsov -
Thank you! I just don't know how to add to download section.
In reply to Grigory Rubtsov

Re: An update on the Geshi filter

by Wouter De Backer -
Could it be that this has to be done manually by the Big Boss? wink
In reply to Grigory Rubtsov

Re: PHP syntax highlight filter

by Nigel McNie -
Hi everyone smile

I'm the GeSHi developer, working at Catalyst. I've just created a new version of this filter that gives a few improvements:

  • Upgraded GeSHi to 1.0.7.5, and fixed security hole in old filter because of old GeSHi version
  • Added support for extra attributes in the <span> tag, such as "linenumbers" and "urls". For example: <span syntax="java" linenumbers="yes">
  • Fixed bug where empty source caused invalid output (<span syntax="lang"></span> was left in output)
  • Fixed up styles when line numbers are enabled (the size should be more like non-line numbers now, although this is at your browser's option)
  • Included script that generates a full stylesheet for GeSHi, with instructions on how to include it in your theme, so you can use Moodle's stylesheet caching and GeSHi's class output to have much less source output and save you bandwidth.
My thanks to Grigory for his work on the filter, it gave me a good base to start with. Thanks also to the Catalyst team who put up with my silly questions smile

If anyone has any suggestions/bugs, please tell me!

[EDIT: filter attached to this post, but you can download from http://geshi.org/downloads/geshi.zip as well]

NOTE: this was developed against HEAD, although I don't think much has changed from 1.5 to HEAD that will affect this filter, my experience with Moodle totals one day :p. If you successfully install it in 1.5, please tell me smile
NOTE2: Even if you've installed this before, please read the README.txt again, there are new instructions.
In reply to Nigel McNie

Re: PHP syntax highlight filter

by Grigory Rubtsov -
Dear Nigel!

Thank you for what you have done for opensource and moodle communities.

If one is using moodle WYSISYG editor it often changes places of attributes.

So it often occasionally change from:
<span syntax="html" linenumbers="yes">
to:
<span linenumbers="yes" syntax="html">

Therefore, we need also to account for the possibility that additional attributes are placed before syntaxt="".

Looking forward for new versions of GeSHi and filter.

In reply to Grigory Rubtsov

Re: PHP syntax highlight filter

by Nigel McNie -
Thanks for pointing that out, I wondered why I could not get things working very well in the WYSIWYG text area.

I have uploaded a new version that takes this into account, as well as providing for some configuration inside the filter.php file (so you could set line numbers to be on by default for all code for example). Download is the same as in the other post, and it's attached to this post.
In reply to Nigel McNie

Re: PHP syntax highlight filter

by Grigory Rubtsov -
Thank you for BC with the original plugin. To parse non-geshi <span> tags correctly we need to check for empty syntax parameter.

--- filter-orig.php Wed Dec 14 21:59:23 2005
+++ filter.php Wed Dec 14 22:17:59 2005
@@ -93,7 +93,8 @@
//echo 'options as set:';
//geshi_dbg($options);
+ if($options['syntax']=='')
+ return $data[0];
// BC for original plugin
if ('_' == $options['syntax'][0]) {
$options['linenumbers'] = true;


I also wish to suggest a feature. We often want to highlight code inline, e.g. short SQL statements. GeSHi supports only <pre> and <div> variants and both are blocks. It will be great if we will be able to use <span> option (for example, if using filter with parameter inline="yes").
In reply to Grigory Rubtsov

Re: PHP syntax highlight filter

by Nigel McNie -
I have added your patch, but also added to it so that it checks: if the syntax attribute is set but empty then put the contents in a <pre>. If it's not set of course, return it as is.

I have also added support for inline highlighting, using inline="yes".

Download from this post's attachment or the link as normal.
In reply to Nigel McNie

Re: PHP syntax highlight filter

by Nigel McNie -
Again, a new version is available. All I have done is upgraded the included GeSHi to 1.0.7.6.

File attached to this post, or available at the link in the parent.
In reply to Nigel McNie

Re: PHP syntax highlight filter

by Nigel McNie -
New version again: GeSHi upgraded to 1.0.7.8
In reply to Nigel McNie

Re: PHP syntax highlight filter

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
I've installed it on moodle.org smile


/**
* Given a set of timezone records, put them in the database, replacing what is there
*
* @uses $CFG
* @param array $timezones An array of timezone records
*/
function update_timezone_records($timezones) {
/// Given a set of timezone records, put them in the database

global $CFG;

/// Clear out all the old stuff
execute_sql('TRUNCATE TABLE '.$CFG->prefix.'timezone', false);

/// Insert all the new stuff
foreach ($timezones as $timezone) {
insert_record('timezone', $timezone);
}
}


In reply to Martin Dougiamas

Re: PHP syntax highlight filter

by Nigel McNie -
Cool! smile SELECT testing FROM moodle_geshi_filter WHERE installed = 1
In reply to Nigel McNie

Re: PHP syntax highlight filter

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
Thanks for the code, Nigel, nice job! approve

When I fix the modules Database, you should put it in there.
In reply to Martin Dougiamas

Re: PHP syntax highlight filter

by Nigel McNie -
I added it now.

It seems that the WYSIWYG editor plays merry hell with the code though, especially when you edit the post - it seems to be stripping a large part of the whitespace. Nasty...

I will have to look into it some time. I still need to dig through some moodle code first to see how it works though!
In reply to Nigel McNie

Re: PHP syntax highlight filter

by Nicolas Sicard -
Hi !

This filter could be very useful for me, but I can't find how to make it work properly with the WYSIWYG editor. Can you help me ?

thanks

nicolas.
In reply to Grigory Rubtsov

Re: PHP syntax highlight filter

by Sandy Pittendrigh -
I installed and activated the filter and used the span tag as you specified.
My code is highlighted, but indenting is still stripped out.
This is why I wanted a filter in the first place (if condition with
action on next line does not honor the indent).

I fiddled with filter.php a bit.
Still not getting anywhere.
I'll keep trying.

In reply to Grigory Rubtsov

Re: PHP syntax highlight filter

by Andrew McMillan -
That's great, Grigory!

GeSHi is actually written by one of the guys here at Catalyst (he just started working for us) and we'd been thinking of doing something like this already so we'll be happy to help with any questions.

Regards,
Andrew McMillan
In reply to Andrew McMillan

Re: PHP syntax highlight filter

by Grigory Rubtsov -
Thank you, Andrew.

In the future I wish to upgrage filter to GeSHi 1.2 as it looks much more robust (according to demo on website), but I don't know when it is best time to switch.

Best wishes,
Grigory Rubtsov.
In reply to Grigory Rubtsov

Re: PHP syntax highlight filter: options

by Wouter De Backer -
Is there a way to change some options of this filter in the admin interface? I'd like to:
  • turn off the effect that every html tag also becomes a link to the december.com site.
  • turn off the specific font selection when line numbers are shown and default to the browser's choice as is the case with the non-numbered representation of a piece of code.
  • to influence the amount of indentation


In reply to Wouter De Backer

Re: PHP syntax highlight filter: options

by Grigory Rubtsov -
I also have encountered some of bugs you mentioned. These are internal features of geshi. Of course some of bugs can be fixed by hacking inside geshi code, but I think filter should be transfered to upcoming geshi 1.2 which is announced to have more transparent interface.
In reply to Grigory Rubtsov

Re: PHP syntax highlight filter: options

by steven lyons -
I have found that the geshi HTML syntax highlighting doesn't work in Moodle. CSS appears to work.
In reply to Grigory Rubtsov

Re: PHP syntax highlight filter: indentation bug ?

by Wouter De Backer -
Bug ? The second level of indentation is wrong when line numbers are shown.
In reply to Grigory Rubtsov

Re: PHP syntax highlight filter

by hans paule -

What is the current version of geshi for moodle 2.x ?

My output over [code php] ... [/code] is more than shit.

i have uploaded the content from moodle-geshi-filter-0.1.0.zip

in my filter dictionary and have aktivate it. But the result is very bad.

The editor under the tinymce field is "HTML format" is it right ?

Or musst i write in the  HTML Source edit ?

 

Sorry for my bad english.

LG