booker 0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7e03cf87b4d4677e8ee2cd82e676e2c37e328c53
4
+ data.tar.gz: 67c4a45af1cd4f92b9cf47c01e368bc0a3de5aff
5
+ SHA512:
6
+ metadata.gz: e3b1ff12d26bd927a4cfbccd737f9cc9b3ede569ec955529ac5b4a729aafe2a29be87597f291765e6a9a1216f3c57a89118999d16df99cda6a98ffdab38b868e
7
+ data.tar.gz: cebe4d14c2d95831891035056d5e9567e4435c0e3ed70eeffdedc30ca24777b1dede6146b75ae6d1d62e11df4f8eedc7fe8cedaeff15ede44e033ad1eb5d532d
data/bin/web ADDED
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+
3
+
4
+ require_relative "../lib/booker"
5
+
6
+
7
+ # web by jeremy warner
8
+ # open up a website or search in google chrome from cli
9
+ # also show and/or open up google chrome bookmarks
10
+
11
+
12
+ VERSION = "0.1"
13
+ HELP_BANNER = <<-EOS
14
+ Open browser:
15
+ $ web [options]
16
+ Options:
17
+ --bookmark, -b: explicity open bookmark
18
+ --search, -s: explicity open search
19
+ --complete: show completions
20
+ EOS
21
+
22
+
23
+ Booker.new(ARGV)
data/lib/booker.rb ADDED
@@ -0,0 +1,75 @@
1
+ # parse command line args
2
+
3
+
4
+ require_relative 'bookmarks'
5
+ require_relative 'browser'
6
+
7
+
8
+ # get web opening command
9
+ class Booker
10
+ include Browser
11
+ def initialize(args)
12
+ parse args
13
+ end
14
+
15
+ def parse(args)
16
+ if args.none? # no args given, show help
17
+ puts HELP_BANNER
18
+ exit 1
19
+ end
20
+
21
+ # doing autocompletion
22
+ if args[0] == "--complete"
23
+ args.shift # remove flag
24
+ allargs = args.join(' ')
25
+ bm = Bookmarks.new(allargs)
26
+ bm.autocomplete
27
+ exit 0
28
+ end
29
+
30
+
31
+ # doing forced bookmarking
32
+ if args[0] == "--bookmark" or args[0] == "-b"
33
+ bm = Bookmarks.new('')
34
+ bookmark = bm.bookmark_id(args[1])
35
+ url = bm.bookmark_url(bookmark)
36
+ puts 'opening ' + url + '...'
37
+ exec browse << wrap(url)
38
+ exit 0
39
+ end
40
+
41
+
42
+ # doing forced searching
43
+ if args[0] == "--search" or args[0] == "-s"
44
+ args.shift # remove flag
45
+ puts 'searching ' + allargs + '...'
46
+ exec browse << search << allargs
47
+ exit 0
48
+ end
49
+
50
+
51
+ # interpret
52
+ while args do
53
+ allargs = "'" + args.join(' ') + "'"
54
+ browsearg = args.shift
55
+
56
+ if /[0-9]/.match(browsearg[0]) # bookmark
57
+ bm = Bookmarks.new('')
58
+ bookmark = bm.bookmark_id(browsearg)
59
+ url = bm.bookmark_url(bookmark)
60
+ puts 'opening ' + url + '...'
61
+ exec browse << wrap(url)
62
+
63
+ elsif domain.match(browsearg) # website
64
+ puts 'opening ' + browsearg + '...'
65
+ exec browse << wrap(prep(browsearg))
66
+
67
+ else # just search for these arguments
68
+ puts 'searching ' + allargs + '...'
69
+ exec browse << search << allargs
70
+
71
+ end
72
+ end
73
+ end
74
+ end
75
+
data/lib/bookmarks.rb ADDED
@@ -0,0 +1,142 @@
1
+ # grab bookmarks from json file on computer - this may need to be changed based
2
+ # on operating system as well
3
+
4
+
5
+ require "rubygems"
6
+ require "json"
7
+ require_relative "folder"
8
+
9
+
10
+ # get the width of the current terminal # [columns, lines]
11
+ #require "highline"
12
+ #COLWIDTH = HighLine::SystemExtensions.terminal_size[0]
13
+
14
+ # thx danhassin
15
+ class String
16
+ def colorize(color, mod)
17
+ "\033[#{mod};#{color};49m#{self}\033[0;0m"
18
+ end
19
+
20
+ def window(width)
21
+ if self.length >= width
22
+ self[0..width-1]
23
+ else
24
+ self.ljust(width)
25
+ end
26
+ end
27
+
28
+ def reset() colorize(0,0) end
29
+ def white() colorize(37,1) end
30
+ def green() colorize(32,0) end
31
+ def blue() colorize(34,0) end
32
+ end
33
+
34
+
35
+ class Bookmarks
36
+ LOCAL_BOOKMARKS = "/Library/Application Support/Google/Chrome/Profile 1/bookmarks"
37
+ RAW_JSON_BOOKMARKS = JSON.parse(open(ENV['HOME'] + LOCAL_BOOKMARKS).read)
38
+ CHROME_BOOKMARKS = RAW_JSON_BOOKMARKS['roots']['bookmark_bar']['children']
39
+
40
+ def initialize(search_term)
41
+ @searching = /#{search_term}/i
42
+ @allurls = []
43
+ parse
44
+ end
45
+
46
+ # output for zsh
47
+ def autocomplete
48
+ width = 80
49
+ @allurls.each do |url|
50
+ # delete anything not allowed in linktitle
51
+ dirty_t = url.title.gsub(/[^a-z0-9\-\/_]/i, '')
52
+ dirty_t = url.folder + dirty_t
53
+ dirty_t = dirty_t.gsub(/\-+/, '-')
54
+ dirty_t = dirty_t.gsub(/ /,'')
55
+ name = dirty_t.window(width)
56
+
57
+ # remove strange things from any linkurls
58
+ dirty_u = url.url.gsub(/[,'"&?].*/, '')
59
+ dirty_u = dirty_u.gsub(/.*:\/+/,'')
60
+ dirty_u = dirty_u.gsub(/ /,'')
61
+ #right = [url.url.length, COLWIDTH-titlewidth].max
62
+ #link = dirty_u[0..right-1]
63
+ link = dirty_u[0..width]
64
+
65
+ # print out title and cleaned url, for autocompetion
66
+ puts url.id + ":" + name + ":" + link
67
+ end
68
+ end
69
+
70
+
71
+ # parse a bookmark's id from tab completed form
72
+ def bookmark_id(url)
73
+ url[0..5].gsub(/[^0-9]/, '')
74
+ end
75
+
76
+ # get link (from id number)
77
+ def bookmark_url(id)
78
+ bm_url = ''
79
+ @allurls.each do |url|
80
+ bm_url = url.url if id == url.id
81
+ end
82
+ bm_url
83
+ end
84
+
85
+ # recursively parse gc bookmarks
86
+ def parse(root=nil)
87
+ # root - parent folder in ruby
88
+ root = Folder.new CHROME_BOOKMARKS if root.nil?
89
+
90
+ # all current urls, to hash
91
+ root.json.each {|x| parse_link root.title, x }
92
+
93
+ # all next-level folders, to array
94
+ root.json.each {|x| parse_folder root, x }
95
+ end
96
+
97
+ # add link to results
98
+ def parse_link(base, link)
99
+ checking = [base, link['name'], link['url'], link['id']]
100
+ if checking.any? {|x| @searching.match x }
101
+ if link['type'] == 'url'
102
+ @allurls.push(Bookmark.new base, link['name'], link['url'], link['id'])
103
+ end
104
+ end
105
+ end
106
+
107
+ # discover and parse folders
108
+ def parse_folder(base, link)
109
+ if link['type'] == 'folder'
110
+ title = base.title + link['name'] + '/'
111
+ subdir = Folder.new(title, link['children'])
112
+ parse(subdir)
113
+ end
114
+ end
115
+ end
116
+
117
+ # just hold data
118
+ class Bookmark
119
+ # clean bookmark title, set attrs
120
+ def initialize(f, t, u, id)
121
+ @title = t.gsub(/[: ,'"+\-]/, '_').downcase
122
+ @folder = f
123
+ @url = u
124
+ @id = id
125
+ end
126
+
127
+ def folder
128
+ @folder
129
+ end
130
+
131
+ def title
132
+ @title
133
+ end
134
+
135
+ def id
136
+ @id
137
+ end
138
+
139
+ def url
140
+ @url
141
+ end
142
+ end
data/lib/browser.rb ADDED
@@ -0,0 +1,55 @@
1
+ # detect operating system
2
+
3
+ module OS
4
+ def OS.windows?
5
+ (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
6
+ end
7
+
8
+ def OS.mac?
9
+ (/darwin/ =~ RUBY_PLATFORM) != nil
10
+ end
11
+
12
+ def OS.linux?
13
+ not (OS.windows? or OS.mac?)
14
+ end
15
+ end
16
+
17
+
18
+ # return browser (chrome) opening command
19
+
20
+ module Browser
21
+ extend OS
22
+ def browse
23
+ if OS.windows?
24
+ '/cygdrive/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe '
25
+ elsif OS.mac?
26
+ 'open -a "Google Chrome" '
27
+ elsif OS.linix?
28
+ 'xdg-open ' # completely guessing here
29
+ end
30
+ end
31
+
32
+ def domain
33
+ /.*(io|com|web|net|org|gov|edu)$/i
34
+ end
35
+
36
+ def search
37
+ #"https://www.google.com/search?q="
38
+ #"http://www.bing.com/search?q=" #lol
39
+ "https://duckduckgo.com/?q="
40
+ end
41
+
42
+ # helper methods
43
+
44
+ def prep(url)
45
+ if /^http/.match(url)
46
+ url
47
+ else
48
+ 'http://' << url
49
+ end
50
+ end
51
+
52
+ def wrap(url)
53
+ "'" + url + "'"
54
+ end
55
+ end
data/lib/folder.rb ADDED
@@ -0,0 +1,21 @@
1
+ # basic folder class, for parsing bookmarks
2
+
3
+ class Folder
4
+ include Enumerable
5
+ def initialize(title='/', json)
6
+ @title = title.gsub(/[: ,'"]/, '-').downcase
7
+ @json = json
8
+ end
9
+
10
+ def title
11
+ @title
12
+ end
13
+
14
+ def json
15
+ @json
16
+ end
17
+
18
+ def each
19
+ @json.each
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: booker
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Jeremy Warner
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Select your bookmarks from the command line.
28
+ email:
29
+ - jeremywrnr@gmail.com
30
+ executables:
31
+ - web
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - bin/web
36
+ - lib/booker.rb
37
+ - lib/bookmarks.rb
38
+ - lib/browser.rb
39
+ - lib/folder.rb
40
+ homepage: http://rubygems.org/gems/booker
41
+ licenses:
42
+ - MIT
43
+ metadata: {}
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 2.4.8
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: CLI parser for google chrome bookmarks
64
+ test_files: []