This repository was archived by the owner on Feb 25, 2021. It is now read-only.
forked from ikspres/hotruby
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspringnote.rb
More file actions
172 lines (133 loc) · 3.75 KB
/
Copy pathspringnote.rb
File metadata and controls
172 lines (133 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
%w(hpricot springnote_client).each{|lib| require lib}
class SpringnoteStore
APP_KEY = 'a7501dcb75519aec11a7049e62e3a0f533c265bb'
ITEM_PER_PAGE = 7
def initialize
@note = Springnote(config('note_name'), :app_key => APP_KEY, :user_openid => config('user_openid'), :user_key => config('user_key'))
end
##############################
#### Public Methods
def sidebar
meta_page config('side_page')
end
def item_by_id(pid)
@note.pages.find(pid)
end
def items(page = 1)
s = (page-1)*ITEM_PER_PAGE
e = s + ITEM_PER_PAGE
pids = index_array[s...e].map{|v| v[1].to_i}.uniq
[*@note.pages.find(*pids)].sort{|x, y| y.title <=> x.title}
end
def entries(page = 1)
items(page).map{|page| page.entries}.flatten
end
def search(query)
[]
end
def write(name, homepage, contents)
today_page.append_entry(name, homepage, contents)
end
protected
##############################
#### Config
def self.config
@config ||= YAML.load(File.read(File.dirname(__FILE__) + '/springnote.yml'))
end
def config(key)
self.class.config[key]
end
##############################
### Meta Pages
def meta_pages
@meta_pages ||= @note.pages.find(config('side_page'), config('index_page'))
end
def meta_page(pid)
meta_pages.select{|page| page.identifier == pid}[0]
end
def index_page
meta_page(config('index_page'))
end
def index_array
index_hash.to_a.sort{|x, y| y[0] <=> x[0]}
end
def index_hash
@index_hash ||= index_page.to_index_hash
end
def update_index
page = index_page.save_index(index_array)
end
##############################
### Contents Pages
def today_page
key = Time.now.strftime('%Y-%m-%d')
(index_hash[key] && @note.pages.find(index_hash[key])) || create_page(key)
end
def create_page(key)
page = @note.pages.build(:title => key, :relation_is_part_of => config('index_page'))
page.save
@index_hash[key] = page.identifier
update_index
page
end
end
class Springnote::Page
def parsed
@parsed ||= Hpricot(self.source.to_s)
end
################################
# For Entry Page
def extract_entry(entry_id)
parsed.search("#item_#{entry_id}").html
end
def entries
parsed.search('.item_container').map do |container|
{
:identifier => container.attributes['id'].split('_')[1].to_s,
:title => container.search('.item_meta').text.split(' - ')[0].to_s,
:source => container.search('.item_body').html,
:page_id => self.identifier
}
end
end
def append_entry(name, homepage, contents)
self.source = entry_html(name, homepage, contents) + self.source.to_s
save
end
################################
# For Index Page
def save_index(ary)
self.source = index_html(ary)
save
end
def to_index_hash
ret = {}
self.source.to_s.
scan(/<li>.*?<a.*?href=\"\/pages\/(\d+)\".*?>(.*?)<\/a>.*?<\/li>/mi).
map do |m|
ret[m[1].to_s] = m[0].to_i
end
ret
end
protected
def entry_html(name, homepage, contents)
now = Time.now
idstr = now.to_i.to_s
permlink = "/items/#{self.identifier}/#{idstr}"
<<-END
<div id="item_#{idstr}" class="item_container">
<div class="item_body">#{contents}</div>
<div class="item_meta">
Posted by <a href="#{homepage}">#{name}</a> at <a href="#{permlink}"}>#{now.strftime("%H:%M")}</a> -
<a href="#{permlink}#disqus_thread">댓글</a>
</div>
</div><p> </p>
END
end
def index_html(ary)
cont = ary.map do |v|
%Q[<li><a href="/pages/#{v[1]}">#{v[0]}</a>]
end.join("\n")
"<ul>#{cont}</ul>"
end
end