forked from unpoly/unpoly-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.rb
374 lines (303 loc) · 10.9 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
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
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
$LOAD_PATH.unshift('lib')
$LOAD_PATH.unshift('vendor/unpoly-local/lib')
require 'ext/rack/support_colons_in_path'
require 'unpoly/rails/version'
require 'unpoly/tasks'
require 'unpoly/guide'
require 'unpoly/example'
require 'fileutils'
require 'kramdown'
require 'kramdown-parser-gfm'
##
# Extensions
#
activate :sprockets
# Produce */index.html files
activate :directory_indexes
##
# Build-specific configuration
#
configure :build do
# Minify CSS on build
activate :minify_css
# Minify Javascript on build
activate :minify_javascript, compressor: proc {
require 'uglifier'
Uglifier.new(harmony: true)
}
# Enable cache buster
activate :asset_hash
# after_build do
# puts 'Copying .htaccess file ...'
# from = 'source/.htaccess'
# to = 'build/.htaccess'
# FileUtils.copy(from, to)
# end
end
##
# Layout
#
page '/*.xml', layout: false
page '/*.json', layout: false
page '/*.txt', layout: false
page '/*.html', layout: 'guide'
sprockets.append_path File.expand_path('vendor/asset-libs')
sprockets.append_path File.expand_path('vendor/unpoly-local/lib/assets/javascripts')
sprockets.append_path File.expand_path('vendor/unpoly-local/lib/assets/stylesheets')
Unpoly::Guide.current.reload
##
# Proxy pages (http://middlemanapp.com/basics/dynamic-pages/)
#
Unpoly::Guide.current.interfaces.each do |interface|
path = "#{interface.guide_path}.html" # the .html will be removed by Middleman's pretty directory indexes
puts "Proxy: #{path}"
proxy path, "/api/interface_template.html", locals: { interface_name: interface.name }, ignore: true
end
Unpoly::Guide.current.all_feature_guide_ids.each do |guide_id|
path = "/#{guide_id}.html" # the .html will be removed by Middleman's pretty directory indexes
puts "Proxy: #{path}"
proxy path, "/api/feature_template.html", locals: { guide_id: guide_id }, ignore: true
end
Unpoly::Guide.current.versions.each do |version|
path = "/changes/#{version}.html" # the .html will be removed by Middleman's pretty directory indexes
puts "Proxy: #{path}"
proxy path, "/changes/release_template.html", locals: { version: version }, ignore: true
end
Unpoly::Example.all.each do |example|
proxy example.index_path, "examples/index.html", locals: { example: example }, layout: false, ignore: true, directory_index: false
example.stylesheets.each do |asset|
puts "Example stylesheet: #{asset.path}"
proxy asset.path, "/examples/stylesheet", locals: { asset: asset }, layout: false, ignore: true, directory_index: false
end
example.javascripts.each do |asset|
puts "Example javascripts: #{asset.path}"
proxy asset.path, "/examples/javascript", locals: { asset: asset }, layout: false, ignore: true, directory_index: false
end
example.pages.each do |asset|
puts "Example pages: #{asset.path}"
proxy asset.path, "/examples/page.html", locals: { asset: asset }, layout: false, ignore: true, directory_index: false
end
end
###
# Helpers
#
helpers do
def guide
@guide ||= Unpoly::Guide.current
end
def gem_version
Unpoly::Tasks.gem_version
end
def pre_release?
Unpoly::Tasks.pre_release?
end
def markdown(text, **options)
# text = text.gsub(/<`(.*?)`>/) do |match|
# code = $1
# slug = Unpoly::Guide::Util.slugify(code)
# "[`#{code}`](/#{slug})"
# end
doc = Kramdown::Document.new(text,
input: 'GFM',
remove_span_html_tags: true,
enable_coderay: false,
smart_quotes: ["apos", "apos", "quot", "quot"],
hard_wrap: false
)
# Blindly remove any HTML tag from the document, including "span" elements
# (see option above). This will NOT remove HTML tags from code examples.
doc.to_remove_html_tags
html = doc.to_html
html = postprocess_markdown(html, **options)
html
end
def postprocess_markdown(html, autolink_code: true, strip_links: false, pictures: true)
if autolink_code || strip_links || pictures
nokogiri_doc = Nokogiri::HTML.fragment(html)
end
if strip_links
nokogiri_doc.css('a').each do |link|
link.replace(link.children)
end
elsif autolink_code
autolink_code_in_nokogiri_doc(nokogiri_doc)
end
if pictures
nokogiri_doc.css('img:not([class])').each do |img|
img[:class] = 'picture has_border'
end
end
nokogiri_doc.to_html
end
def autolink_code_in_nokogiri_doc(nokogiri_doc)
codes = nokogiri_doc.css('code')
current_path = current_page.path
current_path = current_path.sub(/\/index\.html$/, '')
current_path = current_path.sub(/\/$/, '')
current_path = current_path.sub(/\.html$/, '')
current_path = "/#{current_path}" unless current_path[0] == '/'
codes.each do |code_element|
text = code_element.text
unless text.include?("\n") || text =~ /^["']/
if code_element.ancestors('a, pre').blank?
guide_id = text
guide_id = guide_id.sub('#', '.prototype.')
hash = nil
if guide_id =~ /^(up\..+?\.config)\.(.+?)$/
guide_id = $1
hash = "config.#{$2}"
end
guide_id = Unpoly::Guide::Util.slugify(guide_id)
if guide.guide_id_exists?(guide_id)
path = "/#{guide_id}"
unless path == current_path
href = [path, hash].compact.join('#')
code_element.wrap("<a href='#{h href}'></a>")
end
end
end
end
end
end
def hyperlink_to_reference(reference)
label = reference.title
if reference.code?
label = content_tag(:code, label)
end
link_to label, reference.guide_path, class: 'hyperlink'
end
def markdown_prose(text, **options)
"<div class='prose'>#{markdown(text, **options)}</div>"
end
def window_title
page_title = @page_title || current_page.data.title
if page_title.present?
"#{page_title} - Unpoly"
else
"Unpoly: Unobtrusive JavaScript framework"
end
end
def unpoly_library_size(files = nil)
files ||= [
'unpoly.min.js',
'unpoly.min.css'
]
files = Array.wrap(files)
paths = files.map { |file| local_library_file_path(file) }
unless paths.all? { |path| File.file?(path) }
return '?? KB'
end
require 'active_support/gzip'
source = ''
paths.each do |path|
File.exists?(path) or raise "Asset not found: #{path}"
source << File.read(path)
end
kbs = (ActiveSupport::Gzip.compress(source).length / 1024.0).round(1)
"#{kbs} KB"
end
def local_library_file_path(file)
"#{Unpoly::Guide.current.path}/dist/#{file}"
end
def hyperlink(label, href, options = {})
options[:class] = "hyperlink #{options[:class]}"
link_to label, href, options
end
def modal_hyperlink(label, href, options = {})
options[:class] = "hyperlink #{options[:class]}"
options['up-layer'] = 'new modal'
link_to label, href, options
end
def node_link(label, href, options = {})
options[:class] = "node__self #{options[:class]}"
# options['up-layer'] = 'page' # don't open drawer links within the drawer (both drawer and page contain .content)
link_to label, href, options
end
def breadcrumb_link(label, href)
link_to label, href, class: 'breadcrumb', 'up-restore-scroll': true
end
def cdn_url(file)
"https://unpkg.com/unpoly@#{guide.version}/#{file}"
end
def cdn_js_include(file)
%Q(<script src="#{cdn_url(file)}"></script>)
end
def cdn_css_include(file)
%Q(<link rel="stylesheet" href="#{cdn_url(file)}">)
end
# def sri_attrs(file)
# %{integrity="#{sri_hash(file)}" crossorigin="anonymous"}
# end
#
# def sri_hash(file)
# path = local_library_file_path(file)
# hash_base64 = `openssl dgst -sha384 -binary #{path} | openssl base64 -A`.presence or raise "Error calling openssl"
# hash_base64 = hash_base64.strip
# "sha384-#{hash_base64}"
# end
BUILTIN_TYPE_URLS = {
# 'string' => 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String',
'undefined' => 'https://developer.mozilla.org/en-US/docs/Glossary/undefined',
# 'Array' => 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array',
'null' => 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null',
# 'number' => 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number',
# 'boolean' => 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean',
'Object' => 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects',
'Promise' => 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises',
'FormData' => 'https://developer.mozilla.org/en-US/docs/Web/API/FormData',
'Event' => 'https://developer.mozilla.org/en-US/docs/Web/API/Event',
'Error' => 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error',
'XMLHttpRequest' => 'https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest',
'NodeList' => 'https://developer.mozilla.org/en-US/docs/Web/API/NodeList',
'Element' => 'https://developer.mozilla.org/de/docs/Web/API/Element',
'jQuery' => 'https://learn.jquery.com/using-jquery-core/jquery-object/',
}
def type(type_or_types)
types = Array.wrap(type_or_types)
parts = types.map { |type|
type = h(type)
type.gsub(/[a-z\.]+/i) { |subtype|
begin
url = guide.interface_for_name!(subtype).guide_path
rescue Unpoly::Guide::UnknownInterface
url = BUILTIN_TYPE_URLS[subtype]
end
if url
"<a href='#{h url}'>#{subtype}</a>"
else
subtype
end
}
}
or_tag = "<span class='type__or'>or</span>"
"<span class='type'>#{parts.join(or_tag)}</span>"
end
def edit_button(documentable)
commit = config[:environment] == 'development' ? guide.git_revision : guide.git_version_tag
url = documentable.text_source.github_url(guide, commit: commit)
link_to '<i class="fa fa-edit"></i> Edit <span class="edit_link__etc">this page</span>', url, target: '_blank', class: 'hyperlink edit_link'
end
def revision_on_github_button(revision)
url = revision.github_browse_url
link_to '<i class="fa fa-code"></i> Revision code', url, target: '_blank', class: 'hyperlink edit_link'
end
def feature_preview(feature)
partial('api/feature_preview', locals: { feature: feature })
end
def url_link(url, options = {})
link_to url, url, options
end
def menu(search: false, &block)
nodes = capture_html(&block)
@menu_html = partial('menu/menu', locals: { nodes: nodes, search: search })
end
def page_title(title)
@page_title = title
return title
end
def slugify(text)
Unpoly::Guide::Util.slugify(text)
end
end