-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsinandra.rb
More file actions
276 lines (245 loc) · 7.44 KB
/
sinandra.rb
File metadata and controls
276 lines (245 loc) · 7.44 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
require 'rubygems'
require 'sinatra'
require 'erb'
require 'cassandra'
require 'lib/to_slug'
require 'time'
require 'yaml'
require 'simple_uuid'
require 'maruku'
require 'sanitize'
# Load config file
config = begin
YAML::load( File.open('config.yml') )
rescue
{}
end
db = Cassandra.new('sinandra')
before do
@tags = db.get(:Lists, 'tags').keys
@blog_name = config['title'] || 'a simple blog'
@blog_author = config['author'] || 'Barack Obama'
@blog_about = config['about'] || "Lorem Ipsum is simply dummy text of the printing typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."
@blog_username = config['username'] || 'admin'
@blog_password = config['password'] || 'super'
end
get '/' do
@posts = db.get(:TaggedPosts, '__notag__', :reversed => true).map {|time,id| db.get(:BlogEntries, id) }
erb :index
end
get '/archive' do
@archives = []
db.get(:Lists, 'archives').sort_by { |id, time| Time.parse(time) }.map {|id,time| id }.each do |month|
posts = db.get(:Archives, month, :reversed => true).map {|time,id| db.get(:BlogEntries, id) }
@archives << { month => posts }
end
erb :archive
end
get '/tag/:name' do
@posts = db.get(:TaggedPosts, params[:name], :reversed => true).map {|time,id| db.get(:BlogEntries, id) }
erb :index
end
get '/posts/new' do
protected!
erb :new_post
end
get '/posts/:post' do
@post = db.get(:BlogEntries, params[:post])
@comments = db.get(:Comments, params[:post]).map {|key, comments| comments }
erb :show
end
get '/feed' do
"Return rss feed of posts"
end
post '/posts/create' do
protected!
post_body = Sanitize.clean(params['post']['body'], Sanitize::Config::BASIC)
@post = {'title' => params['post']['title'],
'body' => post_body,
'tags' => params['post']['tags'],
'slug' => params['post']['title'].to_slug,
'author' => @blog_author}
archive_key = Date::MONTHNAMES[Time.now.month] + ' ' + Time.now.year.to_s
db.insert(:BlogEntries, @post['title'].to_slug, @post)
db.insert(:TaggedPosts, '__notag__', {SimpleUUID::UUID.new => @post['title'].to_slug})
db.insert(:Lists, 'archives', { archive_key => Time.now.to_s })
db.insert(:Archives, archive_key, {SimpleUUID::UUID.new => @post['title'].to_slug})
@post['tags'].split(',').map {|t| t.strip }.each do |tag|
db.insert(:TaggedPosts, tag, {SimpleUUID::UUID.new => @post['title'].to_slug})
db.insert(:Lists, 'tags', {tag => Time.now.to_s})
end
redirect "/posts/#{@post['title'].to_slug}"
end
post '/comments/create/:post' do
comment_body = Sanitize.clean(params['comment'], Sanitize::Config::BASIC)
@comment = { 'commenter' => params['commenter'],
'email' => params['email'],
'body' => comment_body,
'posted_on' => Time.now.strftime("%B %d, %Y"),
'posted_at' => Time.now.strftime("%H:%M")}
db.insert(:Comments, params[:post], {SimpleUUID::UUID.new => @comment})
redirect "/posts/#{params[:post]}"
end
get '/*' do
erb :not_found
end
helpers do
include Rack::Utils
alias_method :h, :escape_html
def protected!
unless authorized?
response['WWW-Authenticate'] = %(Basic realm="Testing HTTP Auth")
throw(:halt, [401, "Not authorized\n"])
end
end
def authorized?
@auth ||= Rack::Auth::Basic::Request.new(request.env)
@auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials == [@blog_username, @blog_password]
end
end
__END__
@@ layout
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title><%= @blog_name %></title>
<link type="text/css" href="/styles.css" media="screen" rel="stylesheet"/>
</head>
<body>
<div id="header">
<div id="administration">
<a href="/posts/new">Create a post</a>
</div>
<h1><%=h @blog_name %></h1>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/archive">Archive</a></li>
</ul>
</div>
<div id="sidebar">
<h2>About</h2>
<p>
<%=h @blog_about %>
</p>
<h2>Tags</h2>
<ul>
<% @tags.map {|t| t.strip}.each do |tag| %>
<li><a href="/tag/<%=h tag %>"><%=h tag %></a></li>
<% end %>
</ul>
</div>
<div id="main">
<%= yield %>
</div>
<div class="break"></div>
<div id="footer">
Copyright © Intellum
</div>
</body>
</html>
@@ index
<% @posts.each do |post| %>
<div class="post">
<h2><a href="/posts/<%=h post["slug"] %>"><%=h post["title"] %></a></h2>
<p>
by <%=h post["author"] %>
</p>
<p>
<%= Maruku.new(post["body"]).to_html %>
</p>
<div class="tags"><strong>Tags:</strong>
<% post["tags"].split(',').map {|t| t.strip }.each do |tag| %>
<a href="/tag/<%=h tag %>"><%=h tag %></a>
<% end %>
</div>
</div>
<% end %>
@@ new_post
<h2>New blog post:</h2>
<form action="/posts/create" method="POST">
<p>
<label for="post_title">Title</label><br/>
<input id="post_title" name="post[title]" />
</p>
<p>
<label for="post_body">Body</label><br/>
<textarea id="post_body" name="post[body]" style="height: 500px"></textarea><br/>
<small>Use <a target="_new" href="http://en.wikipedia.org/wiki/Markdown">Markdown</a> for formatting.</small><br/>
</p>
<p>
<label for="post_tags">Tags <em>(comma separated)</em></label><br/>
<input id="post_tags" name="post[tags]" />
</p>
<p>
<button type="submit">Save</button>
</p>
</form>
<script>
document.getElementById('post_title').focus();
</script>
@@ show
<div class="post">
<h2><%=h @post["title"] %></h2>
<p>
by <%=h @post["author"] %>
</p>
<p>
<%= Maruku.new(@post["body"]).to_html %>
</p>
<div class="tags"><strong>Tags: </strong>
<% @post["tags"].split(',').map {|t| t.strip }.each do |tag| %>
<a href="/tag/<%=h tag %>"><%=h tag %></a>
<% end %>
</div>
</div>
<div id="comments">
<h3>Comments</h3>
<% if @comments.size == 0 %>
<p><em>No comments. Be the first to post one below</em></p>
<% end %>
<% @comments.each do |comment| %>
<div class="comment">
<div class="comment_headline">
<em>Posted by </em><strong><%=h comment['commenter'] %></strong><em>
on <%=h comment['posted_on'] %>
at <%=h comment['posted_at'] %></em>
</div>
<p>
<%= Maruku.new(comment['body']).to_html %>
</p>
</div>
<% end %>
<form action="/comments/create/<%=h @post['slug'] %>" method="POST">
<p>
<label for="commenter">Name:</label><br/>
<input id="commenter" name="commenter" />
</p>
<p>
<label for="email">Email:</label><br/>
<input id="email" name="email" />
</p>
<p>
<label for="comment">Comment</label><br/>
<textarea id="comment" name="comment"></textarea><br/>
<small>Use <a target="_new" href="http://en.wikipedia.org/wiki/Markdown">Markdown</a> for formatting.</small><br/>
</p>
<p>
<button type="submit">Submit Comment</button>
</p>
</form>
</div>
@@ archive
<% @archives.each do |month| %>
<div id="archive">
<h3><%=h month.keys[0] %></h3>
<ul>
<% month.values[0].each do |post| %>
<li><a href='/posts/<%=h post["slug"] %>'><%=h post['title'] %></a></li>
<% end %>
</ul>
</div>
<% end %>
@@ not_found
<h2>Page Not Found</h2>