#!/usr/bin/lua
-- Copyright 2007-2013 Mitchell mitchell.att.foicica.com. See LICENSE.

local output_dir, template_dir = '.', '.'
local title, navtitle = 'Foicica', 'Navigation'
local pages = {}
for i = 1, #arg do
  local v = arg[i]
  if v == '-d' then
    output_dir = arg[i + 1]
    table.remove(arg, i + 1)
  elseif v == '-t' then
    template_dir = arg[i + 1]
    table.remove(arg, i + 1)
  elseif v == '--title' then
    title = arg[i + 1]
    table.remove(arg, i + 1)
  elseif v == '--navtitle' then
    navtitle = arg[i + 1]
    table.remove(arg, i + 1)
  elseif v == '-h' or v == '--help' then
    --[[
    print("Usage: bombay [options] markdown_files\n\z
           Generates a set of linked web pages from markdown files provided.\n\z
           Uses `discount` (www.pell.portland.or.us/~orc/Code/​discount).\n\z
           Available options are:\n\z
           -d path                output directory path\n\z
           -t path                template directory path\n\z
           -h, --help             print this help and exit\n\z
               --title text       text appended to each page title\n\z
               --navtitle text    navigation menu title text")
    os.exit()
    ]]
  elseif v then
    pages[#pages + 1] = v
  end
end
table.sort(pages, function(a, b)
  return a:match('[^/\\]+$') < b:match('[^/\\]+$')
end)

local HTML = [[
  <!doctype html>
  <html>
    <head>
      <title>%(title)</title>
      <link rel="stylesheet" href="style.css" type="text/css" />
      <link rel="icon" href="icon.png" type="image/png" />
      <meta charset="utf-8" />
    </head>
    <body>
      <div id="content">
        <div id="header">
          %(header)
        </div>
        <div id="nav">
          <h2>%(navtitle)</h2>
          %(nav)
        </div>
        <div id="toc">
          <h2>Contents</h2>
          %(toc)
        </div>
        <div id="main">
          %(main)
        </div>
        <div id="footer">
          %(footer)
        </div>
      </div>
    </body>
  </html>
]]
local template = {}

-- Create the header and footer.
local p = io.popen('markdown "'..template_dir..'/.header.md"')
template.header = p:read('*all')
p:close()
p = io.popen('markdown "'..template_dir..'/.footer.md"')
template.footer = p:read('*all')
p:close()

-- Create the navigation list.
template.navtitle = navtitle
local navfile = output_dir..'/.nav.md'
local f = io.open(navfile, 'wb')
for _, page in ipairs(pages) do
  local page_name = page:match('[^/\\]+$')
  local nice_name = page_name:match('^%A*(.-)%.md$'):gsub('(%l)(%u)', '%1 %2')
  f:write('* [', nice_name, '](', page_name:gsub('%.md$', '.html'), ')\n')
end
f:close()
p = io.popen('markdown "'..navfile..'"')
local nav = p:read('*all')
p:close()
os.remove(navfile)

-- Write HTML.
for _, page in ipairs(pages) do
  local page_name = page:match('[^/\\]+$')
  local nice_name = page_name:match('^%A*(.-)%.md$'):gsub('(%l)(%u)', '%1 %2')
  template.title = nice_name..' - '..title
  template.nav = nav:gsub('<a[^>]+>('..nice_name..')</a>', '%1')
  p = io.popen('markdown -f toc -T "'..page..'"')
  template.toc, template.main = p:read('*all'):match('^(.-\n</ul>\n)(.+)$')
  p:close()
  f = io.open(output_dir..'/'..page_name:gsub('%.md$', '.html'), 'wb')
  local html = HTML:gsub('%%%(([^)]+)%)', template)
  f:write(html)
  f:close()
end
