Sha256: 49f7562c5bf19d9b0065871844c2fe58e98a6ce722fae849bab93919952a247a

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 KB

Contents

require 'nokogiri'
require 'json'

namespace :course do
  COURSE_URL = URI('http://spidr.rubyforge.org/course/')

  STATIC_DIR = File.expand_path(File.join(File.dirname(__FILE__),'..','static'))

  COURSE_DIR = File.join(STATIC_DIR,'course')

  desc "Build the JSON specs file for the course"
  task :specs do
    File.open(File.join(COURSE_DIR,'specs.json'),'w') do |file|
      specs = []

      Dir[File.join(COURSE_DIR,'**','*.html')].each do |page|
        doc = Nokogiri::HTML(open(page))
        page_url = COURSE_URL.merge(page.sub(STATIC_DIR,''))

        link_to_spec = lambda { |link,spec_data|
          relative_url = (link.get_attribute('href') || '')
          absolute_url = page_url.merge(URI.encode(relative_url))

          if absolute_url.path
            absolute_url.path = File.expand_path(absolute_url.path)
          end

          spec_data.merge(
            :message => link.inner_text,
            :link => relative_url,
            :url => absolute_url,
            :example => link.to_html
          )
        }

        doc.search('.follow//a').each do |follow|
          specs << link_to_spec.call(follow, :behavior => :follow)
        end

        doc.search('.nofollow//a').each do |nofollow|
          specs << link_to_spec.call(nofollow, :behavior => :nofollow)
        end

        doc.search('.ignore//a').each do |ignore|
          specs << link_to_spec.call(ignore, :behavior => :ignore)
        end

        doc.search('.fail//a').each do |ignore|
          specs << link_to_spec.call(ignore, :behavior => :fail)
        end
      end

      file.write(specs.to_json)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spidr-0.1.9 tasks/course.rb
spidr-0.1.7 tasks/course.rb
spidr-0.1.8 tasks/course.rb