#!/usr/bin/env moon
require 'alv'
import compile, render, layout, autoref, subnav from require 'docs.gen.layout'
import section, h1, h2, p, ul, li, a, code from require 'docs.gen.dom'
import get_module from require 'docs.gen.shim'

export OUT

slurp = (file) ->
  file = io.open file, 'r'
  with file\read '*all'
    file\close!

spit = (file, str) ->
  file = io.open file, 'w'
  file\write str
  file\close!

{ OUT, file } = arg

md = slurp file
for file in *arg[3,]
  if name = file\match '/module/(.*)%.html'
    modname = name\gsub '/', '.'
    mod = get_module "alv-lib.#{modname}"
    summary = if mod.meta then ": #{mod.meta.summary}" else ''
    md ..= "   - [`#{modname}`](module/#{name}.html)#{summary}\n"

content = compile autoref md
sub = subnav [a for a in *arg[3,]]
spit OUT, layout
  body: table.concat { sub, content }, '\n\n'
