#!/usr/local/bin/ruby -w

DIRNAME = File.dirname( File.expand_path $0 )
STYLES = {}
STYLES["class"] = 
	File.exists?("class.xsl")?
		File.join(".","class.xsl"):
		File.join(DIRNAME,"class.xsl")
STYLES["module"] = 
	File.exists?("module.xsl")?
		File.join(".","module.xsl"):
		File.join(DIRNAME,"module.xsl")
STYLES["style"] =
	File.exists?("style.css")?"style.css":File.join(DIRNAME,"style.css")

def translate src, dest
	pattern = /<!DOCTYPE\s+(\w+)/
	type = File.new(src).grep(pattern)[0].scan(pattern)[0][0]
	# THIS IS THE LINE YOU WANT TO EDIT
	# if you want to change the XSLT processor to something other than xalan
	`xalan -IN #{src} -OUT #{dest} -XSL file:#{STYLES[type]}`
end

def dodir path
	file_list = Dir.entries( path ).delete_if {|item| item !~ /\.xml$/}
	subdir_list = Dir.entries( path ).delete_if {|item| 
		item =~ /^\.+$/ or !File.directory? item
	}
	for file in file_list
		puts file
		prefix, suffix = file.split "."
		src = File.join(path, "#{prefix}.xml")
		dest = File.join(path, "#{prefix}.html")
		translate src, dest
	end
	`cp #{STYLES["style"]} #{path}`

	for subdir in subdir_list
		dodir File.join path,subdir
	end
end

dodir "api"
