<?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>a work on process &#187; csspool</title>
	<atom:link href="http://jystewart.net/process/tag/csspool/feed/" rel="self" type="application/rss+xml" />
	<link>http://jystewart.net/process</link>
	<description>notes from another web developer</description>
	<lastBuildDate>Tue, 27 Jul 2010 09:53:43 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Exploring Ruby CSS parsers: TamTam and CSSPool</title>
		<link>http://jystewart.net/process/2007/12/exploring-ruby-css-parsers-tamtam-and-csspool/</link>
		<comments>http://jystewart.net/process/2007/12/exploring-ruby-css-parsers-tamtam-and-csspool/#comments</comments>
		<pubDate>Wed, 05 Dec 2007 13:14:46 +0000</pubDate>
		<dc:creator>James Stewart</dc:creator>
				<category><![CDATA[Notes]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[csspool]]></category>
		<category><![CDATA[htmlemail]]></category>
		<category><![CDATA[Parser]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[sac]]></category>
		<category><![CDATA[tamtam]]></category>

		<guid isPermaLink="false">http://jystewart.net/process/2007/12/exploring-ruby-css-parsers-tamtam-and-csspool/</guid>
		<description><![CDATA[In response to yesterday&#8217;s post about inlining CSS for HTML emails, I got a couple of comments suggesting alternatives to my CSS parser class. Not wanting to have to maintain code unless I have to, I decided to give them both a try and see how they worked out.
TamTam
First up is TamTam, suggested by batnight. <a href="http://jystewart.net/process/2007/12/exploring-ruby-css-parsers-tamtam-and-csspool/" class="more-link">More &#62;</a>]]></description>
			<content:encoded><![CDATA[<p>In response to <a href="/process/2007/12/a-little-scripting-to-help-with-html-email-bringing-styles-inline/">yesterday&#8217;s post about inlining CSS for HTML emails</a>, I got a couple of comments suggesting alternatives to my CSS parser class. Not wanting to have to maintain code unless I have to, I decided to give them both a try and see how they worked out.</p>
<h4>TamTam</h4>
<p>First up is <a href="http://tamtam.rubyforge.org/" title="TamTam: Inline CSS">TamTam</a>, <a href="/process/2007/12/a-little-scripting-to-help-with-html-email-bringing-styles-inline/#comment-77767">suggested by batnight</a>. I&#8217;d actually spotted TamTam and link blogged it a few weeks ago, which shows how transient attention can be. TamTam is a complete solution for inlining CSS, so I should be able to replace all my code with:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'hpricot'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'tamtam'</span>
&nbsp;
inlined = TamTam.<span style="color:#9900CC;">inline</span><span style="color:#006600; font-weight:bold;">&#40;</span>
  <span style="color:#ff3333; font-weight:bold;">:css</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'/path/to/my.css'</span><span style="color:#006600; font-weight:bold;">&#41;</span>,
  <span style="color:#ff3333; font-weight:bold;">:body</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'/path/to/my.html'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> inlined</pre></div></div>

<p>That looks ideal, but unfortunately as soon as I tried passing my code into it an error emerged.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">/</span>Library<span style="color:#006600; font-weight:bold;">/</span>Ruby<span style="color:#006600; font-weight:bold;">/</span>Gems<span style="color:#006600; font-weight:bold;">/</span>gems<span style="color:#006600; font-weight:bold;">/</span>tamtam<span style="color:#006600; font-weight:bold;">-</span>0.0.2<span style="color:#006600; font-weight:bold;">/</span>lib<span style="color:#006600; font-weight:bold;">/</span>tamtam.<span style="color:#9900CC;">rb</span>:<span style="color:#006666;">77</span>:<span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#996600;">`apply_to': Trouble on style td#email-header-message on element &lt;td id=&quot;email-header-message&quot;&gt; (Exception): can't convert nil into String	from /Library/Ruby/Gems/gems/tamtam-0.0.2/lib/tamtam.rb:24:in `</span>inline<span style="color:#996600;">'
&lt;/td&gt;</span></pre></div></div>

<p>Digging into the library it seems that its parsing fails if there&#8217;s a CSS rule that doesn&#8217;t match any elements in the document. That&#8217;s not a problem if your CSS file is targetted to a specific block of HTML, but if the idea is to build a set of rules that can be applied across a selection of page/emails that may be a problem.</p>
<h4>CSSPool</h4>
<p>CSSPool, <a href="/process/2007/12/a-little-scripting-to-help-with-html-email-bringing-styles-inline/#comment-77765">suggested by Dan Kubb</a>, is a more generic CSS parser which is designed to work with hpricot to map between CSS and HTML. Initially I just used something very much like the examples they offer to get a sense of the format of the objects they give access to:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'hpricot'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'csspool'</span>
&nbsp;
sac = <span style="color:#6666ff; font-weight:bold;">CSS::SAC::Parser</span>.<span style="color:#9900CC;">new</span> 
css_doc = sac.<span style="color:#9900CC;">parse</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'/path/to/my.css'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> 
html_doc = Hpricot.<span style="color:#9900CC;">parse</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'/path/to/my.css'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> 
css_doc.<span style="color:#9900CC;">find_all_rules_matching</span><span style="color:#006600; font-weight:bold;">&#40;</span>html_doc<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>rule<span style="color:#006600; font-weight:bold;">|</span> 
  <span style="color:#CC0066; font-weight:bold;">puts</span> rule
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Unfortunately this too failed on me with:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC00FF; font-weight:bold;">NoMethodError</span>: undefined method <span style="color:#996600;">`accept' for nil:NilClass</span></pre></div></div>

<p>From a quick look at the source code it seemed that this error was occurring when a CSS selector didn&#8217;t match the supplied document, just as in TamTam. It turned out to be pretty easy to patch, though, and I&#8217;ve submitted <a href="http://rubyforge.org/tracker/index.php?func=detail&amp;aid=16104&amp;group_id=4463&amp;atid=17246" title="RubyForge: Error report">a report in their tracker</a>.</p>
<p>That done I was able to iterate over it and access the selectors easily enough, but what I&#8217;ve not yet been able to find is a way to get the parser to give me the actual CSS declarations appropriately formatted for including in the HTML. When I have a rule I can get the selector with:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">rule.<span style="color:#9900CC;">selector</span>.<span style="color:#9900CC;">to_css</span></pre></div></div>

<p>But to find out what styles the selector applies isn&#8217;t so straightforward. If anyone has an easy way to do that, I&#8217;d love to hear about it in the comments!</p>
<p><strong>Update (later that day):</strong> Version 0.2.3 of CSSPool is now out and includes my fix.</p>
]]></content:encoded>
			<wfw:commentRss>http://jystewart.net/process/2007/12/exploring-ruby-css-parsers-tamtam-and-csspool/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
