Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

190 lines (156 sloc) 5.145 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
class Nanoc3::Item
def to_json(*a)
{
'title' => self[:title] || self.identifier,
'chapter' => self[:chapter],
'isCategory' => self[:filename].index('dex.md') != nil
}.to_json(*a)
end
end
Nanoc3::Filter.register 'CodeBlocks', :code_blocks
preprocess do
@order = YAML.load(File.read("order.yml"))
@chapter_articles = {}
@chapter_order = @order.map{ |chapter|
@chapter_articles[ chapter.keys.first ] = chapter[ chapter.keys.first ]
chapter.keys.first
}
@chapters = {}
@github_users = {
"jquery" => nil
}
File.open("order.json", "w") {|f| f.write @order.to_json }
@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
@grouped_items = @items.group_by {|item| item[:chapter]}
@ordered_items = []
@chapter_order.each do |folder|
@chapters [ folder] = {}
@chapters[ folder ][ :items ] = @grouped_items[folder].sort_by {|i|
p @chapter_articles[ i[ :chapter ] ].index( p i.identifier.split('/')[2] ) || 0
@chapter_articles[ i[ :chapter ] ].index( p i.identifier.split('/')[2] ) || 0
}
@ordered_items = @ordered_items + @chapters[ folder ][ :items ]
@chapters[ folder ][ :title ] = folder.gsub(/-/, " ").upcase
@chapters[ folder ][ :folder ] = folder
end
@items.each do |item|
i = item[:ordinal_index] = @ordered_items.index(item)
if i
item[:next_item] = @ordered_items[ i+1 ]
item[:previous_item] = @ordered_items[ 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 'wp_dex'
else
layout 'wp_stub'
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.