forked from web-development/web-development-textbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRules
486 lines (401 loc) · 12.7 KB
/
Rules
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#!/usr/bin/env ruby
# A few helpful tips about the Rules file:
#
# * The order of rules is important: for each item, only the first matching
# rule is applied.
#
# * Item identifiers start and end with a slash (e.g. “/about/” for the file
# “content/about.html”). To select all children, grandchildren, … of an
# item, use the pattern “/about/*/”; “/about/*” will also select the parent,
# because “*” matches zero or more characters.
require "cgi"
require "uri"
require "curb"
require "json"
class Nanoc3::Filter
class ResponsiveImages < Nanoc3::Filter
identifier :responsive_images
def run(content, params={} )
@string = content.dup
@pending = ""
@output = ""
until @string.empty?
# looking for <img src="/images/balken.png" alt="Bild der Balkengrafik" />
match = scan_until /<img src="([^"]*)"([^>]*)\/>/
break if !match
file, rest = match.captures
#puts "found a proper image #{file}"
#@output << match[0]
@output << match.pre_match
@output << %Q{<img src="#{file}" }
@output << srcset(file)
@output << rest
@output << ">"
end # until @string.empty?
@output << @string
@output << ""
@output
end
def srcset(file)
pattern = "content" + file.gsub('.', '*.')
files = Dir.glob(pattern)
set = files.map do |filename|
if match = filename.match(/@(\d)x/)
"#{filename.gsub('content','')} #{match[1]}x"
else
"#{filename.gsub('content','')} 1x"
end
end
return "" if set.length < 2
# puts " srcset for #{file}: #{set.length} versions"
%Q{srcset="#{set.join ', '}" }
end
def scan_until(regex)
match = @string.match(regex)
return unless match
@string = match.post_match
match
end
end
class Peek < Nanoc3::Filter
identifier :peek
def run(content, params={} )
puts "#{content.length} bytes of content."
content
end
end
class NoSlides < Nanoc3::Filter
identifier :noslides
def run(content, params={})
@output = content.dup.gsub(/^.*§.*$/,'');
@output
end
end
class SlideBreaks < Nanoc3::Filter
identifier :slide_breaks
def run(content, params={})
@string = content.dup
@pending = ""
@output = %Q{<div id="slide-1" title="Folie Nr. 1"></div>}
i = 2
until @string.empty?
match = scan_until /^(<h[123].*?>|<hr>+|^(<p>)*§(<.p>)*)$/m
break if !match
@output << match.pre_match
@output << %Q{\n<a class="slide_break" id="slide-#{i}" href="slide.html#slide-#{i}" title="Wechsle zur Präsentations-Ansicht, Folie Nr. #{i}">▻</a>\n\n}
i += 1
if match[0] =~ /§/ then
# discard match
else
@output << match[0]
end
end # untile @string.empty?
@output << @string
@output << ""
@output
end
def scan_until(regex)
match = @string.match(regex)
return unless match
@string = match.post_match
match
end
end
class Slides < Nanoc3::Filter
identifier :slides
def run(content, params={})
@string = content.dup
@output = %Q{<div class="slide">}
@pending = ""
until @string.empty?
match = scan_until /^(<h[123].*?>|<hr>+|^(<p>)*§(<.p>)*)$/m
break if !match
@output << match.pre_match
@output << %Q{</div>\n<div class="slide">\n\n}
if match[0] =~ /§/ then
#puts "ignore #{match[0]}"
# discard match
else
@output << match[0]
end
end
@output << @string
@output << "</div>"
@output
end
def scan_until(regex)
match = @string.match(regex)
return unless match
@string = match.post_match
match
end
end
class PatternTester < Nanoc3::Filter
identifier :pattern_tester
def run(content, params={})
@string = content.dup
@output = ""
@pending = ""
until @string.empty?
match = scan_until /(<patterntester(?: name="(.*?)")?(?: pattern="(.*?)")?>|\z)/m
@pending << match.pre_match
flush
replace_with_link match[2], match[3]
end
flush
@output
end
def scan_until(regex)
match = @string.match(regex)
return unless match
@string = match.post_match
match
end
def u_esc(s)
URI.escape(s).gsub(/\+/, '%2B').gsub(/\[/, '%5B').gsub(/\]/, '%5D')
end
def replace_with_link(name, pattern)
match = scan_until %r{</patterntester>}
if match and match.pre_match then
data = match.pre_match;
data_params = data.split(/\n/).map{|d| "list[]=#{d}" }.join("&")
out = "<a href='/images/match.html?pattern=#{u_esc(pattern)}&listname=#{u_esc name}&#{u_esc data_params}'>RegEx Tester: /#{CGI.escapeHTML(pattern).gsub(/\|/, "|")}/</a>"
@output << out
end
end
def flush
@output << @pending
@pending = ""
end
end
class CodeBlocks < Nanoc3::Filter
identifier :code_blocks
LANGUAGES = { "ruby" => "ruby", "sql" => "sql", "javascript" => "javascript", "php" => "php",
"css" => "css", "plain" => "plain", "erb" => "ruby; html-script: true", "htmlcode" => "html",
"markup" => "xml", "xml" => "xml", "shell" => "shell", "yaml" => "yaml", "dtd"=> "html", "apache" => "ruby" }
def run(content, params={})
@string = content.dup
@output = ""
@pending = ""
languages = LANGUAGES.keys.join("|")
until @string.empty?
# match = scan_until /(\+(\S.+?\S?)\+|<(#{languages})(?: filename=["']([^"']*)["'])?(?: caption=["']([^"']*)["'])?>|\z)/m
match = scan_until /(<(#{languages})(?: filename=["']([^"']*)["'])?(?: caption=["']([^"']*)["'])?>|\z)/m
@pending << match.pre_match
if match[1] # whole <language> tag
flush
generate_brushes match[2], LANGUAGES[match[2]], match[3], match[4]
else
flush
generate_brushes 'htmlcode', LANGUAGES['htmlcode'], match[3], match[4]
end
end
flush
@output
end
def scan_until(regex)
match = @string.match(regex)
return unless match
@string = match.post_match
match
end
def generate_brushes(tag, replace, filename, caption)
match = scan_until %r{</#{tag}>}
if match and match.pre_match then
code = match.pre_match;
if code.match(/<\/javascript>/) then
puts "this is an ERROR!"
elsif match = code.match(/^__\|__$/) then
code1 = match.pre_match
code2 = match.post_match
@output << %{<div class="example twopane">\n}
@output << %{<div class="filename">#{filename}</div>\n} if filename
@output << %{<h4 class="caption">#{replace.capitalize} Code <small>#{caption}</small></h4>\n} if caption
@output << %{<pre class="lang-#{replace} prettyprint linenums left">}
@output << CGI.escapeHTML(code1) << %{</pre>}
@output << %{<pre class="lang-#{replace} prettyprint linenums right">}
@output << CGI.escapeHTML(code2) << %{</pre>}
@output << %{<div class="clearfix"></div></div>\n}
else
@output << %{<div class="example">\n}
@output << %{<div class="filename">#{filename}</div>\n} if filename
@output << %{<h4 class="caption">#{replace.capitalize} Code <small>#{caption}</small></h4>\n} if caption
@output << %{<pre class="lang-#{replace} prettyprint linenums">}
@output << CGI.escapeHTML(code) << %{</pre>}
@output << %{</div>\n}
end
end
end
def flush
@output << @pending
@pending = ""
end
end
end
Nanoc3::Filter.register '::Nanoc3::Filter::PatternTester', :pattern_tester
Nanoc3::Filter.register '::Nanoc3::Filter::CodeBlocks', :code_blocks
Nanoc3::Filter.register '::Nanoc3::Filter::Slides', :slides
Nanoc3::Filter.register '::Nanoc3::Filter::NoSlides', :noslides
Nanoc3::Filter.register '::Nanoc3::Filter::SlideBreaks', :slide_breaks
Nanoc3::Filter.register '::Nanoc3::Filter::Peek', :peek
Nanoc3::Filter.register '::Nanoc3::Filter::ResponsiveImages', :responsive_images
preprocess do
@chapterOrder = [
"das-web-und-html",
"css",
"css-layout",
"urls",
"formulare",
"javascript-dom",
"jquery",
"---",
"kommandozeile",
"git",
"http",
"php-vorbereitung",
"php",
"php-db-lesen",
"session",
"php-db-schreiben",
"php-db-optimierung",
"---",
"grafik",
"javascript",
"applied-js-and-css",
"json",
"xml",
"---",
"security",
"wordpress",
"apache",
"advanced-javascript",
"mobile",
"qualitaet"
]
@chapters = {}
@github_users = {
"bjelline" => nil
}
@items.each do |item|
item[:chapter] = item[:filename].split('/')[1]
item[:chapter_title] = item[:chapter].gsub(/-/, " ").gsub(/Qualitaet/, 'Qualität') # .upcase
item[:github] = "bjelline"
end
@github_users.each do |username, wat|
#request = Curl::Easy.http_get("https://api.github.com/users/"+username)
#request.perform
@github_users[ username ] = {} #JSON.parse(request.body_str)
end
@groupedItems = @items.group_by {|item| item[:chapter]}
@orderedItems = []
last_folder = 0
@chapterOrder.each do |folder|
if folder == "---" then
@chapters[ last_folder ][ :separator ] = true
else
myitems = @groupedItems[ folder ]
@chapters[ folder ] = {}
if ! @groupedItems[folder].nil? then
@chapters[ folder ][ :items ] = @groupedItems[folder].sort_by {|i| i.attributes[:order] || 0 }
@orderedItems = @orderedItems + @chapters[ folder ][ :items ]
end
@chapters[ folder ][ :title ] = folder.gsub(/-/, " ").gsub(/Qualitaet/,"Qualität").split(' ').map {|w| w.capitalize }.join(' ')
@chapters[ folder ][ :folder ] = folder
if last_folder != 0 and @chapters[ last_folder ] then
@chapters[ last_folder ][ :separator ] = false
end
end
last_folder=folder
end
@items.each do |item|
i = item[:ordinal_index] = @orderedItems.index(item)
if i
item[:next_item] = @orderedItems[ i+1 ].identifier if @orderedItems[ i+1 ]
item[:previous_item] = @orderedItems[ i-1 ].identifier if @orderedItems[ i-1 ]
end
item[:github_user] = @github_users[ item[:github] ]
end
@site.config[:chapters] = @chapters
end
compile '/assets/*' do
# don’t filter or layout
end
compile '/images/*' do
# don’t filter or layout
end
filters = {
:markdown => :kramdown,
:md => :kramdown,
:html => :erb
}
# Index files in each content directory
route "/*/dex" do
item.identifier.sub("dex/", "") + 'index.html'
end
# Just the homepage
route "/dex" do
item.identifier.sub("dex/", "") + 'index.html'
end
compile '*' do
# filter :noslides
filter :pattern_tester
filter :code_blocks
filter filters[item[:extension].to_sym] || item[:extension].to_sym
if item[:homepage]
layout 'home'
elsif item.identifier.match /\/dex\/$/
filter :slide_breaks
layout 'dex'
else
filter :slide_breaks
filter :responsive_images
layout 'default'
end
end
route '*' do
#p [item.identifier, item[:extension], item.binary?]
if item.binary? || item[:extension] == 'css' || item[:extension] == 'js' || item[:extension] == 'png' || item[:extension] == 'html' || item[:extension] == 'xml'
# /foo/ -> /foo.ext
#p item.identifier.chop + '.' + item[:extension]
item.identifier.chop + '.' + item[:extension]
elsif item[:extension] == 'txt'
item.identifier.chop + '.' + item[:extension]
elsif item[:extension] == 'html'
item.identifier.chop + '.' + item[:extension]
else
# /foo/ -> /foo/index.html
item.identifier + 'index.html'
end
end
compile '/assets/*', :rep => :slide do
# don’t filter or layout
end
compile '/images/*', :rep => :slide do
end
compile '*', :rep => :slide do
filter :pattern_tester
filter :code_blocks
filter :kramdown
filter :responsive_images
#filter filters[item[:extension].to_sym] || item[:extension].to_sym
filter :slides
if item[:homepage]
# noop
# elsif item.identifier.match /\/dex\/$/
else
layout 'slide'
end
end
route '*', :rep => :slide do
if item.binary? || item[:extension] == 'css' || item[:extension] == 'js' || item[:extension] == 'png' || item[:extension] == 'xml' || item[:extension] == 'html'
nil
elsif item[:homepage]
nil
elsif item.identifier.match /\/dex\/$/
item.identifier.gsub(/dex\//,'slide.html')
else
# /foo/ -> /foo/index.html
item.identifier + 'slide.html'
end
end
layout '*', :erb