Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 189 lines (152 sloc) 4.777 kb
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
#!/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

Something went wrong with that request. Please try again.