<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Scott Yang's Playground &#187; Syntax Hilite</title>
	<atom:link href="http://scott.yang.id.au/tag/syntax-hilite/feed/" rel="self" type="application/rss+xml" />
	<link>http://scott.yang.id.au</link>
	<description>Faith, Technology and Randomness in Life, According to Scott</description>
	<lastBuildDate>Thu, 09 Feb 2012 09:01:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>PHP Syntax Hilighting 1.3</title>
		<link>http://scott.yang.id.au/2005/03/php-syntax-hilighting-13/</link>
		<comments>http://scott.yang.id.au/2005/03/php-syntax-hilighting-13/#comments</comments>
		<pubDate>Wed, 09 Mar 2005 10:26:45 +0000</pubDate>
		<dc:creator>scotty</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Syntax Hilite]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://scott.yang.id.au/2005/03/php-syntax-hilighting-13/</guid>
		<description><![CDATA[I&#8217;ve just got the chance to start visiting my old WordPress plugins after upgrading this site to WP 1.5. Syntax highlighting with Enscript is broken, so I&#8217;ve made some changes to get it going again. Here&#8217;s a list of changes: Detect whether we are running under WP 1.5+ to pick the unmangling routine. WP1.5 double [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just got the chance to start visiting my old WordPress plugins after <a href="http://scott.yang.id.au/2005/03/upgraded-to-wp-15/">upgrading this site to WP 1.5</a>. <a href="http://scott.yang.id.au/2004/05/syntax-hilight-enscript/">Syntax highlighting with Enscript</a> is broken, so I&#8217;ve made some changes to get it going again. Here&#8217;s a list of changes:</p>
<ul>
<li>Detect whether we are running under WP 1.5+ to pick the unmangling routine. WP1.5 double quote fix suggested by <a href="http://www.sturmnet.org/blog/archives/2005/02/24/move-to-wordpress-15/">Oliver</a>.</li>
<li>Eliminate the &#8220;Generated by GNU Enscript&#8221; text (suggested by <a href="http://scott.yang.id.au/2004/05/syntax-hilight-enscript/?comment-2861">Alex</a>), which was caused by a regular expression bug.</li>
</ul>
<p>Also I found with WP 1.5, you no longer needs to escape backslashes &#8216;\&#8217; to &amp;#92;.</p>
<ul>
<li>Download <a href="http://scott.yang.id.au/file/php/syntax_hilight-1.3.zip">syntax_hilight-1.3.php</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://scott.yang.id.au/2005/03/php-syntax-hilighting-13/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP Syntax Hilighting 1.2</title>
		<link>http://scott.yang.id.au/2004/08/php-syntax-hilighting-12/</link>
		<comments>http://scott.yang.id.au/2004/08/php-syntax-hilighting-12/#comments</comments>
		<pubDate>Tue, 17 Aug 2004 06:23:32 +0000</pubDate>
		<dc:creator>scotty</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Syntax Hilite]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://scott.yang.id.au/2004/08/php-syntax-hilighting-12/</guid>
		<description><![CDATA[Here is an updated version of my PHP Syntax Hilighting script (standalone or as WordPress plugin). It fixes the issue whether &#60;font /&#62; tag is used by enscript which renders the whole page non-XHTML compliant. A preg_replace() call replaces them with standard compliant &#60;span style="..." /&#62; XHTML code. Download syntax_hilight-1.2.php]]></description>
			<content:encoded><![CDATA[<p>Here is an updated version of my <a href="http://scott.yang.id.au/category/project/syntax-hilite">PHP Syntax Hilighting script</a> (standalone or as WordPress plugin). It fixes the issue whether <code>&lt;font /&gt;</code> tag is used by enscript which renders the whole page non-XHTML compliant. A <code>preg_replace()</code> call replaces them with standard compliant <code>&lt;span style="..." /&gt;</code> XHTML code.</p>
<ul>
<li>Download <a href="http://scott.yang.id.au/file/php/syntax_hilight-1.2.zip">syntax_hilight-1.2.php</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://scott.yang.id.au/2004/08/php-syntax-hilighting-12/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Syntax Highlighting with Enscript in WordPress</title>
		<link>http://scott.yang.id.au/2004/05/syntax-hilight-enscript/</link>
		<comments>http://scott.yang.id.au/2004/05/syntax-hilight-enscript/#comments</comments>
		<pubDate>Sat, 29 May 2004 12:40:51 +0000</pubDate>
		<dc:creator>scotty</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Syntax Hilite]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://scott.yang.id.au/2004/05/syntax-hilight-enscript/</guid>
		<description><![CDATA[One thing that I dislike about WordPress is its default wptexturize filter that messes up with my HTML code. The default behaviour makes code listing very difficult to read, as it attempts to add paragraph tags to &#60;pre /&#62; tags. Here is one of my first attempts in WordPress plugin coding to rectify this issue. [...]]]></description>
			<content:encoded><![CDATA[<p>One thing that I dislike about <a href="http://wordpress.org/">WordPress</a> is its default <code>wptexturize</code> filter that messes up with my HTML code. The default behaviour makes code listing very difficult to read, as it attempts to add paragraph tags to &lt;pre /&gt; tags. Here is one of my first attempts in WordPress plugin coding to rectify this issue.</p>
<p>A plugin needs to be written, that</p>
<ul>
<li>Fixes up the mess <code>wptexturize</code> has left after paragraphing the contents inside &lt;pre /&gt; tag.</li>
<li>Optionally add syntax highlighting using <a href="http://people.ssh.fi/mtr/genscript/">GNU Enscript</a>, a method that I have <a href="2003/01/source-code-syntax-highlighting-in-php/">previously discussed</a>.</li>
<li>Can be used as a module outside WordPress to provide syntax highlighting to source files or code pieces.</li>
</ul>
<p>Result is an updated version syntax highlighter PHP module.</p>
<ul>
<li>Download the latest version: <a href="http://scott.yang.id.au/file/php/syntax_hilight.zip">syntax_hilight.php</a> (1.3 updated on <a href="http://scott.yang.id.au/2005/03/php-syntax-hilighting-13/">9 March 2005</a>)</li>
</ul>
<h3 id="toc-usage">Usage</h3>
<p>To use it with WordPress 1.2/1.5.</p>
<ul>
<li>Save the PHP source file into <code>wp-content/plugins/</code> directory.</li>
<li>Go to &#8220;Plugin Management&#8221; page under your WordPress admin interface.</li>
<li>Activate plugin &#8220;Syntax Highlighter Enscript&#8221;.</li>
</ul>
<p>Inside your HTML code, all your &lt;pre /&gt; tag should now be properly formatted. If you wish to add syntax highlighting to a piece of code, you will need to add <code>lang="..."</code> attribute to your &lt;pre /&gt; tag. For example,</p>
<pre lang="html">
&lt;pre lang="python"&gt;
if __name__ == '__main__':
    print "Hello Word"
&lt;/pre&gt;
</pre>
<p>The final HTML rendering would be:</p>
<pre lang="python">
if __name__ == '__main__':
    print "Hello Word"
</pre>
<p>&#8220;<code>lang</code>&#8221; attribute would need to be supported by GNU Enscript backend. Please check what languages Enscript can support.</p>
<p>To use it outside the context of WordPress, directly as a PHP module, you can call:</p>
<pre lang="php">
$code = 'int main(void) { printf("Hello World&#92;n"); }';
SyntaxHilighter::hilight($code, "c");
</pre>
<p>To highlight a file on the file system:</p>
<pre lang="php">
SyntaxHilighter::hilght_file('foobar.pl', 'perl');
</pre>
<p>There are still some unresolved problems:</p>
<ul>
<li>Not everyone can run enscript, especially if you are jailed inside PHP safe mode. Well, too bad. Find another host :)</li>
<li>Line numbering &#8211; it should be my next todo item.</li>
<li>Backslashes are stripped after Save, which makes code listing a challenge. Besides escaping characters like &#8216;&lt;&#8217;, &#8216;&gt;&#8217; and &#8216;&amp;&#8217;, I also need to escape backslashes to &#8216;&amp;#92;&#8217;. Looks like a WordPress bug to me.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://scott.yang.id.au/2004/05/syntax-hilight-enscript/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
		<item>
		<title>Source Code Syntax Highlighting in PHP</title>
		<link>http://scott.yang.id.au/2003/01/source-code-syntax-highlighting-in-php/</link>
		<comments>http://scott.yang.id.au/2003/01/source-code-syntax-highlighting-in-php/#comments</comments>
		<pubDate>Fri, 03 Jan 2003 05:48:37 +0000</pubDate>
		<dc:creator>scotty</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Syntax Hilite]]></category>

		<guid isPermaLink="false">http://scott.yang.id.au/2003/01/source-code-syntax-highlighting-in-php/</guid>
		<description><![CDATA[They said programming is an art, and syntax highlighting of source code really makes them look artistic. I have some little scripts that I would to publish on the net, and I would like to add some colour before posting in my blog. PHP has this function show_source() that can syntax highlight a file, however [...]]]></description>
			<content:encoded><![CDATA[<p>They said programming is an art, and <b>syntax highlighting</b> of source code really makes them look artistic.</p>
<p>I have some little scripts that I would to publish on the net, and I would like to add some colour before posting in my blog. PHP has this function <a href="http://www.php.net/manual/en/function.show-source.php">show_source()</a> that can syntax highlight a file, however it only works on PHP files. Instead of writing parsers and generators for all the programming languages known to mankind out there, I opt for something already been written &#8211; <a href="http://people.ssh.fi/mtr/genscript/">GNU Enscript</a>.</p>
<p>I know that Enscript is mainly use to generate Postscript files, but in a tool which I&#8217;ve used before, <a href="http://viewcvs.sourceforge.net/">ViewCVS</a>, I saw how it calls Enscript to generate syntax highlighted source code listing. It then took me a few minutes to hack up a wrapper for it in PHP&#8230;</p>
<ul>
<li>Download the source file: <a href="http://scott.yang.id.au/file/php/syntax_hilight.php">syntax_hilight.php</a></li>
</ul>
<p>This is an demonstration of how to use it.</p>
<pre lang="php">
echo syntax_hilight('syntax_hilight.php');
</pre>
<h3 id="toc-source-code">Source Code</h3>
<pre lang="php">
/**
 * Syntax hilighting a program source file. It calls enscript(1) to parse and
 * insert HTML tags to produce syntax hilighted version of the source. Since
 * the version of enscript I have does not support PHP hilighting, I will use
 * PHP's show_source() if the source file ends with .php.
 *
 * @param  $filename The filename of the source file to be transformed.
 * @return A text string containing syntax hilighting version of the source,
 *         in HTML.
 */
function syntax_hilight($filename) {
    if ((substr($filename, -4) == '.php')) {
        ob_start();
        show_source($filename);
        $buffer = ob_get_contents();
        ob_end_clean();
    } else {
        $argv = '-q -p - -E --language=html --color '.escapeshellcmd($filename);
        $buffer = array();

        exec("enscript $argv", $buffer);

        $buffer = join("&#92;n", $buffer);
        $buffer = eregi_replace('^.*&lt;PRE&gt;',  '&lt;pre&gt;',  $buffer);
        $buffer = eregi_replace('&lt;/PRE&gt;.*$', '&lt;/pre&gt;', $buffer);
    }

    // Making it XHTML compatible.
    $buffer = eregi_replace('&lt;FONT COLOR="', '&lt;span style="color:', $buffer);
    $buffer = eregi_replace('&lt;/FONT&gt;', '&lt;/style&gt;', $buffer);

    return $buffer;
}
</pre>
<p><b>Updated</b>: This has been superseded by the <a href="http://scott.yang.id.au/2004/05/syntax-hilight-enscript/">newer version</a> that also can acts as a WordPress plugin.</p>
]]></content:encoded>
			<wfw:commentRss>http://scott.yang.id.au/2003/01/source-code-syntax-highlighting-in-php/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

