#!/usr/bin/env ruby
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'compass'))

puts "%html"
puts "  %head"
puts "    %link{:rel=>'stylesheet',:type=>'text/css',:media=>'screen,print',:href=>'autodoc.css'}"
puts "  %body"

Compass::Frameworks::ALL.sort_by { |i| i.name }.each do |framework|
  mixins = []
  Dir["#{framework.stylesheets_directory}/**/*.sass"].each do |fn|
    File.open(fn) do |f|
      modname = fn.sub(%r{^.*?/stylesheets/},'').sub('/_','/')
      description = []
      f.each_line do |line|
        case line
        when %r{//(.*)}
          description << $1
        when %r{^=(.*)}
          mixins << ["+#{$1}", modname, description]
        when %r{^!(.*)}
          mixins << [line, modname, description]
          description = []
        when %r{^\S|^\s\s}
          description = []
        end
      end
    end
  end

  puts "    %h1 #{framework.name}"
  puts "    %table"
  puts "      %thead"
  puts "        %tr"
  puts "          %th Mixin"
  puts "          %th Module"
  puts "          %th Description"
  puts "      %tbody"
  mixins.sort.each do |use, src, desc|
    puts "        %tr"
    puts "          %td.mixin #{use}"
    puts "          %td.module #{src}"
    puts "          %td.desc #{desc.join("&#x000a;")}"
  end
end
