#!/usr/bin/env moon
import compile, layout, subnav, autoref from require 'docs.gen.layout'

export OUT, ESCAPE

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!

ESCAPE = (str) ->
  (str\gsub '([*`])', '\\%1')

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