deepblue / hotruby

hot.rubykr.org

This URL has Read+Write access

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