# Parse command line arguments
if(ARGV.size != 1)
	print "Converts a DragonSpeak Text file to colourized HTML.\n"
	print "Revision 1, 2004-10-08, by Tafyrn\n\n"
	print "Usage: ruby dstoHTML myDragonSpeak.ds > myDragonSpeak.html.ds\n"
	exit
else
	dsFileName = ARGV[0]
end

# Open the DS File
dsFile = File.open(dsFileName, "rb")

# Construct HTML Header
print "<HTML>\n<HEAD>\n\t<TITLE>#{dsFileName[0..dsFileName.length - 4]}</TITLE>\n</HEAD>\n<BODY BGCOLOR=\"\#ffffff\">\n<PRE>\n"

while(dsFile.eof? == false)
	# Parse lines
	dsFile.each do |line|
		if(0 == line.index("DSPK"))
			# Handle DSPK Line
			line = "<FONT COLOR=\"#308160\">" + line[0..line.length - 3] + "</FONT>\n"
		elsif(0 == line.index("*Endtriggers*"))
			# Handle Endtriggers Line
			line = "<FONT COLOR=\"#308160\">" + line[0..line.length - 3] + "</FONT>\n"
		else
			if(line[/\([0-9]+:[0-9]+\)/])
				# Handle DS (nn:mm) at beginning of line
				line.gsub!(/\([0-9]+:/) { |substr| "(<FONT COLOR=\"#308160\">" + "#{substr[1..substr.length]}" }
				line.gsub!(/:([0-9]+)\)/) { |substr| "#{substr[0..substr.length - 2]}" + "</FONT>)"}

				# Handle DS (nn,mm) coordinates
				line.gsub!(/\([0-9]+\,/) { |substr| "(<FONT COLOR=\"#835E30\">" + "#{substr[1..substr.length - 2]}" + "</FONT>," }
				line.gsub!(/\,([0-9]+)\)/) { |substr| ",<FONT COLOR=\"#835E30\">" + "#{substr[1..substr.length - 2]}" + "</FONT>)" }

				# Handle DS " nn " numeric values
				line.gsub!(/ [0-9]+ /) { |substr| " <FONT COLOR=\"#835E30\">" + "#{substr[1..substr.length - 2]}" + "</FONT> " }
				line.gsub!(/ [0-9]+./) { |substr| " <FONT COLOR=\"#835E30\">" + "#{substr[1..substr.length - 2]}" + "</FONT>." }
				line.gsub!(/ [0-9]+$/) { |substr| " <FONT COLOR=\"#835E30\">" + "#{substr[1..substr.length - 2]}" + "</FONT>" }

				# Handle DS Strings
				line.gsub!(/\{[^\}]*\}/) { |substr| "{<FONT COLOR=\"#835E30\">" + "#{substr[1..substr.length - 2]}" + "</FONT>}" }

				# Handle DS Variables
				line.gsub!(/\%[^. \[]*[. \[]/) { |substr|
					if(substr[substr.length - 1] == 32)
						"%<FONT COLOR=\"#832F83\">" + "#{substr[1..substr.length - 2]}" + "</FONT> "
					elsif(substr[substr.length - 1] == ?.)
						"%<FONT COLOR=\"#832F83\">" + "#{substr[1..substr.length - 2]}" + "</FONT>."
					elsif(substr[substr.length - 1] == ?[)
						"%<FONT COLOR=\"#832F83\">" + "#{substr[1..substr.length - 2]}" + "</FONT>["
					end
				}
				
				# Handle DS Variables at end of the line
				line.gsub!(/\%[^ ]*$/) { |substr| "%<FONT COLOR=\"#832F83\">" + "#{substr[1..substr.length - 2]}" + "</FONT>" }

				# Handle DS Variable Array Indexes
				line.gsub!(/\[[0-9]+\]/) { |substr| "[<FONT COLOR=\"#835E30\">" + "#{substr[1..substr.length - 2]}" + "</FONT>]" }
			else
				if(line[0] == ?*)
					# Handle comment lines
					line = "<i><FONT COLOR=\"#305E96\">" + line[0..line.length - 3] + "</FONT></i>\n"
				else
					# Handle old style DS Markup
					if(line[/[0-9]/])
						line.gsub!(/ [0-9]+ /) { |substr| " <FONT COLOR=\"#831414\">" + "#{substr[1..substr.length - 2]}" + "</FONT> " }
						line.gsub!(/^[0-9]+ /) { |substr| "<FONT COLOR=\"#831414\">" + "#{substr[1..substr.length - 2]}" + "</FONT> " }
						line.gsub!(/ [0-9]+$/) { |substr| " <FONT COLOR=\"#831414\">" + "#{substr[1..substr.length - 2]}" + "</FONT>" }
					else
						line = "<i><FONT COLOR=\"#305E96\">" + line[0..line.length - 3] + "</FONT></i>\n"
					end
				end
			end
		end
		
		print line
	end
end

# Construct HTML Footer
print "</PRE>\n</BODY>\n</HTML>\n"

dsFile.close