-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.rb
272 lines (220 loc) · 7.2 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# frozen_string_literal: true
PRODUCTION_URL = 'https://example.com'
STAGING_URL = 'https://stg.example.com'
DEPLOY_BRANCH = 'staging'
# remove this dir after build
GENERAL_CSS_DIR = 'build/style/general'
###
# Page options, layouts, aliases and proxies
###
set :css_dir, 'style'
set :js_dir, 'script'
set :images_dir, 'img'
# set :build_dir, '../html'
set :slim, pretty: true, sort_attrs: false, format: :html
# Multiple languages
# activate :i18n
activate :asset_hash
# URL access xxx.hmtl -> /xxx/
activate :directory_indexes
activate :automatic_image_sizes
activate :autoprefixer do |config|
config.browsers = ['last 2 versions', 'Explorer >= 11']
end
activate :external_pipeline,
name: :webpack,
command: if build?
'./node_modules/webpack/bin/webpack.js --bail -p'
else
'./node_modules/webpack/bin/webpack.js --watch -d --progress --color'
end,
source: '.tmp/dist',
latency: 1
activate :deploy do |deploy|
deploy.deploy_method = :git
deploy.build_before = true
deploy.branch = DEPLOY_BRANCH
end
# Per-page layout changes:
#
# With no layout
page '/*.xml', layout: false
page '/*.json', layout: false
page '/*.txt', layout: false
# With alternative layout
# page "/path/to/file.html", layout: :otherlayout
# Proxy pages (http://middlemanapp.com/basics/dynamic-pages/)
# proxy "/this-page-has-no-template.html", "/template-file.html", locals: {
# which_fake_page: "Rendering a fake page with a local variable" }
# General configuration
# Reload the browser automatically whenever files change
configure :development do
activate :livereload
end
# Build-specific configuration
configure :build do
# Minify CSS on build
# activate :minify_css
# Minify Javascript on build
# activate :minify_javascript
before_build do
puts ':::before_build:::'
# avoid losting old deploy history by force push
if Dir.exist?('build')
Dir.chdir 'build'
current_branch = `git rev-parse --abbrev-ref HEAD`
current_branch.gsub!(/\n/, '') # pick up only branch name
puts "current_branch: #{current_branch}"
puts "DEPLOY_BRANCH: #{DEPLOY_BRANCH}"
system "git checkout #{DEPLOY_BRANCH}" if current_branch != DEPLOY_BRANCH
system 'git pull origin'
Dir.chdir '..'
else
puts 'There isn\'t build dir yet, so do nothing.'
end
end
after_build do
puts ':::after_build:::'
puts "delete #{GENERAL_CSS_DIR}"
system "rm -rf #{GENERAL_CSS_DIR}"
end
end
###
# Helpers
###
helpers do
def site_url
if config[:environment] == :development
'http://localhost:4567'
elsif DEPLOY_BRANCH == 'staging'
STAGING_URL
else
PRODUCTION_URL
end
end
def current_page?(path)
current_page.url == path
end
# support high res images
# Usage: =img_tag '/img/example.png', alt: 'description'
def img_tag(src, options = {})
# enable to set attributes in options
retina_src = src.gsub(/\.\w+$/, '@2x\0')
image_tag(src, options.merge(srcset: "#{retina_src} 2x"))
end
# Usage: =img_tag_sp '/img/example.png', alt: 'description'
def img_tag_sp(src, options = {})
sp_src = src.gsub(/\.\w+$/, '-sp\0')
# class treatment
pc_opt = options.merge(class: 'pc') { |_key, v0, v1| "#{v0} #{v1}" }
sp_opt = options.merge(class: 'sp') { |_key, v0, v1| "#{v0} #{v1}" }
# id treatment
sp_opt[:id] = sp_opt[:id] + '_sp' if sp_opt[:id]
img_tag(src, pc_opt) + img_tag(sp_src, sp_opt)
end
# image_tag with sp image
def image_tag_sp(src, options = {})
sp_src = src.gsub(/\.\w+$/, '-sp\0')
# class treatment
pc_opt = options.merge(class: 'pc') { |_key, v0, v1| "#{v0} #{v1}" }
sp_opt = options.merge(class: 'sp') { |_key, v0, v1| "#{v0} #{v1}" }
# id treatment
sp_opt[:id] = sp_opt[:id] + '_sp' if sp_opt[:id]
image_tag(src, pc_opt) + image_tag(sp_src, sp_opt)
end
# width,heightに小数点を許容するimage_tagの代替
# (本家のimage_tagは、svgが来た時にsizeを丸めてしまいデザインがずれる)
def image_tag_strict(path, params = {})
if path =~ /\.svg$/i
image_path = get_image_filepath(path)
if File.exists?(image_path)
xml = File.open(image_path) { |f| Nokogiri::XML(f) }
params.merge!(width: xml.root['width'], height: xml.root['height'])
end
end
image_tag(path, params)
end
# テンプレート内などrubyからasset_hash付きのURLを取得する
def image_url(path)
asset_url(path, config[:images_dir])
end
def get_image_filepath(path)
File.join(config[:source], config[:images_dir], path)
end
def nl2br(txt)
txt.gsub(/(\r\n|\r|\n)/, '<br>')
end
# Get another language page url
# http://forum.middlemanapp.com/t/i18n-list-of-language-siblings-and-links-to-them/978/2
def translated_url(locale)
# Assuming /:locale/page.html
untranslated_path = @page_id.split('/', 2).last.sub(/\..*$/, '')
if untranslated_path == 'index'
untranslated_path = ''
path = locale == :en ? '/' : '/ja/'
else
begin
translated = I18n.translate!("paths.#{untranslated_path}", locale: locale)
rescue I18n::MissingTranslationData
translated = untranslated_path
end
path = locale == :en ? "/#{translated}/" : "/#{locale}/#{translated}/"
end
asset_url(path)
end
def other_langs
langs - [I18n.locale]
end
# Include svg file in line
# https://gist.github.com/bitmanic/0047ef8d7eaec0bf31bb
def inline_svg(relative_image_path, optional_attributes = {})
image_path = get_image_filepath(relative_image_path)
# If the image was found...
if File.exists?(image_path)
# Open the image
image = File.open(image_path, 'r') { |f| f.read }
# Return the image if no optional attributes were passed in
return image if optional_attributes.empty?
# Otherwise, parse the image
document = Oga.parse_xml(image)
svg = document.css('svg').first
# Then, add the attributes
# NOTE: This allows for hash-based values, but we're only going one level
# deep right now. If you know a great way to dig `N` levels deeper,
# feel free to post about it on the Gist.
optional_attributes.each do |attribute, value|
case value
when Hash
value.each do |subattribute, subvalue|
unless subvalue.class == Hash
svg.set(
"#{attribute} #{subattribute}".parameterize,
subvalue.html_safe
)
end
end
else
svg.set(attribute.to_sym, value.html_safe)
end
end
# Finally, return the image
document.to_xml
# If the file wasn't found...
else
# Embed an inline SVG image with an error message
%(
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 30"
width="400px" height="30px"
>
<text font-size="16" x="8" y="20" fill="#cc0000">
Error: '#{relative_image_path}' could not be found.
</text>
<rect
x="1" y="1" width="398" height="28" fill="none"
stroke-width="1" stroke="#cc0000"
/>
</svg>
)
end
end
end