stackoverflow 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/stackoverflow.rb +29 -32
- metadata +2 -2
data/lib/stackoverflow.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
#
|
3
1
|
# Copyright (C) 2012 Xavier Antoviaque <xavier@antoviaque.org>
|
4
2
|
#
|
5
3
|
# This software's license gives you freedom; you can copy, convey,
|
@@ -56,8 +54,8 @@ module StackOverflow
|
|
56
54
|
|
57
55
|
class DB
|
58
56
|
def initialize
|
59
|
-
@db
|
60
|
-
@db_idx
|
57
|
+
@db = SQLite3::Database.new(File.join(Dir.home, ".stackoverflow/stackoverflow.db"))
|
58
|
+
@db_idx = SQLite3::Database.new(File.join(Dir.home, ".stackoverflow/stackoverflow_idx.db"))
|
61
59
|
end
|
62
60
|
|
63
61
|
def db_error_catching
|
@@ -300,28 +298,25 @@ module StackOverflow
|
|
300
298
|
def question_viewer(question)
|
301
299
|
answers = question['answers']
|
302
300
|
nb = 1
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
t << ["[#{nb}] (+#{answer['score']})", "#{text}"]
|
313
|
-
t << :separator
|
314
|
-
nb += 1
|
315
|
-
end
|
301
|
+
|
302
|
+
man = ".TH STACKOVERFLOW \"1\" \"\" \"Stack Overflow\" \"#{question['title']}\"\n"
|
303
|
+
man += ".SH QUESTION\n#{html2text(question['body'])}\n"
|
304
|
+
|
305
|
+
answers.each do |answer|
|
306
|
+
text = html2text(answer['body'])
|
307
|
+
man += ".SH ANSWER [#{nb}] (+#{answer['score']})\n"
|
308
|
+
man += "#{text}\n"
|
309
|
+
nb += 1
|
316
310
|
end
|
317
311
|
|
318
|
-
|
312
|
+
tmp_file_path = "/tmp/.stack_overflow.#{question['id']}"
|
313
|
+
File.open(tmp_file_path, 'w+') { |f| f.write(man) }
|
314
|
+
system "man #{tmp_file_path}"
|
319
315
|
end
|
320
316
|
|
321
317
|
def html2text(html)
|
322
318
|
doc = Nokogiri::HTML(html)
|
323
|
-
doc
|
324
|
-
wordwrap(doc)
|
319
|
+
doc.css('body').text.squeeze(" ").squeeze("\n").gsub(/[\n]+/, "\n\n")
|
325
320
|
end
|
326
321
|
|
327
322
|
def wordwrap(str, columns=80)
|
@@ -333,35 +328,37 @@ module StackOverflow
|
|
333
328
|
|
334
329
|
class Command
|
335
330
|
def run
|
336
|
-
options = {}
|
331
|
+
options = {:run => true}
|
337
332
|
OptionParser.new do |opts|
|
338
333
|
opts.banner = "** Usage: so [options] <search string> [<question_id>]"
|
339
334
|
opts.on("-h", "--help", "Help") do |v|
|
340
335
|
help
|
341
|
-
|
336
|
+
options[:run] = false
|
342
337
|
end
|
343
338
|
opts.on("-u", "--update", "Update local database") do |v|
|
344
339
|
DBUpdater.new.update
|
345
|
-
|
340
|
+
options[:run] = false
|
346
341
|
end
|
347
342
|
opts.on("-o", "--offline", "Offline mode") do |v|
|
348
|
-
options[
|
343
|
+
options[:offline] = true
|
349
344
|
end
|
350
345
|
end.parse!
|
351
346
|
|
352
347
|
if ARGV.length < 1
|
353
348
|
help
|
354
|
-
|
349
|
+
options[:run] = false
|
355
350
|
end
|
356
351
|
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
352
|
+
if options[:run]
|
353
|
+
# last argument is integer when user is specifing a question_nb from the results
|
354
|
+
question_nb = nil
|
355
|
+
if ARGV[-1] =~ /^[0-9]+$/
|
356
|
+
question_nb = ARGV.pop.to_i
|
357
|
+
end
|
362
358
|
|
363
|
-
|
364
|
-
|
359
|
+
search_string = ARGV.join(' ')
|
360
|
+
search(search_string, question_nb, options)
|
361
|
+
end
|
365
362
|
end
|
366
363
|
|
367
364
|
def help
|