Existing parser for formating code for forums?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Existing parser for formating code for forums?

Post by JAB Creations »

I'm not exactly sure what the terminology is though how would I go about implementing a code parser for code within a BB code tag similar to how the forums with phpBB here display the code (including various languages such as HTML, CSS, JavaScript, PHP, and MySQL)?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Existing parser for formating code for forums?

Post by Ollie Saunders »

Use a library. Don't write this yourself.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Existing parser for formating code for forums?

Post by jackpf »

I wrote this myself :P

I just use a ton of regex.

My forum uses it for code parsing between

Code: Select all

tags. However, if the user puts

Code: Select all

I just use PHP's native highlight_string() function, since it obviously highlights code better than I can...

However, it doesn't differentiate between languages. It just has a load of common features of most languages, and highlights them. It's mainly based on C style languages.
User avatar
juma929
Forum Commoner
Posts: 72
Joined: Wed Jun 17, 2009 9:41 am

Re: Existing parser for formating code for forums?

Post by juma929 »

Hello,

Just curious as to why you couldnt approach writing this yourself? (Ollie)

:?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Existing parser for formating code for forums?

Post by Eran »

He's not saying you can't, he's saying you shouldn't. It's a waste of time, as it's just a lot of dirty work, and there are plenty of good libraries to take of it. Unless you enjoy those kind of things.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Existing parser for formating code for forums?

Post by JAB Creations »

The first word of the thread's title is existing. :wink: Writing my own BB code to XHTML validator and converter is one thing...styling code markup for five languages is surely quite another! 8O

I did some searching and was unable to figure out how to find any...so that is why I'm asking for some direction as far as finding and comparing existing solutions please?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Existing parser for formating code for forums?

Post by VladSun »

There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Existing parser for formating code for forums?

Post by Eran »

User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Existing parser for formating code for forums?

Post by Weirdan »

Also GeSHi. And our own d11wtq wrote syntax highlighter in JS: Kodify
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Existing parser for formating code for forums?

Post by Ollie Saunders »

pytrin wrote:He's not saying you can't, he's saying you shouldn't. It's a waste of time, as it's just a lot of dirty work, and there are plenty of good libraries to take of it. Unless you enjoy those kind of things.
Yeah, that's right.
The first word of the thread's title is existing.
Didn't see that.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Existing parser for formating code for forums?

Post by JAB Creations »

Thanks a lot guys! I'm checking them out right now! :)

Jack, you wrote your own? Do you have a link available or is it simply implemented in to your site? I'm more liberal with regex in JavaScript then in PHP for obvious reasons.

Speaking of JavaScript I plan on writing my BB code validator in JavaScript too in order to compliment the serverside version and naturally to reduce page requests (just add the function to form validation pretty much). Chris's solution sounds intriguing, I like the idea of highlighted curly brackets on mouseover!

Performance is only one concern...project activity such as addressing bugs and being open to improvements interests me. I might end up going in and adjust some of the code to implement things like bracket highlighting. I'm not really interested in any other languages other then PHP and MySQL (and naturally XHTML, CSS, and JavaScript). Clientside implementation (the BB code tags) is another thing I'll be looking at. In example are people more likely to use {code=html} or {html}? I typically use {html} myself in example. I'm sure for the most part it wouldn't be too difficult to change that in most of these existing solutions.
Post Reply