March 30, 2004

PHP Pigmentator source


<?php 
$uploaded = $_FILES['my_text']['name'];
if (!$uploaded){
echo '
<div class="phpcode">
PHP\'s <a href="http://www.php.net/manual/en/function.highlight-file.php">
highlight_file </a> function is a quick and dirty way to <span class="comment">
achieve  </span> <span class="default"> basic </span> <span class="keyword">
syntax </span><span class="string"> coloring</span>, which can make your code a
lot easier to read. The dirty part is its egregious use of icky invalid (at
least for XHTML) font tags. 
    <br />
    <br />
Some of the user contributed notes explain various methods for fixing this.
I\'ve cobbled together a few of these and tweaked them to get rid of the 
non-breaking spaces, which seem to make a mess of copying and pasting the code. 
Us lazy people hate to type when we don\'t have to. The downside is that we have 
to use a &lt;pre&gt; tag, so it won\'t wrap nicely. If you don\'t like really
wide pages, then you\'d better hard wrap your code before you feed it to the 
colorizer.
    <br />
    <br />
If you feed this beast some code, it will spit it back to you in two versions.
First, colorized and rendered in the browser, and then as raw XHTML in a text 
box, ready to be copied and pasted into a page. At the bottom of the page is 
the CSS style declaration used to colorize the spans produced by this script.
    <br />
    <br />
    <br />
<form enctype="multipart/form-data" action="';
echo $_SERVER["PHP_SELF"];
echo'" method="post">
    <div>
    <input type="hidden" name="MAX_FILE_SIZE" value="40000" />
    <input name="my_text" type="file" /> 
    <br />
    <input type="submit" value="Feed Me!" /> 
    </div>
</form>
</div>';
}
else
{
echo "<h2>highlighted source of: $uploaded </h2> <ins><hr /></ins>";
// Use class names instead of colors
ini_set('highlight.comment', 'comment');
ini_set('highlight.default', 'default');
ini_set('highlight.keyword', 'keyword');
ini_set('highlight.string',  'string');
ini_set('highlight.html',    'html');

// Highlight PHP code
function highlight_php($code, $return = FALSE)
{
    // Using OB, as highlight_string() only supports
    // returning the result from 4.2.0
    ob_start();
    highlight_string($code);
    $highlighted = ob_get_contents();
    ob_end_clean();   
    // Fix output to use CSS classes and wrap well
    // Hacked by Hal to get rid of ugly nbsps
    $highlighted = '<div class="phpcode">' . str_replace(
        array(
            '<code>',
            '</code>',
            'style="color: ',
            '<br />',
            '&nbsp;'
        ),
        array(
            "<pre><code>",
            "</code></pre>",
            'class="',
            "\n",
            ' '
        ),
        $highlighted
    ) . '</div>';
    
    if ($return) { return $highlighted; }
    else { echo $highlighted; }
}

ob_start();
@highlight_php(join("", file($_FILES['my_text']['tmp_name'])));
$highlighted2 = ob_get_contents();
ob_end_clean();

echo $highlighted2;

if ($uploaded){
echo '<h2>Encoded XHTML:</h2><textarea name="my_text" rows="25" cols="55" id="my_text">';
echo htmlentities($highlighted2);
echo '</textarea>';
}
}
echo '<br /><h2>CSS style declaration</h2>
<div class="phpcode">
<span class="html">div.phpcode span.html { color: #000; }</span><br />
<span class="comment">div.phpcode span.comment { color: #f80; }</span><br />
<span class="default">div.phpcode span.default { color: #00b; }</span><br />
<span class="keyword">div.phpcode span.keyword { color: #070; }</span><br />
<span class="string">div.phpcode span.string { color: #d00; }</span><br />
<br /></div>';
?>

Posted by Hal Eckhart at March 30, 2004 03:58 PM | TrackBack
Comments

We'll be using this to show source of our open source scripts. Thank You!

Posted by: Commnetivity at February 23, 2010 01:10 AM
Post a comment
spam and annoying comments will be deleted







Remember personal info?




enter this number security code in the box below (prevents comment spam)