[web] [ASP] Making [code] tags

Started by
2 comments, last by igni ferroque 18 years, 8 months ago
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]
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 and , 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( "/\/"</span>, $matches[<span class="cpp-number">1</span>], $LangMatch ) )<br> {<br> $Lang = $LangMatch[<span class="cpp-number">1</span>];<br> }<br> <span class="cpp-keyword">else</span><br> {<br> $Lang = <span class="cpp-literal">"VB"</span>;<br> }<br> <br> <span class="cpp-comment">//at this point, html special chars have already been converted, so undo that</span><br> $code = unhtmlspecialchars( $code );<br> <span class="cpp-comment">//$code = str_replace( '\\\\', '\\', $code ); //hack to fix double slashing</span><br> <br> $code = syntax_highlight( $code, $Lang );<br> <br> <span class="cpp-keyword">return</span> <span class="cpp-literal">"&lt;div class='code'&gt;&lt;pre&gt;"</span>.$code.<span class="cpp-literal">"&lt;/pre&gt;&lt;/div&gt;"</span>;<br>}<br><br>function BBCodeParse( $message ) {<br><br> $message = addslashes( $message );<br><br> <span class="cpp-comment">// parse <code></span><br> $message = preg_replace_callback( <span class="cpp-literal">"/(\[code ?.*?\])(.+?)(\[\/code\])/si"</span>, <span class="cpp-literal">"CodeCallback"</span>, $message );<br> <br> <span class="cpp-keyword">return</span> $message;<br>}<br><br><br><br></pre></div><!--ENDSCRIPT--><br><br>(Credit goes partly to the Loudmouth forum software for Mambo, although I've heavily modified their originally...mediocre code.)
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
oh damn, sorry. I was in a rush and forgot to mention I was using ASP. But that reply helps a bit anyway.
Use CSS. Instead of [code], use the HTML tag <code> and set the overflow and height/max-height properties.
Free Mac Mini (I know, I'm a tool)

This topic is closed to new replies.

Advertisement