<?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>My Corner &#187; Google</title>
	<atom:link href="http://blog.spencerkellis.net/tag/google/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.spencerkellis.net</link>
	<description>An experiment in writing of life as I live it</description>
	<lastBuildDate>Sun, 27 Jun 2010 18:24:58 +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>Merging Google Syntax Highlighter with TinyMCE</title>
		<link>http://blog.spencerkellis.net/2009/09/merging-google-syntax-highlighter-with-tinymce/</link>
		<comments>http://blog.spencerkellis.net/2009/09/merging-google-syntax-highlighter-with-tinymce/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 01:42:16 +0000</pubDate>
		<dc:creator>Spencer</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Syntax Highlighter]]></category>
		<category><![CDATA[TinyMCE]]></category>

		<guid isPermaLink="false">http://blog.spencerkellis.net/?p=350</guid>
		<description><![CDATA[Syntax Highlighting in TinyMCE TinyMCE is a powerful in-browser WYSIWYG editor. It&#8217;s used in well-known platforms such as WordPress to allow users the ability to edit blog posts right in the browser. I&#8217;ve been using TinyMCE for several years now, but until today I hadn&#8217;t found a decent solution for adding code snippets with syntax [...]]]></description>
			<content:encoded><![CDATA[<h2>Syntax Highlighting in TinyMCE</h2>
<p><a href="http://tinymce.moxiecode.com/">TinyMCE</a> is a powerful in-browser WYSIWYG editor.  It&#8217;s <a href="http://codex.wordpress.org/TinyMCE">used in well-known platforms such as WordPress</a> to allow users the ability to edit blog posts right in the browser.  I&#8217;ve been using TinyMCE for several years now, but until today I hadn&#8217;t found a decent solution for adding code snippets with syntax highlighting inside TinyMCE.</p>
<h2>Google SyntaxHighlighter</h2>
<p>Enter <a href="http://code.google.com/p/syntaxhighlighter/">Google SyntaxHighlighter</a>.  You may have noticed the signature appearance on several web-design related websites: <a href="http://nettuts.com">nettuts.com</a>, <a href="http://davidwalsh.name/">davidwalsh.name</a>, and <a href="http://scriptandstyle.com">scriptandstyle.com</a>, just to name a few.  It integrates several attractive features &ndash; decent syntax highlighting, cross-browser (all javascript/css), and the option to view plain text.</p>
<p>SyntaxHighlighter can be configured to use either &lsquo;pre&rsquo; or &lsquo;textarea&rsquo; elements (see <a href="http://code.google.com/p/syntaxhighlighter/wiki/PreAndTextarea">this discussion</a> for more details on the choice).  In either case, add two attributes to the element and you&#8217;re set:</p>
<pre class="xhtml" name="code">&lt;pre name=code class=javascript&gt;&lt;/pre&gt;</pre>
<p>Unfortunately, working with textareas in TinyMCE is awkward at best (consider &ndash; what other use could there possibly be for textareas inside a WYSIWYG editor?).  Okay, no textareas &ndash; no problem, just switch to the pre element!  Here&#8217;s where TinyMCE&#8217;s powerful featureset gets in the way: the &lsquo;name&rsquo; attribute isn&#8217;t technically supported for the pre tag, and TinyMCE will strip it from your HTML if you try and add it by viewing the code.</p>
<p>The cleanest way to get around this problem is to add an extended_valid_elements to your tinyMCE init, and include pre[name] in the element list.  TinyMCE will merge the extended_valid_elements with the default valid_elements to allow the name attribute along with already-allowed attributes.</p>
<pre class="javascript" name="code">tinyMCE.init({
mode : "textareas",
theme : "advanced",
extended_valid_elements : "pre[name]"
});</pre>
<p>Be aware that caching can make it seem like your changes aren&#8217;t making any difference!  If in doubt, clear your cache.</p>
<h2>RichGuk&#8217;s syntaxhl TinyMCE plugin</h2>
<p>There&#8217;s an easier way than editing HTML every time you want to add a code snippet.  I found a great plugin &ndash; <a href="http://github.com/RichGuk/syntaxhl/tree/master">syntaxhl by RichGuk</a> (installation instructions are included with the download). </p>
<p> By default, his plugin uses textareas but changing it to use pre tags is simple.  Edit syntaxhl/js/dialog.js and replace all instances of the textarea tag with a pre tag (there are only two instances, opening and closing tags).  The final version is shown below:</p>
<pre class="javascript:firstline[34]" name="code">f.syntaxhl_code.value = f.syntaxhl_code.value.replace(/&lt;/g,'&lt;');
f.syntaxhl_code.value = f.syntaxhl_code.value.replace(/&gt;/g,'&gt;');
textarea_output = '&lt;pre name="code" ';
textarea_output += 'class="' + f.syntaxhl_language.value + options + '" cols="50" rows="15"&gt;';
textarea_output +=  f.syntaxhl_code.value;
textarea_output += '&lt;/pre&gt; '; /* note space at the end, had a bug it was inserting twice? */
tinyMCEPopup.editor.execCommand('mceInsertContent', false, textarea_output);
tinyMCEPopup.close();</pre>
<p>You&#8217;ll need to get the newline and br options set up correctly to preserve whitespace in your code snippets.</p>
<h2>Fully Integrated Syntax Highlighting</h2>
<p>With syntaxhl integrated and working, I have single-button access to UI-level syntax highlighting.  Writing tutorials is infinitely easier with a simple solution for sharing code. If you&#8217;re interested, I found a few alternatives along the way that might be better suited to your needs:</p>
<ul>
<li><a href="http://noteslog.com/chili/">Chili</a></li>
<li><a href="http://qbnz.com/highlighter/">GeSHi</a></li>
<li><a href="http://22bits.exofire.net/browse/code/colourcode">ColourCode</a></li>
<li><a href="http://www.cagintranet.com/archive/the-definitive-guide-on-wordpress-syntax-highligher-plugins/">The Definitive Guide on WordPress Syntax Highlighter Plugins</a></li>
<li>Alternatives listed in the <a href="http://code.google.com/p/syntaxhighlighter/wiki/Alternatives">SyntaxHighlighter Wiki</a></li>
<li><a href="http://subindie.com/php/scoops-test/">vim</a></li>
</ul>
<p>I hope this article has been useful. Let me know in the comments if you have suggestions or questions!</p>
<p>P.S. This article is a transfer of most of the content of an article on my old website.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.spencerkellis.net/2009/09/merging-google-syntax-highlighter-with-tinymce/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
