Code syntax highlighter

340bf64ac6abda6e40f7e860279823cb
0
_oisyn 101 Sep 13, 2005 at 07:45

Hi,

it seems the new interface doesn’t have a code syntax highlighter, and it does weird things to the code as well (like converting multiple tabs to “* “ or something). I made my own highlighter, it’s a PHP module with a function that takes the piece of code, the language and a line number bias as parameters, and colorcodes it accordingly. It can highlight C, C++, Delphi, Java, VB, VB.Net, C# and PHP code. It’s written using C, C++ and flex.

You can try it here: http://www.oisyn.nl/tests/hl.php

Don’t know how extendable vBulletin is, but maybe you can use it? It’s open source btw.

11 Replies

Please log in or register to post a reply.

Fdbdc4176840d77fe6a8deca457595ab
0
dk 158 Sep 13, 2005 at 14:29

Actually, there’s already a php syntax highlighter that was written for the previous forums. So we’ll just have code re-use ;)
But thanks for the sharing your syntax highlighter.

340bf64ac6abda6e40f7e860279823cb
0
_oisyn 101 Sep 14, 2005 at 09:13

No offense, but I didn’t really like the old highlighter also ;). It does weird thing to multiple tabs (converting two tabs to two spaces, while a single tab remains a single tab, it really fucks up the indenting in your code). And there were more issues I can’t remember right now…

Fdbdc4176840d77fe6a8deca457595ab
0
dk 158 Sep 14, 2005 at 16:36

I just took a look at your parser and it looks quite good. The only limitation it has is adding support for new langauges or modifying existing ones would require the modification of the lex files and recompiling. I was hoping for something configurable through a config file or something.

However, for the purposes of this forum, it should be sufficient. I think we can use it, but can you provide me with the generated files from flex so that it doesn’t have to be installed on the server? Thanks.

340bf64ac6abda6e40f7e860279823cb
0
_oisyn 101 Sep 14, 2005 at 20:43

This zipfile also contains the flex’ed files: http://www.oisyn.nl/SyntaxHighlighter.zip
From within PHP, either load the module via dl() or put it in your php.ini, and call the function code_highlight($code, $lang, $line), where $code is the piece of code, $lang the language (“c”, “c++”, “c#”, “vb”, “vb.net”, “delphi”, “java” or “php”) (case insensitive), and $line the line where the counting should start (usually 1). The function returns the string as formatted html. The colorcoded pieces are formatted using html span tags with a class attribute, you can control the actual colors using a stylesheet. I’ve included an example php and css file in the zip.

Let me know if you have any questions or considerations

885a3ffe62609ebdcdecd72365aa7a3b
0
darqSHADOW 101 Sep 30, 2005 at 15:12

We just implemented a fullly configurable highlighter that works with C, C++, VB, VB.Net, C#, and Delphi and includes a fully configurable XML file to set everything up. I did the custom integration into phpBB myself, and it works great (and even uses the build-in caching system). I have a few Regex fixes to implement, but other than that it’s working great. Here’s some shots if it in action:

syntax1.jpg
syntax2.jpg

Dia, if you want to take a look at it, just email me.

DS

340bf64ac6abda6e40f7e860279823cb
0
_oisyn 101 Sep 30, 2005 at 15:16

Hmm, luckily it doesn’t seem to work with the more complex cases to load a script, but it’s flawed nevertheless :)

darqSHADOW: You should definitely use a monospaced font :D

00000000000000000000000000000000
0
Anonymous Dec 07, 2005 at 16:40
6f0a333c785da81d479a0f58c2ccb203
0
monjardin 102 Dec 07, 2005 at 16:46

Since this old thread got bumped, I have a somewhat related question.
.oisyn used flex (the GNU libary I assume?) to write his syntax highlighter. Has anyone here had pleasant experiences with boost::spirit?

885a3ffe62609ebdcdecd72365aa7a3b
0
darqSHADOW 101 Dec 07, 2005 at 18:37

Geshi is a mess. It’s a pain to modify the colors (they don’t match the IDE’s, which is what most people wanted from my system), and the code was a bit bloated for my needs. On top of that, I ran into quite a number of bugs while trying to implement Geshi into phpBB’s bbCode – where-as my highlighter just dropped right now w/o problems.

.oisyn: Yup mono-spaced font was planned, that screenshot was from an initial test run. It is now using a mono-spaced font and it looks beautiful.

DS

Cd577ee1cb56aa2ad5645b7daa0a2830
0
eddie 101 Dec 07, 2005 at 20:18

monjardin:

At the risk of being the forking point on this hijack, I’ve used Spirit. It’s quite elegant, and makes a great use of operator overloading to do Kleene stars and the like.

I haven’t used it for anything more than just playing with it, but I liked it a lot.

340bf64ac6abda6e40f7e860279823cb
0
_oisyn 101 Dec 07, 2005 at 23:28

I’ve looked at spirit, it’s a cool gimmic, but a major downside is that it’s imho not very readable. I rather use a tool such as ANTLR, in which you can specify your scanner, parser and even an AST parser, as it also builds the AST for you. Both the grammar specification and the generated code are really clean and OO, much unlike bison (haven’t tried bison++ though) :P