#!/usr/pkg/bin/ruby193
begin
   require 'plist'
rescue LoadError
   require 'rubygems'
   require 'plist'
end
require 'uv/utility'

base_dir = File.join( File.dirname(__FILE__), '..', 'render' )


def settings
   unless @settings
      @settings = @theme["settings"].find { |s| ! s["name"] }["settings"]
   end
   @settings
end

puts "Processing #{ARGV[0]}"

@theme = Plist::parse_xml( ARGV[0] )
render = {"name" => @theme["name"]}
css = {}

standard_name = File.basename( ARGV[0] ).downcase.gsub(/\s+/, '_').gsub('.tmtheme', '').gsub(/\W/, '').gsub(/_+/, '_')
code_name = "pre.#{standard_name}"

render["tags"] = []
count_names = {}
@theme["settings"].each do |t|
   if t["scope"]
      class_name = t["name"].downcase.gsub(/\W/, ' ').gsub('.tmtheme', '').split(' ').collect{|s| s.capitalize}.join
      if class_name == ""
         class_name = "x" * t["name"].size
      end
      
      if count_names[class_name]
         tname = class_name
         class_name = "#{class_name}#{count_names[class_name]}"
         count_names[tname] += count_names[tname] + 1
      else
         count_names[class_name] = 1
      end
      
      tag = {}
      tag["selector"] = t["scope"]
      tag["begin"] = "<span class=\"#{class_name}\">"
      tag["end"] = "</span>"
      render["tags"] << tag
      
      if s = t["settings"]
         style = {}
         style["color"] = Uv.normalize_color(settings, s["foreground"], true)
         style["background-color"] = Uv.normalize_color(settings, s["background"])
         case s["fontStyle"]
            when /bold/ then style["font-weight"] = "bold"
            when /italic/ then style["font-style"] = "italic"
            when /underline/ then style["text-decoration"] = "underline"
         end
         css[".#{class_name}"] = style
      end
   elsif ! t["name"]
      if s = t["settings"]
         style = {}
         style["color"] = Uv.normalize_color(settings, s["foreground"], true)
         style["background-color"] = Uv.alpha_blend(s["background"], s["background"])
         css[code_name] = style
         @style = style
         style = {}
         style["background-color"] = Uv.alpha_blend(s["selection"], s["selection"])
         style["color"] = Uv.foreground( style["background-color"] )
         css[".line-numbers"] = style
         
         tag = {}
         tag["begin"] = "<span class=\"line-numbers\">"
         tag["end"] = "</span>"
         render["line-numbers"] = tag
      end
   end
end

render["filter"] = "CGI.escapeHTML( @escaped )"

tag = {}
tag["begin"] = ""
tag["end"]   = ""
render["line"] = tag


tag = {}
tag["begin"] = "<pre class=\"#{standard_name}\">"
tag["end"]   = "</pre>"
render["listing"] = tag

tag = {}
tag["begin"] = <<END
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">

<head>
  <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="cache-control" content="no-cache" />
  <meta http-equiv="expires" content="3600" />
  <meta name="revisit-after" content="2 days" />
  <meta name="robots" content="index,follow" />
  <meta name="publisher" content="Dichodaemon" />
  <meta name="copyright" content="Dichodaemon" />

  <meta name="author" content="Dichodaemon" />
  <meta name="distribution" content="global" />
  <meta name="description" content="Ocatarinetabellachithchix" />
  <meta name="keywords" content="arzaversperia flexilimosos toves" />
  <link rel="stylesheet" type="text/css" media="screen,projection,print" href="css/#{standard_name}.css" />
  <title>#{standard_name}</title>

</head>

<body>
END

tag["end"] = <<END
  <p>
    <a href="http://validator.w3.org/check?uri=referer">
      <img style="border:0"
           src="http://www.w3.org/Icons/valid-xhtml10"
           alt="Valid XHTML 1.0 Strict" height="31" width="88" />
    </a>
    <a href="http://jigsaw.w3.org/css-validator/check?uri=referer">
      <img style="border:0;width:88px;height:31px"
           src="http://jigsaw.w3.org/css-validator/images/vcss" 
           alt="Valid CSS!" />
    </a>
  </p>
</body>
</html>
END

render["document"] = tag

File.open( File.join( base_dir, "xhtml", "#{standard_name}.render" ), "w" ) {|f| YAML.dump( render, f ) }

File.open( File.join( base_dir, "xhtml", "files", "css", "#{standard_name}.css" ), "w" ) do |f|
   css.each do |key, values|
      if key == code_name
         f.puts "#{code_name} {"
         #puts @style
      else
         f.puts "#{code_name} #{key} {"
      end
      values.each do |style, value|
         f.puts "   #{style}: #{value};" if value
      end
      f.puts "}"
   end
end
