-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rules
106 lines (81 loc) · 2.62 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
#!/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"
class Nanoc3::Filter
class CodeBlocks < Nanoc3::Filter
LANGUAGES = { "ruby" => "ruby", "sql" => "sql", "javascript" => "javascript",
"css" => "css", "plain" => "plain", "erb" => "ruby; html-script: true",
"html" => "xml", "xml" => "xml", "shell" => "plain", "yaml" => "yaml" }
def run(content, params={})
@string = content.dup
@output = ""
@pending = ""
languages = LANGUAGES.keys.join("|")
until @string.empty?
match = scan_until /(\+(\S.*?\S?)\+|<(#{languages})(?: filename=["']([^"']*)["'])?>|\z)/m
@pending << match.pre_match
if match[2] # +foo+
@pending << "<notextile><tt>#{CGI.escapeHTML(match[2])}</tt></notextile>" if match[2]
elsif match[3] # <language>
flush
generate_brushes match[3], LANGUAGES[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)
match = scan_until %r{</#{tag}>}
@output << %{<div class="code_container">\n}
@output << %{<div class="filename">#{filename}</div>\n} if filename
@output << %{<pre class="brush: #{replace}; gutter: false; toolbar: false">\n} <<
CGI.escapeHTML(match.pre_match) << %{</pre></div>}
end
def flush
@output << @pending
@pending = ""
end
end
end
Nanoc3::Filter.register 'CodeBlocks', :code_blocks
compile '/stylesheets/*' do
# don’t filter or layout
end
compile '/images/*' do
end
compile '/javascript/*' do
end
compile '/html/*' do
end
filters = {
:markdown => :kramdown
}
compile '*' do
filter :code_blocks
filter filters[item[:extension].to_sym] || item[:extension].to_sym
layout 'default'
end
route '*' do
if item.binary? || item[:extension] == 'html' || item[:extension] == 'js' || item[:extension] == 'css'
# /foo/ -> /foo.ext
item.identifier.chop + '.' + item[:extension]
else
# /foo/ -> /foo/index.html
item.identifier + 'index.html'
end
end
layout '*', :erb