aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/notYetAnotherWiki.lua
diff options
context:
space:
mode:
Diffstat (limited to '')
-rwxr-xr-xnotYetAnotherWiki.lua108
1 files changed, 80 insertions, 28 deletions
diff --git a/notYetAnotherWiki.lua b/notYetAnotherWiki.lua
index c5814f3..f53691b 100755
--- a/notYetAnotherWiki.lua
+++ b/notYetAnotherWiki.lua
@@ -215,27 +215,51 @@ else
215 print("Can't open everything.md for writing.") 215 print("Can't open everything.md for writing.")
216end 216end
217 217
218-- Scan the subdirectories looking for our files. 218-- Scan the sub folders looking for our files.
219local Directory = arg[1] 219local Folder = arg[1]
220toSub('') 220toSub('')
221if nil == Directory then Directory = '.' end 221if nil == Folder then Folder = '.' end
222 222--GlobalMetaData.root = Folder
223-- Sort out realURL for symlinked .md.md files. 223
224for l in io.popen('find -L ' .. Directory .. ' -name unsorted -prune -o -name "*.md.md" -xtype l -printf "%P\n"'):lines() do 224--[[ Sort out realURL for symlinked .md.md files.
225 realURL is the generic URL part for this page. By policy it points to whatever is the latest copy / symlink of the original download .md.md.
226 realURL starts out being the path to the downloaded file and friends.
227 If we make a symlink during SuckIt, then that gets updated to point to the place the symlink is in.
228 below we compare timestamps and select the latest version if there's more than one symlink.
229 For the "page symlinked" problem, this should work if realURL is kept updated.
230 For the "page copied" problem, this should work if realURL is kept updated, same as symlinked really, coz that's just another copy.
231 For the "page moved" problem, that'll be the most recent symlink, the old one still has it's symlink pointing to the download .md.md, which gets updated with the current realURL.
232 So when some external old URL points to someplace a page used to be, it's old symlink points to the up to date download .md.md, and we know where to go to find the page now.
233 A left over .md.md file should have a redirect .HTML page created for it.
234]]
235for l in io.popen('find -L ' .. Folder .. ' -name unsorted -prune -o -name "*.md.md" -xtype l -printf "%P\n"'):lines() do
225 local metadata = readMdMd(string.sub(l, 1, -4), {}) 236 local metadata = readMdMd(string.sub(l, 1, -4), {})
226-- FIXME - if this already exists, compare the timestamps, most recent wins. 237 if nil == metadata.realURL then
227 metadata.realURL = string.sub(l, 1, -7) 238 metadata.realURL = string.sub(l, 1, -7)
228 local a, e = io.open(l, 'w') 239 else
229 if nil == a then print('Could not open ' .. l .. ' - ' .. e) else 240 if metadata.realURL ~= string.sub(l, 1, -7) then
230 for k, v in pairs(metadata) do 241 metadata.realURL = string.sub(l, 1, -7)
231 a:write(k .. '=' .. v .. '\n') 242 -- If this already exists, compare the timestamps, most recent wins.
243 local time0 = io.popen('ls -l --time-style=+%s "' .. metadata.realURL .. '.md.md" | cut -d \' \' -f 6'):read('l')
244 local time1 = io.popen('ls -l --time-style=+%s "' .. l .. '" | cut -d \' \' -f 6'):read('l')
245 if time0 > time1 then metadata = nil end
246 else metadata = nil end
247 end
248
249 if nil ~= metadata then
250-- DUNNO if this writes to the original file, or overwrites the symlink.
251 local a, e = io.open(l, 'w')
252 if nil == a then print('Could not open ' .. l .. ' - ' .. e) else
253 for k, v in pairs(metadata) do
254 a:write(k .. '=' .. v .. '\n')
255 end
256 a:close()
232 end 257 end
233 a:close()
234 end 258 end
235end 259end
236 260
237-- Clean up unsorted. 261-- Clean up unsorted.
238for l in io.popen('find -L ' .. Directory .. ' -name unsorted -prune -o -name "*.md" -a -not -name "*.md.md" -xtype l -printf "%P\n"'):lines() do 262for l in io.popen('find -L ' .. Folder .. ' -name unsorted -prune -o -name "*.md" -a -not -name "*.md.md" -xtype l -printf "%P\n"'):lines() do
239 local tp = '_fos' 263 local tp = '_fos'
240 local metadata = readMdMd(l, {}) 264 local metadata = readMdMd(l, {})
241 if nil ~= metadata then 265 if nil ~= metadata then
@@ -245,16 +269,36 @@ for l in io.popen('find -L ' .. Directory .. ' -name unsorted -prune -o -name "*
245 local a, e = io.open(unsort .. tp .. '.md' , 'r') 269 local a, e = io.open(unsort .. tp .. '.md' , 'r')
246 if nil ~= a then 270 if nil ~= a then
247 a:close() 271 a:close()
248 os.execute('rm ' .. unsort .. tp .. '.*') 272 -- Keep the .md.md symlink, delete the rest.
273 os.execute('rm ' .. unsort .. tp .. '.HTML')
274 os.execute('rm ' .. unsort .. tp .. '.md')
275 a, e = io.open(unsort .. tp .. '.HTML', 'w')
276 if nil == a then print('Could not open ' .. unsort .. tp .. '.HTML' .. ' - ' .. e) else
277 local dst = string.sub(l, 1, -4) .. '.HTML'
278 local cnt = 1
279 for j = 1, #metadata.ogFile do
280 if '/' == string.sub(metadata.ogFile, j, j) then cnt = cnt + 1 end
281 end
282 dst = string.rep('../', cnt) .. dst
283 a:write(
284[=[<!DOCTYPE html>
285<html>
286 <head><meta http-equiv="refresh" content="0; url=]=] .. dst .. '"' .. [=[/></head>
287 <body><p>Click this if you don't get redirected to the real page - <a href="]=] .. dst .. '"' .. [=[>Redirect</a></p></body>
288</html>
289]=])
290 a:close()
291 print('REDIRECT ' .. unsort .. tp .. '.HTML \t-> ' .. dst)
292 end
293
249 end 294 end
250 end 295 end
251 end 296 end
252end 297end
253 298
254-- Look for copied pages from the other wikis. 299-- Look for copied pages from the other wikis.
255for l in io.popen('find -L ' .. Directory .. ' -name "*.HTM" -type f,l -printf "%P\n"'):lines() do 300for l in io.popen('find -L ' .. Folder .. ' -name "*.HTM" -type f,l -printf "%P\n"'):lines() do
256-- TODO - Only do this if .HTM is newer than .md, or .md doesn't exist. 301-- Only do this if .HTM is newer than .md, or .md doesn't exist.
257
258 local htime = io.popen("date -ur " .. l .. " +%s"):read('l') 302 local htime = io.popen("date -ur " .. l .. " +%s"):read('l')
259 local mtime = io.popen("date -ur " .. string.sub(l, 1, -4) .. "md +%s 2>/dev/null"):read('l') 303 local mtime = io.popen("date -ur " .. string.sub(l, 1, -4) .. "md +%s 2>/dev/null"):read('l')
260 if (nil == mtime) or (htime > mtime) then 304 if (nil == mtime) or (htime > mtime) then
@@ -404,14 +448,14 @@ writeString(l .. '_ORIGINAL1', body)
404 end 448 end
405end 449end
406 450
407if '.' ~= Directory then 451if '.' ~= Folder then
408 for l in io.popen('find -L . -name "*.md" -type f,l -printf "%P\n"'):lines() do 452 for l in io.popen('find -L . -name "*.md" -type f,l -printf "%P\n"'):lines() do
409 toFile(string.gsub(l, '%.md$', '')) 453 toFile(string.gsub(l, '%.md$', ''))
410 end 454 end
411end 455end
412 456
413-- Can add in a distant directory to, for putting it's results in the current directory. 457-- Can add in a distant folder to, for putting it's results in the current folder.
414for l in io.popen('find -L ' .. Directory .. ' -name "*.md" -type f,l -printf "%P\n"'):lines() do 458for l in io.popen('find -L ' .. Folder .. ' -name "*.md" -type f,l -printf "%P\n"'):lines() do
415 local n = string.gsub(l, '%.md$', '') 459 local n = string.gsub(l, '%.md$', '')
416 if nil == Files[n] then toFile(n) end 460 if nil == Files[n] then toFile(n) end
417end 461end
@@ -472,7 +516,7 @@ for name, sub in pairs(Subs) do
472 print("EMPTY " .. name) 516 print("EMPTY " .. name)
473 h = io.open(name .. '/index.md', 'w') 517 h = io.open(name .. '/index.md', 'w')
474 if nil ~= h then 518 if nil ~= h then
475 h:write('This directory has no files.') 519 h:write('This folder has no files.')
476 h:close() 520 h:close()
477 else 521 else
478 print("Can't open " .. name .. '/index.md for writing.') 522 print("Can't open " .. name .. '/index.md for writing.')
@@ -484,7 +528,7 @@ end
484--------------------------------------------------------------------------------- 528---------------------------------------------------------------------------------
485-- These functions assume the above file and sub scan has completed. 529-- These functions assume the above file and sub scan has completed.
486 530
487-- Which page in this directory should we show? 531-- Which page in this folder should we show?
488-- NOTE - only looking for the .md files we scanned for before, any stray HTML, html, HTM, and htm files will get ignored. 532-- NOTE - only looking for the .md files we scanned for before, any stray HTML, html, HTM, and htm files will get ignored.
489local whichPage = function(f) 533local whichPage = function(f)
490 local fl = '' 534 local fl = ''
@@ -527,7 +571,7 @@ local whichWiki = function(metadata)
527end 571end
528 572
529 573
530-- Calculate a link from the source directory to the destination directory. 574-- Calculate a link from the source folder to the destination folder.
531local linkFrom = function(source, dest) 575local linkFrom = function(source, dest)
532 -- Evil hacks! 576 -- Evil hacks!
533 if 'Profiles' == dest then dest = 'PmWiki/Profiles' end 577 if 'Profiles' == dest then dest = 'PmWiki/Profiles' end
@@ -763,7 +807,7 @@ function Writer.header(s, level)
763 local text = Lunamark.util.rope_to_string(s) 807 local text = Lunamark.util.rope_to_string(s)
764-- FIXME - Work around a bug in Lunamark? 808-- FIXME - Work around a bug in Lunamark?
765 text = RE.gsub(text, "{[\\]}", "") 809 text = RE.gsub(text, "{[\\]}", "")
766 return '<h' .. level .. ' id="' .. RE.gsub(text, '{[ ]}', '_') .. '">' .. text .. ' <a style="font-size: 0.42em;" href="#top">👆</a></h' .. level .. '>' 810 return '<h' .. level .. ' id="' .. RE.gsub(text, '{[ ]}', '_') .. '">' .. text .. ' <a style="font-size: 0.42em;" href="#top">🔼</a></h' .. level .. '>'
767end 811end
768local OgWriterLink = Writer.link -- So we can call the original from within mine, we are just changing the URL. 812local OgWriterLink = Writer.link -- So we can call the original from within mine, we are just changing the URL.
769function Writer.link(lab, url, tit) 813function Writer.link(lab, url, tit)
@@ -810,7 +854,7 @@ for name, file in pairs(Files) do
810 local pth = file.path 854 local pth = file.path
811 if '' ~= file.path then pth = file.path .. '/' end 855 if '' ~= file.path then pth = file.path .. '/' end
812 if 'true' ~= Subs[pth .. f].metadata.hidden then 856 if 'true' ~= Subs[pth .. f].metadata.hidden then
813 metadata.header = metadata.header .. '<a href="' .. f .. '/' .. whichPage(pth .. f) .. '">' .. f .. '</a> &nbsp; ' 857 metadata.header = metadata.header .. '<a href="' .. f .. '/' .. whichPage(pth .. f) .. '">' .. f .. '</a> &nbsp; &#128194; '
814 end 858 end
815 end 859 end
816 860
@@ -847,7 +891,15 @@ for name, file in pairs(Files) do
847 end 891 end
848 else 892 else
849 if nil ~= url then metadata.menu = metadata.menu .. '<p><a href="' .. url .. '">' .. title .. ' &#9757;</a></p>' 893 if nil ~= url then metadata.menu = metadata.menu .. '<p><a href="' .. url .. '">' .. title .. ' &#9757;</a></p>'
850 else metadata.menu = metadata.menu .. '<p><a href="' .. f .. '.HTML">' .. title .. '</a></p>' 894 else
895 local pth = file.path
896 if '' ~= pth then pth = pth .. '/' end
897 -- Don't include any left over .md.md files, so don't do this if f.md doesn't exist.
898 local a, e = io.open(pth .. f .. '.md' , 'r')
899 if nil ~= a then
900 a:close()
901 metadata.menu = metadata.menu .. '<p><a href="' .. f .. '.HTML">' .. title .. '</a></p>'
902 end
851 end 903 end
852 end 904 end
853 end 905 end
@@ -865,7 +917,7 @@ for name, file in pairs(Files) do
865 metadata.history = '' 917 metadata.history = ''
866 end 918 end
867 if nil ~= metadata.sourcecode then metadata.footer = '<a href="' .. metadata.sourcecode .. '">source code</a>' end 919 if nil ~= metadata.sourcecode then metadata.footer = '<a href="' .. metadata.sourcecode .. '">source code</a>' end
868 if nil ~= metadata.feedatom then metadata.footer = '<a href="' .. metadata.feedatom .. '">atom feed</a> &nbsp; &nbsp; ' .. metadata.footer end 920 if nil ~= metadata.feedatom then metadata.footer = '<a href="' .. metadata.feedatom .. '">atom feed<img src="feed-icon-14x14.png"></img></a> &nbsp; &nbsp; ' .. metadata.footer end
869 if metadata.footer ~= '' then metadata.footer = 'Web site ' .. metadata.footer end 921 if metadata.footer ~= '' then metadata.footer = 'Web site ' .. metadata.footer end
870 -- Add a link to the original page. 922 -- Add a link to the original page.
871 if nil ~= metadata.ogURL then 923 if nil ~= metadata.ogURL then