<?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>Hacking so Existing &#187; Wordpress</title>
	<atom:link href="http://dev.zhourenjian.com/blog/category/wordpress/feed" rel="self" type="application/rss+xml" />
	<link>http://dev.zhourenjian.com/blog</link>
	<description>Zhou Renjian&#039;s Development Blog</description>
	<lastBuildDate>Fri, 23 Apr 2010 01:54:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Add Source Highlighting to WordPress Editor</title>
		<link>http://dev.zhourenjian.com/blog/2007/06/09/add-source-highlighting-to-wordpress-editor.html</link>
		<comments>http://dev.zhourenjian.com/blog/2007/06/09/add-source-highlighting-to-wordpress-editor.html#comments</comments>
		<pubDate>Sat, 09 Jun 2007 08:29:45 +0000</pubDate>
		<dc:creator>Zhou Renjian</dc:creator>
				<category><![CDATA[Blog Tips]]></category>
		<category><![CDATA[Java2Script]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://dev.zhourenjian.com/blog/2007/06/09/add-source-highlighting-to-wordpress-editor.html</guid>
		<description><![CDATA[If you are a technical geek, you may want to write some blog articles attaching some sources for discussion. And if you are using WordPress, you may find a lot of difficulties in struggling with WordPress&#8217; messy TinyMCE editor in &#8230; <a href="http://dev.zhourenjian.com/blog/2007/06/09/add-source-highlighting-to-wordpress-editor.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>If you are a technical geek, you may want to write some blog articles attaching some sources for discussion. And if you are using WordPress, you may find a lot of difficulties in struggling with WordPress&#8217; messy TinyMCE editor in preserving codes format.</p>
<p>Thanks for early WordPress bloggers, there are some plugins trying to solve this problem. A known plugin is &#8220;<a href="http://www.coffee2code.com/archives/2005/03/29/plugin-preserve-code-formatting/">Preserve Code Formatting</a>&#8220;. But that plugin may be not good enough for you, at least not good enough for me.</p>
<p>I modify that plugin a little, and deploy it in my WordPress 2.2. And I also add my own source syntax highlighting tools.</p>
<p>Here is my modified preserve-code-formatting.php:</p>
<blockquote class="code"><p><code><span class="tag">&lt;</span>?php<br />
<span class="comment">/*<br />
Copyright (c) 2004-2005 by Scott Reilly (aka coffee2code)</p>
<p>Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation<br />
files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,<br />
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the<br />
Software is furnished to do so, subject to the following conditions:</p>
<p>The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.</p>
<p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES<br />
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE<br />
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR<br />
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.<br />
*/</span></p>
<p><span class="keyword">function</span> c2c_prep_code( $text ) <span class="scope">{</span><br />
    $use_nbsp_<span class="keyword">for</span>_spaces = <span class="keyword">false</span>;</p>
<p>    $text = preg_replace(<span class="string">&quot;/(\r\n|\n|\r)/&quot;</span>, <span class="string">&quot;\n&quot;</span>, $text);<br />
    $text = preg_replace(<span class="string">&quot;/\n\n+/&quot;</span>, <span class="string">&quot;\n\n&quot;</span>, $text);<br />
    $text = str_replace(array(<span class="string">&quot;&amp;#36&amp;;&quot;</span>, <span class="string">&quot;&amp;#39&amp;;&quot;</span>), array(<span class="string">&quot;$&quot;</span>, <span class="string">&quot;'&quot;</span>), $text);<br />
    <span class="comment">//$text = htmlspecialchars($text, ENT_QUOTES);</span><br />
    $text = preg_replace(<span class="string">&quot;/\n\s+\n/&quot;</span>, <span class="string">&quot;\n&amp;#160;\n&quot;</span>, $text);<br />
    $text = str_replace(<span class="string">&quot;\t&quot;</span>, <span class="string">&apos;    &apos;</span>, $text);<br />
    <span class="keyword">if</span> ($use_nbsp_<span class="keyword">for</span>_spaces)  $text = str_replace(<span class="string">&apos;  &apos;</span>, <span class="string">&apos;&amp;#160;&amp;#160;&apos;</span>, $text);<br />
    <span class="comment">// Change other special characters before wptexturize() gets to them</span><br />
    $text = c2c_anti_wptexturize($text);<br />
    <span class="comment">//$text = nl2br($text);</span><br />
    <span class="keyword">return</span> $text;<br />
<span class="scope">}</span> <span class="comment">//end c2c_prep_code()</span></p>
<p><span class="comment">// This short-circuits wptexturize process by making ASCII substitutions before wptexturize sees the text</span><br />
<span class="keyword">function</span> c2c_anti_wptexturize( $text ) <span class="scope">{</span><br />
    $text = str_replace(<span class="string">&apos;---&apos;</span>, <span class="string">&apos;&amp;#45;&amp;#45;-&apos;</span>, $text);<br />
    $text = str_replace(<span class="string">&apos;--&apos;</span>, <span class="string">&apos;&amp;#45;-&apos;</span>, $text);<br />
    $text = str_replace(<span class="string">&apos;...&apos;</span>, <span class="string">&apos;&amp;#46;..&apos;</span>, $text);<br />
    $text = str_replace(<span class="string">&apos;``&apos;</span>, <span class="string">&apos;&amp;#96;`&apos;</span>, $text);</p>
<p>    <span class="comment">// This is a hack, look at this more later. It works pretty well though.</span><br />
    $cockney = array(<span class="string">&quot;'tain't&quot;</span>,<span class="string">&quot;'twere&quot;</span>,<span class="string">&quot;'twas&quot;</span>,<span class="string">&quot;'tis&quot;</span>,<span class="string">&quot;'twill&quot;</span>,<span class="string">&quot;'til&quot;</span>,<span class="string">&quot;'bout&quot;</span>,<span class="string">&quot;'nuff&quot;</span>,<span class="string">&quot;'round&quot;</span>);<br />
    $cockneyreplace = array(<span class="string">&quot;&amp;#39;tain&amp;#39;t&quot;</span>,<span class="string">&quot;&amp;#39;twere&quot;</span>,<span class="string">&quot;&amp;#39;twas&quot;</span>,<span class="string">&quot;&amp;#39;tis&quot;</span>,<span class="string">&quot;&amp;#39;twill&quot;</span>,<span class="string">&quot;&amp;#39;til&quot;</span>,<span class="string">&quot;&amp;#39;bout&quot;</span>,<span class="string">&quot;&amp;#39;nuff&quot;</span>,<span class="string">&quot;&amp;#39;round&quot;</span>);<br />
    $text = str_replace($cockney, $cockneyreplace, $text);</p>
<p>    $text = preg_replace(<span class="string">&quot;/'s/&quot;</span>, <span class="string">&apos;&amp;#39;s&apos;</span>, $text);<br />
    $text = preg_replace(<span class="string">&quot;/'(\d\d(?:&amp;#8217;|')?s)/&quot;</span>, <span class="string">&quot;&amp;#39;$1&quot;</span>, $text);<br />
    $text = preg_replace(<span class="string">&apos;/(\s|\A|&quot;)\&apos;</span>/<span class="string">&apos;, &apos;</span>$1&amp;#39;', $text);<br />
<span class="comment">//	$text = preg_replace('/(\d+)"/', '$1&quot;', $text);</span><br />
    $text = preg_replace(<span class="string">&quot;/(\d+)'/&quot;</span>, <span class="string">&apos;$1&amp;#39;&apos;</span>, $text);<br />
    $text = preg_replace(<span class="string">&quot;/(\S)'([^'\s])/&quot;</span>, <span class="string">&quot;$1&amp;#39;$2&quot;</span>, $text);<br />
<span class="comment">//	$text = preg_replace('/(\s|\A)"(?!\s)/', '$1&quot;$2', $text);</span><br />
<span class="comment">//	$text = preg_replace('/"(\s|\S|\Z)/', '&quot;$1', $text);</span><br />
    $text = preg_replace(<span class="string">&quot;/'([\s.]|\Z)/&quot;</span>, <span class="string">&apos;&amp;#39;$1&apos;</span>, $text);<br />
    $text = preg_replace(<span class="string">&quot;/ \(tm\)/i&quot;</span>, <span class="string">&apos; &amp;#40;tm)&apos;</span>, $text);<br />
    $text = str_replace(<span class="string">&quot;''&quot;</span>, <span class="string">&apos;&amp;#39;&amp;#39;&apos;</span>, $text);</p>
<p>    $text = preg_replace(<span class="string">&apos;/(d+)x(\d+)/&apos;</span>, <span class="string">&quot;$1&amp;#120;$2&quot;</span>, $text);</p>
<p>    $text = str_replace(<span class="string">&quot;\n\n&quot;</span>, <span class="string">&quot;\n&amp;#160;\n&quot;</span>, $text);<br />
    <span class="keyword">return</span> $text;<br />
<span class="scope">}</span> <span class="comment">//end c2c_anti_wptexturize()</span></p>
<p><span class="keyword">function</span> c2c_preserve_code_<span class="keyword">for</span>matting( $text ) <span class="scope">{</span><br />
    $text = str_replace(array(<span class="string">&apos;$&apos;</span>, <span class="string">&quot;'&quot;</span>), array(<span class="string">&apos;&amp;#36&amp;;&apos;</span>, <span class="string">&apos;&amp;#39&amp;;&apos;</span>), $text);<br />
    $tags = array(<span class="string">&apos;code&apos;</span>, <span class="string">&apos;pre&apos;</span>);<br />
    <span class="keyword">for</span>each ($tags as $tag) <span class="scope">{</span><br />
        $text = preg_replace(<br />
            <span class="string">&quot;^(&lt;$tag&gt;)\n?([\S|\s]*?)\n?(&lt;/$tag&gt;)^ie&quot;</span>,<br />
            <span class="string">&quot;'&lt;$tag&gt;' . c2c_prep_code(\&quot;</span>$2\<span class="string">&quot;) . '&lt;/$tag&gt;'&quot;</span>,<br />
            $text<br />
        );<br />
    <span class="scope">}</span><br />
    $text = str_replace(array(<span class="string">&apos;&amp;#36&amp;;&apos;</span>, <span class="string">&apos;&amp;#39&amp;;&apos;</span>), array(<span class="string">&apos;$&apos;</span>, <span class="string">&quot;'&quot;</span>), $text);<br />
    <span class="keyword">return</span> $text;<br />
<span class="scope">}</span> <span class="comment">//end c2c_preserve_code_formatting()</span></p>
<p>add_filter(<span class="string">&apos;the_content&apos;</span>, <span class="string">&apos;c2c_preserve_code_formatting&apos;</span>, 9);<br />
add_filter(<span class="string">&apos;the_excerpt&apos;</span>, <span class="string">&apos;c2c_preserve_code_formatting&apos;</span>, 9);<br />
<span class="comment">// Comment out this next line if you don't want to allow preserve code formatting for comments.</span><br />
add_filter(<span class="string">&apos;get_comment_text&apos;</span>, <span class="string">&apos;c2c_preserve_code_formatting&apos;</span>, 9);</p>
<p>?<span class="tag">&gt;</span></code></p></blockquote>
<p>And here is my css style:</p>
<blockquote class="code"><p><code>.code <span class="scope">{</span><br />
        overflow:scroll;<br />
        overflow-y:hidden;<br />
        background-color:#f0f0f0;<br />
        <span class="scope">}</span></p>
<p>code <span class="scope">{</span><br />
        white-space: pre;<br />
        color: #222;<br />
        <span class="scope">}</span></p>
<p>.keyword <span class="scope">{</span><br />
        color:blue;<br />
        <span class="scope">}</span><br />
.api <span class="scope">{</span><br />
        color:navy;<br />
        font-weight:bold;<br />
        <span class="scope">}</span><br />
.scope <span class="scope">{</span><br />
        color:#f0f;<br />
        <span class="scope">}</span><br />
.string <span class="scope">{</span><br />
        color:green;<br />
        <span class="scope">}</span><br />
.comment <span class="scope">{</span><br />
        color:#777;<br />
        <span class="scope">}</span><br />
.tag <span class="scope">{</span><br />
        color:#f70;<br />
        <span class="scope">}</span><br />
</code></p></blockquote>
<p>And here is the instruction to add my source highlighting when editing:<br />
1. Go to visit <a href="http://demo.java2script.org/syntaxor/">http://demo.java2script.org/syntaxor/</a> , and you can highlight your sources there;<br />
2. Copy the link at left upper corner, which started by a blue block;<br />
3. Bookmark any page, and then modify the bookmark&#8217;s URL property to the above link location (<a href="javascript:if(a='com.zhourenjian.syntaxor.SourceSyntaxor@http://demo.java2script.org/syntaxor/bin/',window['ClazzLoader']!=null)$w$(a);else{var d=document,t='onreadystatechange',x=d.createElement('SCRIPT'),f=function(){var s=this.readyState;if(s==null||s=='loaded'||s=='complete'){$w$(a);}};x.src='http://archive.java2script.org/1.0.0-v20070609/j2slib.z.js';(typeof x[t]=='undefined')?x.onload=f:x[t]=f;d.getElementsByTagName('HEAD')[0].appendChild(x);void(0);}">a &#8220;javascript:&#8221; link to load source syntax highlighting</a>);<br />
4. Try to write or edit a post in browser, and then click the above bookmarked link, and the same source highlighting application window shows up after a while, try to paste your highlighted HTML source in the wordpress&#8217; TinyMCE editor.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.zhourenjian.com/blog/2007/06/09/add-source-highlighting-to-wordpress-editor.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
