-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.rb
111 lines (89 loc) · 2.98 KB
/
config.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
require 'fastimage'
###
# Page options, layouts, aliases and proxies
###
# Per-page layout changes:
#
# With no layout
page '/*.xml', layout: false
page '/*.json', layout: false
page '/*.txt', layout: false
# General configuration
# Reload the browser automatically whenever files change
configure :development do
activate :livereload, apply_css_live: false, apply_js_live: false
end
# create a folder for each .html file and place the built template file as the index of that folder
activate :directory_indexes
activate :autoprefixer
# get pages with 'amp: true' in the frontmatter, proxy them to amp versions, and add to an array so that styles are inlined in after_build
PAGES_TO_AMP = []
#
# https://middlemanapp.com/advanced/sitemap/#using-the-sitemap-in-config-rb
ready do
sitemap.resources.group_by {|p| p.data["amp"] }.each do |make_amp_version, pages|
if make_amp_version
pages.each do |page|
proxy "/amp/#{page.path}", page.path
PAGES_TO_AMP.push(page.path.sub('.html', ''))
end
end
end
end
###
# Helpers
###
# Methods defined in the helpers block are available in templates
helpers do
# https://www.ampproject.org/docs/get_started/create/include_image.html
def amp_image(path, options = {})
imagePath = 'images/'+path
size = FastImage.size('source/'+imagePath)
altText = options[:alt] ? options[:alt] : path.gsub(/\.[\w]*/, '')
if current_page.path.match(/^amp/)
'<amp-img '+(options[:layout] ? 'layout="'+options[:layout]+'"' : nil)+' src="../'+imagePath+'" alt="'+altText+'" width="'+size[0].to_s+'" height="'+size[1].to_s+'"></amp-img>'
else
'<img src="'+imagePath+'" alt="'+altText+'"></img>'
end
end
# link the AMP version with non-AMP version
# https://www.ampproject.org/docs/get_started/create/prepare_for_discovery.html
def amp_link_pages(url, path, is_amp)
unless path.match('index.html')
path = path.sub('.html', '/index.html')
end
if is_amp
"<link rel='canonical' href='#{url+path.sub(/amp\//,'')}'>"
else
"<link rel='amphtml' href='#{url+'amp/'+path}'>"
end
end
def is_amp()
current_page.path.match(/^amp/)
end
end
# Build-specific configuration
configure :build do
# Use relative URLs
activate :relative_assets
# Minify CSS on build
activate :minify_css
end
def inline_stylesheet(html_path, file)
full_path = file == 'index' ? html_path + '.html' : html_path + '/index.html'
@INDEX = File.read( full_path )
path_to_index_dir = full_path.match(/([\w\-\/]*\/)[\w\-]*\.html$/)
stylesheet_link_regex = /<link href="([..\/]*?stylesheets\/[\w\-]*\.css)" rel="stylesheet" \/>/
@STYLESHEET = File.read( path_to_index_dir[1] + @INDEX.match(stylesheet_link_regex)[1] )
# inline the stylesheet and save file
File.write(full_path, @INDEX.gsub(
stylesheet_link_regex,
'<style amp-custom>'+@STYLESHEET+'</style>')
)
end
after_build do
PAGES_TO_AMP.each do |name|
# inline in AMP version
inline_stylesheet("build/amp/#{name}", name)
end
end