2010-03-01から1ヶ月間の記事一覧

hpricot:Hpricot::Elements#empty

まんま >> require "hpricot" => true >> doc = Hpricot("<p> We have <i>so much</i> to say.</p>") => #<Hpricot::Doc {elem <p> " We have " {elem <i> "so much" </i>} " to say." </p>}> >> doc.search("i").empty => #<Hpricot::Elements[{emptyelem <i>}]> >> doc.to_html => "<p> We have <i></i> to say.</p>"</hpricot::elements[{emptyelem>

hpricot:Hpricot::Elements#before

>> require "hpricot" => true >> doc = Hpricot('<p>foo1</p><p>foo2</p>') => #<Hpricot::Doc {elem <p> "foo1" </p>} {elem <p> "foo2" </p>}> >> (doc / "p").before("foo") => #<Hpricot::Elements[{elem <p> "foo1" </p>}, {elem <p> "foo2" </p>}]> >> doc.to_html => "foo<p>foo1</p>foo<p>foo2</p>" >> doc = Hpri…

hpricot:Hpricot::Elements#attr, #set

まんま >> require "hpricot" => true >> doc = Hpricot('<a href="http://www.foo.org/">foo</a>') => #<Hpricot::Doc {elem <a href="http://www.foo.org/"> "foo" </a>}> >> doc.search("a").attr("href") => "http://www.foo.org/" >> doc = Hpricot('<p>foo1</p><p>foo2</p>') => #<Hpricot::Doc {elem <p> "foo1" </p>} …

hpricot:Hpricot::Elements#append

>> require "hpricot" => true >> doc = Hpricot('<p>foo1</p><p>foo2</p>') => #<Hpricot::Doc {elem <p> "foo1" </p>} {elem <p> "foo2" </p>}> >> (doc / "p").append("foo") => #<Hpricot::Elements[{elem <p> "foo1" "foo" </p>}, {elem <p> "foo2" "foo" </p>}]> >> doc.to_html => "<p>foo1foo</p><p>foo2foo</p>"

hpricot:Hpricot::Elements#after

>> require "hpricot" => true >> doc = Hpricot('<p>foo1</p><p>foo2</p>') => #<Hpricot::Doc {elem <p> "foo1" </p>} {elem <p> "foo2" </p>}> >> (doc / "p").after("foo") => #<Hpricot::Elements[{elem <p> "foo1" </p>}, {elem <p> "foo2" </p>}]> >> doc.to_html => "<p>foo1</p>foo<p>foo2</p>foo" >> doc = Hpric…

hpricot:Hpricot::Elements#add_class

途中まで、まんま >> require "hpricot" => true >> doc = Hpricot('<p>foo1</p><p>foo2</p>') => #<Hpricot::Doc {elem <p> "foo1" </p>} {elem <p> "foo2" </p>}> >> (doc/"p").add_class("bacon") => #<Hpricot::Elements[{elem <p class="bacon"> "foo1" </p>}, {elem <p class="bacon"> "foo2" </p>}]> >> doc.to_html => "

hpricot:Hpricot::Elements#/, #search

>> require "hpricot" => true >> doc = Hpricot('<p>foo1</p><p>foo2</p>') => #<Hpricot::Doc {elem <p> "foo1" </p>} {elem <p> "foo2" </p>}> >> doc / :p => #<Hpricot::Elements[{elem <p> "foo1" </p>}, {elem <p> "foo2" </p>}]> >> doc / "p" => #<Hpricot::Elements[{elem <p> "foo1" </p>}, {elem <p> "foo2" </p>

hpricot:Hpricot::Elements#%, #at

Elements.filter 不明… >> require "hpricot" => true >> doc = Hpricot('<p>foo1</p><p>foo2</p>') => #<Hpricot::Doc {elem <p> "foo1" </p>} {elem <p> "foo2" </p>}> >> doc % :p => {elem <p> "foo1" </p>} >> doc.at :p => {elem <p> "foo1" </p>} >> doc % "p" => {elem <p> "foo1" </p>} >> doc % "//p" => {elem <p>…</p>

hpricot:Hpricot::Elements.expand

>> require "hpricot" => true >> doc = Hpricot('<p>foo1</p><p>foo2</p>') => #<Hpricot::Doc {elem <p> "foo1" </p>} {elem <p> "foo2" </p>}> >> doc.search("//p") => #<Hpricot::Elements[{elem <p> "foo1" </p>}, {elem <p> "foo2" </p>}]> >> doc.search("//p")[0] => {elem <p> "foo1" </p>} >> doc.search("//p")…

hpricot:Hpricot::Elements:Getting More Detailed

マニュアルだと、 doc.search("h3").each do |h3| while ele = h3.next_sibling ary << ele # stuff away all the elements under the h3 end end となっているが、これはうまく動くのか? ary = [] doc.search("h3").each do |h3| ele = h3 while ele = ele…

hpricot:Hpricot::Elements:Altering Elements

まんま >> require "hpricot" => true >> doc = Hpricot("That's my <b>spoon</b>, Tyler.") => #<Hpricot::Doc "That's my " {elem <b> "spoon" </b>} ", Tyler."> >> doc.at("b").swap("<i>fork</i>") => [{elem <i> "fork" </i>}] >> doc.to_html => "That's my <i>fork</i>, Tyler."

hpricot:Hpricot::Elements:Continuing Searches

かなり、まんま >> require "hpricot" => true >> doc = Hpricot('<div><p><a href="http://www.foo.org/">foo</a></p><p><img src="foo.png"></p></div>') => #<Hpricot::Doc {elem <div> {elem <p> {elem <a href="http://www.foo.org/"> "foo" </a>} </p>} {elem <p> {emptyelem <img src="foo.png">} </p>} </div>}> >> elemen…

hpricot:An Hpricot Showcase:Hpricot XML:Transforming XML

まんま require 'hpricot' xml = <<XML <entries> <entry> <name>c</name> <value>d</value> </entry> </entries> XML Hpricot::Builder.set :indent, 2 doc = Hpricot::XML(xml) doc.at('entry').before do tag!'entry' do tag!'name', 'a' tag!'value', 'b' end end puts doc で、 <entries> <entry><name>a</name><value>b</value></entry></entries>

hpricot:An Hpricot Showcase:Hpricot XML:The Hpricot.XML Method

>> require "hpricot" => true >> doc = Hpricot.XML('<foo>foo 1</foo> hoge <bar>bar 1</bar> <foo>foo 2</foo>') => #<Hpricot::Doc {elem <foo> "foo 1" </foo>} " hoge " {elem <bar> "bar 1" </bar>} " " {elem <foo> "foo 2" </foo>}> >> (doc/:foo).each do |item| ?> p item >> end {elem <foo> "foo 1" </foo>} {…

hpricot:An Hpricot Showcase:Hpricot Challenge:Finding the element with the largest attribute

まんま >> require "hpricot" => true >> doc = Hpricot('<image height="300">x</image><image hieght="200">y</image>') => #<Hpricot::Doc {elem <image height="300"> "x" </image>} {elem <image hieght="200"> "y" </image>}> >> (doc / 'image').sort_by {|img| img['height'].to_i}[-1] => {elem <image height="300">…</image>

hpricot:An Hpricot Showcase:Hpricot Challenge:Finding all text elements within a html document

まんま >> require "hpricot" => true >> doc = Hpricot('<p>foo 1</p> hoge <p>foo 2</p>') => #<Hpricot::Doc {elem <p> "foo 1" </p>} " hoge " {elem <p> "foo 2" </p>}> >> doc.search("*") => #<Hpricot::Elements[{elem <p> "foo 1" </p>}, "foo 1", " hoge ", {elem <p> "foo 2" </p>}, "foo 2"]> >> doc.search("*").…

hpricot:An Hpricot Showcase:Hpricot Challenge:Getting namespaced element

まんま >> require "hpricot" => true >> doc = Hpricot('<evil:Tag>FAIL</evil:Tag>') => #<Hpricot::Doc {elem <evil:tag> "FAIL" </evil:Tag>}> >> doc.%('evil:tag') => {elem <evil:tag> "FAIL" </evil:Tag>}

hpricot:An Hpricot Showcase:Hpricot Challenge:Order Of Elements returned

>> require "hpricot" => true >> doc = Hpricot('<form><input name="1" /><input name="2" /><input name="3" /></form>') => #<Hpricot::Doc {elem <form> {emptyelem <input name="1">} {emptyelem <input name="2">} {emptyelem <input name="3">} </form>}> >> x = doc.search("form input") => #

hpricot:An Hpricot Showcase:Hpricot Challenge:Getting a hold of malformed data that isn’t in an element.

まんま require "hpricot" X =<<EOS <tr><td class="oreinfoleft"><b class=ul>Veldspar</b><br> <b>Units per batch:</b> 333<br> <b>Volume:</b> 0.1<br> <b>Cargo per batch:</b> 33.33 <td class="oreinforight"><b>Minerals:</b> Tritanium 100%<br> <b>Variations:</b> Concentrated Veldspar (+5%), Dense Veldspar (+1…</td></td></eos>

hpricot:An Hpricot Showcase:Hpricot Challenge:Warning while using :last-child

まんま require 'hpricot' foo = "<div class=\"blah\"> <p>test</p> <p>go away</p> </div>" doc = Hpricot(foo) p doc (doc/"p:last-child").remove p doc (doc/"p:last-of-type").remove p doc で、 #<Hpricot::Doc {elem <div class="blah"> "\n " {elem <p> "test" </p>} "\n " {elem <p> "go away" </p>} "\n" </div>}> /u…

hpricot:An Hpricot Showcase:Hpricot Challenge:Wildcard in Attribute Search

まんま require "hpricot" X =<<EOS <a href="blah.com" id="p-1">Some text</a> <a href="blah.com" id="p-2">Some text</a> <a href="blah.com" id="p-3">Some text</a> <a href="blah.com" id="P-4">Some text</a> EOS doc = Hpricot(X) p doc/"a[@id^=p-]" で、 #<Hpricot::Elements[{elem <a href="blah.com" id="p-1"> "So…</hpricot::elements[{elem>

hpricot:An Hpricot Showcase:Hpricot Challenge:Putting embedded script in attributes

>> require "hpricot" => true >> a = Hpricot('<a href="http://w.w.w/">Hello</a>') => #<Hpricot::Doc {elem <a href="http://w.w.w/"> "Hello" </a>}> >> (a/"a").first[:href] = "" => "" >> a.to_html => "<a href=\"&lt;? echo 'boo' ?&gt;\">Hello</a>" >> a = Hpricot('

hpricot:An Hpricot Showcase:Hpricot Challenge:Selecting the value of an element's attribute using just XPath

ほぼ、まんま require "hpricot" X =<<EOS <div id="foo"> Fnord! </div> EOS doc = Hpricot(X) #p (doc/"div[@id]").get_attribute("id") p (doc/"div[@id]").first.get_attribute("id") p (doc/"div").first.get_attribute("id") p (doc/"div[@id]").first で、 "foo" "foo" {elem <div id="foo"> </div>…

hpricot:An Hpricot Showcase:Hpricot Challenge:Selecting the text from actual node only

まんま require "hpricot" X =<<EOS <div id="myid"> <h4>title</h4> sample text </div> EOS doc = Hpricot(X) p doc.at('#myid').children.select{|e| e.text?} で、 ["\n ", "\n sample text\n"]

hpricot:An Hpricot Showcase:Hpricot Challenge:Selecting part of a String with Dynamic Contents

まんま >> require "hpricot" => true >> doc = Hpricot('<a href="out.php?id=1112&url=www.website.com"></a><a href="out.php?id=2232&url=www.website.com"></a><a href="out.php?id=3346&url=www.website.com"></a>') => #<Hpricot::Doc {emptyelem <a href="out.php?id=1112&url=www.website.com">} {emptyelem </hpricot::doc>

hpricot:An Hpricot Showcase:Hpricot Challenge:Getting the contents of tag multiple attributes?

まんま >> require "hpricot" => true >> doc = Hpricot('<a href="http://www.foo1.org/">foo1</a><a href="http://www.foo2.org/">foo2</a>') => #<Hpricot::Doc {elem <a href="http://www.foo1.org/"> "foo1" </a>} {elem <a href="http://www.foo2.org/"> "foo2" </a>}> >> doc.search('a[@href]').map {|x| x['href']} =…

hpricot:An Hpricot Showcase:Hpricot Challenge:Getting the contents of a tag attribute?

まんま >> require "hpricot" => true >> doc = Hpricot('<a href="http://www.foo1.org/">foo1</a><a href="http://www.foo2.org/">foo2</a>') => #<Hpricot::Doc {elem <a href="http://www.foo1.org/"> "foo1" </a>} {elem <a href="http://www.foo2.org/"> "foo2" </a>}> >> doc.search('a').first[:href] => "http://www.…

hpricot:An Hpricot Showcase:Hpricot Challenge:Retrieving non-text elements only?

まんま >> require "hpricot" => true >> doc = Hpricot('<p>foo 1</p> hoge <p>foo 2</p>') => #<Hpricot::Doc {elem <p> "foo 1" </p>} " hoge " {elem <p> "foo 2" </p>}> >> doc.search("*").grep(Hpricot::Elem) => [{elem <p> "foo 1" </p>}, {elem <p> "foo 2" </p>}] >> doc.search("*").select{ |e| e.elem? } => […

hpricot:An Hpricot Showcase:Hpricot Challenge:Follow-up to `Preceding/Following Children' (text nodes)

まんま require "hpricot" X =<<EOS <div> <A>...</A> ... <B>...</B> <a name='articlestart'> </a> <C>...</C> Some text <tag> </tag> More text Even more text <D>...</D> </div> EOS doc = Hpricot(X) p doc.at('//a[@name="articlestart"]') p doc.at('//a[@name="articlestart"]').following_siblings p doc.at('//a[@…

hpricot:An Hpricot Showcase:Hpricot Challenge:Preceding / Following Children?

まんま require "hpricot" X =<<EOS <div> <A>...</A> ... <B>...</B> <a name='articlestart'> </a> <C>...</C> ... <D>...</D> </div> EOS doc = Hpricot(X) p doc.at('//a[@name="articlestart"]') p doc.at('//a[@name="articlestart"]').following_siblings で、 {elem <a name="articlestart"> " " </a>} #