From 783c0aa0231554e7d4c5ab154c80ba88544e214c Mon Sep 17 00:00:00 2001 From: onefang Date: Wed, 28 Jan 2026 13:58:42 +1000 Subject: Various style cleanups. --- notYetAnotherWiki.lua | 178 ++++++++++++++++++++++++++------------------------ 1 file changed, 92 insertions(+), 86 deletions(-) diff --git a/notYetAnotherWiki.lua b/notYetAnotherWiki.lua index f53691b..be41c3a 100755 --- a/notYetAnotherWiki.lua +++ b/notYetAnotherWiki.lua @@ -6,10 +6,9 @@ Normally I define functions and globals at the top, but here I'm interleaving them. ]] -local Lunamark = require("lunamark") -- https://github.com/jgm/lunamark -local Lpeg = require("lpeg") -- https://www.inf.puc-rio.br/~roberto/lpeg/lpeg.html Lunamark uses this, so we can to. -local RE = require("re") -- Part of lpeg. https://www.inf.puc-rio.br/~roberto/lpeg/re.html - +local Lunamark = require'lunamark' -- https://github.com/jgm/lunamark +local Lpeg = require'lpeg' -- https://www.inf.puc-rio.br/~roberto/lpeg/lpeg.html Lunamark uses this, so we can to. +local RE = require're' -- Part of lpeg. https://www.inf.puc-rio.br/~roberto/lpeg/re.html --------------------------------------------------------------------------------- -- Some global data. @@ -34,9 +33,9 @@ local Files, Subs, xLinks = {}, {}, {} local Context = {} -- Coz can't otherwise pass context through to the deeper Lunamark functions I'm overriding. local Template = '' -local h = io.open("default.template", 'r') +local h = io.open('default.template', 'r') if nil ~= h then - Template = h:read('*a') + Template = h:read('*a') -- NOTE - Lua 5.3 changes to not need the *, but ignores it if it's there. h:close() else print('oops! No such file ' .. 'default.template') @@ -73,12 +72,12 @@ local stringBits = function(l) local bits = {} local last = 1 for j = 1, #l do - if '/' == string.sub(l, j, j) then - table.insert(bits, string.sub(l, last, j - 1)) + if '/' == l:sub(j, j) then + table.insert(bits, l:sub(last, j - 1)) last = j + 1 end end - return bits, string.sub(l, last) + return bits, l:sub(last) end @@ -97,7 +96,7 @@ local toFile = function(name, key, value) end Files[name].path = path -- Files[name].body = '' - if ("Foswiki" == bits[1]) or ("PmWiki" == bits[1]) then Files[name].ogWiki = bits[1] end + if ('Foswiki' == bits[1]) or ('PmWiki' == bits[1]) then Files[name].ogWiki = bits[1] end end if nil ~= key then Files[name][key] = value end for i, v in ipairs{'metadata', 'bits', } do @@ -105,7 +104,7 @@ local toFile = function(name, key, value) end -- Open the files and do the initial cleanups. local body = '' - if '.md' ~= string.sub(name, -3, -1) then + if '.md' ~= name:sub(-3, -1) then h = io.open(name .. '.md', 'r') if nil ~= h then -- print('Parsing ' .. name .. '.md') @@ -168,7 +167,7 @@ local readMdMd = function(name, metadata) return {} else for l in h1:lines() do - for k, v in string.gmatch(l, "(%w+)%s*=%s*(.+)") do + for k, v in l:gmatch'(%w+)%s*=%s*(.+)' do if nil == v then print(name .. ' ' .. k) else @@ -190,8 +189,8 @@ local commonLinky = function(l, body, u, url, beg, en, beg0, en0, bump) -- if nil ~= md then if nil ~= md.realURL then url = md.realURL end -- end - body = string.sub(body, 1, beg - bump) .. url .. string.sub(body, en0 + 1) - here = here + string.len(url) + body = body:sub(1, beg - bump) .. url .. body:sub(en0 + 1) + here = here + url:len() end if 1 == bump then here = here + 1 @@ -204,6 +203,10 @@ end +--[[ +]] + + --------------------------------------------------------------------------------- -- Actually start doing things. @@ -217,7 +220,7 @@ end -- Scan the sub folders looking for our files. local Folder = arg[1] -toSub('') +toSub'' if nil == Folder then Folder = '.' end --GlobalMetaData.root = Folder @@ -233,12 +236,12 @@ if nil == Folder then Folder = '.' end A left over .md.md file should have a redirect .HTML page created for it. ]] for l in io.popen('find -L ' .. Folder .. ' -name unsorted -prune -o -name "*.md.md" -xtype l -printf "%P\n"'):lines() do - local metadata = readMdMd(string.sub(l, 1, -4), {}) + local metadata = readMdMd(l:sub(1, -4), {}) if nil == metadata.realURL then - metadata.realURL = string.sub(l, 1, -7) + metadata.realURL = l:sub(1, -7) else - if metadata.realURL ~= string.sub(l, 1, -7) then - metadata.realURL = string.sub(l, 1, -7) + if metadata.realURL ~= l:sub(1, -7) then + metadata.realURL = l:sub(1, -7) -- If this already exists, compare the timestamps, most recent wins. local time0 = io.popen('ls -l --time-style=+%s "' .. metadata.realURL .. '.md.md" | cut -d \' \' -f 6'):read('l') local time1 = io.popen('ls -l --time-style=+%s "' .. l .. '" | cut -d \' \' -f 6'):read('l') @@ -263,7 +266,7 @@ for l in io.popen('find -L ' .. Folder .. ' -name unsorted -prune -o -name "*.md local tp = '_fos' local metadata = readMdMd(l, {}) if nil ~= metadata then - if "PmWiki" == metadata.ogWiki then tp = '_pm' end + if 'PmWiki' == metadata.ogWiki then tp = '_pm' end if nil ~= metadata.ogFile then local unsort = 'unsorted/' .. metadata.ogFile local a, e = io.open(unsort .. tp .. '.md' , 'r') @@ -274,10 +277,10 @@ for l in io.popen('find -L ' .. Folder .. ' -name unsorted -prune -o -name "*.md os.execute('rm ' .. unsort .. tp .. '.md') a, e = io.open(unsort .. tp .. '.HTML', 'w') if nil == a then print('Could not open ' .. unsort .. tp .. '.HTML' .. ' - ' .. e) else - local dst = string.sub(l, 1, -4) .. '.HTML' + local dst = l:sub(1, -4) .. '.HTML' local cnt = 1 for j = 1, #metadata.ogFile do - if '/' == string.sub(metadata.ogFile, j, j) then cnt = cnt + 1 end + if '/' == metadata.ogFile:sub(j, j) then cnt = cnt + 1 end end dst = string.rep('../', cnt) .. dst a:write( @@ -299,8 +302,8 @@ end -- Look for copied pages from the other wikis. for l in io.popen('find -L ' .. Folder .. ' -name "*.HTM" -type f,l -printf "%P\n"'):lines() do -- Only do this if .HTM is newer than .md, or .md doesn't exist. - local htime = io.popen("date -ur " .. l .. " +%s"):read('l') - local mtime = io.popen("date -ur " .. string.sub(l, 1, -4) .. "md +%s 2>/dev/null"):read('l') + local htime = io.popen('date -ur ' .. l .. ' +%s'):read('l') + local mtime = io.popen('date -ur ' .. l:sub(1, -4) .. 'md +%s 2>/dev/null'):read('l') if (nil == mtime) or (htime > mtime) then print('pandoc converting ' .. l .. ' -> ' .. string.sub(l, 1, -4) .. 'md') os.execute('cp ' .. l .. ' ' .. l .. '_ORIGINAL0') @@ -309,20 +312,20 @@ os.execute('cp ' .. l .. ' ' .. l .. '_ORIGINAL0') if nil ~= h then local body = h:read('*a') ; h:close() writeString(l .. '_ORIGINAL1', body) - if 'Foswiki' == string.sub(l, 1, 7) then + if 'Foswiki' == l:sub(1, 7) then -- Strip out the actual content. - local beg, en = RE.find(body, [['
']]) if nil ~= beg then body = string.sub(body, en + 1) end - beg, en = RE.find(body, [['
']]) if nil ~= beg then body = string.sub(body, en + 1) end + local beg, en = RE.find(body, [['
']]) if nil ~= beg then body = body:sub(en + 1) end + beg, en = RE.find(body, [['
']]) if nil ~= beg then body = body:sub(en + 1) end beg, en = RE.find(body, [['
']]) if nil ~= beg then - if ' -- ' == string.sub(body, en + 1, en + 4) then + if ' -- ' == body:sub(en + 1, en + 4) then beg, en = RE.find(body, '[%nl]', en + 4) - body = string.sub(body, en + 1) + body = body:sub(en + 1) end end - beg, en = RE.find(body, [['
']]) if nil ~= beg then body = string.sub(body, 1, beg - 1) end --- beg, en = RE.find(body, [['
']]) if nil ~= beg then body = string.sub(body, 1, en + 1) end - beg, en = RE.find(body, [['
']]) if nil ~= beg then body = string.sub(body, 1, beg - 1) end - beg, en = RE.find(body, [['
']]) if nil ~= beg then body = string.sub(body, 1, beg - 1) end + beg, en = RE.find(body, [['
']]) if nil ~= beg then body = body:sub(1, beg - 1) end +-- beg, en = RE.find(body, [['
']]) if nil ~= beg then body = body:sub(1, en + 1) end + beg, en = RE.find(body, [['
']]) if nil ~= beg then body = body:sub(1, beg - 1) end + beg, en = RE.find(body, [['
']]) if nil ~= beg then body = body:sub(1, beg - 1) end -- Some clean ups. local result = RE.compile( [[{~ ( @@ -346,17 +349,17 @@ writeString(l .. '_ORIGINAL1', body) here = beg + 1 local beg0, en0 local url = nil - if '"' == string.sub(body, beg - 1, beg - 1) then + if '"' == body:sub(beg - 1, beg - 1) then beg0, en0 = RE.find(body, [['"']], en) - url = string.sub(body, en + 1, en0 - 1) + url = body:sub(en + 1, en0 - 1) end - if "'" == string.sub(body, beg - 1, beg - 1) then + if "'" == body:sub(beg - 1, beg - 1) then beg0, en0 = RE.find(body, [["'"]], en) - url = string.sub(body, en + 1, en0) + url = body:sub(en + 1, en0) end if nil ~= url then - if ('pub/' == string.sub(url, 1, 4)) then + if ('pub/' == url:sub(1, 4)) then -- FIXME? - evil hack? url = 'Foswiki/' .. url --print('FOSWIKI HTM ' .. url) @@ -376,7 +379,7 @@ writeString(l .. '_ORIGINAL1', body) -- if nil ~= md then if nil ~= md.realURL then url = md.realURL end -- end - body = string.sub(body, 1, beg - 1) .. url .. string.sub(body, en0 + 1) + body = body:sub(1, beg - 1) .. url .. body:sub(en0 + 1) here = here + #url end beg, en = RE.find(body, [['https://fos.wiki.devuan.org/']], here) @@ -384,12 +387,12 @@ writeString(l .. '_ORIGINAL1', body) end writeString(l .. '_NEW', body) - elseif 'PmWiki' == string.sub(l, 1, 6) then - local beg, en = RE.find(body, [['']]) if nil ~= beg then body = string.sub(body, en + 2) end - beg, en = RE.find(body, [["div id='wikitext'>"]]) if nil ~= beg then body = string.sub(body, en + 2) end - beg, en = RE.find(body, [["
"]]) if nil ~= beg then body = string.sub(body, 1, beg - (2 + 9)) end -- There's a
to get rid of to. - beg, en = RE.find(body, [['']]) if nil ~= beg then body = string.sub(body, 1, beg - 2) end local result = RE.compile( [[{~ + elseif 'PmWiki' == l:sub(1, 6) then + local beg, en = RE.find(body, [['']]) if nil ~= beg then body = body:sub(en + 2) end + beg, en = RE.find(body, [["div id='wikitext'>"]]) if nil ~= beg then body = body:sub(en + 2) end + beg, en = RE.find(body, [["
"]]) if nil ~= beg then body = body:sub(1, beg - (2 + 9)) end -- There's a
to get rid of to. + beg, en = RE.find(body, [['']]) if nil ~= beg then body = body:sub(1, beg - 2) end ( {"class='categorylink'"} -> blank / {"class='createlink'"} -> blank / @@ -425,11 +428,11 @@ writeString(l .. '_ORIGINAL1', body) here = beg + 1 local beg0, en0 = RE.find(body, [["'"]], en) -- FIXME? - This might be working around a bug elsewhere. - if "'" == string.sub(body, en0, en0) then en0 = en0 - 1 end - local url = string.sub(body, en + 1, en0) - if '?n=' == string.sub(url, 1, 3) then - url = string.sub(url, 4):gsub('[%a]+%.([%a-]+)', '%1_pm.HTML') - elseif ("'" == url) or ('uploads/' == string.sub(url, 1, 8)) then + if "'" == body:sub(en0, en0) then en0 = en0 - 1 end + local url = body:sub(en + 1, en0) + if '?n=' == url:sub(1, 3) then + url = url:sub(4):gsub('[%a]+%.([%a-]+)', '%1_pm.HTML') + elseif ("'" == url) or ('uploads/' == url:sub(1, 8)) then -- FIXME - evil hack? Yep, evil hack, need to know the depth of the source, which isn't here. url = 'PmWiki/' .. url else @@ -444,19 +447,19 @@ writeString(l .. '_ORIGINAL1', body) end end - ok, rslt, status = os.execute('pandoc --wrap=preserve -f html -t commonmark_x --self-contained ' .. l .. '_NEW' .. ' >' .. string.sub(l, 1, -4) .. 'md') + os.execute('pandoc --wrap=preserve -f html -t commonmark_x --self-contained ' .. l .. '_NEW' .. ' >' .. l:sub(1, -4) .. 'md') end end if '.' ~= Folder then for l in io.popen('find -L . -name "*.md" -type f,l -printf "%P\n"'):lines() do - toFile(string.gsub(l, '%.md$', '')) + toFile(l:gsub('%.md$', '')) end end -- Can add in a distant folder to, for putting it's results in the current folder. for l in io.popen('find -L ' .. Folder .. ' -name "*.md" -type f,l -printf "%P\n"'):lines() do - local n = string.gsub(l, '%.md$', '') + local n = l:gsub('%.md$', '') if nil == Files[n] then toFile(n) end end @@ -482,14 +485,14 @@ for name, file in pairs(Files) do end end - if '.md' == string.sub(name, -3, -1) then + if '.md' == name:sub(-3, -1) then -- This is a metadata only file, no content, stash the matadata. metadata = readMdMd(name, metadata) if '.md' == name then toSub(path, 'metadata', metadata) - elseif '/.md' == string.sub(name, -4, -1) then toSub(path, 'metadata', metadata) --- else toFile(string.sub(name, 1, -4), 'metadata', metadata) - else NewMeta[string.sub(name, 1, -4)] = metadata -- Coz we can't add to Files here. + elseif '/.md' == name:sub(-4, -1) then toSub(path, 'metadata', metadata) +-- else toFile(name:sub(1, -4), 'metadata', metadata) + else NewMeta[name:sub(1, -4)] = metadata -- Coz we can't add to Files here. end Files[name] = nil end @@ -505,7 +508,7 @@ end -- Fix up subs now we have all the file bits. for name, file in pairs(Files) do - if '.md' ~= string.sub(name, -3, -1) then + if '.md' ~= name:sub(-3, -1) then table.insert(Subs[file.path].files, file.bit) end end @@ -539,7 +542,7 @@ local whichPage = function(f) -- Standard files to search for. for i, v in ipairs{'about', 'readme', 'index', 'homepage', 'mainpage', 'webhome'} do for j, w in ipairs(Subs[f].files) do - if v == string.lower(w) then + if v == w:lower() then fl = w break end @@ -631,7 +634,7 @@ for name, file in pairs(Files) do if ('everything' ~= name) then local ln, fw, pw, ts = 'DUNNO', '', '', '' local title, link = whichWiki(metadata) - link = string.gsub(link, 'https://', 'HTTPS://') -- Prevent this one from being converted. + link = link:gsub('https://', 'HTTPS://') -- Prevent this one from being converted. if 'PmWiki' == metadata.ogWiki then pw = 'PmWiki [' .. title .. '](' .. link .. ')' end if 'Foswiki' == metadata.ogWiki then fw = 'Foswiki [' .. title .. '](' .. link .. ')' end if nil ~= metadata.timestamp then ts = metadata.timestamp end @@ -647,7 +650,7 @@ for name, file in pairs(Files) do end end end -table.sort(Pages, function(a, b) return (string.lower(a) < string.lower(b)) end) +table.sort(Pages, function(a, b) return (a:lower() < b:lower()) end) for i, f in ipairs(Pages) do Bdy = Bdy .. f end @@ -667,7 +670,7 @@ SUBS = {} for name, sub in pairs(Subs) do table.insert(SUBS, sub) end -table.sort(SUBS, function(a, b) return (string.lower(a.path) < string.lower(b.path)) end) +table.sort(SUBS, function(a, b) return (a.path:lower() < b.path:lower()) end) for n, sub in pairs(SUBS) do local name = sub.path sub.whichPage = whichPage(name) @@ -746,51 +749,52 @@ local Writer = Lunamark.writer.html5.new(LunamarkOpts) local lunaLinky = function(url) -- Fix up the links. if ('https://wiki.devuan.org/' ~= url) and ('https://fos.wiki.devuan.org/' ~= url) then -- TODO - This might be covering up a bug elsewhere. - if '/Main/' == string.sub(url, 1, 6) then + if '/Main/' == url:sub(1, 6) then local link = linkFrom(Context.path, 'Foswiki/Main') if '' == link then - url = string.sub(url, 7) .. '.HTML' + url = url:sub(7) .. '.HTML' else - url = link .. '/' .. string.sub(url, 7) .. '.HTML' + url = link .. '/' .. url:sub(7) .. '.HTML' end end - if '/System/' == string.sub(url, 1, 8) then + if '/System/' == url:sub(1, 8) then url = 'https://fos.wiki.devuan.org' .. url end for i, p in ipairs{'https://wiki.devuan.org/?n=', 'https://wiki.devuan.org?n=', 'PmWiki/uploads/', 'Foswiki/pub/', 'https://fos.wiki.devuan.org/'} do - if p == string.sub(url, 1, #p) then - local ur = string.sub(url, #p + 1) + if p == url:sub(1, #p) then + local ur = url:sub(#p + 1) -- TODO - could probably replace some of this mess with RE.gsub() and friends. Meh, it works. - local f4, f5, tk1 = string.find(ur, '?', 1, true) + local f4, f5, tk1 = ur:find('?', 1, true) if fail ~= f4 then - local u = string.sub(ur, 1, f4 - 1) + local u = ur:sub(1, f4 - 1) ur = u end local md - if ('fos' == string.sub(p, 9, 11)) or ('Fos' == string.sub(p, 1, 3)) then + + if ('fos' == p:sub(9, 11)) or ('Fos' == p:sub(1, 3)) then md = readMdMd('Foswiki/' .. ur .. '.md', {}) else - md = readMdMd('PmWiki/' .. string.gsub(ur, '%.', '/', 1) .. '.md', {}) + md = readMdMd('PmWiki/' .. ur:gsub('%.', '/', 1) .. '.md', {}) end if (nil ~= md) and (nil ~= md.realURL) then url = md.realURL end - if ('https://fos.wiki.devuan.org/bin/' ~= string.sub(url, 1, 32)) and ('https://fos.wiki.devuan.org/System/' ~= string.sub(url, 1, 35)) then - local xlnk = xLinks[string.gsub(ur, '%..*', '', 1)] + if ('https://fos.wiki.devuan.org/bin/' ~= url:sub(1, 32)) and ('https://fos.wiki.devuan.org/System/' ~= url:sub(1, 35)) then + local xlnk = xLinks[ur:gsub('%..*', '', 1)] if nil == Context.path then - url = string.gsub(ur, '%.', '/', 1) + url = ur:gsub('%.', '/', 1) else - if nil == xlnk then xlnk = string.gsub(string.gsub(ur, '%..*', '', 1), '/.*', '', 1) end + if nil == xlnk then xlnk = string.gsub(ur:gsub('%..*', '', 1), '/.*', '', 1) end if '' ~= Context.path then xlnk = linkFrom(Context.path, xlnk) end - if '/' == string.sub(xlnk, 1, 1) then xlnk = string.sub(xlnk, 2) end - if ('' ~= xlnk) and ('/' ~= string.sub(xlnk, -1)) then xlnk = xlnk .. '/' end if 'DUNNO/' == xlnk then print('OOPS! page not found - @' .. Context.path .. ' / ' .. Context.bit .. '\t' .. url .. ' -> ' .. xlnk .. ' ' .. string.gsub(ur, '.*%.', '', 1) .. '.HTML') end + if '/' == xlnk:sub(1, 1) then xlnk = xlnk:sub(2) end + if ('' ~= xlnk) and ('/' ~= xlnk:sub(-1)) then xlnk = xlnk .. '/' end end -- if (nil ~= md) and (nil ~= md.realURL) then url = md.realURL -- else - url = xlnk .. string.gsub(ur, '.*%.', '', 1) + url = xlnk .. ur:gsub('.*%.', '', 1) -- end if 'PmWiki/uploads/' == p then - url = '../../' .. p .. string.gsub(ur, '%.', '.', 1) + url = '../../' .. p .. ur:gsub('%.', '.', 1) elseif 'Foswiki/pub/' == p then url = '../../' .. p .. ur else @@ -806,7 +810,7 @@ end function Writer.header(s, level) local text = Lunamark.util.rope_to_string(s) -- FIXME - Work around a bug in Lunamark? - text = RE.gsub(text, "{[\\]}", "") + text = RE.gsub(text, '{[\\]}', '') return '' .. text .. ' 🔼' end local OgWriterLink = Writer.link -- So we can call the original from within mine, we are just changing the URL. @@ -849,7 +853,7 @@ for name, file in pairs(Files) do for i, f in pairs(Subs[file.path].subs) do table.insert(subs, f) end - table.sort(subs, function(a, b) return (string.lower(a) < string.lower(b)) end) + table.sort(subs, function(a, b) return (a:lower() < b:lower()) end) for i, f in ipairs(subs) do local pth = file.path if '' ~= file.path then pth = file.path .. '/' end @@ -861,7 +865,7 @@ for name, file in pairs(Files) do -- Figure out this pages menu links. metadata.menu = '' if nil == metadata.title then metadata.title = bit end - if nil ~= Subs[file.path].files then table.sort(Subs[file.path].files, function(a, b) return (string.lower(a) < string.lower(b)) end) end + if nil ~= Subs[file.path].files then table.sort(Subs[file.path].files, function(a, b) return (a:lower() < b:lower()) end) end for i, f in ipairs(Subs[file.path].files) do local title, url = nil, nil if '' == file.path then @@ -872,15 +876,17 @@ for name, file in pairs(Files) do url = Files[file.path .. '/' .. f].metadata.URL end if nil == title then title = f end + + if bit == f then metadata.menu = metadata.menu .. '

' .. title .. '

' for j, g in ipairs(file.headers) do local beg, en = RE.find(g, [['{']]) if nil ~= beg then - g = string.sub(g, 1, beg - 2) + g = g:sub(1, beg - 2) end local h = string.sub(RE.gsub(g, '{[#]}', ''), 2) - local l = string.len(g) - string.len(h) + local l = g:len() - h:len() g = h h = RE.gsub(h, '{[ ]}', ' ') -- FIXME - Work around a bug in Lunamark? @@ -922,7 +928,7 @@ for name, file in pairs(Files) do -- Add a link to the original page. if nil ~= metadata.ogURL then local title, link = whichWiki(metadata) - link = string.gsub(link, 'https://', 'HTTPS://') -- Prevent this one from being converted. + link = link:gsub('https://', 'HTTPS://') -- Prevent this one from being converted. -- TODO - wrap "edit" in a link to actually edit it. PmWiki "?action=edit" Foswiki "https://fos.wiki.devuan.org/bit/edit/" .. page .. "?t=" .. 10 digit random number? local edit = 'T' if 'PmWiki' == metadata.ogWiki then edit = 'Maybe you can edit t' end @@ -944,7 +950,7 @@ for name, file in pairs(Files) do result = RE.compile ('{~ ({[$][A-Za-z_]+[$]} -> meta / .)* ~}', { meta = function(a) - a = string.sub(a, 2, -2) + a = a:sub(2, -2) local md = metadata[a] if nil == md then md = GlobalMetaData[a] -- cgit v1.1