-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.rb
92 lines (83 loc) · 2.4 KB
/
app.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
#encoding: utf-8
# Use the app.rb file to load Ruby code, modify or extend the models, or
# do whatever else you fancy when the theme is loaded.
require 'i18n'
require 'i18n/backend/fallbacks'
require 'json'
module Rack
class Codehighlighter
def coderay(string)
lang = 'unknown'
refs = @opts[:pattern].match(string) # extract language name
if refs
opts = {:line_numbers => :inline}
lang = refs[1]
str = unescape_html(string.sub(@opts[:pattern], ""))
"<pre class='CodeRay'>#{::CodeRay.encoder(:html).encode str, lang, opts}</pre>"
else
"<pre class='CodeRay'>#{string}</pre>"
end
end
end
end
module Nesta
class App
configure do
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
I18n.load_path = Dir[File.join(settings.root, 'locales', '*.yml')]
I18n.backend.load_translations
I18n.locale = :sv
end
helpers do
def format_date(date)
I18n.localize date, :format => "%d %B %Y"
end
def sub_menu_items(level = 1)
ancestors = []
page = @page
while page
ancestors << page
page = page.parent
end
if ancestors.length <= level
return []
end
menu = Nesta::Menu.for_path(ancestors.reverse[level].abspath)
menu.shift
menu.flatten(1)
end
def articles_heading
@page.metadata('articles heading') || "Artiklar i #{@page.heading}"
end
end
# Add new routes here.
post '/api/github-update-hook' do
content_type 'application/json'
# Github IP block: 192.30.252.0/22
return JSON.generate({:error => 'POST variable "payload" missings'}) if params[:payload].nil?
begin
payload = JSON.parse(params[:payload])
rescue
return JSON.generate({:error => 'Failed to parse payload'})
end
cmd = 'cd %s && git fetch && git reset --hard origin/master' % Nesta::Config.content
if payload['ref'] == 'refs/heads/master' and system(cmd)
JSON.generate({:result => 'Updated content'})
else
JSON.generate({:error => 'Master branch not updated'})
end
end
end
class Page
def read_more
metadata('read more') || 'Läs mer'
end
end
module Navigation
module Renderers
def breadcrumb_label(page)
(page.abspath == '/') ? 'Hem' : page.heading
end
end
end
end