<?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>soledad penadés &#187; ruby</title>
	<atom:link href="http://soledadpenades.com/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://soledadpenades.com</link>
	<description>repeat 4[fd 100 rt 90]</description>
	<lastBuildDate>Sun, 29 Jan 2012 23:03:40 +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>ruby in the pub #4 :after</title>
		<link>http://soledadpenades.com/2010/06/23/ruby-in-the-pub-4-after/</link>
		<comments>http://soledadpenades.com/2010/06/23/ruby-in-the-pub-4-after/#comments</comments>
		<pubDate>Wed, 23 Jun 2010 08:36:22 +0000</pubDate>
		<dc:creator>sole</dc:creator>
				<category><![CDATA[Events]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[london]]></category>
		<category><![CDATA[processing]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby in the pub]]></category>

		<guid isPermaLink="false">http://soledadpenades.com/?p=2689</guid>
		<description><![CDATA[Julian Burgess (who I had virtually met via Twitter) suggested me I should attend the next ruby in the pub. It sounded decidedly odd: mixing journalists with an interest for code with ruby developers; even more strange considering I&#8217;m not a ruby expert as much as I try, so I thought it could be interesting [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://twitter.com/aubergene">Julian Burgess</a> (who I had virtually met via Twitter) suggested me I should attend the next ruby in the pub. It sounded decidedly odd: mixing journalists with an interest for code with ruby developers; even more strange considering I&#8217;m not a ruby expert as much as I try, so I thought it could be interesting to go and see what an event like that could turn out to be.</p>
<p>So there I went after mentally memorising the map from Liverpool Street Station to <a href="http://www.lbi.co.uk/">LBi</a>&#8216;s offices in Brick Lane. Funny because I have done the Brick Lane-Liverpool St. route several times already, but since it was dark and I got an <a href="http://mrdoob.com">excellent guide</a>, I never paid much attention. Anyway, I arrived, gave my name to the security guy which was kind of confused with my Spanish name and then went downstairs where the people with laptops on the couches were.</p>
<p>I took a look and didn&#8217;t know anyone &#8212; so I decided the direct approach would be better: waved and said &#8220;hi&#8221; to whoever was looking at me at that point, and got a friendly response. Yay!</p>
<p>In a minute I was happily talking with <a href="http://twitter.com/just_francesca">Francesca</a>, a nice front end web developer, then with someone else whose name I didn&#8217;t find out, then I got to associate Julian&#8217;s name with his face. A little while after, <a href="http://www.paulcarvill.com/">Paul Carvill</a> came downstairs with lots of pizza boxes &#8211;all vegetarian, and yummy!&#8211; and as we were discussing about Rails 3.0, <a href="http://joannageary.com">Joanna Geary</a> made a little speech, more or less on the lines of this:</p>
<blockquote><p>&#8216;Journalists, raise your hands!&#8217;</p>
<p>They do.</p>
<p>&#8216;Journalists, find a developer and explain them what do you have in mind that could be done with code, and let&#8217;s see if it&#8217;s doable or not!&#8217;</p></blockquote>
<p>And then a girl approached Francesca and me. She looked strangely familiar but I couldn&#8217;t really tell why. We sat in a table, took the laptops out of their sleeves and set out to try and find if the data we needed was available. But the wireless didn&#8217;t quite work. So we chatted about something fascinating and kind of related to the topic: Volcanoes! It really helped that she had a degree in Geology so she could really offer interesting insight about how volcanoes work. Also, amazing as it sounds, she could pronounce Eyjafjallajökull in one go without hesitation!</p>
<p>We ate some more pizza, and then the wireless started working. So we checked and saw that the data wasn&#8217;t available &#8212; but we agreed she would contact the organisation and ask them for it. Maybe they already have it but it&#8217;s not publicly advertised.</p>
<p>Then it&#8217;s when the real coding part began; she said she would love to learn programming, but didn&#8217;t know where to start, or the guides she had tried were boring. I immediately proposed Processing; I know there&#8217;s <a href="http://hacketyhack.heroku.com">Hackety Hack</a> by _why, which is ruby based, but I don&#8217;t have any experience with it, so I went for the safer option. </p>
<p>I actually hadn&#8217;t done much Processing recently and as such I always forget the names of the basic functions (was it <em>setup</em>, or was it <em>load</em>? was it <em>loop</em> or was it <em>play</em>?) but it was even better, so that way they could follow along. She downloaded Processing and was typing in stuff in the sketchbook immediately&#8230; which was amazing! No setup, no preparation, no rebooting the computer after the installation; just download and experiment!</p>
<p>We&#8217;ve done just extremely simple things such as opening a window, setting its background colour, changing its size and&#8230; drawing a line! Still, I find it really enlightening to observe how &#8216;normal people&#8217; (i.e. not <em>hardcore</em> developers) react to all these concepts when exposed to them.</p>
<p>Interesting things I&#8217;ve noticed (teachers, take note!):</p>
<ul>
<li>people love to see results immediately &#8211; no theory to start with!</li>
<li>people love to have some starting snippets, which they can modify and experiment with, learning how they work meanwhile</li>
</ul>
<p>So that makes Processing an excellent programming environment for beginners, because it&#8217;s got that beatiful <a href="http://processing.org/reference/">reference</a> written in pretty much plain language and with example snippets that can be copied and pasted for each function/class.</p>
<p>In a few minutes the word had spread: there were people doing stuff with Processing in the room! And so a little audience gathered, another guy (a journalist) came with his laptop, downloaded Processing and was doing little simple things as the aforementioned ones, copying code from her wife&#8217;s screen &#8211;isn&#8217;t it sweet?</p>
<p>On the other side of the table I was showing Paul and Francesca my word processing and statistical experiments with <a href="http://5013.es/p/1">Tolkien</a> works and we also spoke about Processing versus other environments such as Javascript or Flash.</p>
<p>In between, the girl had told us her name, and if the face looked familiar, the name was insanely familiar! She humbly told me she was in lots of events and etc, but didn&#8217;t make a big thing of it. I asked her about events I had been in (that I could recall at that point) to no avail &#8211;we didn&#8217;t seem to have been in the same events. But then I came home and looked for &#8220;<a href="http://suw.charman-anderson.com/">Suw Charman</a>&#8221; and found that not only had I seen her speak at resfest&#8217;05 but she had also posted a few messages to a mailing list I follow. I think that&#8217;s why the name sounded sooo familiar!</p>
<p>Of course, there couldn&#8217;t be a nice event without Rob McKinnon in it! I just love how I always find him everywhere I go, whether it&#8217;s a full fledged event or just a coffee shop anywhere in London, it&#8217;s just so funny. Couldn&#8217;t exchange more than a few &#8220;hey hey hi!&#8221; waves because we both were busy with our new friends, so I don&#8217;t know which sorts of interesting stuff did he show this time (the last time <a href="http://soledadpenades.com/2007/03/13/london-ruby-users-group-brings-you-back-to-uni/">he spoke about Hpricot</a>).</p>
<p>The event ended at around 22h, but mainly because people had to leave and take trains and all those things, but I&#8217;m pretty sure otherwise we could have stayed and playing with Processing for a long while :-)</p>
<p>And that&#8217;s how <em>ruby in the pub</em>, &#8220;with hardly any ruby and certainly no pub&#8221;, was. See you in the next one?</p>
 <p><a href="http://soledadpenades.com/?flattrss_redirect&amp;id=2689&amp;md5=3c650f8fa0662dd57054c1ccc6fb9b43" title="Flattr" target="_blank"><img src="http://soledadpenades.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://soledadpenades.com/2010/06/23/ruby-in-the-pub-4-after/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to install hpricot in Ubuntu 8.4</title>
		<link>http://soledadpenades.com/2008/10/24/how-to-install-hpricot-in-ubuntu-84/</link>
		<comments>http://soledadpenades.com/2008/10/24/how-to-install-hpricot-in-ubuntu-84/#comments</comments>
		<pubDate>Fri, 24 Oct 2008 09:56:59 +0000</pubDate>
		<dc:creator>sole</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[hpricot]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://soledadpenades.com/?p=778</guid>
		<description><![CDATA[This could be considered a fresh installation, speaking in ruby terms. I just had ruby installed, no ruby gems, nor ruby dev nor anything else ruby. So this should be enough for installing hpricot as well as ruby gems (which are required for installing hpricot). As you can see, I didn&#8217;t download any source file, [...]]]></description>
			<content:encoded><![CDATA[<p>This could be considered a fresh installation, speaking in ruby terms. I just had ruby installed, no ruby gems, nor ruby dev nor anything else ruby. So this should be enough for installing hpricot as well as ruby gems (which are required for installing hpricot). </p>
<p>As you can see, I didn&#8217;t download any source file, instead I was happy with using apt-get and the hpricot version from ubuntu repositories, although they are relatively old (for example rubygems is more than a year old). If I find any problem and need to update to newer versions I&#8217;ll report that here ;-)</p>
<div class="syhi_block"><code><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> rubygems<br />
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1.8</span><span style="color: #000000; font-weight: bold;">/</span>source_cache<br />
<span style="color: #c20cb9; font-weight: bold;">sudo</span> gem update<br />
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> ruby1.8-dev<br />
<span style="color: #c20cb9; font-weight: bold;">sudo</span> gem <span style="color: #c20cb9; font-weight: bold;">install</span> hpricot</code></div>
<p>It&#8217;s a pity they don&#8217;t have a metapackage for ruby&#8217;s development files (the ruby1.8-dev package), the same way there&#8217;s a <strong>ruby</strong> metapackage which depends on the <strong>ruby1.8</strong> package, so whenever ruby is updated it will update the ruby version as well, without the user having to worry about the version number.</p>
<p>Even more, I instinctively tried a <em>naive</em><strong> sudo apt-get install rubydev</strong> and was greeted with a sad<em> &#8220;Couldn&#8217;t find package rubydev&#8221;</em>. It somehow proves that a metapackage called rubydev would be quite useful&#8230; at least for instinctive users.</p>
<p>Enjoy your screen scrapping!</p>
]]></content:encoded>
			<wfw:commentRss>http://soledadpenades.com/2008/10/24/how-to-install-hpricot-in-ubuntu-84/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Parsing a del.icio.us export with Hpricot</title>
		<link>http://soledadpenades.com/2008/03/25/parsing-a-delicious-export-with-hpricot/</link>
		<comments>http://soledadpenades.com/2008/03/25/parsing-a-delicious-export-with-hpricot/#comments</comments>
		<pubDate>Tue, 25 Mar 2008 08:54:11 +0000</pubDate>
		<dc:creator>sole</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[bookmarks]]></category>
		<category><![CDATA[data scrapping]]></category>
		<category><![CDATA[delicious]]></category>
		<category><![CDATA[hpricot]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://soledadpenades.com/2008/03/25/parsing-a-delicious-export-with-hpricot/</guid>
		<description><![CDATA[The trickiest part is to detect if a bookmark has a corresponding description. The export is in the same format that Netscape used for its bookmarks export, which means it is a simple html file with a definition list (dl) and a series of definition terms (dt). A term (=bookmarks) may have a description (dd). [...]]]></description>
			<content:encoded><![CDATA[<p>The trickiest part is to detect if a bookmark has a corresponding description. The export is in the same format that Netscape used for its bookmarks export, which means it is a simple html file with a definition list (<strong>dl</strong>) and a series of definition terms (<strong>dt</strong>). A term (=bookmarks) may have a description (<strong>dd</strong>).</p>
<p>But how do you detect if there&#8217;s a description? It seems the answer was rather simple: use <strong>term.next</strong> and if the <em>next</em> element&#8217;s name is <em>dd</em>, we&#8217;re lucky and have a description. The only problem was that I didn&#8217;t know how to access the name of an element, until I just thought: what if I simply use <em>name</em>? and guess what&#8230; it worked! So term.next.name was exactly what I looked for :-)</p>
<div class="syhi_block"><code><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span><br />
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'hpricot'</span><br />
<br />
doc = <span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;bookmarks.html&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> Hpricot<span style="color:#006600; font-weight:bold;">&#40;</span>f<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span><br />
<br />
bookmarks = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
<br />
<span style="color:#006600; font-weight:bold;">&#40;</span>doc<span style="color:#006600; font-weight:bold;">/</span><span style="color:#996600;">&quot;dl/dt&quot;</span><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>term<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; link = <span style="color:#006600; font-weight:bold;">&#40;</span>term<span style="color:#006600; font-weight:bold;">/</span><span style="color:#996600;">&quot;a&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> term.<span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#9966CC; font-weight:bold;">and</span> term.<span style="color:#9966CC; font-weight:bold;">next</span>.<span style="color:#9900CC;">name</span> == <span style="color:#996600;">'dd'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; desc = term.<span style="color:#9966CC; font-weight:bold;">next</span>.<span style="color:#9900CC;">inner_text</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; desc = <span style="color:#0000FF; font-weight:bold;">nil</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> link.<span style="color:#9900CC;">attr</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'tags'</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tags = link.<span style="color:#9900CC;">attr</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'tags'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;,&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tags = <span style="color:#0000FF; font-weight:bold;">nil</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; bookmarks <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#ff3333; font-weight:bold;">:address</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">=&gt;</span>&nbsp; &nbsp; &nbsp; link.<span style="color:#9900CC;">attr</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'href'</span><span style="color:#006600; font-weight:bold;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#ff3333; font-weight:bold;">:created_at</span> &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">=&gt;</span>&nbsp; &nbsp; &nbsp; link.<span style="color:#9900CC;">attr</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'last_visit'</span><span style="color:#006600; font-weight:bold;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#ff3333; font-weight:bold;">:tags</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">=&gt;</span>&nbsp; &nbsp; &nbsp; tags,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#ff3333; font-weight:bold;">:description</span>&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">=&gt;</span>&nbsp; &nbsp; &nbsp; desc,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#ff3333; font-weight:bold;">:title</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">=&gt;</span>&nbsp; &nbsp; &nbsp; link.<span style="color:#9900CC;">inner_text</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color:#9966CC; font-weight:bold;">end</span></code></div>
<p><a href="http://github.com/sole/snippets/blob/master/web/scrapping/delicious_dump_parse/extract.rb">Source</a> at supersnippets.</p>
<p>I also extended this a bit to save the results into a database, using ActiveRecord, but since <em>each db schema is a different world</em>, I didn&#8217;t post that version here. If anybody thinks it might be useful just let me know.</p>
<p>Also, this code is not very <em>rubyesque</em> yet, suggestions in order to improve it will be really appreciated. I&#8217;m specially thinking about the <em>if &#8230; else</em> parts, I&#8217;m pretty sure there&#8217;s a way to shorten those lines :-)</p>
 <p><a href="http://soledadpenades.com/?flattrss_redirect&amp;id=690&amp;md5=b18abb1777f8d959fefc87e1c4b5e248" title="Flattr" target="_blank"><img src="http://soledadpenades.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://soledadpenades.com/2008/03/25/parsing-a-delicious-export-with-hpricot/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Removing elements with Hpricot</title>
		<link>http://soledadpenades.com/2007/10/05/removing-elements-with-hpricot/</link>
		<comments>http://soledadpenades.com/2007/10/05/removing-elements-with-hpricot/#comments</comments>
		<pubDate>Fri, 05 Oct 2007 10:08:53 +0000</pubDate>
		<dc:creator>sole</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[hpricot]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.soledadpenades.com/2007/10/05/removing-elements-with-hpricot/</guid>
		<description><![CDATA[Something like a month ago, a guy asked me how to remove elements with Hpricot. I told him I would look into it but it&#8217;s been a month already! So I hope I can compensate for the delay with this minitutorial on removing stuff with Hpricot! :-) First I created a simple test page. It&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>Something like a month ago, <a href="http://xbelanch.wordpress.com/">a guy</a> <a href="http://www.soledadpenades.com/2007/06/15/extracting-data-with-hpricot/#comment-44218">asked me</a> how to remove elements with Hpricot. I told him I would look into it but it&#8217;s been a month already! So I hope I can compensate for the delay with this minitutorial on removing stuff with Hpricot! :-)</p>
<p>First I created a simple test page. It&#8217;s got some html elements, some have id&#8217;s, some contain certain text nodes. It looks like this:</p>
<div class="syhi_block"><code><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span>This is a paragraph without attributes<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;bad_attribute&quot;</span>&gt;</span>This is a paragraph with one attribute: id=bad_attribute<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">ul</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span>Element 1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span>This will be removed because the text doesn't begin with an E<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">ul</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">ul</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;second_list&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;border:1px solid red;&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span>Element 1 in the list with id=second_list<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span>element 2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">ul</span></a>&gt;</span></code></div>
<p>The question was how to remove certain individual elements given certain conditions &#8211; more specifically, when the element attributes matched a condition. I don&#8217;t see why he had problems removing stuff with the <strong>remove</strong> method, since that&#8217;s what I have used. Since <strong>search</strong> returns a collection of elements, you just need to get a collection which contains only the element you want to remove, and then apply <strong>remove</strong> to that collection.</p>
<p>Here are three examples:</p>
<h3>Removing the paragraph with id = bad_attribute</h3>
<p>We find out the element using CSS selectors, where the hash means &#8216;id&#8217;.</p>
<div class="syhi_block"><code>doc.<span style="color:#9900CC;">search</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;p#bad_attribute&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">remove</span></code></div>
<h3>Removing all the unordered lists (ul&#8217;s) which have an style attribute</h3>
<p>Again, using CSS selectors:</p>
<div class="syhi_block"><code>doc.<span style="color:#9900CC;">search</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;ul[@style]&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">remove</span></code></div>
<p>There&#8217;s more info about CSS selectors in the <a href="http://code.whytheluckystiff.net/hpricot/wiki/HpricotCssSearch">Hpricot CSS search documentation</a>. One can get very creative with this and allows for filtering almost everything!</p>
<h3>Removing elements whose contents match certain conditions</h3>
<p>When it&#8217;s not enough with CSS selectors, we can perfectly take advantage of ruby!</p>
<p>For example, if you want to remove list items (li&#8217;s) whose text doesn&#8217;t begin with E, you could do it with this:</p>
<div class="syhi_block"><code>doc.<span style="color:#9900CC;">search</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;li&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">collect</span>!<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>node<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; node <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#9966CC; font-weight:bold;">not</span> <span style="color:#006600; font-weight:bold;">/</span>^E<span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span>node.<span style="color:#9900CC;">inner_text</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">compact</span>.<span style="color:#9900CC;">remove</span></code></div>
<p>which is the same as saying:</p>
<ul>
<li>Look for every list item in the document</li>
<li>Take the results of that search (which is an Array of Hpricot Elements) and apply the <a href="http://www.ruby-doc.org/core/classes/Array.html#M002211">collect!</a> function to them</li>
<li><strong>collect!</strong> executes the code in the block for each element and stores the return value in an array</li>
<li>But as it can return nils (when the inner_text doesn&#8217;t begin with &#8216;E&#8217; and hence doesn&#8217;t match our little regular expression), we remove nil values from the array with <a href="http://www.ruby-doc.org/core/classes/Array.html#M002239">compact</a>, so that we don&#8217;t get errors when removing.</li>
<li>And finally, remove the elements which are in the resulting array, with the classical Hpricot remove</li>
</ul>
<p>Note how I used collect! instead of just collect, so that the changes are applied over the search results, and we don&#8217;t get a new array instead.</p>
<p>You should try using <strong>collect</strong> instead of <strong>collect!</strong>, and removing <strong>compact</strong> from the chain, to see what happens.</p>
<h3>Final result</h3>
<p>If one applies all these evil removals to the original code, the final result is this:</p>
<div class="syhi_block"><code><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span>This is a paragraph without attributes<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">ul</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span>Element 1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">ul</span></a>&gt;</span></code></div>
<p>Pretty empty, isn&#8217;t it?!</p>
<h3>Download these examples</h3>
<p>I&#8217;ve uploaded the hpricot_remove_elements.rb and test.html together in a zip file: <a href="/files/hpricot/hpricot_remove_elements.zip">hpricot_remove_elements.zip</a>. For running it, just unpack, and type ruby hpricot_remove_elements.rb</p>
<p>Or open with textmate and press Option+R ;-)</p>
]]></content:encoded>
			<wfw:commentRss>http://soledadpenades.com/2007/10/05/removing-elements-with-hpricot/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Superminigallery: a gallery with ruby, rmagick and builder</title>
		<link>http://soledadpenades.com/2007/06/27/superminigallery-a-gallery-with-ruby-rmagick-and-builder/</link>
		<comments>http://soledadpenades.com/2007/06/27/superminigallery-a-gallery-with-ruby-rmagick-and-builder/#comments</comments>
		<pubDate>Wed, 27 Jun 2007 08:31:52 +0000</pubDate>
		<dc:creator>sole</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[builder]]></category>
		<category><![CDATA[photo]]></category>
		<category><![CDATA[rmagick]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.soledadpenades.com/2007/06/27/superminigallery-a-gallery-with-ruby-rmagick-and-builder/</guid>
		<description><![CDATA[Imagine you have been in a very nice place for holidays. You took a lot of pictures and want to show them to your family and friends, but you don&#8217;t feel like using services like flickr or programs like iPhoto. You just want to put them in your own server and give the url to [...]]]></description>
			<content:encoded><![CDATA[<p>Imagine you have been in a very nice place for holidays. You took a lot of pictures and want to show them to your family and friends, but you don&#8217;t feel like using services like flickr or programs like iPhoto. You just want to put them in your own server and give the url to your friends.</p>
<p>What can you do? Well, you could do like me and create a little script to generate an HTML file, with thumbnails and even watermarked images (just in case some creepy individual decides to use your stuff without asking first).</p>
<p><img src="/imgs/superminigallery.jpg" alt="Superminigallery thumbnail" /></p>
<h3>Requirements</h3>
<p>This script requires a couple of gems to be installed: RMagick and builder (but if you&#8217;ve done some stuff with Rails you might already have them). RMagick is used for dealing with the images and builder is used for generating the XHTML. This is because I didn&#8217;t want to write any html by hand, with their less than and greater than signs, attributes, etc.</p>
<h3>Using it</h3>
<ol>
<li>Create a folder in your computer. For example: holidays.</li>
<li>Then you copy there the pictures you want to show to the world.</li>
<li>Open a terminal and cd to that directory. E.g.
<pre>cd ~/Desktop/holidays</pre>
</li>
<li>Execute the script! E.g.
<pre>ruby ~/code/superminigallery.rb</pre>
<li>Wait until it finishes</li>
</ol>
<p>When it finishes you&#8217;ll find there&#8217;s an <strong>output</strong> folder in the <strong>holidays</strong> folder. That&#8217;s where the index.html file, as well as all the thumbnails and watermarked images are. Simply upload the contents of this folder to your host and let everybody know about it!</p>
<h3>Ok, but show me the code</h3>
<p>The first lines act like a configuration area. You can change the output folder name, so that it is called superoutput, gallery, whatever you like (as long as it is a valid path name).</p>
<p>You may change the sizes of the generated pictures; these sizes are defined in the versions variable. Each pair means [width, height]. For example, the thumbnails are 300 pixels wide and 150 pixels high.</p>
<div class="syhi_block"><code>output_path = <span style="color:#996600;">'output'</span><br />
<br />
versions = <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; <span style="color:#996600;">'thumbnail'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> &nbsp;<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">300</span>,<span style="color:#006666;">150</span><span style="color:#006600; font-weight:bold;">&#93;</span>,<br />
&nbsp; <span style="color:#996600;">'big'</span> &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">=&gt;</span> &nbsp;<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1024</span>,<span style="color:#006666;">768</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
<span style="color:#006600; font-weight:bold;">&#125;</span></code></div>
<p>You can also configure which EXIF tags need to be retrieved. Since their names are too obscure for non-technical savvy people I decided to create this hash for storing the key (Exif tag) and the <em>nice name</em> to show with the value. So instead of showing <em>DateTimeOriginal</em>, it will simply output <em>Taken</em>.</p>
<div class="syhi_block"><code>exif_fields = <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; <span style="color:#996600;">'Taken'</span> &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">=&gt;</span> &nbsp;<span style="color:#996600;">'DateTimeOriginal'</span>,<br />
&nbsp; <span style="color:#996600;">'Camera'</span> &nbsp; &nbsp;<span style="color:#006600; font-weight:bold;">=&gt;</span> &nbsp;<span style="color:#996600;">'Model'</span>,<br />
&nbsp; <span style="color:#996600;">'Exposure'</span> &nbsp;<span style="color:#006600; font-weight:bold;">=&gt;</span> &nbsp;<span style="color:#996600;">'ExposureTime'</span>, &nbsp;<br />
&nbsp; <span style="color:#996600;">'Shutter Speed'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> &nbsp;<span style="color:#996600;">'ShutterSpeedValue'</span><br />
<span style="color:#006600; font-weight:bold;">&#125;</span></code></div>
<p>There are way more tags you could show, but they can be confusing for normal people and only entertain geeks, so it&#8217;s better to keep them down to a minimum.</p>
<p>Declare the builder object, and initialize it with the XHTML header.</p>
<div class="syhi_block"><code>x = <span style="color:#6666ff; font-weight:bold;">Builder::XmlMarkup</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:target</span><span style="color:#006600; font-weight:bold;">=&gt;</span>xhtml, <span style="color:#ff3333; font-weight:bold;">:indent</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
x.<span style="color:#9900CC;">instruct</span>!<br />
x.<span style="color:#9900CC;">declare</span>! <span style="color:#ff3333; font-weight:bold;">:DOCTYPE</span>, <span style="color:#ff3333; font-weight:bold;">:html</span>, <span style="color:#ff3333; font-weight:bold;">:PUBLIC</span>, <span style="color:#996600;">&quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot;</span>, <span style="color:#996600;">&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;</span><br />
x.<span style="color:#9900CC;">html</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#996600;">&quot;xmlns&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;http://www.w3.org/1999/xhtml&quot;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></code></div>
<p>Now it would be amazing to have some styling in the page so that it doesn&#8217;t look so ugly. We can put an style tag inside the head, and use the <strong>text!</strong> method for adding literal text to the builder object:</p>
<div class="syhi_block"><code>x.<span style="color:#9900CC;">style</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#996600;">&quot;type&quot;</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;text/css&quot;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> x.<span style="color:#9900CC;">text</span>! <span style="color:#996600;">&quot;<br />
&nbsp; &nbsp; &nbsp; body{<br />
&nbsp; &nbsp; &nbsp; &nbsp; font-family:georgia,serif<br />
&nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; h1,h2 {<br />
&nbsp; &nbsp; &nbsp; &nbsp; margin-top: 0;<br />
&nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; ...<br />
&nbsp; &nbsp; &nbsp; &quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></code></div>
<p>(the <em>&#8230;</em> means there&#8217;s more code but I have reduced it for clarity purposes)</p>
<p>Now, we need to create the output directory. I haven&#8217;t bothered with outputting error messages if the directory already exists or anything. It will always try to create it:</p>
<div class="syhi_block"><code><span style="color:#9966CC; font-weight:bold;">begin</span><br />
&nbsp; &nbsp; <span style="color:#CC00FF; font-weight:bold;">FileUtils</span>.<span style="color:#9900CC;">mkdir</span> output_path<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">rescue</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></code></div>
<p>We need to create a <a href="http://www.simplesystems.org/RMagick/doc/draw.html">Magick::Draw</a> object for watermarking the images, and define its parameters:</p>
<div class="syhi_block"><code>draw = <span style="color:#6666ff; font-weight:bold;">Magick::Draw</span>.<span style="color:#9900CC;">new</span><br />
draw.<span style="color:#9900CC;">gravity</span> = <span style="color:#6666ff; font-weight:bold;">Magick::CenterGravity</span><br />
draw.<span style="color:#9900CC;">pointsize</span> = <span style="color:#006666;">64</span><br />
draw.<span style="color:#9900CC;">font_family</span> = <span style="color:#996600;">&quot;Helvetica&quot;</span><br />
draw.<span style="color:#9900CC;">font_weight</span> = <span style="color:#6666ff; font-weight:bold;">Magick::BoldWeight</span><br />
draw.<span style="color:#9900CC;">stroke</span> = <span style="color:#996600;">'none'</span><br />
draw.<span style="color:#9900CC;">fill</span> = <span style="color:#996600;">&quot;#ffffff99&quot;</span></code></div>
<p>Basically we are saying: <q>use Helvetica bold 64pt, painting it with white (ffffff) and some transparency (99 for alpha channel)</q>. If you don&#8217;t have Helvetica installed in your system, replace it with your favourite font. </p>
<p>(But since 2007 is Helvetica&#8217;s 50th anniversary, you should do everything possible to use Helvetica!)</p>
<p>Now we open the current directory (where the script was executed) and find all files with jpg and JPG extensions, and sort them. That&#8217;s because sometimes the images don&#8217;t get listed in alphabetical order, and us humans like to see things in sequential order. Specially because they usually are numbered incrementally, and older numbers mean older images, so IMG001 should appear before IMG100.</p>
<div class="syhi_block"><code><span style="color:#CC00FF; font-weight:bold;">Dir</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'*.jpg'</span>,<span style="color:#996600;">'*.JPG'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">sort</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span></code></div>
<p>Read each file into a Magick::Image object:</p>
<div class="syhi_block"><code>img = <span style="color:#6666ff; font-weight:bold;">Magick::Image</span>.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span>f<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">first</span></code></div>
<p>And for each version&#8230;</p>
<div class="syhi_block"><code>versions.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>k,v<span style="color:#006600; font-weight:bold;">|</span></code></div>
<p>&#8230; create the version filename by appending the version name to the filename, like big_IMG_1234.jpg, and the output filename, by prepending the output path to the version filename:</p>
<div class="syhi_block"><code>version_file = &nbsp;k <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'_'</span> <span style="color:#006600; font-weight:bold;">+</span> f<br />
output_img_path = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span>output_path, version_file<span style="color:#006600; font-weight:bold;">&#41;</span></code></div>
<p>If the version is &#8216;thumbnail&#8217;, we&#8217;ll add the image metadata to the builder object. Note how you don&#8217;t need to open or close tags, but just include things in blocks or parenthesis to get the mark up done.</p>
<div class="syhi_block"><code><span style="color:#9966CC; font-weight:bold;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span>k==<span style="color:#996600;">'thumbnail'</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; x.<span style="color:#9900CC;">div</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'class'</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">'picture'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x.<span style="color:#9900CC;">h2</span><span style="color:#006600; font-weight:bold;">&#40;</span>f<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x.<span style="color:#9900CC;">a</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'href'</span><span style="color:#006600; font-weight:bold;">=&gt;</span> version_file.<span style="color:#CC0066; font-weight:bold;">sub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'thumbnail_'</span>, <span style="color:#996600;">'big_'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x.<span style="color:#9900CC;">img</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'src'</span><span style="color:#006600; font-weight:bold;">=&gt;</span>version_file<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x.<span style="color:#9900CC;">dl</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x.<span style="color:#9900CC;">dt</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'Dimensions'</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x.<span style="color:#9900CC;">dd</span><span style="color:#006600; font-weight:bold;">&#40;</span>img.<span style="color:#9900CC;">columns</span>.<span style="color:#9900CC;">to_s</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">' x '</span> <span style="color:#006600; font-weight:bold;">+</span> img.<span style="color:#9900CC;">rows</span>.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exif_fields.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>title, field<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; key = <span style="color:#996600;">&quot;Exif:#{field}&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> img<span style="color:#006600; font-weight:bold;">&#91;</span>key<span style="color:#006600; font-weight:bold;">&#93;</span>!=<span style="color:#0000FF; font-weight:bold;">nil</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x.<span style="color:#9900CC;">dt</span><span style="color:#006600; font-weight:bold;">&#40;</span>title<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x.<span style="color:#9900CC;">dd</span><span style="color:#006600; font-weight:bold;">&#40;</span>img<span style="color:#006600; font-weight:bold;">&#91;</span>key<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></code></div>
<p>Resizing the image is as simple as</p>
<div class="syhi_block"><code>version = img.<span style="color:#9900CC;">crop_resized</span><span style="color:#006600; font-weight:bold;">&#40;</span>v<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>, v<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span></code></div>
<p>crop_resized returns another Image object which we store in the version variable.</p>
<p>Now, if we are dealing with the &#8216;big&#8217; version, we&#8217;ll add the watermark that we prepared at the beginning. That is done with</p>
<div class="syhi_block"><code><span style="color:#9966CC; font-weight:bold;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span>k==<span style="color:#996600;">'big'</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; draw.<span style="color:#9900CC;">annotate</span><span style="color:#006600; font-weight:bold;">&#40;</span>version, <span style="color:#006666;">0</span>, <span style="color:#006666;">0</span>, <span style="color:#006666;">0</span>, <span style="color:#006666;">0</span>, <span style="color:#996600;">&quot;(c) soledadpenades.com&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></code></div>
<p>You can replace my <em>(c) soledadpenades.com</em> with your text, of course!</p>
<p>And for writing the resulting image to disk:</p>
<div class="syhi_block"><code>version.<span style="color:#9900CC;">write</span> output_img_path</code></div>
<p><strong>Very very important:</strong> do not forget to call the Garbage Collector. For some reason which I still haven&#8217;t been able to elucidate, the RMagick gem leaks memory furiously. So if you forget to do a</p>
<div class="syhi_block"><code><span style="color:#CC00FF; font-weight:bold;">GC</span>.<span style="color:#9900CC;">start</span></code></div>
<p>as I did with the first version of the script, your computer will mostly hung if you make it generate a lot of thumbnails. If you look at the current processes with <a href="http://en.wikipedia.org/wiki/Top_%28Unix%29">top</a> or a similar tool, you&#8217;ll find a ruby process eating more and more memory with each picture it processes.</p>
<p>And finally, we just need to output the generated XHTML to index.html:</p>
<div class="syhi_block"><code><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span>output_path, <span style="color:#996600;">'index.html'</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#996600;">'w+'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>file<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; file.<span style="color:#CC0066; font-weight:bold;">puts</span> xhtml<br />
<span style="color:#9966CC; font-weight:bold;">end</span></code></div>
<p>Here&#8217;s the <a href="http://www.soledadpenades.com/files/superminigallery/">result</a> and here&#8217;s <a href="http://github.com/sole/snippets/blob/master/web/superminigallery/superminigallery.rb">the source code</a>. With only 120 lines of code (excluding the license text :D), it&#8217;s way easy to modify to suit your tastes.</p>
<h3>Don&#8217;t tell anyone but&#8230;</h3>
<p>I must confess I got the inspiration for this from herotyc&#8217;s jGallery (edit: link pointed to a dead site so I removed it). But he used a bash script and I thought there should be a way of doing the same with ruby :-)</p>
 <p><a href="http://soledadpenades.com/?flattrss_redirect&amp;id=641&amp;md5=0a1c4fdd92adf00cb0b5042139ecc5a9" title="Flattr" target="_blank"><img src="http://soledadpenades.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://soledadpenades.com/2007/06/27/superminigallery-a-gallery-with-ruby-rmagick-and-builder/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>

