Jump to content


Code syntax highlighter


11 replies to this topic

#1 .oisyn

    DevMaster Staff

  • Moderators
  • 1842 posts

Posted 13 September 2005 - 07:45 AM

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.

#2 Dia

    DevMaster Staff

  • Administrators
  • 1120 posts

Posted 13 September 2005 - 02:29 PM

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.

#3 .oisyn

    DevMaster Staff

  • Moderators
  • 1842 posts

Posted 14 September 2005 - 09:13 AM

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

#4 Dia

    DevMaster Staff

  • Administrators
  • 1120 posts

Posted 14 September 2005 - 04:36 PM

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.

#5 .oisyn

    DevMaster Staff

  • Moderators
  • 1842 posts

Posted 14 September 2005 - 08:43 PM

This zipfile also contains the flex'ed files: http://www.oisyn.nl/...Highlighter.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

#6 darqSHADOW

    Member

  • Members
  • PipPip
  • 69 posts

Posted 30 September 2005 - 03:12 PM

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:

Posted Image
Posted Image

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

DS

#7 .oisyn

    DevMaster Staff

  • Moderators
  • 1842 posts

Posted 30 September 2005 - 03:16 PM

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
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.

#8 Guest_Unregistered_*

  • Guests

Posted 07 December 2005 - 04:40 PM

Look at Geshi (http://qbnz.com/highlighter/)

#9 monjardin

    Senior Member

  • Members
  • PipPipPipPip
  • 1033 posts

Posted 07 December 2005 - 04:46 PM

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?
monjardin's JwN Meter (1,2,3,4,5,6):
|----|----|----|----|----|----|----|----|----|----|
*

#10 darqSHADOW

    Member

  • Members
  • PipPip
  • 69 posts

Posted 07 December 2005 - 06:37 PM

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

#11 eddie

    Senior Member

  • Members
  • PipPipPipPip
  • 751 posts

Posted 07 December 2005 - 08:18 PM

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.

#12 .oisyn

    DevMaster Staff

  • Moderators
  • 1842 posts

Posted 07 December 2005 - 11:28 PM

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
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users