**EDIT** I edited the code a bit to add a header bar and a box around the code that has a max size and overflows the rest of the code with scrollbars if the code is too long. **END EDIT**
Well, I ended up coming up with my own solution to the problem. I did some research and found that php has a built in function for code highlighting named highlight_string(). My next step was to figure out how I could use it. After racking my brain and trying different things, I ended up creating myself a UDT and named it "sourcecode" for PHP code highlighting. The way the UDT is written it currently only highlights PHP code, but could surely be adapted.
The theory behind the UDT is somewhat of a bbcode style format in that it uses bracketed tags. I decided to model the tags around the standard PHP tags:
[?php
//some php code
$test = "code";
?]
I then just needed some code to parse out the php code blocks and format them. I opted for splitting the text into chunks and passing them to a foreach loop. Then it was just a matter of checking each line of text and formatting it. I suppose I could have figured out a regex to use with preg_replace or something similar, but this seemed to work fine and didn't seem to slow things down too terribly much.
Now, to use the UDT with the Article module, I edited the display_details template line:
<div class="docdetails">{$doc_desc}</div>
to now read:
<div class="docdetails">{sourcecode text="$doc_desc"}</div>
One other thing to note before dishing out the code. I am using the tinyMCE editor. I had to set the encoding of entities to named encoding. The default I believe is raw encoding. This allowed me to add non-breaking spaces to be able to indent my code. Raw encoding will remove non-breaking spaces.
And without further ado, here is the code for the UDT for anyone that wants to use it. I have it fairly well commented so you can see my thought process behind it.
Code: Select all
/**
* CMS Made Simple UDT code for parsing php code and syntax highlighting it
*/
$text = $params['text'];
//Break the text up into manageable blocks of text
$lines = explode("\n", $text);
//Initialize some variables
$in_code = false;
$code = $out = "";
$temp = array();
$count = 0;
//Loop through each line of text
foreach ($lines as $line) {
$line = str_replace("[?PHP", "[?php", $line);
while(!empty($line)) {
//Check the line for a bbcode style bracketed opening php tag "[?php"
if (strpos($line, "[?php") !== false) {
$in_code = true;
$temp = explode("[?php", $line);
$out .= $temp[0];
$line = $temp[1];
$temp = array();
$count = 0;
}
//If we are not in a code block, add the line of text to the output and clear the line
if (!$in_code) {
$out .= $line;
$line = "";
}
//Check if we are in a code block and if the line contains a bbcode style bracketed closing php tag "?]"
if ($in_code && strpos($line, "?]") !== false) {
$in_code = false;
$temp = explode("?]", $line);
$code .= $temp[0];
$lines = count(explode("<br />", $code)) + 4;
//Turn on the output buffer to grab the code block
ob_start();
//Since the highlighter will mess up non-breaking spaces, we need to temporarily replace them with something else
$code = str_replace(" ", "^^", $code);
//Cut out the html line breaks and replace them with "\n" new lines
$code = implode("\n", explode("<br />", $code));
//Strip any remaining html tags and add the beginning and ending php tags
$code = "<?php \n" . strip_tags(trim($code)) . "\n?>";
//Use PHPs built in function to highlight the code
highlight_string($code);
//Get the highlighted code block
$hl_code = ob_get_contents();
//Now put back our non-breaking spaces
$hl_code = str_replace("^^", " ", $hl_code);
$out .= "<div style=\"width: 100%; border: 1px solid gray; background-color: gray; color: white;\"> PHP Code: </div>\n";
$out .= "<div style=\"width: 100%; border: 1px solid gray; height: " . (($lines > 16) ? "16" : $lines) . "em; overflow: auto;\"><div style=\"width: 100%; margin: 1px 4px;\">$hl_code</div></div>\n";
//Clear the output buffer
ob_end_clean();
$code = "";
$line = $temp[1];
$temp = array();
//if we are still in a code block, add the text to the code block
} else if ($in_code) {
$code = (empty($code)) ? $line : $code . $line;
$count ++;
if ($count > 1000) {
$line = "";
$in_code = false;
}
}
}
echo $out;
$out = "";
}
And for those of you that read this far, here is a link to an article I created using the new UDT.
Sample of my new UDT