Add embedded template generator (need to convert to normal tool)

master
teknomunk 10 months ago
parent d94711a326
commit 41f6f54239

@ -0,0 +1,115 @@
class Templator
def initialize( filename )
output_filename = filename.gsub(/\.template/,".inc")
if File.exist?(output_filename) && File.mtime(filename) < File.mtime(output_filename)
return
end
puts "[EMBED] #{filename} -> #{output_filename}"
@filename = filename
@i = File.open(filename,"r")
@o = File.open( output_filename, "w" )
@count = 0
@arg_list = []
self.process
end
def output( text )
@o.write(text)
@count += text.size
if @count > 65
@o.write "\"\n\t\t\""
@count = 0
end
end
def process()
@o.puts "#ifdef HEADERS"
@o.puts "\t#include \"#{ @filename.gsub(/\.template$/,'.h') }\""
@o.puts "#else"
@o.write "\tfprintf( f, \n\t\t\""
while !@i.eof()
c = @i.read(1)
if c == "%"
c = @i.read(1)
if c == "%"
output "%%"
elsif c == "("
# Logic
@o.write "\"\n"
@arg_list.each {|item|
@o.write "\t\t, #{item.strip}\n"
}
@arg_list = []
@o.write "\t);\n"
levels = 1
while !@i.eof() && levels > 0
c = @i.read(1)
if c == ')'
levels -= 1
elsif c == '('
levels += 1
end
if levels > 0
@o.write c
end
end
#if( (c=@i.read(1)) != "\n" )
# puts "Expecting endline after %(), got #{c}"
# exit
#end
@o.write "\n"
@o.write "\tfprintf( f, \""
else
# Variable
output("%")
output(c)
while !@i.eof() && (c=@i.read(1)) != "{"
output(c)
end
argument = ""
while !@i.eof() && (c=@i.read(1)) != "}"
argument += c
end
@arg_list.push(argument)
end
elsif c == "\n"
output( "\\n" )
elsif c == "\""
output( "\\\"" )
elsif c == "\t"
output( "\\t" )
elsif c == "\\"
output( "\\\\" )
else
output(c)
end
end
# Close out printf
@o.write "\"\n"
@arg_list.each {|item|
@o.write "\t\t, #{item.strip}\n"
}
arg_list = []
@o.write "\t);\n"
@o.puts "#endif"
@o.close
end
end
#Templator.new("src/view/series.html.template").process
Templator.new(ARGV[0])
Loading…
Cancel
Save