From 2adacbe580ae8ce68d8e48add43a812e6ae8b759 Mon Sep 17 00:00:00 2001 From: dvs1 Date: Sat, 21 Dec 2024 17:56:41 +1000 Subject: Major rewrite, mostly of the directory scanning code. It can now populate the links around the edges, and turn an entire directory structure into a web site. --- notYetAnotherWiki.lua | 170 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 101 insertions(+), 69 deletions(-) (limited to 'notYetAnotherWiki.lua') diff --git a/notYetAnotherWiki.lua b/notYetAnotherWiki.lua index 1746b0a..9d6f3f7 100755 --- a/notYetAnotherWiki.lua +++ b/notYetAnotherWiki.lua @@ -2,13 +2,12 @@ local lcmark = require("lcmark") -local globalData = {header = '', footer = '', menu = '', ['_'] = ' '} +local globalData = {header = '', footer = '', menu = '', ['_'] = ' ', ['dlr'] = '$'} local site = {} - - +local dirs = {} local createHTML = function(cm, file) -- cm = string.gsub(cm, '. ', '.  ') - if nil ~= file then print('About to parse file ' .. file) end + if nil ~= file then print('\nAbout to parse file ' .. file) end local result = '' local body, metadata, err = lcmark.convert(cm, "html", {smart = true, yaml_metadata = true, columns = 0}) @@ -25,11 +24,62 @@ local createHTML = function(cm, file) 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 = dirs[''] + 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 = dirs[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] = ' ' .. m .. '

\n' + end + end + if nil ~= dir.subs then + for m, x in pairs(dir.subs) do + local u = string.sub(x, 1 + #pdr) + metadata.header = metadata.header .. '' .. m .. '   ' + end + end + end + metadata.body = lcmark.apply_template(bod, metadata) local tm = '' if nil ~= templateFile then - local h = io.open(templateFile, 'r') + local h = io.open(templateFile, 'r') if nil ~= h then tm = tm .. h:read('*a') h:close() @@ -60,82 +110,64 @@ local createHTML = function(cm, file) end + for l in io.popen('find . -name "*.md" -type f -printf "%P\n"'):lines() do - local s = {} - local f = '' - local c = 1 + local dir = '' + local files, subs = {}, {} + local c, parent = 1, '' + for p in string.gmatch(l, '(%w+)/') do - table.insert(s, p) + if '' == dir then + dir = p + if nil ~= dirs[dir] then + subs = dirs[dir].subs + files = dirs[dir].files + end + end + + local path = string.sub(l, 1, -1 - #(string.gsub(l, '.*/', ''))) + if nil ~= dirs[parent] then + if nil == dirs[parent].subs then dirs[parent].subs = {} end + dirs[parent].subs[p] = path + elseif 1 == c then subs[p] = path + else + if ('' ~= parent) and (dir == parent) then subs[p] = path end + dirs[parent] = {subs = {[p] = path}} + end c = c + #p + 1 + parent = p + end + + if (1 == c) and (nil ~= dirs[dir]) then + subs = dirs[dir].subs + files = dirs[dir].files end + local base = string.gsub(string.sub(l, c, -1), '%.md$', '') - table.insert(s, base) - --- TODO - should do the same for header.md and footer.md - if 'menu' == base then - local h = io.open(l, 'r') - if nil ~= h then - globalData.menuFound = true - print('Found ' .. l) - globalData.menu = string.gsub(createHTML(h:read('*a')), 'ul>', 'menu>') - h:close() - end + if nil ~= dirs[parent] then dirs[parent].files[base] = string.sub(l, 1, -4) + elseif 1 == c then files[base] = string.sub(l, 1, -4) else - if 1 ~= #s then site[l] = s else site[l] = base end + if ('' ~= parent) and (dir == parent) then files[base] = string.sub(l, 1, -4) end + dirs[parent] = {files = {[base] = string.sub(l, 1, -4)}} end -end -for k, v in pairs(site) do - if 'string' == type(v) then - if not globalData.menuFound then - local m = '[' .. v .. '](' .. v .. '.HTML)\n' - globalData.menu = globalData.menu .. string.gsub(createHTML(m), 'ul>', 'menu>') - end - elseif not globalData.headerFound then - local path = '' - for i, w in ipairs(v) do - path = path .. w .. '/' - end - globalData.header = globalData.header .. '' .. v[1] .. '   ' - 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 + dirs[dir] = {files = files, subs = subs} end - -local cm, filename = '', '' - -if 0 == #arg then - for k, v in pairs(site) do - local path = '' - if 'string' == type(v) then - path = v - elseif not globalData.headerFound then - for i, w in ipairs(v) do - path = path .. w .. '/' +for k, v in pairs(dirs) 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 - path = string.sub(path, 1, -2) - end - path = path .. '.md' - cm = '' - local h = io.open(path, 'r') - if nil ~= h then - createHTML(h:read('*a'), path) - h:close() - else - print('oops! No such file ' .. path) - 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) end -- cgit v1.1