howdoi 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +66 -0
- data/bin/howdoi +22 -0
- data/lib/howdoi.rb +81 -0
- metadata +49 -0
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
howdoi - a code search tool
|
2
|
+
===========================
|
3
|
+
|
4
|
+
This is a ruby clone of original Python [howdoi][1] by gleitz.
|
5
|
+
|
6
|
+
Are you a hack programmer? Do you find yourself constantly Googling for
|
7
|
+
how to do basic programing tasks?
|
8
|
+
|
9
|
+
Suppose you want to know how to format a date in bash. Why open your
|
10
|
+
browser and read through blogs when you can just...
|
11
|
+
|
12
|
+
$ howdoi format string bash
|
13
|
+
> http://stackoverflow.com/questions/9832393/how-to-bash-script-format-unix-time-to-string
|
14
|
+
>
|
15
|
+
> [foo@bar ~]$date --date "2012-02-13" +%s
|
16
|
+
> 1329055200
|
17
|
+
> [foo@bar ~]$date --date @1329055200
|
18
|
+
> Mon Feb 13 00:00:00 EST 2012
|
19
|
+
> [foo@bar ~]$date --date @1329055200 +"%Y-%m-%d"
|
20
|
+
> 2012-02-13
|
21
|
+
|
22
|
+
howdoi will answer all sorts of queries
|
23
|
+
|
24
|
+
$ howdoi print stack trace python
|
25
|
+
> http://stackoverflow.com/questions/7901238/javas-printstacktrace-equivalent-in-python
|
26
|
+
>
|
27
|
+
> import traceback
|
28
|
+
>
|
29
|
+
> try:
|
30
|
+
> 1/0
|
31
|
+
> except:
|
32
|
+
> print '>>> traceback <<<'
|
33
|
+
> traceback.print_exc()
|
34
|
+
> print '>>> end of traceback <<<'
|
35
|
+
> traceback.print_exc()
|
36
|
+
|
37
|
+
Installation
|
38
|
+
------------
|
39
|
+
|
40
|
+
gem install howdoi
|
41
|
+
|
42
|
+
Usage
|
43
|
+
-----
|
44
|
+
|
45
|
+
::
|
46
|
+
|
47
|
+
Usage: bin/howdoi [options] QUERY ...
|
48
|
+
|
49
|
+
-p, --pos N select answer in specified position (default: 1)
|
50
|
+
-a, --all display the full text of the answer
|
51
|
+
|
52
|
+
Author
|
53
|
+
------
|
54
|
+
|
55
|
+
- Roy Zuo ([@roylez][2])
|
56
|
+
|
57
|
+
Notes
|
58
|
+
-----
|
59
|
+
|
60
|
+
- Requires Nokogiri
|
61
|
+
- Special thanks to Rich Jones ([@miserlou][3]) for the idea, and Benjamin Gleitzman ([@gleitz][4]) for original Python implementation
|
62
|
+
|
63
|
+
[1]: https://github.com/gleitz/howdoi/tree/master/howdoi
|
64
|
+
[2]: http://roylez.heroku.com
|
65
|
+
[3]: https://github.com/miserlou
|
66
|
+
[4]: https://github.com/gleitz
|
data/bin/howdoi
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# coding: utf-8
|
3
|
+
#Author: Roy L Zuo (roylzuo at gmail dot com)
|
4
|
+
#Description:
|
5
|
+
|
6
|
+
require 'optparse'
|
7
|
+
require 'howdoi'
|
8
|
+
options = {}
|
9
|
+
|
10
|
+
OptionParser.new do |opts|
|
11
|
+
opts.banner = "Usage: #{$0} [options] QUERY ..."
|
12
|
+
opts.separator " "
|
13
|
+
options[:pos] = 1
|
14
|
+
opts.on("-p", "--pos N", Integer, "select answer in specified position (default: 1)") do |pos|
|
15
|
+
options[:pos] = pos
|
16
|
+
end
|
17
|
+
opts.on("-a", "--all", 'display the full text of the answer') do
|
18
|
+
option[:all] = true
|
19
|
+
end
|
20
|
+
end.parse!
|
21
|
+
|
22
|
+
how_do_i(ARGV, options)
|
data/lib/howdoi.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# coding: utf-8
|
3
|
+
#Author: Roy L Zuo (roylzuo at gmail dot com)
|
4
|
+
#Description:
|
5
|
+
|
6
|
+
require 'uri'
|
7
|
+
require 'net/http'
|
8
|
+
require 'rubygems'
|
9
|
+
require 'nokogiri'
|
10
|
+
|
11
|
+
GOOGLE_SEARCH_URL = "http://www.google.com/search?q=site:stackoverflow.com+"
|
12
|
+
USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1309.0 Safari/537.17"
|
13
|
+
|
14
|
+
def get_url(url)
|
15
|
+
uri = URI.parse url
|
16
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
17
|
+
http.get(uri.request_uri, 'User-Agent' => USER_AGENT).body
|
18
|
+
end
|
19
|
+
|
20
|
+
def is_question?(stackoverflow_url)
|
21
|
+
stackoverflow_url =~ %r(/questions/\d+/)
|
22
|
+
end
|
23
|
+
|
24
|
+
def get_google_links(args)
|
25
|
+
page = get_url(URI.escape(GOOGLE_SEARCH_URL + args.join("+")))
|
26
|
+
html = Nokogiri.HTML(page)
|
27
|
+
posts = []
|
28
|
+
html.css('a.l').each{|l| posts << l[:href] if is_question?(l[:href]) }
|
29
|
+
posts
|
30
|
+
end
|
31
|
+
|
32
|
+
def get_link_at_pos(links, pos)
|
33
|
+
link = nil
|
34
|
+
pos.times do |i|
|
35
|
+
break if i > links.size
|
36
|
+
link = links[i]
|
37
|
+
end
|
38
|
+
link
|
39
|
+
end
|
40
|
+
|
41
|
+
def how_do_i(args, opts = {})
|
42
|
+
links = get_google_links(args)
|
43
|
+
link = get_link_at_pos(links, opts[:pos])
|
44
|
+
if link
|
45
|
+
puts "\e[32m" + link + "\e[m"
|
46
|
+
puts
|
47
|
+
page = get_url link
|
48
|
+
html = Nokogiri.HTML page
|
49
|
+
ans = html.at_css(".answer")
|
50
|
+
instruction = ans.css("pre").children.
|
51
|
+
collect(&:content).
|
52
|
+
join(" " * 5 + '-' * 50 + "\n") ||
|
53
|
+
ans.at_css("code").content
|
54
|
+
unless opts[:all] or instruction.empty?
|
55
|
+
puts instruction
|
56
|
+
else
|
57
|
+
puts ans.at('.post-text').content
|
58
|
+
end
|
59
|
+
else
|
60
|
+
puts "Sorry, couldn't find any help with that topic"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
if __FILE__ == $0
|
65
|
+
require 'optparse'
|
66
|
+
options = {}
|
67
|
+
|
68
|
+
OptionParser.new do |opts|
|
69
|
+
opts.banner = "Usage: #{$0} [options] QUERY ..."
|
70
|
+
opts.separator " "
|
71
|
+
options[:pos] = 1
|
72
|
+
opts.on("-p", "--pos N", Integer, "select answer in specified position (default: 1)") do |pos|
|
73
|
+
options[:pos] = pos
|
74
|
+
end
|
75
|
+
opts.on("-a", "--all", 'display the full text of the answer') do
|
76
|
+
option[:all] = true
|
77
|
+
end
|
78
|
+
end.parse!
|
79
|
+
|
80
|
+
how_do_i(ARGV, options)
|
81
|
+
end
|
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: howdoi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Roy Zuo
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-09 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: This is ruby clone of original Python howdoi by Benjamin Gleitzman (@gleitz)
|
15
|
+
email: roylzuo@gmail.com
|
16
|
+
executables:
|
17
|
+
- howdoi
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/howdoi.rb
|
22
|
+
- bin/howdoi
|
23
|
+
- README.md
|
24
|
+
homepage: http://rubygems.org/gems/howdoi
|
25
|
+
licenses: []
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ! '>='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubyforge_project:
|
44
|
+
rubygems_version: 1.8.11
|
45
|
+
signing_key:
|
46
|
+
specification_version: 3
|
47
|
+
summary: a code search tool
|
48
|
+
test_files: []
|
49
|
+
has_rdoc:
|