public
Fork of adamwiggins/scanty
Description: trying to turn scanty into a microblog
Homepage: http://www.least-significant-bit.com/
Clone URL: git://github.com/joahking/scanty.git
scanty / main.rb
100644 190 lines (159 sloc) 4.748 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
require 'rubygems'
 
# this is to load the vendored sinatra
require File.dirname(__FILE__) + '/vendor/sinatra/lib/sinatra'
require 'sinatra'
 
$LOAD_PATH.unshift File.dirname(__FILE__) + '/vendor/sequel'
require 'sequel'
 
configure do
  def sequel_db_uri
    if Blog.db and Blog.db['adapter'] == 'mysql'
      mysql_auth = "#{ Blog.db['username'] }:#{ Blog.db['password'] }@#{ Blog.db['host'] }/"
      "#{ Blog.db['adapter'] }://#{ mysql_auth }#{ Blog.db['database'] }"
    else
      "sqlite://blog.db"
    end
  end
 
  require 'ostruct'
  config = YAML.load_file 'config/config.yml'
  Blog = OpenStruct.new( config["scanty"].
                         merge({
                                 :title => "Least Significant Bit",
                                 :header => "%(ask hack learn share)"
                               }) )
  Sequel.connect(sequel_db_uri)
end
 
error do
  e = request.env['sinatra.error']
  puts e.to_s
  puts e.backtrace.join("\n")
  "Application error"
end
 
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/lib')
require 'post'
 
helpers do
  def admin?
    request.cookies[Blog.admin_cookie_key] == Blog.admin_cookie_value
  end
 
  def auth
    stop [ 401, 'Not authorized' ] unless admin?
  end
 
  # this one will load all js for code highlighting
  def all_javascripts
    Dir.entries('public/js').inject("") do |all_js, js|
      if js =~ /\.js$/
        all_js << "<script src='/js/#{js}' type='text/javascript'></script>"
      end
      all_js
    end
  end
 
  def all_styles
    Dir.entries('public/css').inject("") do |all_css, css|
      if css =~ /\.css$/
        all_css << "<link href='/css/#{css}' rel='stylesheet' type='text/css' />"
      end
      all_css
    end
  end
 
  def write
    '<li><a href="posts/new">write</a></li>' if admin?
  end
 
  # change your friends
  FRIENDS = [{ :url => "http://www.xuehka.blogspot.com",
               :text => "xuehka" },
             { :url => "http://www.mrdias.com",
               :text => "mrdias" }]
  def friends
    FRIENDS.inject("") do |friends, f|
      friends << "<li><a href='#{f[:url]}'>#{f[:text]}</a></li>"
    end
  end
 
  def tags
    unless @tags.nil?
      list = @tags.inject("<span>") do |html, t|
        html << "<a href='/past/tags/#{t}'>#{t}</a> "
      end
      "#{list}</span>"
    end
  end
end
 
layout 'layout'
 
### Public
 
get '/' do
  #FIXME doesn't this looks UNDRY?
  @tags = Scanty::Post.tags
  posts = Scanty::Post.reverse_order(:created_at).limit(10)
  erb :index, :locals => { :posts => posts }
end
 
get '/past/:year/:month/:day/:slug/' do
  #FIXME doesn't this looks UNDRY?
  @tags = Scanty::Post.tags
  post = Scanty::Post.filter(:slug => params[:slug]).first
  stop [ 404, "Page not found" ] unless post
  @title = post.title
  erb :post, :locals => { :post => post }
end
 
get '/past/:year/:month/:day/:slug' do
  redirect "/past/#{params[:year]}/#{params[:month]}/#{params[:day]}/#{params[:slug]}/", 301
end
 
get '/past' do
  posts = Scanty::Post.reverse_order(:created_at)
  @title = "Archive"
  erb :archive, :locals => { :posts => posts }
end
 
get '/past/tags/:tag' do
  #FIXME doesn't this looks UNDRY?
  @tags = Scanty::Post.tags
  tag = params[:tag]
  posts = Scanty::Post.filter(:tags.like("%#{tag}%")).reverse_order(:created_at).limit(30)
  @title = "Posts tagged #{tag}"
  erb :tagged, :locals => { :posts => posts, :tag => tag }
end
 
get '/feed' do
  @posts = Scanty::Post.reverse_order(:created_at).limit(10)
  content_type 'application/atom+xml', :charset => 'utf-8'
  builder :feed
end
 
get '/rss' do
  redirect '/feed', 301
end
 
### Admin
 
get '/auth' do
  erb :auth
end
 
post '/auth' do
  set_cookie(Blog.admin_cookie_key, Blog.admin_cookie_value) if params[:password] == Blog.admin_password
  redirect '/'
end
 
get '/posts/new' do
  auth
  erb :edit, :locals => { :post => Scanty::Post.new, :url => '/posts' }
end
 
post '/posts' do
  auth
  post = Scanty::Post.new({ :title => params[:title], :tags => params[:tags],
                    :body => params[:body], :created_at => Time.now,
                    :slug => Scanty::Post.make_slug(params[:title]) })
 
  begin
    post.save
    redirect(post.url)
  rescue
    #FIXME are there better ways of recognizing request origin?
    redirect 'posts/new'
  end
end
 
get '/past/:year/:month/:day/:slug/edit' do
  auth
  post = Scanty::Post.filter(:slug => params[:slug]).first
  stop [ 404, "Page not found" ] unless post
  erb :edit, :locals => { :post => post, :url => post.url }
end
 
post '/past/:year/:month/:day/:slug/' do
  auth
  post = Scanty::Post.filter(:slug => params[:slug]).first
  stop [ 404, "Page not found" ] unless post
  post.title = params[:title]
  post.tags = params[:tags]
  post.body = params[:body]
  post.save
  redirect post.url
end