class Bookmarks

try to read bookmarks

Public Class Methods

new(search_term = '') click to toggle source
# File lib/bookmarks.rb, line 38
def initialize(search_term = '')
  @conf = BConfig.new
  begin
    local_bookmarks = JSON.parse(open(@conf.bookmarks).read)
    @chrome_bookmarks = local_bookmarks['roots']['bookmark_bar']['children']
  rescue
    puts "Warning: ".yel +
      "Chrome JSON Bookmarks not found."
    puts "Suggest: ".grn +
      "booker --install bookmarks"
    @chrome_bookmarks = {}
  end

  # clean search term, set urls
  @searching = /#{search_term}/i
  @allurls = []
  parse
end

Public Instance Methods

autocomplete() click to toggle source

output for zsh autocompetion, print out id, title and cleaned url

# File lib/bookmarks.rb, line 58
def autocomplete
  @allurls.each do |url|
    name = clean_name(url)
    link = clean_link(url)
    puts url.id + ":" + name + ":" + link
  end
end
bookmark_url(id) click to toggle source

get link (from id number)

# File lib/bookmarks.rb, line 83
def bookmark_url(id)
  @allurls.each do |url|
    return url.url if id == url.id
  end
end
clean_name(url) click to toggle source

clean title for completion, delete anything not allowed in linktitle

# File lib/bookmarks.rb, line 67
def clean_name(url)
  name = url.folder + ' |' + url.title.gsub(/[^a-z0-9\-\/_ ]/i, '')
  name.gsub!(/\-+/, '-')
  name.gsub!(/[ ]+/,' ')
  name = name.window(TERMWIDTH)
end
parse(root=nil) click to toggle source

recursively parse gc bookmarks

# File lib/bookmarks.rb, line 90
def parse(root=nil)
  # root - parent folder in ruby
  root = Folder.new @chrome_bookmarks if root.nil?

  # all current urls, to hash
  root.json.each {|x| parse_link root.title, x }

  # all next-level folders, to array
  root.json.each {|x| parse_folder root, x }
end
parse_folder(base, link) click to toggle source

discover and parse folders

# File lib/bookmarks.rb, line 112
def parse_folder(base, link)
  if link['type'] == 'folder'
    title = base.title + link['name'] + '/'
    subdir = Folder.new(title, link['children'])
    parse(subdir)
  end
end