forked from ai/easings.net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
289 lines (232 loc) · 5.94 KB
/
Rakefile
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# encoding: utf-8
require 'pathname'
class Pathname
def glob(pattern, &block)
Pathname.glob(self.join(pattern), &block)
end
end
ROOT = Pathname(__FILE__).dirname
VIEWS = ROOT.join('views/')
PUBLIC = ROOT.join('public/')
IMAGES = ROOT.join('images/')
STANDALONE = %w( favicon.ico apple-touch-icon.png )
require 'evil-front-all'
JqueryCdn.local_url = proc { '/jquery.js' }
require 'r18n-core'
R18n.default_places = ROOT.join('i18n')
R18n::Filters.add('code') do |text, config|
text.gsub(/`([^`]+)`/, '<code>\1</code>')
end
R18n::Filters.add('format') do |text, config|
'<p>' +
text.gsub(/~([^~]+)~/, '<strong>\1</strong>').gsub("\n", '</p><p>') +
'</p>'
end
class R18n::TranslatedString
def link(href, args = { })
args[:href] = href
args = args.map { |k, v| "#{k}=\"#{v}\"" }.join(' ')
self.sub(/\^([^\^]+)\^/, "<a #{args}>\\1</a>")
end
end
class Easing
attr_reader :name
attr_reader :css
def initialize(attrs)
@name = attrs['name']
@css = attrs['css']
end
def linear?
@name == 'linear'
end
def in_out?
@name =~ /InOut/
end
def x(t)
if linear?
t
else
jquery_easings.eval("jQuery.easing.#{@name}(null, #{t}, 0, 1, 1)")
end
end
def dots(count, x, y)
dots = count.times.to_a.map { |i| (x.to_f / count) * (i + 1) }
dots.map do |i|
iy = y - (y * self.x(i / x.to_f))
[i.round(1), iy.round(1)]
end
end
def bezier
css.match(/cubic-bezier\(([^\)]+)\)/)[1].gsub(' ', '').gsub('0.', '.')
end
def jquery_easings
@@jquery_easings ||= begin
require 'execjs'
js = 'jQuery = { easing: {},' +
' extend: function (a, b) { jQuery.easing = b } };'
js += ROOT.join('vendor/jquery.easing.js').read
ExecJS.compile(js)
end
end
end
class Sprockets::Context
include R18n::Helpers
end
class Builder
include R18n::Helpers
include EvilFront::Helpers
attr_accessor :env
def self.instance(env = :development)
@@instance ||= self.new
@@instance.env = env
@@instance
end
def assets
@sprockets ||= begin
Sprockets::Environment.new(ROOT) do |env|
env.append_path(IMAGES)
env.append_path(ROOT.join('scripts/'))
env.append_path(ROOT.join('styles/'))
env.append_path(ROOT.join('vendor'))
EvilFront.install_all(env)
if @env == :production
env.js_compressor = Uglifier.new(copyright: false)
env.css_compressor = :csso
end
end
end
end
def all_easings
@all_easings ||= begin
YAML.load_file(ROOT.join('easings.yml')).map { |i| Easing.new(i) }
end
end
def css_easings
all_easings.reject(&:linear?).reject { |i| !i.css }
end
def js_easings
all_easings.reject(&:linear?).reject { |i| i.css }
end
def blocked(easings)
blocks = []
block = []
easings.each do |easing|
block << easing
if easing.in_out?
blocks << block
block = []
end
end
blocks
end
def linear_easing
@linear_easings ||= all_easings.find(&:linear?)
end
def render(file, &block)
options = { format: :html5, disable_escape: true, pretty: false }
Slim::Template.new(file.to_s, options).render(self, &block)
end
def to_path(dots)
dots.map { |i| i.join(',') }.join(' ')
end
def fonts_list(fonts)
fonts.map! { |i| i =~ /\s/ ? "\"#{i}\"" : i }
fonts.uniq.join(', ')
end
def production?
@env == :production
end
def each_locale(&block)
r18n.available_locales.sort { |a, b| a.code <=> b.code }.each do |locale|
yield(locale.code, locale)
end
end
def include_statistics
VIEWS.join('statistics.html').read
end
def partial(name)
render(VIEWS.join("_#{name}.slim"))
end
end
def copy_with_extra_js(from, to, js)
to.open('w') do |io|
io << from.read.gsub(/<\/title>/, "<\/title><script>#{js}</script>")
end
end
def build_index(production = false)
index = VIEWS.join('index.html.slim')
locale = R18n.get.locale.code.downcase
builder = Builder.instance(production ? :production : :development)
PUBLIC.mkpath
file = PUBLIC.join("#{locale}.html")
file.open('w') { |io| io << builder.render(index) }
if locale == 'en'
redirect = builder.assets['language-redirect.js']
copy_with_extra_js(file, PUBLIC.join("index.html"), redirect)
end
end
desc 'Build site files'
task :build do
PUBLIC.mkpath
PUBLIC.glob('*') { |i| i.rmtree }
print 'build'
R18n.available_locales.each do |locale|
R18n.set(locale.code)
build_index(true)
print '.'
end
STANDALONE.each { |i| FileUtils.cp IMAGES.join(i), PUBLIC.join(i) }
PUBLIC.join('jquery.js').open('w') do |io|
io << Builder.instance(:production).assets['jquery.js']
end
print "\n"
end
desc 'Run server for development'
task :server do
require 'sinatra/base'
class EasingsNet < Sinatra::Base
set :public_folder, nil
set :port, 3000
set :lock, true
get /^(\/|\/index\.html)$/ do
build_page('en')
send_file PUBLIC.join('index.html')
end
STANDALONE.each do |image|
get "/#{image}" do
send_file IMAGES.join(image)
end
end
get '/jquery.js' do
content_type 'text/javascript'
Builder.instance.assets['jquery.js'].to_s
end
get '/:locale' do
file = params[:locale]
file += '.html' unless file =~ /\.html/
locale = params[:locale]
locale = locale.match(/index\.(\w+)\.html/)[1] if locale =~ /index\./
build_page(locale)
send_file PUBLIC.join(file)
end
def build_page(locale_code)
R18n.clear_cache!
R18n.set(locale_code).reload!
build_index
end
end
EasingsNet.run!
end
desc 'Prepare commit to GitHub Pages'
task :deploy => :build do
sh ['git checkout gh-pages',
'git rm *.ico',
'git rm *.png',
'git rm *.html',
'git rm *.js',
'cp public/* ./',
'git add *.js',
'git add *.html',
'git add *.png',
'git add *.ico'].join(' && ')
end