<?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>Joakim Andersson &#187; popular</title>
	<atom:link href="http://joakimandersson.se/archives/category/popular/feed/" rel="self" type="application/rss+xml" />
	<link>http://joakimandersson.se</link>
	<description>Passionate climber, brilliant coder</description>
	<lastBuildDate>Thu, 12 Jan 2012 11:01:55 +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>Rails + Tidy + REXML</title>
		<link>http://joakimandersson.se/archives/2007/03/01/rails-tidy-rexml/</link>
		<comments>http://joakimandersson.se/archives/2007/03/01/rails-tidy-rexml/#comments</comments>
		<pubDate>Thu, 01 Mar 2007 13:02:52 +0000</pubDate>
		<dc:creator>Joakim Andersson</dc:creator>
				<category><![CDATA[popular]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://joakimandersson.se/archives/2007/03/01/rails-tidy-rexml/</guid>
		<description><![CDATA[It wasn&#8217;t totally straight forward to get Tidy, REXML and Rails to play together, so I thought I would write down what and how I did it to save time for others. The reason for doing this is that I get text in (X)HTML format through RSS feeds and I want to make excerpts of [...]]]></description>
			<content:encoded><![CDATA[<p>It wasn&#8217;t totally straight forward to get <a href="http://tidy.sourceforge.net/">Tidy</a>, <a href="http://www.germane-software.com/software/XML/rexml/">REXML</a> and <a href="http://rubyonrails.com/">Rails</a> to play together, so I thought I would write down what and how I did it to save time for others.</p>

<p>The reason for doing this is that I get text in (X)HTML format through RSS feeds and I want to make excerpts of it. So given a long text as input I want to make a short extract of it.</p>

<p>After a bit of thinking and googling I figured out that slicing a HTML document after a given amount of characters is not super trivial to do. Because of the tags you need to actually parse the HTML document and keep track of which tags you need to close then reaching the given amount of characters. Luckily for us <a href="http://mikeburnscoder.wordpress.com/">Mike Burns</a> has already written a function for <a href="http://mikeburnscoder.wordpress.com/2006/11/11/truncating-html-in-ruby/">Truncating HTML in
Ruby</a>. Perfect!</p>

<p>However, after adding that piece of code (and unit tests for that of course) you will find out that REXML barfs if the input is not well-formed HTML and naturally having no control of the content of the RSS feeds there is no way you can guarantee that.</p>

<p>Luckily Tidy comes to the rescue. Tidy is a library that corrects invalid HTML. Install the tidy library and then the <a href="http://tidy.rubyforge.org/">tidy ruby gem</a>.</p>

<p><code>gem install tidy</code></p>

<p>Unfortunately you have to manually set the path to the library before you can use it with</p>

<p><pre class="brush: ruby; title: ; notranslate">Tidy.path = '/usr/lib/tidylib.so'</pre></p>

<p>If you, like me use an apple laptop for development and linux on the server that path is going to be different between the environments. So what I did was to introduce a constant in the rails environment files. In the <em>config/environments/production.rb</em> file I put:</p>

<p><pre class="brush: ruby; title: ; notranslate">TIDY_LIB_PATH = '/usr/lib/libtidy.so'</pre></p>

<p>And naturally I set it to the correct path for my powerbook in the <em>config/environments/development.rb</em> file. Then I just do</p>

<p><pre class="brush: ruby; title: ; notranslate">Tidy.path = TIDY_LIB_PATH</pre></p>

<p>before using Tidy and everything is good.</p>

<p>To make Tidy behave decently you need to set the following options:</p>

<ul>
    <li><strong>tidy.options.show_body_only = true</strong> &#8211; don&#8217;t output body and html tags</li>
    <li><strong>tidy.options.output_xhtml = true</strong> &#8211; output xhtml</li>
    <li><strong>tidy.options.wrap = 0</strong> &#8211; don&#8217;t write newlines all over the place</li>
    <li><strong>tidy.options.char_encoding = &#8216;utf8&#8242;</strong> &#8211; use utf8 to play nice with rails</li>
</ul>

<p>so in the end this is what I ended up with:</p>

<p><pre class="brush: ruby; title: ; notranslate">
require 'rexml/parsers/pullparser'
require 'tidy'&lt;/p&gt;

&lt;p&gt;def make_excerpt
excerpt = slice(tidy_up_html(content), 2000)
end&lt;/p&gt;

&lt;p&gt;def tidy_up_html(html)
Tidy.path = TIDY_LIB_PATH&lt;/p&gt;

&lt;p&gt;cleaned_up = Tidy.open do |tidy|
tidy.options.show_body_only = true
tidy.options.output_xhtml = true
tidy.options.wrap = 0
tidy.options.char_encoding = 'utf8'
cleaned_up = tidy.clean(html)
cleaned_up
end
end&lt;/p&gt;

&lt;p&gt;def slice(string, length, ellipsis = '...')
p = REXML::Parsers::PullParser.new(string)
tags = []
new_len = length
results = ''
while p.has_next? &amp;amp;&amp;amp; new_len &amp;gt; 0
p_e = p.pull
case p_e.event_type
when :start_element
tags.push p_e[0]
results &amp;lt;&amp;lt; &amp;quot;&amp;lt;#{tags.last} #{attrs_to_s(p_e[1])}&amp;gt;&amp;quot;
when :end_element
results &amp;lt;&amp;lt; &amp;quot;&amp;lt;!--#{tags.pop}--&amp;gt;&amp;quot;
when :text
results &amp;lt;&amp;lt; p_e[0].first(new_len)
current_len = new_len
new_len -= p_e[0].length
if new_len &amp;lt; 0&lt;/p&gt;

&lt;h1&gt;find next dot&lt;/h1&gt;

&lt;p&gt;i = p_e[0].index('.', current_len)
results &amp;lt;&amp;lt; p_e[0].slice(current_len, i-current_len) if i
results &amp;lt;&amp;lt; p_e[0].slice(current_len, p_e[0].length) unless i
results &amp;lt;&amp;lt; ellipsis
end
else
results &amp;lt;&amp;lt; &amp;quot;&amp;lt;!-- #{p_e.inspect} --&amp;gt;&amp;quot;
end
end
tags.reverse.each do |tag|
results &amp;lt;&amp;lt; &amp;quot;&amp;lt;!--#{tag}--&amp;gt;&amp;quot;
end
results
end
</pre></p>

<p>I modified Mike Burns&#8217; method so that after the given number of characters has been reached it will still include text until the next &#8216;.&#8217; character. I figured it&#8217;s much nicer with an excerpt that ends with a complete sentence.</p>

<p>Feel free to use this code if you want.</p>
]]></content:encoded>
			<wfw:commentRss>http://joakimandersson.se/archives/2007/03/01/rails-tidy-rexml/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Make your Sony Ericsson K800 look like an iPhone</title>
		<link>http://joakimandersson.se/archives/2007/02/05/make-your-sony-ericsson-k800-look-like-an-iphone/</link>
		<comments>http://joakimandersson.se/archives/2007/02/05/make-your-sony-ericsson-k800-look-like-an-iphone/#comments</comments>
		<pubDate>Mon, 05 Feb 2007 15:54:00 +0000</pubDate>
		<dc:creator>Joakim Andersson</dc:creator>
				<category><![CDATA[apple]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[popular]]></category>

		<guid isPermaLink="false">http://joakimandersson.se/archives/2007/02/05/make-your-sony-ericsson-k800-look-like-an-iphone/</guid>
		<description><![CDATA[Last week I mentioned the iPhone-theme for Motorola RAZR now there is an iPhone theme for K800i phones as well. I&#8217;m still waiting for a theme for my Nokia N70&#8230; Update: The above link seems to have gone broken. Here is another working link. via macfeber.]]></description>
			<content:encoded><![CDATA[<script type="text/javascript"><!--
google_ad_client = "pub-9691140303330735";
//468x15, skapad 2007-11-19
google_ad_slot = "4318456575";
google_ad_width = 468;
google_ad_height = 15;
//--></script>

<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

<p>Last week I mentioned the <a href="http://joakimandersson.se/archives/2007/01/30/iphone-tema-for-motorola-razr/">iPhone-theme for Motorola RAZR</a> now there is an <a href="http://www.pjdavis.freehosting123.com/blog/2007/01/27/iphone-theme-for-k800i/">iPhone theme for K800i phones</a> as well.</p>

<script type="text/javascript"><!--
google_ad_client = "pub-9691140303330735";
//468x60, skapad 2007-11-19
google_ad_slot = "1114135960";
google_ad_width = 468;
google_ad_height = 60;
//--></script>

<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

<p>I&#8217;m still waiting for a theme for my Nokia N70&#8230;</p>

<p><strong>Update:</strong> The above link seems to have gone broken. <a href="http://mikil.deviantart.com/art/K800iPhone-46444415">Here is another working link</a>.</p>

<p><em>via </em><a href="http://mac.feber.se//feber/art/10092/iphonetema_till_sony_ericsson_/"><em>macfeber</em></a><em>.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://joakimandersson.se/archives/2007/02/05/make-your-sony-ericsson-k800-look-like-an-iphone/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Could not find rails (&gt; 0) in the repository</title>
		<link>http://joakimandersson.se/archives/2006/10/24/could-not-find-rails-0-in-the-repository/</link>
		<comments>http://joakimandersson.se/archives/2006/10/24/could-not-find-rails-0-in-the-repository/#comments</comments>
		<pubDate>Tue, 24 Oct 2006 11:43:13 +0000</pubDate>
		<dc:creator>Joakim Andersson</dc:creator>
				<category><![CDATA[popular]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://joakimandersson.se/archives/2006/10/24/could-not-find-rails-0-in-the-repository/</guid>
		<description><![CDATA[If you get this when trying to install rails with rubygem you apparently need to remove your source cache. Not totally obvious. Update: As you can see from the comments re-running the command should solve it for most.]]></description>
			<content:encoded><![CDATA[<p>If you get this when trying to install rails with rubygem you <a href="http://paulgoscicki.com/archives/2005/09/ruby-on-rails-on-ubuntu/">apparently</a> need to <a href="http://onestepback.org/index.cgi/Tech/Ruby/DeleteYourCache.red">remove your source cache</a>.</p>

<p>Not totally obvious.</p>

<p><em><strong>Update</strong>: As you can see from the comments re-running the command should solve it for most.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://joakimandersson.se/archives/2006/10/24/could-not-find-rails-0-in-the-repository/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>Chris Sharma&#8217;s arch project</title>
		<link>http://joakimandersson.se/archives/2006/09/26/chris-sharmas-arch-project/</link>
		<comments>http://joakimandersson.se/archives/2006/09/26/chris-sharmas-arch-project/#comments</comments>
		<pubDate>Tue, 26 Sep 2006 13:33:39 +0000</pubDate>
		<dc:creator>Joakim Andersson</dc:creator>
				<category><![CDATA[climbing]]></category>
		<category><![CDATA[popular]]></category>

		<guid isPermaLink="false">http://joakimandersson.se/archives/2006/09/26/chris-sharmas-arch-project/</guid>
		<description><![CDATA[Via joost.climbing.nl we get the awesome videos below of Chris Sharma working on his deep water solo project in Mallorca. Watch them! Teaser video Interview]]></description>
			<content:encoded><![CDATA[<p><img class="align-right" src="http://joakimandersson.se/wp-content/files/es_pontas.jpg" /></p>

<p>Via <a href="http://joost.climbing.nl/?p=183">joost.climbing.nl</a> we get the awesome videos below of <a href="http://en.wikipedia.org/wiki/Chris_Sharma">Chris Sharma</a> working on his deep water solo project in Mallorca. Watch them!</p>

<p>
<ul>
<li><a href="http://www.bigupproductions.com/bigUpSite2/movies.html?14&#038;ArchTeaserLarge.mov&#038;282&#038;480">Teaser video</a></li>
<li><a href="http://www.bigupproductions.com/bigUpSite2/movies.html?14&#038;ArchInterview.mov&#038;282&#038;480">Interview</a></li>
</ul>
</p>
]]></content:encoded>
			<wfw:commentRss>http://joakimandersson.se/archives/2006/09/26/chris-sharmas-arch-project/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fartkameravarnare</title>
		<link>http://joakimandersson.se/archives/2006/07/03/fartkameravarnare/</link>
		<comments>http://joakimandersson.se/archives/2006/07/03/fartkameravarnare/#comments</comments>
		<pubDate>Mon, 03 Jul 2006 09:16:28 +0000</pubDate>
		<dc:creator>Joakim Andersson</dc:creator>
				<category><![CDATA[misc]]></category>
		<category><![CDATA[popular]]></category>

		<guid isPermaLink="false">http://joakimandersson.se/archives/2006/07/03/fartkameravarnare/</guid>
		<description><![CDATA[Efter att ha läst en artikel i Aftonbladet om en liten apparat som varnar för fartkameror så beställde jag en ActiveGPS från Ad-Teknik AB för någon månad sedan. I fredags kom den äntligen på posten och efter att ha använt den igår när vi åkte och klättrade så måste jag tillstå att den fungerar precis [...]]]></description>
			<content:encoded><![CDATA[<p><img class="align-right" src="http://joakimandersson.se/wp-content/files/gps257liten.jpg" /></p>

<p>Efter att ha läst en <a href="http://aftonbladet.se/vss/bil/story/0,2789,827904,00.html">artikel i Aftonbladet</a> om en liten apparat som varnar för fartkameror så beställde jag en <a href="http://www.adteknik.se/swedish/gps.htm">ActiveGPS från Ad-Teknik AB</a> för någon månad sedan.</p>

<script type="text/javascript"><!--
google_ad_client = "pub-9691140303330735";
//468x60, single post banner
google_ad_slot = "5197835159";
google_ad_width = 468;
google_ad_height = 60;
//--></script>

<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

<p>I fredags kom den äntligen på posten och efter att ha använt den igår när vi åkte och klättrade så måste jag tillstå att den fungerar precis så bra som jag hoppades att den skulle. Man ser exakt hur fort man kör, våran Audi 80 visade runt 5 km/h för fort vid 100 km/h, man kan se hur högt över havet man befinner sig (alltid väldigt användbart!) och viktigast av allt, den piper när man närmar sig en kamera om man råkar köra för fort.</p>

<p>Nu återstår bara att montera den lite snyggare. Jag funderar på att försöka bygga in den i askkoppslådan, men det kanske är lättare sagt än gjort.</p>

<script type="text/javascript"><!--
google_ad_client = "pub-9691140303330735";
//468x15, skapad 2007-11-19
google_ad_slot = "7192979523";
google_ad_width = 468;
google_ad_height = 15;
//--></script>

<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
]]></content:encoded>
			<wfw:commentRss>http://joakimandersson.se/archives/2006/07/03/fartkameravarnare/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MacBook recension som inte är helt igenom positiv</title>
		<link>http://joakimandersson.se/archives/2006/06/09/macbook-recension-som-inte-ar-helt-igenom-positiv/</link>
		<comments>http://joakimandersson.se/archives/2006/06/09/macbook-recension-som-inte-ar-helt-igenom-positiv/#comments</comments>
		<pubDate>Fri, 09 Jun 2006 11:22:08 +0000</pubDate>
		<dc:creator>Joakim Andersson</dc:creator>
				<category><![CDATA[apple]]></category>
		<category><![CDATA[popular]]></category>

		<guid isPermaLink="false">http://joakimandersson.se/archives/2006/06/09/macbook-recension-som-inte-ar-helt-igenom-positiv/</guid>
		<description><![CDATA[MacPro.se har testat nya MacBook och är inte helt nöjda. Intressant att läsa om någon som inte är helt bländad av Apples glans och vågar vara kritisk. Men jag vill fortfarande ha en MacBook. Min gamla iBook G3 trotjänare var nästintill perfekt förutom att 1024&#215;768 är lite i minsta laget för att jobba effektivt med [...]]]></description>
			<content:encoded><![CDATA[<script type="text/javascript"><!--
google_ad_client = "pub-9691140303330735";
//468x15, skapad 2007-11-19
google_ad_slot = "1351757944";
google_ad_width = 468;
google_ad_height = 15;
//--></script>

<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

<p><a href="http://www.macpro.se/?p=3929">MacPro.se har testat nya MacBook</a> och är inte helt nöjda. Intressant att läsa om någon som inte är helt bländad av Apples glans och vågar vara kritisk. Men jag vill fortfarande ha en MacBook.</p>

<script type="text/javascript"><!--
google_ad_client = "pub-9691140303330735";
//468x60, skapad 2007-11-19
google_ad_slot = "2936570485";
google_ad_width = 468;
google_ad_height = 60;
//--></script>

<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

<p>Min gamla iBook G3 trotjänare var nästintill perfekt förutom att 1024&#215;768 är lite i minsta laget för att jobba effektivt med så jag tror att MacBookens 13.3&#8243; display är ett steg i rätt riktning.</p>
]]></content:encoded>
			<wfw:commentRss>http://joakimandersson.se/archives/2006/06/09/macbook-recension-som-inte-ar-helt-igenom-positiv/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>REXML Tutorial</title>
		<link>http://joakimandersson.se/archives/2006/02/16/rexml-tutorial/</link>
		<comments>http://joakimandersson.se/archives/2006/02/16/rexml-tutorial/#comments</comments>
		<pubDate>Thu, 16 Feb 2006 08:03:59 +0000</pubDate>
		<dc:creator>Joakim Andersson</dc:creator>
				<category><![CDATA[misc]]></category>
		<category><![CDATA[popular]]></category>

		<guid isPermaLink="false">http://morrdusk.net/archives/2006/02/16/rexml-tutorial/</guid>
		<description><![CDATA[REXML is a superb way to parse XML in Ruby, however it can might be a bit hard to grok at first. This tutorial might make it easier to understand how it works.]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.germane-software.com/~ser/software/rexml">REXML</a> is a superb way to parse XML in Ruby, however it can might be a bit hard to grok at first. <a href="http://www.germane-software.com/software/rexml/docs/tutorial.html">T</a><a href="http://www.germane-software.com/software/rexml/docs/tutorial.html">his tutorial</a> might make it easier to understand how it works.</p>
]]></content:encoded>
			<wfw:commentRss>http://joakimandersson.se/archives/2006/02/16/rexml-tutorial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Kim Hartman citat</title>
		<link>http://joakimandersson.se/archives/2005/04/29/kim-hartman-citat/</link>
		<comments>http://joakimandersson.se/archives/2005/04/29/kim-hartman-citat/#comments</comments>
		<pubDate>Fri, 29 Apr 2005 12:26:44 +0000</pubDate>
		<dc:creator>Joakim Andersson</dc:creator>
				<category><![CDATA[non-tech]]></category>
		<category><![CDATA[på svenska]]></category>
		<category><![CDATA[popular]]></category>
		<category><![CDATA[snooker]]></category>

		<guid isPermaLink="false">http://morrdusk.net/blog/archives/2005/04/29/kim-hartman-citat/</guid>
		<description><![CDATA[Om du tittar på snooker på svenska eurosport så bara måste du gå till denna sida som har samlat ihop en stor mängd fullkompligt underbara kommentarer från allas vår älskade kommentator Kim Hartman. Jag skrattade så otroligt mycket när jag läste igenom dom första gången.]]></description>
			<content:encoded><![CDATA[<p>Om du tittar på snooker på svenska eurosport så bara måste du gå till <a href="http://fly.to/kimhartman">denna sida</a> som har samlat ihop en stor mängd fullkompligt underbara kommentarer från allas vår älskade kommentator Kim Hartman.</p>

<p>Jag skrattade så otroligt mycket när jag läste igenom dom första gången.</p>
]]></content:encoded>
			<wfw:commentRss>http://joakimandersson.se/archives/2005/04/29/kim-hartman-citat/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

