On Sun, Dec 16, 2012 at 11:18 AM, 7stud -- <[email protected]> wrote:
> Robert Klemme wrote in post #1089225:
>>
>> puts dom.xpath('/html/body//pre/text()').map(&:to_s)
>
> Calling map() is redundant because puts calls to_s on its arguments.
Right, thanks for the reminder! That was an artifact of IRB testing. :-)
>> 2) to print all of the non-tagged text between the
>> "body" comments
>
> Your html doesn't even test your requirements because there is no text
> after the body="end' comment. And there is no non-tagged text:
I overlooked the comment thing.
#!/usr/bin/ruby
require 'nokogiri'
# require 'irb'
text = <<HTML
<html>
<head>
<title>My Title</title>
</head>
<body>
<h1>My Heading</h1>
<strong>From:</strong>Me<br>
<strong>Date:</strong> Wed Dec 05 2012 - 18:17:49 EST
<!-- body="start" -->
<p>
text line 1
<br>
text line 2
<br>
text line 3
<br>
<p><pre>
very important text
more important text
would you believe even more important text?
</pre>
<p><!-- body="end" -->
not to print
</body>
</html>
HTML
dom = Nokogiri.HTML(text)
puts dom.xpath('/html/body//pre/text()')
puts '---'
puts dom.xpath('//text()[contains(preceding::comment(),"start") and
contains(following::comment(),"end") and not(ancestor::pre)]')
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
-- You received this message because you are subscribed to the Google Groups
ruby-talk-google group. To post to this group, send email to
[email protected]. To unsubscribe from this group, send email
to [email protected]. For more options, visit this
group at https://groups.google.com/d/forum/ruby-talk-google?hl=en