From: 7stud -- Date: 2012-12-17T06:17:43+09:00 Subject: Re: print - and strip text between tags using Nokogiri require "nokogiri" class PlainTextExtractor < Nokogiri::XML::SAX::Document attr_reader :plaintext # Initialize the state of interest variable with false def initialize @interesting = false @pre = false @plaintext = "" end def start_element(name, attrs = []) if name == "pre" @pre = true end end # This method is called whenever a comment occurs and # the comments text is passed in as string. def comment(string) case string.strip # strip leading and trailing whitespaces when /^body="start"/ # match starting comment @interesting = true when /^body="end"/ @interesting = false # match closing comment end end # This callback method is called with any string between # a tag. def characters(string) if @interesting and not @pre @plaintext << string end end end pte = PlainTextExtractor.new parser = Nokogiri::HTML::SAX::Parser.new(pte) parser.parse_file ARGV[0] p pte.plaintext --output:-- "\n\nwatching the news\n\nI feel guilty\n\nfor being alive\n\n" -- Posted via http://www.ruby-forum.com/.