Jump to content
  • Advertisement
Sign in to follow this  
boolean

[web] [ASP] Making [code] tags

This topic is 4849 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a page that, at some points, will be displaying a lot of code (just like here on the forums). The GDnet forums get around the problem of having code being the cause of the page going for 500 lines when large chunks of code are pasted by inserting them into handy dandy forms
Just like this



I am just wondering how I would go about doing something like this. I am not at home at the moment, so I can't test it, but would doing something like a Replace(item_description, [ source], <form>) or however it is that works do the trick? Anyone have any experience setting these up? Added ASP tag to title [Edited by - boolean on August 8, 2005 9:25:06 PM]

Share this post


Link to post
Share on other sites
Advertisement
Are you working in PHP? If so, do what I did and simply steal the paste code that rafb.net uses. The link to the source is at the bottom; it's BSD licensed, so no worries. It simply generates a couple span tags around the things to be highlighted/colored, with classes. Define those classes in your CSS, and you're good to go.

In terms of recognizing text in between [ code ] and [ /code ], it's a simpel matter of a preg_match (regex match).
This is the code I use on some forums to do it:

function CodeCallback( $matches )
{
//the processing has injected br tags into newlines
//we need to strip then out because we're using pre tags
$code = preg_replace( "/(<br.?\/>)/", "\n", $matches[2] );

//determine language
if( preg_match( "/\[code lang=(.+?)\]/", $matches[1], $LangMatch ) )
{
$Lang = $LangMatch[1];
}
else
{
$Lang = "VB";
}

//at this point, html special chars have already been converted, so undo that
$code = unhtmlspecialchars( $code );
//$code = str_replace( '\\\\', '\\', $code ); //hack to fix double slashing

$code = syntax_highlight( $code, $Lang );

return "<div class='code'><pre>".$code."</pre></div>";
}

function BBCodeParse( $message ) {

$message = addslashes( $message );

// parse
$message = preg_replace_callback( "/(\[code ?.*?\])(.+?)(\[\/code\])/si", "CodeCallback", $message );

return $message;
}





(Credit goes partly to the Loudmouth forum software for Mambo, although I've heavily modified their originally...mediocre code.)

Share this post


Link to post
Share on other sites
oh damn, sorry. I was in a rush and forgot to mention I was using ASP. But that reply helps a bit anyway.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!