#!/usr/bin/env luajit local lcmark = require("lcmark") local globalData = {version = '-0.1', header = '', footer = '', menu = '', ['_'] = ' ', ['dlr'] = '$'} local site = {} local createHTML = function(cm, file) -- cm = string.gsub(cm, '. ', '.  ') if (nil ~= file) and ('' ~= file) then io.write('Parsing ' .. 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 if nil == file then templateFile = nil else if nil == templateFile then templateFile = 'default' end templateFile = templateFile .. '.template' for k, v in pairs(globalData) do if nill == metadata[k] then metadata[k] = v else print('metadata already has ' .. k) end end end if nil ~= file then local depth = 0 local base = '' for p in string.gmatch(file, '(%w+)/') do depth = depth + 1 base = p end local path = string.sub(file, 1, #base) metadata.header = '' metadata.menu = '' local trl = {} metadata.trail = '' local dir = site[''] local dr, pdr, tr = '', '', '' while nil ~= dir.subs do local old = dir for m, x in pairs(dir.subs) do if x == string.sub(file, 1, #x) then pdr = pdr .. '/' .. m tr = tr .. '../' dr = m dir = site[dr] table.insert(trl, '">' .. m .. ' 👣') break end end if old == dir then break end end table.remove(trl) for m, x in ipairs(trl) do tr = string.sub(tr, 4) trl[m] = ' ' .. x.m .. '

\n' end end if nil ~= dir.subs then local l = {} for m, x in pairs(dir.subs) do table.insert(l, {m = m; x = x}) end table.sort(l, function(a, b) return (a.m <= b.m) end) for m, x in pairs(l) do metadata.header = metadata.header .. '' .. x.m .. '   ' end end 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(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 else print('') end return result end local directory = arg[1] if nil == directory then directory = '.' end local all = {} if '.' ~= directory then for l in io.popen('find . -name "*.md" -type f,l -printf "%P\n"'):lines() do all[l] = l end end for l in io.popen('find ' .. directory .. ' -name "*.md" -type f,l -printf "%P\n"'):lines() do if nil == all[l] then all[l] = l end end for i, l in pairs(all) do local dir = '' local files, subs = {}, {} local c, parent = 1, '' for p in string.gmatch(l, '(%w+)/') do if '' == dir then dir = p if nil ~= site[dir] then subs = site[dir].subs files = site[dir].files end end local path = string.sub(l, 1, -1 - #(string.gsub(l, '.*/', ''))) if nil ~= site[parent] then if nil == site[parent].subs then site[parent].subs = {} end site[parent].subs[p] = path elseif 1 == c then subs[p] = path else if ('' ~= parent) and (dir == parent) then subs[p] = path end site[parent] = {subs = {[p] = path}} end c = c + #p + 1 parent = p end if (1 == c) and (nil ~= site[dir]) then subs = site[dir].subs files = site[dir].files end local base = string.gsub(string.sub(l, c, -1), '%.md$', '') if nil ~= site[parent] then site[parent].files[base] = string.sub(l, 1, -4) elseif 1 == c then files[base] = string.sub(l, 1, -4) else if ('' ~= parent) and (dir == parent) then files[base] = string.sub(l, 1, -4) end site[parent] = {files = {[base] = string.sub(l, 1, -4)}} end -- FIXME - still some minor bug somewhere, this fixes that, but causes other problems. Meh, I can live with excess empty subs tables. -- if (nil ~= subs) and (0 == #subs) then subs = nil end site[dir] = {files = files, subs = subs} end for k, v in pairs(site) do if nil ~= v.files then for m, x in pairs(v.files) do local file = x .. '.md' local h = io.open(file, 'r') if nil ~= h then createHTML(h:read('*a'), file) h:close() else print('oops! No such file ' .. file) end end end end