From a5e7cd15a460e5d65ddc349be36983c3ca88c45a Mon Sep 17 00:00:00 2001 From: dvs1 Date: Tue, 17 Dec 2024 03:32:20 +1000 Subject: Major code reorg, to get the menus working better. --- notYetAnotherWiki.lua | 81 ++++++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 36 deletions(-) (limited to 'notYetAnotherWiki.lua') diff --git a/notYetAnotherWiki.lua b/notYetAnotherWiki.lua index cfe7459..1746b0a 100755 --- a/notYetAnotherWiki.lua +++ b/notYetAnotherWiki.lua @@ -2,8 +2,12 @@ local lcmark = require("lcmark") -local createHTML = function(cm, file, header, menu) --- cm = string.gsub(cm, '._ ', '.  ') +local globalData = {header = '', footer = '', menu = '', ['_'] = ' '} +local site = {} + + +local createHTML = function(cm, file) +-- 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}) @@ -12,14 +16,14 @@ local createHTML = function(cm, file, header, menu) 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 + if nil == file then templateFile = nil else - metadata.menu = menu - metadata.header = header + 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 metadata.body = lcmark.apply_template(bod, metadata) @@ -56,9 +60,6 @@ local createHTML = function(cm, file, header, menu) 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 = '' @@ -67,53 +68,61 @@ for l in io.popen('find . -name "*.md" -type f -printf "%P\n"'):lines() do table.insert(s, p) c = c + #p + 1 end - local full = string.sub(l, c, -1) - local base = string.gsub(full, '%.md$', '') + local base = string.gsub(string.sub(l, c, -1), '%.md$', '') table.insert(s, base) - if 'menu' == s[#s] then +-- 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) - menu = createHTML(h:read('*a')) + globalData.menu = string.gsub(createHTML(h:read('*a')), 'ul>', 'menu>') h:close() - menu = string.gsub(menu, 'ul>', 'menu>') end else - if 1 ~= #s then - site[l] = s - else - site[l] = base + if 1 ~= #s then site[l] = s else site[l] = base end + 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 end --- TODO - Only do these first things if menu.md doesn't exist, the second for headers.md ---if '' == menu then + +local cm, filename = '', '' + +if 0 == #arg then for k, v in pairs(site) do + local path = '' if 'string' == type(v) then - local m = '[' .. v .. '](' .. v .. '.HTML)\n' - menu = menu .. string.gsub(createHTML(m), 'ul>', 'menu>') - else - local path = '' + path = v + elseif not globalData.headerFound then for i, w in ipairs(v) do path = path .. w .. '/' end - header = header .. '' .. v[1] .. '   ' + path = string.sub(path, 1, -2) end - end ---end - + path = path .. '.md' -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') + local h = io.open(path, 'r') if nil ~= h then - createHTML(h:read('*a'), l, header, menu) + createHTML(h:read('*a'), path) h:close() else - print('oops! No such file ' .. l) + print('oops! No such file ' .. path) end end else @@ -128,5 +137,5 @@ else end end if filename == '' then filename = 'test.md' end - createHTML(cm, filename, header, menu) + createHTML(cm, filename) end -- cgit v1.1