-
Notifications
You must be signed in to change notification settings - Fork 1
/
renderer.rb
220 lines (187 loc) · 4.46 KB
/
renderer.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
require 'erb'
require 'kramdown'
require 'yaml'
require 'json'
class Renderer
def initialize(source_dir = nil)
@source_dir = source_dir
end
def render(lang, url)
file = resolve_path(url)
type = resolve_type(file)
if type == 'text/html' || type == 'text/markdown'
type = 'text/html'
template = ERB.new(File.read(template_for(file)))
page = render_page(lang, url, file)
values = TemplateValues.new(lang, url, file, page.content, page.meta)
Result.new(template.result(values._context), url, type)
else
page = render_page(lang, url, file)
Result.new(page.content, url, type)
end
end
def template_for(file)
if file.start_with? 'blog/'
'template-blog.html.erb'
else
'template.html.erb'
end
end
def render_asset(file)
type = resolve_type(file)
Result.new(File.read(File.join('assets', file)), file, type)
end
private
def path(file)
File.join('pages', file)
end
def dir?(path)
File.directory? path
end
def file?(path)
File.file? path
end
def resolve_path(file)
return file if file?(path(file))
return file.gsub(/\.html$/, '.md') if file?(path(file).gsub(/\.html$/, '.md'))
return resolve_path(file + '.erb') if file?(path(file + '.erb'))
return resolve_path(File.join(file, 'index.html')) if dir?(path(file))
raise RenderError.new(404, "File not found: #{file}")
end
def resolve_type(filename)
case File.extname(filename.gsub(/\.erb$/, ''))
when '.css'
'text/css'
when '.svg'
'image/svg+xml'
when '.png'
'image/png'
when '.md'
'text/html'
else
'text/html'
end
end
def render_page(lang, url, file)
result = RenderedPage.new
if File.extname(file) == '.erb'
template = ERB.new(File.read(path(file)))
values = PageValues.new(lang, url, file)
result.content = template.result(values._context)
elsif File.extname(file) == '.md'
text = File.read(path(file))
doc = Kramdown::Document.new(text, input: 'MetadataKramdown')
result.content = doc.to_html
result.meta = doc.root.metadata
else
result.content = File.read(path(file))
end
result
end
end
class Renderer
class RenderedPage
attr_accessor :content
attr_writer :meta
def meta
@meta ||= {}
end
end
end
class Renderer
class Result
attr_reader :content, :type
def initialize(content, filename, type)
@content = content
@filename = filename
@type = type
end
def filename
if @filename == ''
'index.html'
else
@filename
end
end
end
end
class Renderer
class Values
attr_reader :lang, :url, :file
attr_accessor :locals
def initialize(lang, url, file)
@lang = lang
@url = url
@file = file
@locals = {}
end
def partial(key, locals = {})
copy = dup
copy.locals = locals
ERB.new(File.read("partials/#{key}.html.erb")).result(copy._context)
end
def content(key)
text = File.read("content/#{lang}/#{key}.md")
Kramdown::Document.new(text).to_html
end
def t(key, values = {})
file = File.join('content', 'translations.yml')
result = YAML.safe_load(File.read(file))[lang]
key.split('.').each do |k|
result = result.fetch(k)
end
if values.empty?
result
else
result % values
end
end
def data(key)
nodes = key.split('.')
filename = nodes.shift
file = File.join 'data', filename + '.json'
result = JSON.parse File.read(file), symbolize_names: true
nodes.each do |k|
result = result.fetch(k.to_sym)
end
result
end
def feature?(type)
ENV.fetch("FEATURE_#{type.to_s.upcase}", false)
end
def _context
binding
end
end
end
class Renderer
class TemplateValues < Values
attr_reader :main, :meta
def initialize(lang, url, file, main, meta = {})
super lang, url, file
@main = main
@meta = meta.each_with_object({}) do |(k, v), result|
result[k.to_sym] = v
end
end
def is_blog_post?
file.start_with?('blog/') && file.end_with?('md')
end
end
end
class Renderer
class PageValues < Values
end
end
class Renderer
class RenderError < RuntimeError
attr_reader :type, :text
def initialize(type, text)
@type = type
@text = text
end
def message
"#{type}: #{text}"
end
end
end