#!/usr/local/bin/lua ---------------------------------------------------------------- -- -- htmpp.lua -- -- Copyright (C) 2003 Ian King -- All Rights Reserved. -- -- Redistribution and use in source and binary forms, with or without -- modification, are permitted provided that this entire copyright notice -- is duplicated in all such copies. -- -- This software is provided "as is" and without any expressed or implied -- warranties, including, without limitation, the implied warranties of -- merchantibility and fitness for any particular purpose. -- ---------------------------------------------------------------- -- uses it's own tag syntax -- <$insert "filename.html"> inserts filename.html into this file -- <$set varname sometext> sets a variable -- <$varname> inserts value of variable -- <$datetime "format"> inserts a date/time string. format same as os.date("format") i.e. same as strftime() ------------------------------------------------ --debug_htmpp = 1 -- comment out for 'release' version Vars = {} Vars.Store = {} Vars.Store.htmpp_name = "htmpp" Vars.Store.htmpp_version = "0.1" Vars.Store.htmpp_author = "Ian King" function Vars:AddVar(name, value) self.Store[name] = value end function Vars:GetValue(name) local v = self.Store[name] assert(v, "Variable '"..name.."' not found") return v end -- debug_htmpp function function Vars:Dump() table.foreach(self.Store, print) end ------------------------------------------------ function EvaluateTag(str, outfile) if str then local f, l, s1, s2 if debug_htmpp then print("EvaluateTag '"..str.."'") end f, l, s1, s2 = string.find(str, "^set%s+([_%w]+)%s*([^\>]*)") if f then Vars:AddVar(s1, s2) return end f, l, s1 = string.find(str, "^insert%s+\"([^\"]*)\"%s*") if f then ProcessFile(s1, outfile) return end f, l, s1 = string.find(str, "^datetime%s+\"([^\"]*)\"%s*") if f then assert(outfile:write(os.date(s1))) return end f, l, s1 = string.find(str, "^([_%w]+)%s*") if f then assert(outfile:write(Vars:GetValue(s1))) return end assert("Unknown htmpp syntax [["..str.."]]") end end ------------------------------------------------ function ParseLine(str, outfile) local f, l, s1, s2 if str and str ~= "" then if debug_htmpp then print("ParseLine '"..str.."'") end f, l, s1, s2 = string.find(str, "\<\$([^\>]*)\>(.*)") if debug_htmpp then print(f, l, s1, s2) end if f then if f ~= 1 then assert(outfile:write(string.sub(str, 1, f-1))) end EvaluateTag(s1, outfile) ParseLine(s2, outfile) else assert(outfile:write(str)) end end end ------------------------------------------------ function ProcessFile(filename, outfile) -- open file local infile, ferr = io.open(filename, "r") assert(infile, ferr) -- process file local line for line in infile:lines() do ParseLine(line, outfile) assert(outfile:write("\n")) end -- close file infile:close() end ------------------------------------------------ outfile, ferr = io.open(arg[2], "w+") assert(outfile, ferr) ProcessFile(arg[1], outfile) outfile:close() if debug_htmpp then print("--- Final variable values ---") Vars:Dump() end -- The End -- ------------------------------------------------