Sha256: 76141fb2e1ad81e8f82d7a0615969ca157121a153f8e0fc86d8cad68f9d62ce5

Contents?: true

Size: 850 Bytes

Versions: 20

Compression:

Stored size: 850 Bytes

Contents

require "chop/diff"
      
module Chop
  class UnorderedList < Diff
    self.default_selector = "ul"
    self.rows_finder = ->(root) { root.all("li") }
    self.cells_finder = ->(row) { [row] }

    def nested
      self.rows_finder = ->(root) {
        recurse_tree [], root
      }
      self.text_finder = ->(cell) {
        cell.chop_prefix + cell.all(:xpath, "*[not(self::ul)]").map(&:text).join(" ").strip
      }
    end

    private

    def recurse_tree structure, root, prefix: "- "
      root.all(:xpath, "./li").each do |li|
        li.define_singleton_method(:chop_prefix) { prefix }
        structure << li

        if li.has_xpath?("./ul")
          root = li.find(:xpath, "./ul")
          structure = recurse_tree(structure, root, prefix: prefix + "  ")
        end
      end
      structure
    end
  end

  Ul = UnorderedList
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
chop-0.30.0 lib/chop/unordered_list.rb
chop-0.29.0 lib/chop/unordered_list.rb
chop-0.28.1 lib/chop/unordered_list.rb
chop-0.28.0 lib/chop/unordered_list.rb
chop-0.27.0 lib/chop/unordered_list.rb
chop-0.26.1 lib/chop/unordered_list.rb
chop-0.26.0 lib/chop/unordered_list.rb
chop-0.25.2 lib/chop/unordered_list.rb
chop-0.25.1 lib/chop/unordered_list.rb
chop-0.25.0 lib/chop/unordered_list.rb
chop-0.24.0 lib/chop/unordered_list.rb
chop-0.23.8 lib/chop/unordered_list.rb
chop-0.23.7 lib/chop/unordered_list.rb
chop-0.23.6 lib/chop/unordered_list.rb
chop-0.23.5 lib/chop/unordered_list.rb
chop-0.23.4 lib/chop/unordered_list.rb
chop-0.23.3 lib/chop/unordered_list.rb
chop-0.23.2 lib/chop/unordered_list.rb
chop-0.23.1 lib/chop/unordered_list.rb
chop-0.23.0 lib/chop/unordered_list.rb