-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
191 lines (164 loc) · 5.72 KB
/
Copy pathapp.rb
File metadata and controls
191 lines (164 loc) · 5.72 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
require "sinatra/base"
require "builder"
require "haml"
require "sass"
require File.expand_path('lib/cache', File.dirname(__FILE__))
require File.expand_path('lib/config', File.dirname(__FILE__))
require File.expand_path('lib/models', File.dirname(__FILE__))
require File.expand_path('lib/path', File.dirname(__FILE__))
require File.expand_path('lib/plugins', File.dirname(__FILE__))
require File.expand_path('lib/overrides', File.dirname(__FILE__))
Nesta::Plugins.load_local_plugins
module Nesta
class App < Sinatra::Base
register Sinatra::Cache
set :root, File.dirname(__FILE__)
set :cache_enabled, Config.cache
helpers Overrides::Renderers
helpers do
def set_from_config(*variables)
variables.each do |var|
instance_variable_set("@#{var}", Nesta::Config.send(var))
end
end
def set_from_page(*variables)
variables.each do |var|
instance_variable_set("@#{var}", @page.send(var))
end
end
def set_title(page)
if page.respond_to?(:parent) && page.parent
@title = "#{page.heading} - #{page.parent.heading}"
else
@title = "#{page.heading} - #{Nesta::Config.title}"
end
end
def display_menu(menu, options = {})
defaults = { :class => nil, :levels => 2 }
options = defaults.merge(options)
if options[:levels] > 0
haml_tag :ul, :class => options[:class] do
menu.each do |item|
haml_tag :li do
if item.respond_to?(:each)
display_menu(item, :levels => (options[:levels] - 1))
else
haml_tag :a, :href => item.abspath do
haml_concat item.heading
end
end
end
end
end
end
end
def no_widow(text)
text.split[0...-1].join(" ") + " #{text.split[-1]}"
end
def set_common_variables
@menu_items = Nesta::Menu.for_path('/')
@site_title = Nesta::Config.title
set_from_config(:title, :subtitle, :google_analytics_code)
@heading = @title
end
def url_for(page)
File.join(base_url, page.path)
end
def base_url
url = "http://#{request.host}"
request.port == 80 ? url : url + ":#{request.port}"
end
def absolute_urls(text)
text.gsub!(/(<a href=['"])\//, '\1' + base_url + '/')
text
end
def nesta_atom_id_for_page(page)
published = page.date.strftime('%Y-%m-%d')
"tag:#{request.host},#{published}:#{page.abspath}"
end
def atom_id(page = nil)
if page
page.atom_id || nesta_atom_id_for_page(page)
else
"tag:#{request.host},2009:/"
end
end
def format_date(date)
date.strftime("%d %B %Y")
end
def local_stylesheet?
# Checks for the existence of local/views/local.sass. Useful for
# themes that want to give the user the option to add their own
# CSS rules.
File.exist?(
File.join(File.dirname(__FILE__), *%w[local views local.sass]))
end
end
not_found do
set_common_variables
haml(:not_found)
end
error do
set_common_variables
haml(:error)
end unless Sinatra::Application.environment == :development
# If you want to change Nesta's behaviour, you have two options:
#
# 1. Edit the code. You can merge in future upstream changes with git.
# 2. Add code to local/app.rb that overrides the default behaviour,
# leaving the default files untouched (no "tricky" merging required).
#
# Neither way is necessarily *better* than the other; it's up to you to
# choose the most appropriate course of action for your site. Merging
# future changes in will typically be a straightforward task, but you may
# find the ./local directory to be an easy way to manage more significant
# changes to Nesta's behaviour that are likely to conflict with future
# changes to the main code base.
#
# Note that you can modify the behaviour of any of the default objects
# in local/app.rb, or replace any of the default view templates by
# creating replacements of the same name in local/views.
Overrides.load_local_app
Overrides.load_theme_app
get "/css/:sheet.css" do
content_type "text/css", :charset => "utf-8"
cache sass(params[:sheet].to_sym)
end
get "/" do
set_common_variables
set_from_config(:title, :subtitle, :description, :keywords)
@heading = @title
@title = "#{@title} - #{@subtitle}"
@articles = Page.find_articles[0..7]
@body_class = "home"
cache haml(:index)
end
get %r{/attachments/([\w/.-]+)} do
file = File.join(Nesta::Config.attachment_path, params[:captures].first)
send_file(file, :disposition => nil)
end
get "/articles.xml" do
content_type :xml, :charset => "utf-8"
set_from_config(:title, :subtitle, :author)
@articles = Page.find_articles.select { |a| a.date }[0..9]
cache builder(:atom)
end
get "/sitemap.xml" do
content_type :xml, :charset => "utf-8"
@pages = Page.find_all
@last = @pages.map { |page| page.last_modified }.inject do |latest, page|
(page > latest) ? page : latest
end
cache builder(:sitemap)
end
get "*" do
set_common_variables
parts = params[:splat].map { |p| p.sub(/\/$/, "") }
@page = Nesta::Page.find_by_path(File.join(parts))
raise Sinatra::NotFound if @page.nil?
set_title(@page)
set_from_page(:description, :keywords)
cache haml(@page.template, :layout => @page.layout)
end
end
end