forked from web-development/web-development-textbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRules
416 lines (344 loc) · 10.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
#!/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 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 = "<a name='slide-1'></a>"
i = 2
until @string.empty?
match = scan_until /^(<h[123].*?>|<hr>+|^(<p>)*§(<.p>)*)$/m
break if !match
@output << match.pre_match
@output << "\n<a class='slide_break' name='slide-#{i}' href='slide.html#slide-#{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 = "<div class='slide'>"
@pending = ""
until @string.empty?
match = scan_until /^(<h[123].*?>|<hr>+|^(<p>)*§(<.p>)*)$/m
break if !match
@output << match.pre_match
@output << "</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 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=#{URI.escape(pattern).gsub(/\+/, '%2B')}&listname=#{URI.escape name}&#{URI.escape 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
preprocess do
@chapterOrder = [
"das-web-und-html",
"css",
"css-layout",
"urls",
"formulare",
"javascript-dom",
"jquery",
"---",
"kommandozeile",
"git",
"http",
"php-vorbereitung",
"wordpress",
"php",
"php-mysql",
"session",
"php-mysql-2",
"---",
"javascript",
"applied-javascript",
"security",
"xml",
"json",
"php-mysql-3",
"qualitaet",
"---",
"apache",
]
@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 ]
item[:previous_item] = @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\/$/
layout 'dex'
else
filter :slide_breaks
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] == '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 filters[item[:extension].to_sym] || item[:extension].to_sym
filter :slides
if item[:homepage]
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\/$/
nil
else
# /foo/ -> /foo/index.html
item.identifier + 'slide.html'
end
end
layout '*', :erb