#!/usr/bin/env luajit local lcmark = require("lcmark") local createHTML = function(cm, file, header, menu) -- cm = string.gsub(cm, '._ ', '.  ') if nil ~= file then print('About to parse file ' .. file) end local result = '' local body, metadata, err = lcmark.convert(cm, "html", {smart = true, yaml_metadata = true, columns = 0}) if nil == body then print('oops! ' .. err) else local bod, err = lcmark.compile_template(body) if nil == bod then print('oops! ' .. err) else local templateFile = metadata.template metadata['_'] = ' ' if nil == templateFile then templateFile = 'default' end templateFile = templateFile .. '.template' if nil == menu then templateFile = nil else metadata.menu = menu metadata.header = header end metadata.body = lcmark.apply_template(bod, metadata) local tm = '' if nil ~= templateFile then local h = io.open(templateFile, 'r') if nil ~= h then tm = tm .. h:read('*a') h:close() else print('oops! No such file ' .. templateFile) end local template, err = lcmark.compile_template(tm) if nil == template then print('oops! ' .. err) else result = lcmark.apply_template(template, metadata) end else result = body end end end if ('' ~= result) and (nil ~= file) then local base = string.gsub(file, '%.md$', '') print('Creating file ' .. base .. '.HTML') local a, e = io.open(base .. '.HTML', 'w') if nil == a then print('Could not open ' .. base .. '.HTML - ' .. e) else a:write(result) a:close() end end return result end local cm, filename, header, menu = '', '', '', '' local site = {} for l in io.popen('find . -name "*.md" -type f -printf "%P\n"'):lines() do local s = {} local f = '' local c = 1 for p in string.gmatch(l, '(%w+)/') do table.insert(s, p) c = c + #p + 1 end local full = string.sub(l, c, -1) local base = string.gsub(full, '%.md$', '') table.insert(s, base) if 'menu' == s[#s] then local h = io.open(l, 'r') if nil ~= h then print('Found ' .. l) menu = createHTML(h:read('*a')) h:close() menu = string.gsub(menu, 'ul>', 'menu>') end else if 1 ~= #s then site[l] = s else site[l] = base end end end -- TODO - Only do these first things if menu.md doesn't exist, the second for headers.md --if '' == menu then for k, v in pairs(site) do if 'string' == type(v) then local m = '[' .. v .. '](' .. v .. '.HTML)\n' menu = menu .. string.gsub(createHTML(m), 'ul>', 'menu>') else local path = '' for i, w in ipairs(v) do path = path .. w .. '/' end header = header .. '' .. v[1] .. '   ' end end --end if 0 == #arg then for l in io.popen('find . -name "*.md" -type f -printf "%P\n"'):lines() do cm = '' local h = io.open(l, 'r') if nil ~= h then createHTML(h:read('*a'), l, header, menu) h:close() else print('oops! No such file ' .. l) end end else for i,a in ipairs(arg) do if filename == '' then filename = a end local h = io.open(a, 'r') if nil ~= h then cm = cm .. h:read('*a') h:close() else print('oops! No such file ' .. a) end end if filename == '' then filename = 'test.md' end createHTML(cm, filename, header, menu) end