Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

189 lines (152 sloc) 4.777 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"
require "curb"
require "json"
class Nanoc3::Filter
class CodeBlocks < Nanoc3::Filter
LANGUAGES = { "ruby" => "ruby", "sql" => "sql", "javascript" => "javascript",
"css" => "css", "plain" => "plain", "erb" => "ruby; html-script: true",
"markup" => "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=["']([^"']*)["'])?(?: caption=["']([^"']*)["'])?>|\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], match[5]
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}>}
@output << %{<div class="example">\n}
@output << %{<div class="filename">#{filename}</div>\n} if filename
@output << %{<div class="caption">#{caption}</div>\n} if caption
@output << %{<pre class="brush: #{replace}; gutter: false; toolbar: false"><code>} <<
CGI.escapeHTML(match.pre_match) << %{</code></pre>}
@output << %{</div>\n}
end
def flush
@output << @pending
@pending = ""
end
end
end
Nanoc3::Filter.register 'CodeBlocks', :code_blocks
preprocess do
@chapterOrder = [
"getting-started",
"javascript-101",
"jquery-basics",
"using-jquery-core",
"events",
"effects",
"ajax",
"plugins",
"performance",
"code-organization",
"custom-events",
"how-to"
]
@chapters = {}
@github_users = {
"jquery" => nil
}
@items.each do |item|
item[:chapter] = item[:filename].split('/')[1]
item[:chapter_title] = item[:chapter].gsub(/-/, " ").upcase
if item[:github]
@github_users[ item[:github] ] = nil
else
item[:github] = "jquery"
end
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 = []
@chapterOrder.each do |folder|
myitems = @groupedItems[ folder ]
@chapters [ folder] = {}
@chapters[ folder ][ :items ] = @groupedItems[folder].sort_by {|i| i[:section] || 0 }
@orderedItems = @orderedItems + @chapters[ folder ][ :items ]
@chapters[ folder ][ :title ] = folder.gsub(/-/, " ").upcase
@chapters[ folder ][ :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
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 :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
layout 'default'
end
end
route '*' do
#p [item.identifier, item[:extension], item.binary?]
if item.binary? || item[:extension] == 'css' || item[:extension] == 'js'
# /foo/ -> /foo.ext
#p item.identifier.chop + '.' + item[:extension]
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.