Sha256: 9a984790b9ea7241910f074153e04b1c6d9bcd26ac2d5716d645c787fb0ff3b3

Contents?: true

Size: 1.76 KB

Versions: 2

Compression:

Stored size: 1.76 KB

Contents

lib_dir = File.expand_path(File.join(File.dirname(__FILE__),'..','lib'))
unless $LOAD_PATH.include?(lib_dir)
  $LOAD_PATH.unshift(lib_dir)
end

require 'spidr/extensions/uri'

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 = URI.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

2 entries across 2 versions & 1 rubygems

Version Path
spidr-0.2.1 tasks/course.rb
spidr-0.2.0 tasks/course.rb