forked from ai/easings.net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
308 lines (248 loc) · 6.53 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# encoding: utf-8
require 'pathname'
ROOT = Pathname(__FILE__).dirname
CONTENT = ROOT.join('content/')
PUBLIC = ROOT.join('public/')
LAYOUT = ROOT.join('layout/')
require 'uglifier'
require 'sprockets'
require 'slim'
require 'compass'
require 'ceaser-easing'
Compass.configuration.images_path = LAYOUT.to_s
require 'r18n-core'
R18n.default_places = ROOT.join('i18n')
require 'coffee_script'
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 Pathname
def glob(pattern, &block)
Pathname.glob(self.join(pattern), &block)
end
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 SassCompiler < Sprockets::SassTemplate
def sass_options(context)
{
filename: eval_file,
line: line,
syntax: syntax,
cache_store: Sprockets::SassCacheStore.new(context.environment),
importer: Sprockets::SassImporter.new(context, context.pathname),
sprockets: { context: context, environment: context.environment },
load_paths: context.environment.paths.map { |path|
Sprockets::SassImporter.new(context, path)
}
}
end
def evaluate(context, locals, &block)
::Sass::Engine.new(data, sass_options(context)).render
rescue ::Sass::SyntaxError => e
context.__LINE__ = e.sass_backtrace.first[:line]
raise e
end
end
class CompressedSassCompiler < SassCompiler
def sass_options(*params)
super.merge(style: :compressed)
end
end
class Helpers
include R18n::Helpers
attr_accessor :path
attr_accessor :env
def self.instance(env)
@@instance ||= self.new
@@instance.env = env
@@instance
end
def assets
@sprockets ||= begin
Sprockets.register_engine '.sass',
@env == :production ? CompressedSassCompiler : SassCompiler
Sprockets::Environment.new(ROOT) do |env|
env.append_path(LAYOUT)
env.append_path(ROOT.join('vendor'))
Sass.load_paths.concat(Compass.sass_engine_options[:load_paths])
if @env == :production
env.js_compressor = Uglifier.new(copyright: false)
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 }
Slim::Template.new(file.to_s, options).render(self, &block)
end
def to_path(dots)
dots.map { |i| i.join(',') }.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
LAYOUT.join('statistics.html').read
end
def easing_example(name = t.howtos.name)
name = name.gsub(' ', ' ').gsub('-', '‑') # non-break space and hyphen
"<strong>#{ name }</strong>"
end
def partial(name)
render(LAYOUT.join("_#{name}.slim"))
end
end
def build_file(slim, production = false)
layout = LAYOUT.join('layout.html.slim')
helper = Helpers.instance(production ? :production : :development)
locale = R18n.get.locale
path = slim.relative_path_from(LAYOUT).sub_ext('').sub_ext('').to_s
subpath = locale.code == 'en' ? '.html' : ".#{locale.code.downcase}.html"
PUBLIC.mkpath
file = PUBLIC.join(path + subpath)
helper.path = path
file.open('w') do |html|
html << helper.render(layout) { helper.render(slim) }
end
file
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)
LAYOUT.glob('**/*.html.slim') do |slim|
next if slim.basename.to_s == 'layout.html.slim'
build_file(slim, true)
print '.'
end
end
%w( favicon.ico apple-touch-icon.png ).each do |file|
FileUtils.cp LAYOUT.join(file), PUBLIC.join(file)
end
print "\n"
end
desc 'Run server for development'
task :server do
require 'sinatra/base'
class EasingsNet < Sinatra::Base
set :public_folder, nil
get /^(\/|\/index\.html)$/ do
send_file build_page('index', 'en')
end
get '/index.:locale.html' do
send_file build_page('index', params[:locale])
end
get '/favicon.ico' do
send_file LAYOUT.join('favicon.ico')
end
get '/apple-touch-icon.png' do
send_file LAYOUT.join('apple-touch-icon.png')
end
def build_page(page, locale_code)
R18n.clear_cache!
path = LAYOUT.join("#{page}.html.slim")
R18n.set(locale_code).reload!
build_file(path)
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',
'cp public/* ./',
'git add *.html',
'git add *.png',
'git add *.ico'].join(' && ')
end