bulletin 0.0.5 → 0.0.6
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/bulletin +17 -12
- data/bulletin.rb +37 -26
- data/bulletin_test.rb +10 -2
- metadata +2 -2
data/bulletin
CHANGED
@@ -3,26 +3,31 @@ require 'optparse'
|
|
3
3
|
require File.expand_path('bulletin', File.dirname(__FILE__))
|
4
4
|
|
5
5
|
ARGV.options do |o|
|
6
|
-
|
6
|
+
action = ['run', 1]
|
7
7
|
bulletin = Bulletin::App.new(true)
|
8
|
-
bulletin.load_config
|
9
8
|
o.set_summary_indent(' ')
|
10
9
|
o.banner = "Usage: #{File.basename($0)} [OPTIONS]"
|
11
|
-
o.
|
12
|
-
o.on('-
|
10
|
+
o.on('-a', '--all', 'show all links') { action = ['run', 0] }
|
11
|
+
o.on('-e', '--edit', 'edit config') { action = ['edit'] }
|
13
12
|
o.on('-f', '--filter uri', 'filter news') { |f| bulletin.filter(f) }
|
14
|
-
o.on('-o', '--open id', 'open link') { |id|
|
15
|
-
o.on('-p', '--page page', 'show page') { |page|
|
16
|
-
o.on('-r', '--read id', 'read a link') { |id|
|
17
|
-
o.on('-R', '--refresh', 'refresh
|
18
|
-
o.on('-s', '--save id', 'save link') { |id|
|
19
|
-
o.on('-S', '--saved', 'show all saved') {
|
20
|
-
o.on('-u', '--unsave id', 'undo save') { |id|
|
13
|
+
o.on('-o', '--open id', 'open link') { |id| action = ['open_item', id] }
|
14
|
+
o.on('-p', '--page page', 'show page') { |page| action = ['run', page] }
|
15
|
+
o.on('-r', '--read id', 'read a link') { |id| action = ['read', id] }
|
16
|
+
o.on('-R', '--refresh', 'refresh stories') { action = ['refresh'] }
|
17
|
+
o.on('-s', '--save id', 'save link') { |id| action = ['save', id] }
|
18
|
+
o.on('-S', '--saved', 'show all saved') { action = ['saved'] }
|
19
|
+
o.on('-u', '--unsave id', 'undo save') { |id| action = ['unsave', id] }
|
21
20
|
o.on('-v', '--version', 'show version') do
|
22
21
|
puts "bulletin version #{Bulletin::VERSION}"
|
23
22
|
exit
|
24
23
|
end
|
25
24
|
o.on('-h', '--help', 'show this help message') { puts o; exit }
|
26
25
|
o.parse!
|
27
|
-
|
26
|
+
begin
|
27
|
+
bulletin.setup_db
|
28
|
+
bulletin.load_config
|
29
|
+
ARGV.empty? ? bulletin.send(*action) : puts(o)
|
30
|
+
rescue Exception => e
|
31
|
+
puts e
|
32
|
+
end
|
28
33
|
end
|
data/bulletin.rb
CHANGED
@@ -1,14 +1,17 @@
|
|
1
1
|
require 'rubygems'
|
2
|
+
require 'feedzirra'
|
2
3
|
require 'uri'
|
3
4
|
require 'data_mapper'
|
4
5
|
require 'dm-types'
|
5
6
|
require 'colorize'
|
6
|
-
require 'feedzirra'
|
7
7
|
require 'nokogiri'
|
8
8
|
require 'date'
|
9
9
|
|
10
10
|
module Bulletin
|
11
|
-
VERSION = '0.0.
|
11
|
+
VERSION = '0.0.6'
|
12
|
+
EDITOR = ENV['EDITOR'] || 'vi'
|
13
|
+
BULLETINRC = File.expand_path('~/.bulletinrc')
|
14
|
+
BULLETINDB = File.expand_path('~/.bulletindb')
|
12
15
|
|
13
16
|
class App
|
14
17
|
attr_reader :options, :feeds
|
@@ -21,6 +24,10 @@ module Bulletin
|
|
21
24
|
@term_height = `tput lines`.to_i
|
22
25
|
end
|
23
26
|
|
27
|
+
def edit
|
28
|
+
`#{EDITOR} #{BULLETINRC} < \`tty\` > \`tty\``
|
29
|
+
end
|
30
|
+
|
24
31
|
def filter(site)
|
25
32
|
@filter = site
|
26
33
|
end
|
@@ -32,8 +39,8 @@ module Bulletin
|
|
32
39
|
options = {:order => [:rank]}
|
33
40
|
if page > -1
|
34
41
|
options.merge!(
|
35
|
-
:
|
36
|
-
:
|
42
|
+
:offset => per_page * page,
|
43
|
+
:limit => per_page)
|
37
44
|
end
|
38
45
|
if @filter
|
39
46
|
options.merge!(
|
@@ -114,19 +121,26 @@ module Bulletin
|
|
114
121
|
@feeds << uri
|
115
122
|
end
|
116
123
|
|
124
|
+
def setup_db(production = true)
|
125
|
+
DataMapper.setup(:default, production ?
|
126
|
+
"sqlite://#{File.expand_path(BULLETINDB)}" :
|
127
|
+
"sqlite3::memory:")
|
128
|
+
if !DataMapper.repository(:default).adapter.storage_exists?('bulletin_items')
|
129
|
+
DataMapper.auto_migrate!
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
117
133
|
def load_config
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
define_method(:feed) { |uri| app.feed(uri) }
|
124
|
-
end
|
125
|
-
load(config, true)
|
126
|
-
@options[:browser] ||= 'firefox'
|
127
|
-
@options[:per_page] ||= (@term_height - 2)
|
128
|
-
@options[:expire] ||= 30
|
134
|
+
raise "No configuration found at '#{BULLETINRC}'." if !File.exists?(BULLETINRC)
|
135
|
+
app = self
|
136
|
+
Object.class_eval do
|
137
|
+
define_method(:set) { |opt, val| app.set(opt, val) }
|
138
|
+
define_method(:feed) { |uri| app.feed(uri) }
|
129
139
|
end
|
140
|
+
load(BULLETINRC, true)
|
141
|
+
@options[:browser] ||= 'firefox'
|
142
|
+
@options[:per_page] ||= (@term_height - 2)
|
143
|
+
@options[:expire] ||= 30
|
130
144
|
end
|
131
145
|
|
132
146
|
private
|
@@ -165,7 +179,13 @@ module Bulletin
|
|
165
179
|
lists = %w(li)
|
166
180
|
swaps = {'br'=>"\n", 'hr'=>"\n"}
|
167
181
|
node = Nokogiri::HTML(html)
|
168
|
-
node.xpath('.//text()').each
|
182
|
+
node.xpath('.//text()').each do |t|
|
183
|
+
if t.ancestors('pre').empty?
|
184
|
+
t.content = t.text.gsub(/\s+/,' ')
|
185
|
+
else
|
186
|
+
t.content = t.text + "\n"
|
187
|
+
end
|
188
|
+
end
|
169
189
|
node.css(swaps.keys.join(',')).each { |n| n.replace(swaps[n.name]) }
|
170
190
|
node.css(blocks.join(',')).each { |n| n.after("\n\n") }
|
171
191
|
node.css(lists.join(',')).each { |n| n.after("\n").before("* ") }
|
@@ -193,15 +213,6 @@ module Bulletin
|
|
193
213
|
end
|
194
214
|
buffer.join("\n")
|
195
215
|
end
|
196
|
-
|
197
|
-
def self.setup_db(production = true)
|
198
|
-
DataMapper.setup(:default, production ?
|
199
|
-
"sqlite://#{File.expand_path('~/.bulletindb')}" :
|
200
|
-
"sqlite3::memory:")
|
201
|
-
if !DataMapper.repository(:default).adapter.storage_exists?('bulletin_items')
|
202
|
-
DataMapper.auto_migrate!
|
203
|
-
end
|
204
|
-
end
|
205
216
|
end
|
206
217
|
|
207
218
|
class Item
|
@@ -221,7 +232,7 @@ module Bulletin
|
|
221
232
|
Item.new(:published_at => (entry.published || Time.now),
|
222
233
|
:title => entry.title.to_s.strip,
|
223
234
|
:uri => entry.url,
|
224
|
-
:body => entry.content)
|
235
|
+
:body => entry.content || entry.summary)
|
225
236
|
end
|
226
237
|
|
227
238
|
def full_title
|
data/bulletin_test.rb
CHANGED
@@ -8,7 +8,7 @@ require File.join(File.dirname(__FILE__), 'bulletin')
|
|
8
8
|
require 'minitest/autorun'
|
9
9
|
|
10
10
|
class BulletinTest < MiniTest::Unit::TestCase
|
11
|
-
Bulletin::App.setup_db(false)
|
11
|
+
Bulletin::App.new.setup_db(false)
|
12
12
|
|
13
13
|
def setup
|
14
14
|
@bulletin = Bulletin::App.new(false)
|
@@ -16,6 +16,7 @@ class BulletinTest < MiniTest::Unit::TestCase
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def test_refresh
|
19
|
+
@bulletin.options[:expire] = 30
|
19
20
|
assert_equal(0, Bulletin::Item.count)
|
20
21
|
@bulletin.feed sample_uri
|
21
22
|
@bulletin.refresh
|
@@ -47,8 +48,15 @@ class BulletinTest < MiniTest::Unit::TestCase
|
|
47
48
|
refute(@bulletin.send(:production?))
|
48
49
|
end
|
49
50
|
|
51
|
+
def test_html_to_text
|
52
|
+
text = @bulletin.send(
|
53
|
+
:html_to_text,
|
54
|
+
"<!-- comment -->begin<pre><code>line 1\nline2</code></pre>end")
|
55
|
+
assert_equal("begin\n\nline 1\nline2\nend", text)
|
56
|
+
end
|
57
|
+
|
50
58
|
private
|
51
59
|
def sample_uri
|
52
|
-
File.join(File.expand_path(File.dirname(__FILE__)), 'sample.xml')
|
60
|
+
"file://#{File.join(File.expand_path(File.dirname(__FILE__)), 'sample.xml')}"
|
53
61
|
end
|
54
62
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bulletin
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: .
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-02-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: dm-sqlite-adapter
|