#! lua
(getmetatable "").__call = string.format
local infile = arg[1]
assert (infile:match "/wve$")
local WRITE, walk
do ---------------------------------------[
local istk = { }
local insert, remove in table
local badtype = "bad type %s at %s"
walk = \ (tree, leaf, item)
    local ok, err
    local itype = type (tree)
    if itype == "string" then leaf (tree)
    elseif itype == "table" then
      insert (istk, item)
      for i = 1, #tree do
          ok, err = (walk (tree[i], leaf, tostring (i)))
      end -- for
      remove (istk)
    else
      error (badtype (itype, table.concat (istk, ".")))
    end -- if
    end -- function
local leafaction = \ (f) => \ (s)
    if #s > 0 then f:write (s) end -- if
    end end -- function function
WRITE = \ (tree, filename)
      local errout = "cannot open %s"
      local f = io.open (filename, "w")
      assert (f, errout (filename))
      walk (tree or "", leafaction (f), "1")
      f:close ( )
      end -- function
end ---------------------------------------]
----------------- html stuff ----------------
local nl = "\n"

TAG = setmetatable ( { }, { __index =
\ (_, t) => \ (x) => \ (y) =>
{ "<" , t, " ", x, ">\n", y, "\n</", t, ">" ,} end end end; })
MONOTAG = setmetatable ( { }, { __index =
\ (_, t) => \ (x) =>
{ "<", t, " ", x, ">" } end end; })
LINK = \ (url) => TAG.a { [[href="]], url, [["]]}
       end -- function
REM = \ (s) => { "\n<!-- ", s, " -->\n", }
      end -- function
do ----------------------------------[
local catchall = "(.)"
local entity = {
  ["<"] = "&lt;",
  [">"] = "&gt;",
  ["&&"] = "&amp;",
               }
local entify = \ (c)
               local n, fmts = c:byte ( ),"&#%s;"
               return (n > 127) and fmts (n) or c
               end -- function
local eval = \ (e) => \ (s)
      local x = assert (load ("=> ".. s, "", "bt", e))
      => x ( )
      end end -- function function
local dollar = "%$(%b())"
TEXT = \ (x, e) -- convert to HTML entities
       local tx = type (x)
       if tx == "string" then
           for symb, ent in pairs (entity) do
              x = x:gsub (symb, ent)
           end -- for
           x = x:gsub (catchall, entify)
           => e and x:gsub (dollar, eval (e or _ENV)) or x
        elseif tx == "table" then
           local o = { }
           for i = 1, #x do
                  o[#o+1] = TEXT (x[i])
           end -- for
           => o
        else
          => x
        end -- if
      end -- function
CLASS = setmetatable ({ }, { __index =
\ (_, x) => { [[class="]], x, [["]], } end; })
STYLE = \ (x) => { [[style="]], x, [["]], } end
OBEY$DIR = infile:gsub ("[^%.]+$", "%%s")
local dofile = \ (file)
  local x = assert (loadfile (file))
  => x ( ) end -- function
span = TAG.span
div = TAG.div
pre = TAG.pre
p = TAG.p
end ------------------------------------]

local META in MONOTAG

local mk_css
do -----------------------[
 local cssstyle = [[rel=stylesheet href="%s" type="text/css"]]
 local link in MONOTAG
 local badcss = "bad type %s for CSS"
 mk_css = \ (x)
   local tx = type (x)
   if tx == "string" then
      => link (cssstyle (x))
   elseif tx == "table" then
     local o = { }
     for i = 1,#x do
        o[i] = mk_css (x[i])
     end -- for
     => o
   else
     error (badcss (tx))
   end -- if
 end -- function
end ----------------------]

local HTML = \ (body)
  local DOCTYPE, CSS, TITLE, HEADER, CHARSET, BODYSTYLE, FILE, LANG, QUIET in body
  local doctype = [[<!DOCTYPE html>]]
  
  local charset =  [[http-equiv="Content-Type" content="text/html; charset=%s"]]
  local header = TAG.head "" {
      TAG.title "" (TITLE or "???"), nl,
      META (charset (CHARSET or "utf-8")), nl,
      META [[name="Generator"  content="Weave"]], nl,
      META [[name="MSSmartTagsPreventParsing" content="TRUE"]], nl,
      CSS and mk_css (CSS) or "", nl,
      HEADER or "",
      }
     local bstyle = BODYSTYLE and STYLE (BODYSTYLE) or ""
     local lang = [[lang="%s"]]
    => {
         DOCTYPE or doctype, nl,
         TAG.html (lang (LANG or "en")) { header, TAG.body (bstyle) (body), }
        }, FILE or infile:gsub ("/wve$", "/html"), QUIET
  end -- function
----------------------- main action -------------

local rope = dofile (infile)
local doc, target, q = HTML (rope)
WRITE (doc, target)
local execute in os
local settype = "settype %s FAF"
execute (settype (target))
if not q then
 print ((target:match "[^%.]+$"))
end -- if