forked from dlobraico/csd_sidecar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csd.rb
168 lines (137 loc) · 3.94 KB
/
csd.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
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
require 'sinatra'
require 'sinatra/content_for'
require 'rack/cache'
require 'sanitize'
require './models/article.rb'
require './models/published_issue.rb'
set :haml, :format => :html5
# Set RACK_ENV to development to override this.
set :environment, :production
#set :environment, :development
def get_top_issue()
return PublishedIssue.find(:all).sort_by {|x| x.distribution }.last.id
end
if settings.environment == :production
use Rack::Cache,
:verbose => true,
:metastore => "file:/tmp/cache/meta",
:entitystore => "file:/tmp/cache/body"
before do
cache_control :public, :must_revalidate, :max_age => 900
end
end
helpers do
def img(path, alt)
"<img src='#{path}' alt='#{alt}' />"
end
def primary_img(article)
image = article.image
return "" unless image.file.primary.url.present?
path = image.file.primary.url
alt = image.description
"<img src='#{path}' alt='#{alt}' />"
end
def secondary_img(article)
image = article.image
return "" unless image.file.primary.url.present?
path = image.file.secondary.url
alt = image.description
"<img src='#{path}' alt='#{alt}' />"
end
def h(text)
Rack::Utils.escape_html(text)
end
def pretty_url(article)
"/articles/#{article.id}-#{article.headline.downcase.gsub(/\W/,'-').squeeze('-').chomp('-')}"
end
def headline_link(article)
headline = article.headline
"<a href='#{pretty_url(article)}'>#{headline}</a>"
end
def pdf_link(url)
"<a href='#{url}'>(PDF)</a>"
end
def excerpt(article, length)
body = Sanitize.clean(article.clean_body)
trunc = body.split(' ')[0..length].join(' ')
"#{trunc} <a href='/articles/#{article.id}' id='continued'>(continued)</a>"
end
end
get '/' do
@controller = "index"
print "About to get current issue"
current_issue = Article.find(:all, :params => {:issue_id => get_top_issue})
STDERR.puts current_issue
@articles_by_images = current_issue.sort_by {|a| a.image.file.url.present? ? 0 : 1 }
top_story = @articles_by_images.shift
@primary = top_story
@s1 = @articles_by_images[0]
@s2 = @articles_by_images[1]
@s3 = @articles_by_images[2]
used_ids = [@primary, @s1].map {|a| a.id}
@rest = current_issue.select {|a| not a.id.in? used_ids}
@sidebar = @rest.shift(4)
@headlines = @rest
STDERR.puts "Finished index"
STDERR.puts @sidebar
STDERR.puts @headlines
haml :index
end
get '/afd' do
haml :afd, :layout => :plain
end
get '/articles' do
redirect '/'
end
get '/articles/:id' do
@controller = "show"
@article = Article.find(params[:id])
last_modified @article.updated_at
@in_the_news = Article.find(:all, :params => {:issue_id => get_top_issue})
haml :show
end
get '/about' do
@controller = "about"
haml :about
end
get '/archive' do
@controller = "archive"
@issues = {}
PublishedIssue.find(:all).map do |i|
if @issues[i.volume].nil?
@issues[i.volume] = {}
end
@issues[i.volume][i.issue] =
[i.published_issue.url,
if i.volume >= 9 then
Article.find(:all, :params => {:issue_id => i})
else [] end]
end
haml :archive
end
get '/feed.xml' do
@articles = Article.find(:all).last(10)
builder do |xml|
xml.instruct! :xml, :version => '1.0'
xml.rss :version => '2.0' do
xml.channel do
xml.title "Chicago Shady Dealer"
xml.description "The latest stories from the University of Chicago's only intentional humor publication."
xml.link "http://chicagoshadydealer.com"
@articles.each do |article|
xml.item do
xml.title article.headline
xml.link pretty_url(article)
xml.description article.clean_body
xml.pubDate Time.parse(article.created_at.to_s).rfc822()
xml.guid pretty_url(article)
end
end
end
end
end
end
get "/styles/*.css" do |path|
content_type "text/css", charset: "utf-8"
scss :"scss/#{path}"
end