Skip to content

Instantly share code, notes, and snippets.

@silentrob
Created July 21, 2010 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save silentrob/484766 to your computer and use it in GitHub Desktop.
Save silentrob/484766 to your computer and use it in GitHub Desktop.
UC 1
<p id="test">A simple <b>test</b> string.</p>
A simple <b>test</b> string.
UC 2
<p>This is a test file</p>
This is a test file
Looping 1
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
Deep Searching
image.gif
Looping 2
http://www.joyent.com/developers/
http://www.joyent.com/contact/
https://my.joyent.com/
/
http://www.joyent.com
http://www.joyent.com/technology/
http://www.joyent.com/services/
http://www.joyent.com/customers/
/category/news/
http://www.joyent.com/support/
http://www.joyent.com/partners/
/technology/
/technology/smartplatform/
/services/cloudhosting/
/services/
/technology/smartmachines/
/technology/smartdatacenter/
http://smart.joyent.com/
/customers/
/services/performance-comparison/
/2010/03/dell-chooses-joyent-software-to-power-cloud-computing/
http://rev1.www.joyent.com/
/services/network-hardware-specs/
/services/joyent-professional-services/
/category/events/
/whitepapers/
/about/
/about/board/
/about/management/
/about/policies/
/partners/
/partners/partner-program/
/developers/
http://joyeur.wordpress.com
http://codesnippets.joyent.com/
http://wiki.joyent.com/
http://discuss.joyent.com
<html>
<p>This is a test file</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
<div id="deep">
<p>Para 1</p>
<p>Para 2</p>
<p>Para 1<img id="img" alt="" src="image.gif"/></p>
</div>
</html>
Hpricot = require('./hpricot').Hpricot;
open = require('./hpricot').open;
sys = require('sys');
// Use Case one
sys.puts("UC 1");
var doc = new Hpricot("<p id='test'>A simple <b>test</b> string.</p>");
var doc1 = doc.search('#test');
sys.puts(doc1.matches[0].outerHTML);
sys.puts(doc1.matches[0].innerHTML);
// Use case two
open('test.html', function(doc){
sys.puts("UC 2");
var doc1 = doc.search('p');
sys.puts(doc1.matches[0].outerHTML);
sys.puts(doc1.matches[0].innerHTML);
})
// Looping 1
open('test.html', function(doc){
sys.puts("Looping 1");
var doc1 = doc.search('li');
doc1.matches.forEach(function(item){
sys.puts(item.outerHTML);
})
})
// Deeping searching
// TODO: Fix race condition between these two blocks.
open('test.html', function(doc){
sys.puts("Deep Searching");
var doc1 = doc.search('#deep');
var doc2 = doc.search("img",doc1.matches[0]);
sys.puts(doc2.matches[0].src);
});
// Looping 2
// Another Example of looping
open('http://www.joyent.com/', function(doc){
sys.puts("Looping 2");
var doc = doc.search('a');
var links = [];
doc.matches.forEach(function(a){
links.push(a.href);
})
sys.puts((links.unique()).join('\n\r'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment