Repository navigation
Expand file tree
/
Copy pathspringnote.rb
More file actions
121 lines (92 loc) · 2.8 KB
/
Copy pathspringnote.rb
File metadata and controls
121 lines (92 loc) · 2.8 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
%w(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 write(name, homepage, contents)
target = today_page
target.source = item_html(name, homepage, contents, target) + target.source.to_s
target.save
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_array
indexes.to_a.sort{|x, y| y[0] <=> x[0]}
end
def indexes
return @indexes if @indexes
@indexes = {}
meta_page(config('index_page')).source.to_s.
scan(/<li>.*?<a.*?href=\"\/pages\/(\d+)\".*?>(.*?)<\/a>.*?<\/li>/mi).
map do |m|
@indexes[m[1].to_s] = m[0].to_i
end
@indexes
end
def update_index
cont = index_array.map do |v|
%Q[<li><a href="/pages/#{v[1]}">#{v[0]}</a>]
end.join("\n")
page = meta_page(config('index_page'))
page.source = "<ul>#{cont}</ul>"
page.save
end
##############################
### Contents Pages
def today_page
key = Time.now.strftime('%Y-%m-%d')
(indexes[key] && @note.pages.find(indexes[key])) || create_page(key)
end
def create_page(key)
page = @note.pages.build(:title => key, :relation_is_part_of => config('index_page'))
page.save
@indexes[key] = page.identifier
update_index
page
end
def item_html(name, homepage, contents, target)
now = Time.now
idstr = now.to_i.to_s
permlink = "/items/#{target.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
end