Sha256: b49d60dffc3856291e780359a434ec2b47678b75757dd9119528340dec445dea

Contents?: true

Size: 1 KB

Versions: 8

Compression:

Stored size: 1 KB

Contents

require "nokogiri"

class Nori
  module Parser

    # = Nori::Parser::Nokogiri
    #
    # Nokogiri SAX parser.
    module Nokogiri

      class Document < ::Nokogiri::XML::SAX::Document
        attr_accessor :options

        def stack
          @stack ||= []
        end

        def start_element(name, attrs = [])
          stack.push Nori::XMLUtilityNode.new(options, name, Hash[*attrs.flatten])
        end

        def end_element(name)
          if stack.size > 1
            last = stack.pop
            stack.last.add_node last
          end
        end

        def characters(string)
          stack.last.add_node(string) unless string.strip.length == 0 || stack.empty?
        end

        alias cdata_block characters

      end

      def self.parse(xml, options)
        document = Document.new
        document.options = options
        parser = ::Nokogiri::XML::SAX::Parser.new document
        parser.parse xml
        document.stack.length > 0 ? document.stack.pop.to_hash : {}
      end

    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
nori-2.4.0 lib/nori/parser/nokogiri.rb
nori-ng-1.6-2.3.0 lib/nori/parser/nokogiri.rb
nori-2.3.0 lib/nori/parser/nokogiri.rb
nori-2.2.0 lib/nori/parser/nokogiri.rb
nori-2.1.0 lib/nori/parser/nokogiri.rb
nori-2.0.4 lib/nori/parser/nokogiri.rb
nori-2.0.3 lib/nori/parser/nokogiri.rb
nori-2.0.0 lib/nori/parser/nokogiri.rb