#!/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

@theme = Plist::parse_xml( ARGV[0] )
render = {"name" => @theme["name"]}
codecolumn = ""
numbercolumn = ""
standard_name = File.basename( ARGV[0] ).downcase.gsub(/\s+/, '_').gsub('.tmtheme', '').gsub(/\W/, '').gsub(/_+/, '_')

render["tags"] = []

@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
      
      tag = {}
      tag["selector"] = t["scope"]
      render["tags"] << tag
      
      begin_string = ""
      pcount = 0
      if s = t["settings"]
         if s["background"]
            begin_string << "\\setlength{\\fboxsep}{0ex}\\colorbox[HTML]{#{Uv.normalize_color(settings, s["background"])[1..-1]}}{\\rule[-0.5ex]{0pt}{2.0ex}"
         else
            begin_string << "{"
         end
         pcount += 1
         if s["foreground"]
            begin_string << "\\color[HTML]{#{Uv.normalize_color(settings, s["foreground"], true)[1..-1]}}" 
         end
         case s["fontStyle"]
            when /bold/
               begin_string << "\\textbf{"
               pcount += 1
            when /italic/ 
               begin_string << "\\textit{"
               pcount += 1
            when /underline/ 
               begin_string << "\\underline{"
               pcount += 1
            end
         tag["begin"] = begin_string
         tag["end"] = "}" * pcount
      end
      

   elsif ! t["name"]
      if s = t["settings"]
         codecolumn = "\\newcolumntype{C}{>{"
         codecolumn << "\\color[HTML]{#{Uv.normalize_color(settings, s["foreground"], true)[1..-1]}}" if s["foreground"]
         codecolumn << "\\columncolor[HTML]{#{Uv.alpha_blend(s["background"], s["background"])[1..-1]}}" if s["background"]
         codecolumn << "}l}"
         bg = Uv.alpha_blend(s["selection"], s["selection"]) if s["selection"]
         numbercolumn = "\\newcolumntype{N}{>{"
         numbercolumn << "\\color[HTML]{#{Uv.foreground(bg)[1..-1]}}" if bg
         numbercolumn << "\\columncolor[HTML]{#{bg[1..-1]}}" if s["selection"]
         numbercolumn << "}l}"
         
         tag = {}
         tag["begin"] = "\\texttt{"
         tag["end"] = "}&\\mbox{\\texttt{"
         render["line-numbers"] = tag      end
   end
end

render["filter"] = '@escaped.gsub(/(\$)/, \'\\\\\\\\\1\').gsub(/\\\\(?!\$)/, \'$\\\\\\\\backslash$\').gsub(/(_|\{|\}|&|\#|%)/, \'\\\\\\\\\1\').gsub(/~/, \'\\\\textasciitilde \').gsub(/ /,\'\\\\hspace{1ex}\').gsub(/\t| /,\'\\\\hspace{3ex}\').gsub(/\"/, "\'\'").gsub(/(\^)/,\'\\\\\\\\\1{}\')'

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

tag = {}
tag["begin"] = <<END
#{codecolumn}
#{numbercolumn}
\\begin{longtable}{NC}
END

tag["end"]   = <<END
\\end{longtable}
END
render["listing"] = tag

tag = {}
tag["begin"] = <<END
\\documentclass[a4paper,landscape]{article}
\\usepackage{xcolor}
\\usepackage{colortbl}
\\usepackage{longtable}
\\usepackage[left=2cm,top=1cm,right=3cm,nohead,nofoot]{geometry}
\\usepackage[T1]{fontenc}
\\usepackage[scaled]{beramono}
\\begin{document}
END

tag["end"] = <<END
\\end{document}
END

render["document"] = tag

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