Class: Bookmarks
- Inherits:
-
Object
- Object
- Bookmarks
- Defined in:
- lib/bookmarks.rb
Constant Summary collapse
- LOCAL_BOOKMARKS =
"/Library/Application Support/Google/Chrome/Profile 1/bookmarks"- RAW_JSON_BOOKMARKS =
JSON.parse(open(ENV['HOME'] + LOCAL_BOOKMARKS).read)
- CHROME_BOOKMARKS =
RAW_JSON_BOOKMARKS['roots']['bookmark_bar']['children']
Instance Method Summary collapse
-
#autocomplete ⇒ Object
output for zsh.
-
#bookmark_id(url) ⇒ Object
parse a bookmark’s id from tab completed form.
-
#bookmark_url(id) ⇒ Object
get link (from id number).
-
#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.
40 41 42 43 44 |
# File 'lib/bookmarks.rb', line 40 def initialize(search_term) @searching = /#{search_term}/i @allurls = [] parse end |
Instance Method Details
#autocomplete ⇒ Object
output for zsh
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/bookmarks.rb', line 47 def autocomplete width = 80 @allurls.each do |url| # delete anything not allowed in linktitle dirty_t = url.title.gsub(/[^a-z0-9\-\/_]/i, '') dirty_t = url.folder + dirty_t dirty_t = dirty_t.gsub(/\-+/, '-') dirty_t = dirty_t.gsub(/ /,'') name = dirty_t.window(width) # remove strange things from any linkurls dirty_u = url.url.gsub(/[,'"&?].*/, '') dirty_u = dirty_u.gsub(/.*:\/+/,'') dirty_u = dirty_u.gsub(/ /,'') #right = [url.url.length, COLWIDTH-titlewidth].max #link = dirty_u[0..right-1] link = dirty_u[0..width] # print out title and cleaned url, for autocompetion puts url.id + ":" + name + ":" + link end end |
#bookmark_id(url) ⇒ Object
parse a bookmark’s id from tab completed form
72 73 74 |
# File 'lib/bookmarks.rb', line 72 def bookmark_id(url) url[0..5].gsub(/[^0-9]/, '') end |
#bookmark_url(id) ⇒ Object
get link (from id number)
77 78 79 80 81 82 83 |
# File 'lib/bookmarks.rb', line 77 def bookmark_url(id) bm_url = '' @allurls.each do |url| bm_url = url.url if id == url.id end bm_url end |
#parse(root = nil) ⇒ Object
recursively parse gc bookmarks
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/bookmarks.rb', line 86 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
108 109 110 111 112 113 114 |
# File 'lib/bookmarks.rb', line 108 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
98 99 100 101 102 103 104 105 |
# File 'lib/bookmarks.rb', line 98 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 |