class Markio::Parser

Public Class Methods

new(data) click to toggle source
# File lib/deps/markio/parser.rb, line 6
def initialize data
  @document = Nokogiri::HTML data
end

Public Instance Methods

parse() click to toggle source
# File lib/deps/markio/parser.rb, line 10
def parse
  bookmarks = []
  traverse(@document, []) do |bookmark|
    bookmarks << bookmark
  end
  consolidate bookmarks
end

Private Instance Methods

consolidate(bookmarks) click to toggle source

Makes sure there are no repeated links on the final bookmarks.

# File lib/deps/markio/parser.rb, line 66
def consolidate(bookmarks)
  consolidated = []

  # Will only add to the final Array
  # links that are unique within the set
  bookmarks.each do |b|
    index = consolidated.index b

    consolidated << b if index.nil?
  end

  consolidated
end
parse_bookmark(node, folders) click to toggle source
# File lib/deps/markio/parser.rb, line 39
def parse_bookmark(node, folders)
  data = {}
  node.attributes.each do |k, a|
    data[k.downcase] = a.value
  end
  bookmark               = Bookmark.new
  bookmark.href          = data['href']
  bookmark.title         = node.text
  bookmark.folders       = Array.new(folders)
  bookmark.tags          = parse_tags(data['tags'])
  bookmark.add_date      = parse_timestamp data['add_date']
  bookmark.last_visit    = parse_timestamp data['last_visit']
  bookmark.last_modified = parse_timestamp data['last_modified']
  bookmark
end
parse_tags(tags) click to toggle source
# File lib/deps/markio/parser.rb, line 59
def parse_tags(tags)
  tags ? tags.split(/,/x) : []
end
parse_timestamp(timestamp) click to toggle source
# File lib/deps/markio/parser.rb, line 55
def parse_timestamp(timestamp)
  Time.at(timestamp.to_i).to_datetime if timestamp
end
traverse(node, folders) { |parse_bookmark(child, folders)| ... } click to toggle source
# File lib/deps/markio/parser.rb, line 20
def traverse node, folders, &block
  node.xpath("./*").each do |child|
    case child.name.downcase
      when 'dl'
        traverse child, folders, &block
        folders.pop
      when 'a'
        yield parse_bookmark(child, folders)
      when 'h3'
        folders << child.text
      else
        if child.children.any?
          traverse child, folders, &block
        end
    end
  end

end