This repository has been archived by the owner on Jun 9, 2020. It is now read-only.
/
blog.rb
134 lines (112 loc) · 2.77 KB
/
blog.rb
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
require 'bundler'
Bundler.require
require 'sinatra/content_for'
require_relative "user"
require_relative "warden"
require_relative "source_file"
require_relative "redirects"
class Blog < Sinatra::Base
use Rack::Session::Cookie, :secret => ENV['SESSION_SECRET']
use Warden::Manager do |manager|
manager.default_strategies :password
manager.failure_app = Blog
end
helpers WardenHelpers
helpers Sinatra::ContentFor
use Rack::Flash
helpers do
def title
title = @title ? "#{@title} - " : ""
"#{title}Literate Programming"
end
def format_outline(outline)
prev_level = 2
"<ul>" + outline.inject("") do |html, data|
level, link = data
if prev_level < level
html += "<ul>"
elsif prev_level > level
html += "</ul>"
end
prev_level = level
html += "<li>#{link}</li>"
html
end + "</ul>"
end
end
get '/' do
haml :index
end
get '/archive' do
@archives = SourceFile.archive_list
haml :archive
end
get '/atom.xml' do
require "rss"
archives = SourceFile.archive_data
rss = RSS::Maker.make("atom") do |maker|
maker.channel.author = "Steve Klabnik"
maker.channel.updated = SourceFile.last_updated
maker.channel.title = "Literate Programming"
maker.channel.id = "http://blog.steveklabnik.com/"
archives.each do |link, title, updated, summary|
maker.items.new_item do |item|
item.link = link
item.title = title
item.updated = updated
item.summary = summary
end
end
end
rss.to_s
end
get '/signup' do
haml :signup, :layout => :full_page_layout
end
post '/signup' do
User.create(params[:user])
flash[:notice] = "All signed up! Thanks!"
redirect '/'
end
get '/posts/:id' do
begin
source = SourceFile.new(params[:id])
@title = source.metadata['title']
@title_hidden = source.metadata['title-hidden']
@content = source.content
@outline = source.outline
haml :post
rescue Errno::ENOENT #lol i suck
pass
end
end
get '/login/?' do
haml :login, :layout => :full_page_layout
end
post '/login/?' do
authenticate!
redirect "/"
end
get '/logout/?' do
log_out!
redirect '/'
end
post '/unauthenticated/?' do
# If we're in our own failure app, bad things happen
warden.custom_failure!
status 401
haml :login, :layout => :full_page_layout
end
#lol posterous
REDIRECTS.each do |hsh|
get hsh[:from] do
redirect hsh[:to], 301 #yeah, this move is permanent
end
end
#lol octopress
get '/*' do
post = params[:splat].first.gsub("/", "-").gsub(".html", "")
pass if post.nil?
redirect "posts/" + post, 301
end
end