-
Notifications
You must be signed in to change notification settings - Fork 17
/
site.rb
460 lines (370 loc) · 13.8 KB
/
site.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
require 'base64'
require 'time'
require 'money'
#Set the root directory
Hardwired::Paths.root = ::File.expand_path('.', ::File.dirname(__FILE__))
I18n.config.available_locales = :en
Money.locale_backend = :i18n
I18n.locale = :en
DEFAULT_DOCS_VERSION = "v4"
##The location of the current file is used for calculating the default 'root' setting
class Site < Hardwired::Bootstrap
#require 'debugger' if development?
#Debugger.start(:post_mortem => true) if development?
helpers Hardwired::ContentFor
#Load config.yml from the root
config_file 'config.yml'
helpers do
def benefits_widths(*cols); cols.map{|w| (w / cols.inject(:+).to_f * (269 - 10 * (cols.count - 1))).round } end;
def benefits_normal_width(count); ((269 - 10 * (count - 1)) / count.to_f).round end;
\
def canonical
pn = request[:page]
uri = URI::join(config.url,page.path)
if pn
uri.query = URI.encode_www_form({"page" => pn})
end
uri
end
def versions
[{id: "v2",
folder: "/docs/v2/",
home: "/docs/v2",
title: "v2.X"},
{id: "v3",
folder: "/docs/v3/",
home: "/docs/v3",
title: "v3.x"},
{id: "v4",
folder: "/docs/v4/",
home: "/docs/v4",
title: "v4.x"}]
end
def nav_generate_plugins(version, arg_1)
index.pages_tagged("plugin").sort_by{|page| page.heading}.select{|page| page.path.start_with?("/docs/#{version}/")}.map do |page|
{"path" => page.path, "title" => page.heading}
end
end
def nav_generate_releases(version, arg_1)
releases.map{|page| {"path" => page.path, "title" => page.heading}}
end
def nav_resolve(yaml_tree, relative_to, version_id)
#Process generated content
gen_name = yaml_tree["generate"]
gen_arg_1 = yaml_tree["generate_arg_1"]
if (gen_name == "plugins")
yaml_tree["items"] = nav_generate_plugins(version_id, gen_arg_1)
end
if (gen_name == "releases")
yaml_tree["items"] = nav_generate_releases(version_id, gen_arg_1)
end
#resolve relative URLs
path = yaml_tree["path"]
unless path.nil?
path = File.join(relative_to,path) unless path.start_with?('/')
yaml_tree["path"] = path
end
#recurse
yaml_tree["items"].each do |child|
nav_resolve(child, relative_to, version_id)
end unless yaml_tree["items"].nil?
yaml_tree
end
def nav_docs_tree(version)
yml = YAML.load(File.read(Hardwired::Paths.content_path("docs/#{version}/nav.yml")))
nav_resolve(yml, "/docs/#{version}/", version)
end
def default_version
DEFAULT_DOCS_VERSION
end
def active_version
m = /\A\/docs\/(v[0-9\.]+|latest)/.match(request.path)
return m[1] unless m.nil?
default_version
end
def cache_for(time)
response['Cache-Control'] = "public, max-age=#{time.to_i}"
end
def parse_domain(filename)
w = filename
parts = w.split(/\-/)
tags = []
while parts.last =~ /^[a-z]+$/ && parts.last != "v" do
tags << parts.pop
end
w = parts * "-"
vert = parts.last == "v"
parts.pop if vert
ix = parts.pop;
tld = parts.last =~ /^(dk|com|ru|no|nl|org|pl|eu|se|ie|io|hr|it|uk|nz|au)$/ ? parts.pop : ""
tld = "#{parts.pop}.#{tld}" if parts.last =~ /^(ac|co|com)$/;
domain = (parts * "-") + "." + tld
Hardwired::NormalizingRecursiveOpenStruct.new({quote: nil, domain: domain, vertical:vert, tags: tags, index: ix, url: "https://z.zr.io/rw/showcase/#{w}.png"})
end
def generate_products(discount, coupon)
Hash[{
elite_basic: {
yearly: {id: "enterprise-wide-elite-edition-yearly",
price: 7200,
addons: ["basic-email-support", "promotional-imageflow-license"]},
monthly: {id: "enterprise-wide-elite-edition-monthly",
price: 700,
addons: ["basic-email-support", "promotional-imageflow-license"]},
},
elite: {
yearly: {id: "enterprise-wide-elite-edition-yearly",
price: 3600,
addons: ["promotional-imageflow-license"]},
monthly: {id: "enterprise-wide-elite-edition-monthly",
price: 400,
trial: 30,
addons: ["promotional-imageflow-license"]},
},
performance: {
yearly: {id: "enterprise-wide-performance-edition-yearly",
price: 2400,
addons: ["promotional-imageflow-license"]},
monthly: {id: "enterprise-wide-performance-edition-monthly",
price: 260,
trial: 30,
addons: ["promotional-imageflow-license"]},
},
project_performance: {
yearly: {id: "project-performance-yearly",
price: 1800}
},
addon_server_performance: {
monthly: {nodiscount_price: 70},
},
addon_server_elite: {
monthly: {nodiscount_price: 100},
},
addon_247: {
monthly: {nodiscount_price: 599},
},
addon_basic: {
monthly: {price: 300},
},
addon_oem: {
monthly: {price: 400},
},
addon_imageflow: {
monthly: {price: 140},
}
}.map{ |k, d|
# Add price and signup link
d = Hash[d.map{ |k, v|
query = {}
query["subscription[coupon]"] = coupon if coupon
if v[:addons]
v[:addons].each_with_index do |e, ix|
query["addons[id][#{ix}]"] = e
end
end
def get_price(h, discount)
Money.new((h[:nodiscount_price] || (h[:price] - (h[:price] * discount))) * 100, "USD")
end
adjustments = {
price: get_price(v, discount),
first_price: v[:first] ? get_price(v[:first], discount) : nil
}
adjustments[:link] = "https://account.imazen.io/hosted_pages/plans/#{v[:id]}?#{URI.encode_www_form(query)}" if v[:id]
[k, v.merge(adjustments)]
}]
# Calculate yearly discount
d = Hash[d.map{ |k, v|
price = v[:price]
first_price = v[:first_price]
trial_prefix = v[:trial] ? "Free #{v[:trial]} day trial • " : ""
text = ""
price_summary = ""
raise "not implemented" if first_price && k != :yearly
if k == :monthly then
text = v[:trial] ?
"#{trial_prefix} #{price.format}/month" :
"Start now • #{price.format}/month"
price_summary = "#{price.format}/month"
elsif k == :yearly
if first_price then
price_summary = text = "#{trial_prefix} Pay #{first_price.format} 1st year, then #{price.format} yearly"
else
if d[:monthly] && d[:yearly] then
yearly_saves = d[:monthly][:price] * 12 - d[:yearly][:price]
text = "#{trial_prefix} Pay #{d[:yearly][:price].format} yearly • Save #{yearly_saves.format} yearly"
else
text = "#{trial_prefix} Sign up yearly billing"
end
price_summary = "#{(price / 12).format}/month billed yearly"
end
elsif k == :once
text = "#{trial_prefix} #{price.format}"
price_summary = "#{price.format}"
else
raise "not supported"
end
[k, v.merge({
price_summary: price_summary.strip,
button: text.strip
})]
}]
d[:price_summary] = (d[:yearly] || d[:monthly] || d[:once])[:price_summary]
[k, d]
}]
end
def generate_org_sizes
Hash[{
"large" => {
name: "Large Business",
restricted: "with 500 or more employees.",
icon: "icon-globe",
summary: "Large business - more than 500 employees.",
discount: 0
},
"medium" => {
name: "Medium Business",
restricted: "with fewer than 500 employees.",
icon: "icon-building",
summary: "Medium business - fewer than 500 employees.",
discount: 0.5,
coupon: "SMB_ONLY"
},
"small" => {
name: "Small Business",
restricted: "with fewer than 30 employees.",
icon: "icon-group",
summary: "Small business - fewer than 30 employees.",
discount: 0.6,
coupon: "SMALLBIZ_ONLY"
},
"micro" => {
name: "Microenterprise",
restricted: "with fewer than 5 employees and gross revenue below $250,000/quarter USD.",
icon: "icon-user",
summary: "Microenterprise - fewer than 5 employees. Gross revenue below $250,000/quarter USD.",
discount: 0.8,
coupon: "MICRO_ENTERPRISE_ONLY"
},
"nonprofit" => {
name: "Non-profit",
restricted: "with non-profit and tax-exempt status.",
icon: "icon-medkit",
summary: "Charitable organizations - with non-profit and tax-exempt status.",
discount: 0.6,
coupon: "NONPROFIT_ONLY"
}
}.map{|k,v|
adjustments = {tag: k, products: generate_products(v[:discount], v[:coupon])}
#STDERR << adjustments.inspect
[k, adjustments.merge(v)]
}]
end
def org_sizes
@@org_sizes ||= generate_org_sizes
end
end
before do
redirect request.url.sub(/\/nathanaeljones\.com/, '/www.nathanaeljones.com'), 301 if request.host.start_with?("nathanaeljones.com")
#store_version! if request.path.start_with?("/docs/v")
cache_for(dev? ? 30 : 60 * 60 * 24) #1 day
end
get %r{/blog/(\d\d\d\d)} do |year|
request[:year] = year
select_menu = '/blog'
render_file('/blog')
end
get '/blog/tags/:tag' do |tag|
request[:tag] = tag
select_menu = '/blog'
render_file('/blog')
end
get '/pricing/for/:tag' do |tag|
sizes = org_sizes
request[:org] = sizes[tag]
select_menu = '/pricing'
render_file('/pricing/for')
end
get %r{/google([0-9a-z]+).html?} do |code|
"google-site-verification: google#{code}.html" if config.google_verify.include?(code)
end
Hardwired::Index.add_common_file('atom.xml.slim','atom.xml')
add_alias('/rss.xml','atom.xml')
add_alias('articles.xml','atom.xml')
Hardwired::Index.add_common_file('sitemap.xml.slim','sitemap.xml')
#debugger
helpers do
# Add new helpers here.
def latest_release
releases.first
end
def releases
@@releases ||= Hardwired::Index.posts_tagged(:releases)
end
def editions
Hardwired::Index.enum_files { |p| p.flag?(:edition)}.to_a.sort_by { |i| i.meta.sort_field || 0 }
end
def optimize_js(options, &block)
dev? ? block.call : Hardwired::JsOptimize.filter_includes(options,block.call)
end
def store_hyperlinks_in(hash, varname, &block)
require 'nokogiri'
dom = Nokogiri::HTML::fragment(block.call)
list = []
dom.css('a').each do |a|
list << a['href']
end
hash[varname] = list.uniq.reject { |v| v.nil? || v.empty? }
block.call
end
def nullify (&block)
block.call
""
end
end
get '/alljs/jquery.min.map' do
status 404
end
get '/openletter' do
redirect to ('http://www.nathanaeljones.com/openletter')
end
get '/alljs/:scripts' do |scripts|
content_type "text/javascript"
last_modified Time.new(request["m"])
cache_for 60 * 60 * 24 * 30 #1 month
Hardwired::JsOptimize.create_combined_response(Site, scripts, no_minify: dev?)
end
# We want to redirect any /pricing*, /licenses*, /support*., /purchase* urls to https://imazen.io/{path}
get '/pricing*' do
redirect to("https://imazen.io/#{request.path_info}")
end
get '/licenses*' do
redirect to("https://imazen.io/#{request.path_info}")
end
get '/licences*' do
redirect to("https://imazen.io/#{request.path_info}")
end
get '/support*' do
redirect to("https://imazen.io/#{request.path_info}")
end
get '/purchase*' do
redirect to("https://imazen.io/#{request.path_info}")
end
#Fall back to having no version
get '*' do
output = render_file(request.path_info.gsub(/\/docs\/(v[0-9\.]+|latest)\//, ""))
pass if output.nil?
output
end
end
module Hardwired
class Template
def hidden?
flag?('hidden') or draft?
end
def edition
Hardwired::Index["/plugins/editions/#{meta.edition}"]
end
def edition_plugins
@edition_plugins ||= Hardwired::Index.enum_files { |p| p.path.start_with?("/docs/#{DEFAULT_DOCS_VERSION}/plugins") && p.is_page? && p.can_render? && !meta.edition.nil? && !p.meta.edition.nil? && meta.edition.casecmp(p.meta.edition) == 0 && !p.flag?('edition')}.to_a.sort { |x, y| y.meta.sort_field <=> x.meta.sort_field }
end
end
end