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.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/bookmarks.rb', line 35 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 |
Instance Method Details
#autocomplete ⇒ Object
output for zsh autocompetion, print out id, title and cleaned url
55 56 57 58 59 60 61 |
# File 'lib/bookmarks.rb', line 55 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)
80 81 82 83 84 |
# File 'lib/bookmarks.rb', line 80 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
72 73 74 75 76 77 |
# File 'lib/bookmarks.rb', line 72 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
64 65 66 67 68 69 |
# File 'lib/bookmarks.rb', line 64 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
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/bookmarks.rb', line 87 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
109 110 111 112 113 114 115 |
# File 'lib/bookmarks.rb', line 109 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
99 100 101 102 103 104 105 106 |
# File 'lib/bookmarks.rb', line 99 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 |