-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpage.rb
64 lines (57 loc) · 1.28 KB
/
page.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
require 'haml'
require 'time'
# base
class Base
def initialize(opts)
@opts = opts
%w(title description).each do |var|
eval "def #{var}=(v)\n @opts[:#{var}]=v\nend"
eval "def #{var}\n @opts[:#{var}]\nend"
end
end
def html
hash = { lang: 'en' }
hash['⚡'] = '' if @opts[:amp]
hash
end
def css(name)
if @opts[:amp]
'<style amp-custom="amp-custom">' +
File.read("target/css/#{name}") +
'</style>'
else
"<link type='text/css'
href='/css/#{name}?#{@opts[:revision]}'
rel='stylesheet'/>"
end
end
def youtube(id)
if @opts[:amp]
"<amp-youtube data-videoid='#{id}'
layout='responsive' width='480' height='270'></amp-youtube>"
else
"<iframe class='video'
src='https://www.youtube.com/embed/#{id}?controls=2'
frameborder='0' allowfullscreen='yes'>​</iframe>"
end
end
def input(path)
Page.new(File.read(path), @opts).html
end
end
# Page to render
class Page
def initialize(haml, opts)
@haml = haml
@opts = opts
end
def html
engine = Haml::Engine.new(
@haml,
format: :xhtml,
style: :indented
)
@opts[:published] = Time.now unless @opts.key?(:published)
engine.render(Base.new(@opts), @opts)
end
end