Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: d76fa4d014
Fetching contributors…

Cannot retrieve contributors at this time

107 lines (81 sloc) 2.685 kb
#!/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
Jump to Line
Something went wrong with that request. Please try again.