Class: Bookmarks
- Inherits:
-
Object
- Object
- Bookmarks
- Defined in:
- lib/bookmarks.rb
Overview
try to read bookmarks
Instance Method Summary collapse
-
#autocomplete ⇒ Object
output for zsh autocompetion, print out id, title and cleaned url.
-
#bookmark_url(id) ⇒ Object
get link (from id number).
-
#clean_link(url) ⇒ Object
clean link for completion, remove strange things from any linkurls.
-
#clean_name(url) ⇒ Object
clean title for completion, delete anything not allowed in linktitle.
-
#initialize(search_term) ⇒ Bookmarks
constructor
A new instance of Bookmarks.
-
#parse(root = nil) ⇒ Object
recursively parse gc bookmarks.
-
#parse_folder(base, link) ⇒ Object
discover and parse folders.
-
#parse_link(base, link) ⇒ Object
add link to results.
Constructor Details
#initialize(search_term) ⇒ Bookmarks
Returns a new instance of Bookmarks.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/bookmarks.rb', line 36 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 + "web --install bookmarks" @chrome_bookmarks = {} end # clean search term, set urls @searching = /#{search_term}/i @allurls = [] parse end |
Instance Method Details
#autocomplete ⇒ Object
output for zsh autocompetion, print out id, title and cleaned url
56 57 58 59 60 61 62 |
# File 'lib/bookmarks.rb', line 56 def autocomplete @allurls.each do |url| name = clean_name(url) link = clean_link(url) puts url.id + ":" + name + ":" + link end end |
#bookmark_url(id) ⇒ Object
get link (from id number)
81 82 83 84 85 |
# File 'lib/bookmarks.rb', line 81 def bookmark_url(id) @allurls.each do |url| return url.url if id == url.id end end |
#clean_link(url) ⇒ Object
clean link for completion, remove strange things from any linkurls
73 74 75 76 77 78 |
# File 'lib/bookmarks.rb', line 73 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) ⇒ Object
clean title for completion, delete anything not allowed in linktitle
65 66 67 68 69 70 |
# File 'lib/bookmarks.rb', line 65 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) ⇒ Object
recursively parse gc bookmarks
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/bookmarks.rb', line 88 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) ⇒ Object
discover and parse folders
110 111 112 113 114 115 116 |
# File 'lib/bookmarks.rb', line 110 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) ⇒ Object
add link to results
100 101 102 103 104 105 106 107 |
# File 'lib/bookmarks.rb', line 100 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 |