-
Notifications
You must be signed in to change notification settings - Fork 5
/
liquid.rb
373 lines (295 loc) · 11.7 KB
/
liquid.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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# Dependencies
require 'liquid'
require 'json'
require 'digest/md5'
require 'uri'
# Args/params from CLI usage
$template = ARGV[0]
$data = JSON.parse( File.read("site/temp/#{ARGV[1]}.json") )
# Liquid includes
Liquid::Template.file_system = Liquid::LocalFileSystem.new('src/theme/snippets/', '%s.liquid')
module Liquid
class LocalFileSystem
def full_path(template_path)
raise FileSystemError, "Illegal template name '#{template_path}'" unless template_path =~ /\A[^\/][a-zA-Z0-9_.\-\/]+\z/
full_path = if template_path.include?('/'.freeze)
File.join(root, File.dirname(template_path), @pattern % File.basename(template_path))
else
File.join(root, @pattern % template_path)
end
raise FileSystemError, "Illegal template path '#{File.expand_path(full_path)}'" unless File.expand_path(full_path) =~ /\A#{File.expand_path(root)}/
full_path
end
end
end
# HTML filters
module HtmlFilters
def stylesheet_tag(input)
"<link href='#{input}' rel='stylesheet' type='text/css' media='all' />"
end
def script_tag(input)
"<script src='#{input}' type='text/javascript'></script>"
end
def img_tag(input, alt = nil, classes = nil)
"<img src='#{input}' alt='#{alt}' class='#{classes}' />"
end
end
Liquid::Template.register_filter(HtmlFilters)
# Money filters
module MoneyFilters
def money(cents, format = $data['shop']['money_format'])
return '' if cents.nil?
if cents.is_a? String
cents = cents[/[^.]+/]
cents = cents.to_i
end
value = ''
placeholderRegex = /\{\{\s*(\w+)\s*\}\}/
formatString = format
def format_with_delimiters(number, precision = 2, thousands = ',', decimal = '.')
return 0 if !number.is_a? Numeric || number.nil?
number = (number/100.0)
number = "%.#{precision}f" % number
parts = "#{number}".split(".")
dollars = parts[0].gsub(/(\d)(?=(\d\d\d)+(?!\d))/, '\\1' + thousands)
cents = parts[1] ? (decimal + parts[1]) : ''
return dollars + cents
end
case formatString.match(placeholderRegex)[1]
when 'amount'
value = format_with_delimiters(cents)
when 'amount_no_decimals'
value = format_with_delimiters(cents, 0)
when 'amount_with_comma_separator'
value = format_with_delimiters(cents, 2, '.', ',')
when 'amount_no_decimals_with_comma_separator'
value = format_with_delimiters(cents, 0, '.', ',')
end
formatString.gsub(placeholderRegex, value)
end
def money_with_currency(cents)
money(cents, format = $data['shop']['money_with_currency_format'])
end
def money_without_trailing_zeros(cents, currency = nil)
money(cents).sub!(".00", "")
end
def money_without_currency(cents)
money(cents, "{{amount}}")
end
end
Liquid::Template.register_filter(MoneyFilters)
# String filters
module StringFilters
def pluralize(number, singular, plural = nil)
number = number.to_i
if number == 1
"#{singular}"
elsif plural.nil?
"#{singular}s"
else
"#{plural}"
end
end
def camelcase(input)
input = input.gsub(/[^0-9A-Za-z]/, ' ')
input = input.split.map(&:capitalize).join(' ')
input = input.gsub(/\s+/, "")
input
end
def handle(input)
input = input.gsub(/[^0-9A-Za-z]/, '-').gsub('--', '-')
input = input.downcase
input
end
def handleize(input)
handle(input)
end
def md5(input)
Digest::MD5.hexdigest(input)
end
def url_escape(input)
URI.escape(input)
end
def url_param_escape(input)
url_escape(input).gsub('&', '%26')
end
end
Liquid::Template.register_filter(StringFilters)
# URL filters
module UrlFilters
def shopify_asset_url(input)
scripts = {
:'gift-card.css' => "//cdn.shopify.com/s/assets/themes_support/gift-card-3beec6c8bd8796542b856b476db7b451672def9717738b62af01bae1b91fb999.css",
:'modernizr.gift-card.js' => "//cdn.shopify.com/s/assets/themes_support/modernizr.gift-card-4752c7825ca00ad578f2519c935eaec9b7935f5d75c3f3f03e8e8a4238f9b0c3.js",
:'vendor/qrcode.js' => "//cdn.shopify.com/s/assets/themes_support/vendor/qrcode-38dd6050897a069ec84f8bddc58687ebb811a98fc7bc590403aa51d8864005ed.js",
:'vendor/html5shiv.js' => "//cdn.shopify.com/s/assets/themes_support/vendor/html5shiv-38ba6f0f7fa20f650eadee708c9f77d1a0781c79a2453a22da2db72506484015.js",
:'gift-card/card.jpg' => "//cdn.shopify.com/s/assets/themes_support/gift-card/card-ab9d5ddd65960cb0517b60490896c3431b8ca69e1085651ec5f50afa9f852c56.jpg",
:'gift-card/icon-bug.png' => "//cdn.shopify.com/s/assets/themes_support/gift-card/icon-bug-e645ea11d9edae4cf5230c1eea955694fd12b27fcf6d2bf11cb0184558525b96.png",
:'shopify_common.js' => "//cdn.shopify.com/s/assets/themes_support/shopify_common-c14dd13bd5df45b21d8926574bf1aea3f0f1202f080e6ddb3eb954bdb8fa195e.js",
:'customer_area.js' => "//cdn.shopify.com/s/assets/themes_support/customer_area-8e117b99b6e8454435d157c197e9375e762aba8a5888846d0f7a76b930bf1ece.js",
:'option_selection.js' => "//cdn.shopify.com/s/assets/themes_support/option_selection-6e8dd69c6db6118b1aa534eec7452e7dd224ae1d8ac4f0d466af6e3ef9d2ef2c.js",
:'api.jquery.js' => "//cdn.shopify.com/s/assets/themes_support/api.jquery-3fdf7f8ecde4022383bde5e52e9d15002ce0866a679c7fa8a02be79b7a21c3ec.js",
:'customer.css' => "//cdn.shopify.com/s/assets/themes_support/customer-be6a3d94e17643f1864112560e8d4c9d06e170e4a2c4c3d640eedcc2a66b5c10.css"
}
script = ''
scripts.each do |key, value|
if "#{key}" == input
script = value
end
end
script
end
def asset_url(input)
path = input
if input.include? ".scss"
"assets/#{input}".sub! '.scss.css', '.css'
elsif input.include? ".css"
"assets/#{input}"
elsif input.include? ".js"
"assets/#{input}"
else
"../src/theme/assets/#{input}"
end
end
def file_url(input)
"files/#{input}"
end
def customer_login_link(input)
"<a href='/account/login' id='customer_login_link'>#{input}</a>"
end
def global_asset_url(input)
"//cdn.shopify.com/s/global/#{input}"
end
def img_url(input, size)
hash = input.to_json
hash = JSON.parse(hash, :symbolize_names => true)
if hash['image'.to_sym].kind_of?(String)
"#{hash['image'.to_sym]}/?_#{size}.jpg"
else
"#{hash['image'.to_sym]['src'.to_sym]}/?_#{size}.jpg"
end
end
def product_img_url(input, size)
json = input.to_json
if json.gsub(/["|']/, '') == input
"#{input}/?_#{size}.jpg"
else
hash = JSON.parse(json, :symbolize_names => true)
"#{hash['src'.to_sym]}/?_#{size}.jpg"
end
end
def collection_img_url(input, size = nil)
"The `collection_img_url` filter is deprecated. Use the `img_url` filter instead."
end
def link_to(input, url, title = nil)
"<a href='#{url}' title='#{title}'>#{input}</a>"
end
def link_to_vendor(input)
"<a href='collection.html' title='#{input}'>#{input}</a>"
end
def link_to_type(input)
"<a href='collection.html' title='#{input}'>#{input}</a>"
end
def link_to_tag(input, tag = nil)
"<a href='collection.html' title='#{input}'>#{input}</a>"
end
def link_to_add_tag(input, tag = nil)
"<a href='collection.html' title='#{input}'>#{input}</a>"
end
def link_to_remove_tag(input, tag = nil)
"<a href='collection.html' title='#{input}'>#{input}</a>"
end
def url_for_type(input)
"collection.html"
end
def url_for_vendor(input)
"collection.html"
end
def within(input, collection)
input
end
def payment_type_img_url(input)
assets = {
:'visa' => "//cdn.shopify.com/s/assets/global/payment_types/creditcards_visa-e9f829d15f5ec3b1953ba8b9bc59b448ddb1ec9235ae70c7936178744cb31489.svg",
:'master' => "//cdn.shopify.com/s/assets/global/payment_types/creditcards_master-94df290d56a3c6424296953282e18e46895ba94bdef368640eb52f8349915610.svg",
:'american_express' => "//cdn.shopify.com/s/assets/global/payment_types/creditcards_american_express-54be4e6bfc1dbd42f93af603e102fde2356dfe2372a7dd851074964337533296.svg",
:'paypal' => "//cdn.shopify.com/s/assets/global/payment_types/creditcards_paypal-dd71910a20fd73f78b4eed60e89331d4f4ceb38d55ef42e1e9935d78070ba3e2.svg",
:'jcb' => "//cdn.shopify.com/s/assets/global/payment_types/creditcards_jcb-8f8079695e74d5688707997b208994c4d8078baa58b866ed47852cf9674d9332.svg",
:'diners_club' => "//cdn.shopify.com/s/assets/global/payment_types/creditcards_diners_club-d07838507a213e5e52ef4272d3ccfe3da294642628264cd0934baf6878ceb197.svg",
:'maestro' => "//cdn.shopify.com/s/assets/global/payment_types/creditcards_maestro-e2bbae787fceedc1ffffbc57b6fb487ec70a781b549dccbe8cb6c335f04c997e.svg",
:'google_wallet' => "//cdn.shopify.com/s/assets/global/payment_types/creditcards_google_wallet.svg",
:'discover' => "//cdn.shopify.com/s/assets/global/payment_types/creditcards_discover-96a6318d3deeede785ba12114a8d00bd23731f9f16e14ddcd0bdfd5e1d846307.svg",
:'solo' => "//cdn.shopify.com/s/assets/global/payment_types/creditcards_solo-45cb0fbbcdcab59108d459100087afe1fb58b30a6f2de84bdf12fe5d2c103453.svg",
:'switch' => "//cdn.shopify.com/s/assets/global/payment_types/creditcards_switch-e7989e629a63b619528b08c3ff54ee0d6ea733e0500e650acd6d3bf5077bdf4c.svg",
:'laser' => "//cdn.shopify.com/s/assets/global/payment_types/creditcards_laser-5440f3fcdddd6b354a92670ef0473e446aed22e1c96f6e6381b20346612fbfd5.svg",
:'dankort' => "//cdn.shopify.com/s/assets/global/payment_types/creditcards_dankort-b173653edc11a28324ed184bcd61735fc1855e73d643a01ddfc5725ea8056a61.svg",
:'forbrugsforeningen' => "//cdn.shopify.com/s/assets/global/payment_types/creditcards_forbrugsforeningen-ba4af2dab3d288898e94f43470c5299e04f3df414dee7a5ed07df5a1ff8fca34.svg",
:'dwolla' => "//cdn.shopify.com/s/assets/global/payment_types/creditcards_dwolla-fb1a5ac0b4aaf67d1fa6caf35ea3f7ebb6e1864fb9dee569967848f26dd0bad3.svg",
:'bitcoin' => "//cdn.shopify.com/s/assets/global/payment_types/creditcards_bitcoin-4d4d7fba75da840a79283046ed1491138e986b3a57597cd7574fdbfe3f9e5605.svg"
}
asset = ''
assets.each do |key, value|
if "#{key}" == input
asset = value
end
end
asset
end
end
Liquid::Template.register_filter(UrlFilters)
# Additional filters
module AdditionalFilters
def json(input)
input.to_json
end
def default_errors(input)
input
end
def default_pagination(input)
"
<span class='previous'><a href='#' title=''>« Previous</a></span>
<span class='page current'>1</span>
<span class='page'><a href='#' title=''>2</a></span>
<span class='page'><a href='#' title=''>3</a></span>
<span class='next'><a href='#' title=''>Next »</a></span>
"
end
def highlight(input, text)
input.gsub(text, "<strong class='highlight'>#{text}</strong>")
end
def highlight_active_tag(input)
input
end
def weight_with_unit(input, unit = "kg")
"#{input} #{unit}"
end
end
Liquid::Template.register_filter(AdditionalFilters)
# Dummy `{% paginate %}{% endpaginate %}` tag, all it does is render everything
# inside the tags
class Paginate < Liquid::Block
def render(context)
super
end
end
Liquid::Template.register_tag('paginate', Paginate)
# Dummy `{% layout %}` tag, it doesn't actually render anything, I use the Gulp
# task to assemble the layouts
class Layout < Liquid::Tag
end
Liquid::Template.register_tag('layout', Layout)
# Dummy `{% form %}{% endform %}` tag, all it does is wrap the contents of the
# tag in a HTML `<form />`
class Form < Liquid::Block
def render(context)
"
<form accept-charset='UTF-8' action='#' method='post'>
#{super}
</form>
"
end
end
Liquid::Template.register_tag('form', Form)
# Return generated HTML for usage in the Gulp task
template = Liquid::Template.parse( $template )
puts template.render( $data )