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_link(url)
click to toggle source
clean link for completion, remove strange things from any linkurls
# File lib/bookmarks.rb, line 75 def clean_link(url) link = url.url.gsub(/[,'"&?].*/, '') link.gsub!(/.*:\/+/,'') link.gsub!(/ /,'') link = link[0..TERMWIDTH-CODEWIDTH] # need space for term. color codes 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
parse_link(base, link)
click to toggle source
add link to results
# File lib/bookmarks.rb, line 102 def parse_link(base, link) checking = [base, link['name'], link['url'], link['id']] if checking.any? {|x| @searching.match x } if link['type'] == 'url' @allurls.push(Bookmark.new base, link['name'], link['url'], link['id']) end end end