diff options
Diffstat (limited to 'notYetAnotherWiki.lua')
-rwxr-xr-x | notYetAnotherWiki.lua | 172 |
1 files changed, 143 insertions, 29 deletions
diff --git a/notYetAnotherWiki.lua b/notYetAnotherWiki.lua index 4207905..29ed9fb 100755 --- a/notYetAnotherWiki.lua +++ b/notYetAnotherWiki.lua | |||
@@ -25,7 +25,7 @@ local GlobalMetaData = { | |||
25 | -- <body> has alink, link, vlink; CSS has active, link, visited, and hover. | 25 | -- <body> has alink, link, vlink; CSS has active, link, visited, and hover. |
26 | devuanDevuanalink = '#03a4ff', devuanDevuanlink = '#0076b6', devuanDevuanvlink = '#6aa4db', devuanDevuanhlink = '#03a4ff', | 26 | devuanDevuanalink = '#03a4ff', devuanDevuanlink = '#0076b6', devuanDevuanvlink = '#6aa4db', devuanDevuanhlink = '#03a4ff', |
27 | devuanSDevuanalink = '#98c3db', devuanSDevuanlink = '#ffffff', devuanSDevuanvlink = '#ffffff', devuanSDevuanhlink = '#98c3db', | 27 | devuanSDevuanalink = '#98c3db', devuanSDevuanlink = '#ffffff', devuanSDevuanvlink = '#ffffff', devuanSDevuanhlink = '#98c3db', |
28 | karenPurple = '#8800ff', onefangPurple = '#cc00ff', | 28 | karenPurple = '#8800ff', onefangPurple = '#cc00ff', onefangGreen = '#42ff00', |
29 | PinkFloyd = '#AA00AA', DeepPurple = '#220022', -- From an ancient site of mine, which went from PinkFloyd to DeepPurple as a background gradient. | 29 | PinkFloyd = '#AA00AA', DeepPurple = '#220022', -- From an ancient site of mine, which went from PinkFloyd to DeepPurple as a background gradient. |
30 | favicon = 'nYAW_icon.png', logo = 'nYAW.png', | 30 | favicon = 'nYAW_icon.png', logo = 'nYAW.png', |
31 | footer = 'Powered by <a href="https://sledjhamr.org/cgit/notYetAnotherWiki/about/">notYetAnotherWiki</a> version 0.0. ', | 31 | footer = 'Powered by <a href="https://sledjhamr.org/cgit/notYetAnotherWiki/about/">notYetAnotherWiki</a> version 0.0. ', |
@@ -155,6 +155,49 @@ end | |||
155 | 155 | ||
156 | 156 | ||
157 | 157 | ||
158 | local readMdMd = function(name, metadata) | ||
159 | local h1 = io.open(name .. '.md') | ||
160 | if nil == h1 then | ||
161 | -- print('Could not open ' .. name .. '.md') | ||
162 | return {} | ||
163 | else | ||
164 | for l in h1:lines() do | ||
165 | for k, v in string.gmatch(l, "(%w+)%s*=%s*(.+)") do | ||
166 | if nil == v then | ||
167 | print(name .. ' ' .. k) | ||
168 | else | ||
169 | metadata[k] = v | ||
170 | end | ||
171 | end | ||
172 | end | ||
173 | end | ||
174 | return metadata | ||
175 | end | ||
176 | |||
177 | |||
178 | |||
179 | local commonLinky = function(l, body, u, url, beg, en, beg0, en0, bump) | ||
180 | if nil == url then | ||
181 | -- print('OOPS! unknown linky - @' .. l .. '\t\t\t' .. string.sub(body, beg - 9, en) .. ' ' .. string.sub(body, en + 1, en0)) | ||
182 | else | ||
183 | local md = readMdMd(url, {}) | ||
184 | -- if nil ~= md then | ||
185 | if nil ~= md.realURL then url = md.realURL end | ||
186 | -- end | ||
187 | body = string.sub(body, 1, beg - bump) .. url .. string.sub(body, en0 + 1) | ||
188 | here = here + string.len(url) | ||
189 | end | ||
190 | if 1 == bump then | ||
191 | here = here + 1 | ||
192 | beg, en = RE.find(body, [['https://fos.wiki.devuan.org/']], here) | ||
193 | else | ||
194 | beg, en = RE.find(body, [["'https://wiki.devuan.org/"]], here) | ||
195 | end | ||
196 | return beg, en, body, here | ||
197 | end | ||
198 | |||
199 | |||
200 | |||
158 | --------------------------------------------------------------------------------- | 201 | --------------------------------------------------------------------------------- |
159 | -- Actually start doing things. | 202 | -- Actually start doing things. |
160 | 203 | ||
@@ -166,12 +209,25 @@ else | |||
166 | print("Can't open everything.md for writing.") | 209 | print("Can't open everything.md for writing.") |
167 | end | 210 | end |
168 | 211 | ||
169 | |||
170 | -- Scan the subdirectories looking for our files. | 212 | -- Scan the subdirectories looking for our files. |
171 | local Directory = arg[1] | 213 | local Directory = arg[1] |
172 | toSub('') | 214 | toSub('') |
173 | if nil == Directory then Directory = '.' end | 215 | if nil == Directory then Directory = '.' end |
174 | 216 | ||
217 | -- Sort out realURL for symlinked .md.md files. | ||
218 | for l in io.popen('find -L ' .. Directory .. ' -name "*.md.md" -xtype l -printf "%P\n"'):lines() do | ||
219 | local metadata = readMdMd(string.sub(l, 1, -4), {}) | ||
220 | -- FIXME - if this already exists, compare the timestamps, most recent wins. | ||
221 | metadata.realURL = string.sub(l, 1, -7) | ||
222 | local a, e = io.open(l, 'w') | ||
223 | if nil == a then print('Could not open ' .. l .. ' - ' .. e) else | ||
224 | for k, v in pairs(metadata) do | ||
225 | a:write(k .. '=' .. v .. '\n') | ||
226 | end | ||
227 | a:close() | ||
228 | end | ||
229 | end | ||
230 | |||
175 | for l in io.popen('find -L ' .. Directory .. ' -name "*.HTM" -type f,l -printf "%P\n"'):lines() do | 231 | for l in io.popen('find -L ' .. Directory .. ' -name "*.HTM" -type f,l -printf "%P\n"'):lines() do |
176 | -- print('pandoc converting ' .. l .. ' -> ' .. string.sub(l, 1, -4) .. 'md') | 232 | -- print('pandoc converting ' .. l .. ' -> ' .. string.sub(l, 1, -4) .. 'md') |
177 | -- Open the HTM files and do the initial cleanups, then pandoc them. | 233 | -- Open the HTM files and do the initial cleanups, then pandoc them. |
@@ -203,11 +259,37 @@ for l in io.popen('find -L ' .. Directory .. ' -name "*.HTM" -type f,l -printf " | |||
203 | {'rel="nofollow"'} -> blank / {"rel='nofollow'"} -> blank / | 259 | {'rel="nofollow"'} -> blank / {"rel='nofollow'"} -> blank / |
204 | {"target='_blank'"} -> blank / | 260 | {"target='_blank'"} -> blank / |
205 | {"</div>" ([%nl])* } -> blank / | 261 | {"</div>" ([%nl])* } -> blank / |
206 | {'style="' ([^"])+ '"'} -> '' / {"style='" ([^'])+ "'"} -> '' / | 262 | -- {'style="' ([^"])+ '"'} -> blank / {"style='" ([^'])+ "'"} -> blank / |
207 | . | 263 | . |
208 | )* ~}]], { blank = function(a) return '' end } ):match(body) | 264 | )* ~}]], { blank = function(a) return '' end } ):match(body) |
209 | body = result | 265 | body = result |
210 | -- body = RE.gsub(body, [=[{"<!-- ".*"-->"}]=], '') -- FIXME | 266 | -- body = RE.gsub(body, [=[{"<!-- ".*"-->"}]=], '') -- FIXME |
267 | local here = 1 | ||
268 | beg, en = RE.find(body, [['https://fos.wiki.devuan.org/']], here) | ||
269 | while nil ~= beg do | ||
270 | here = beg + 1 | ||
271 | local beg0, en0 | ||
272 | local url = nil | ||
273 | if '"' == string.sub(body, beg - 1, beg - 1) then | ||
274 | beg0, en0 = RE.find(body, [['"']], en) | ||
275 | url = string.sub(body, en + 1, en0 - 1) | ||
276 | end | ||
277 | if "'" == string.sub(body, beg - 1, beg - 1) then | ||
278 | beg0, en0 = RE.find(body, [["'"]], en) | ||
279 | url = string.sub(body, en + 1, en0) | ||
280 | end | ||
281 | |||
282 | if nil ~= url then | ||
283 | if ('pub/' == string.sub(url, 1, 4)) then | ||
284 | -- FIXME? - evil hack? | ||
285 | url = 'Foswiki/' .. url | ||
286 | else | ||
287 | url = nil | ||
288 | end | ||
289 | end | ||
290 | beg, en, body, here = commonLinky(l, body, 'https://fos.wiki.devuan.org/', url, beg, en, beg0, en0, 1) | ||
291 | end | ||
292 | |||
211 | writeString(l .. '_NEW', body) | 293 | writeString(l .. '_NEW', body) |
212 | elseif 'PmWiki' == string.sub(l, 1, 6) then | 294 | elseif 'PmWiki' == string.sub(l, 1, 6) then |
213 | local beg, en = RE.find(body, [['<!--PageText-->']]) if nil ~= beg then body = string.sub(body, en + 2) end | 295 | local beg, en = RE.find(body, [['<!--PageText-->']]) if nil ~= beg then body = string.sub(body, en + 2) end |
@@ -230,11 +312,30 @@ for l in io.popen('find -L ' .. Directory .. ' -name "*.HTM" -type f,l -printf " | |||
230 | {"class='wikilink'"} -> blank / | 312 | {"class='wikilink'"} -> blank / |
231 | {'rel="nofollow"'} -> blank / {"rel='nofollow'"} -> blank / | 313 | {'rel="nofollow"'} -> blank / {"rel='nofollow'"} -> blank / |
232 | {"target='_blank'"} -> blank / | 314 | {"target='_blank'"} -> blank / |
233 | {'style="' ([^"])+ '"'} -> '' / {"style='" ([^'])+ "'"} -> '' / | 315 | -- {'style="' ([^"])+ '"'} -> blank / {"style='" ([^'])+ "'"} -> blank / |
234 | {"<span class='hlt " {([a-z])+} "'></span><pre" } -> "<pre class='%2'" / | 316 | {"<span class='hlt " {([a-z])+} "'></span><pre" } -> "<pre class='%2'" / |
235 | . | 317 | . |
236 | )* ~}]], { blank = function(a) return '' end } ):match(body) | 318 | )* ~}]], { blank = function(a) return '' end } ):match(body) |
237 | body = result | 319 | body = result |
320 | here = 1 | ||
321 | beg, en = RE.find(body, [["'https://wiki.devuan.org/"]], here) | ||
322 | while nil ~= beg do | ||
323 | here = beg + 1 | ||
324 | local beg0, en0 = RE.find(body, [["'"]], en) | ||
325 | -- FIXME? - This might be working around a bug elsewhere. | ||
326 | if "'" == string.sub(body, en0, en0) then en0 = en0 - 1 end | ||
327 | local url = string.sub(body, en + 1, en0) | ||
328 | if '?n=' == string.sub(url, 1, 3) then | ||
329 | url = string.sub(url, 4):gsub('[%a]+%.([%a-]+)', '%1_pm.HTML') | ||
330 | elseif ("'" == url) or ('uploads/' == string.sub(url, 1, 8)) then | ||
331 | -- FIXME - evil hack? Yep, evil hack, need to know the depth of the source, which isn't here. | ||
332 | url = 'PmWiki/' .. url | ||
333 | else | ||
334 | url = nil | ||
335 | end | ||
336 | beg, en, body, here = commonLinky(l, body, "'https://wiki.devuan.org/", url, beg, en, beg0, en0, 0) | ||
337 | end | ||
338 | |||
238 | writeString(l .. '_NEW', body) | 339 | writeString(l .. '_NEW', body) |
239 | end | 340 | end |
240 | end | 341 | end |
@@ -278,18 +379,8 @@ for name, file in pairs(Files) do | |||
278 | 379 | ||
279 | if '.md' == string.sub(name, -3, -1) then | 380 | if '.md' == string.sub(name, -3, -1) then |
280 | -- This is a metadata only file, no content, stash the matadata. | 381 | -- This is a metadata only file, no content, stash the matadata. |
281 | local h1 = io.open(name .. '.md') | 382 | |
282 | if nil == h1 then print('Could not open ' .. name .. '.md') else | 383 | metadata = readMdMd(name, metadata) |
283 | for l in h1:lines() do | ||
284 | for k, v in string.gmatch(l, "(%w+)%s*=%s*(.+)") do | ||
285 | if nil == v then | ||
286 | print(name .. ' ' .. k) | ||
287 | else | ||
288 | metadata[k] = v | ||
289 | end | ||
290 | end | ||
291 | end | ||
292 | end | ||
293 | if '.md' == name then toSub(path, 'metadata', metadata) | 384 | if '.md' == name then toSub(path, 'metadata', metadata) |
294 | elseif '/.md' == string.sub(name, -4, -1) then toSub(path, 'metadata', metadata) | 385 | elseif '/.md' == string.sub(name, -4, -1) then toSub(path, 'metadata', metadata) |
295 | -- else toFile(string.sub(name, 1, -4), 'metadata', metadata) | 386 | -- else toFile(string.sub(name, 1, -4), 'metadata', metadata) |
@@ -367,6 +458,7 @@ local linkFrom = function(source, dest) | |||
367 | -- Evil hacks! | 458 | -- Evil hacks! |
368 | if 'Profiles' == dest then dest = 'PmWiki/Profiles' end | 459 | if 'Profiles' == dest then dest = 'PmWiki/Profiles' end |
369 | if 'Onefang' == dest then dest = 'PmWiki/Onefang' end | 460 | if 'Onefang' == dest then dest = 'PmWiki/Onefang' end |
461 | if 'Tiki' == dest then dest = 'PmWiki/Tiki' end | ||
370 | 462 | ||
371 | if source == dest then return '' end | 463 | if source == dest then return '' end |
372 | local depth = 0 | 464 | local depth = 0 |
@@ -532,17 +624,11 @@ local LunamarkOpts = { | |||
532 | escaped_line_breaks=true, -- Pandoc-style escaped hard line breaks | 624 | escaped_line_breaks=true, -- Pandoc-style escaped hard line breaks |
533 | } | 625 | } |
534 | local Writer = Lunamark.writer.html5.new(LunamarkOpts) | 626 | local Writer = Lunamark.writer.html5.new(LunamarkOpts) |
535 | |||
536 | -- Can override the various writer functions, there's something for each of the basic HTML elements. | 627 | -- Can override the various writer functions, there's something for each of the basic HTML elements. |
537 | local Context = {} -- Coz can't otherwise pass context through to the deeper Lunamark functions I'm overriding. | 628 | local Context = {} -- Coz can't otherwise pass context through to the deeper Lunamark functions I'm overriding. |
538 | -- Fix up the links. | 629 | local lunaLinky = function(url) -- Fix up the links. |
539 | local OgWriterLink = Writer.link -- So we can call the original from within mine, we are just changing the URL. | ||
540 | function Writer.link(lab, url, tit) | ||
541 | if ('https://wiki.devuan.org/' ~= url) and ('https://fos.wiki.devuan.org/' ~= url) then | 630 | if ('https://wiki.devuan.org/' ~= url) and ('https://fos.wiki.devuan.org/' ~= url) then |
542 | local label = lab | 631 | 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 |
543 | local uri = url | ||
544 | if 'string' ~= type(lab) then label = type(lab) end | ||
545 | for i, p in ipairs{'https://wiki.devuan.org/?n=', 'https://wiki.devuan.org?n=', 'https://fos.wiki.devuan.org/'} do | ||
546 | if p == string.sub(url, 1, #p) then | 632 | if p == string.sub(url, 1, #p) then |
547 | local ur = string.sub(url, #p + 1) | 633 | local ur = string.sub(url, #p + 1) |
548 | -- TODO - could probably replace some of this mess with RE.gsub() and friends. Meh, it works. | 634 | -- TODO - could probably replace some of this mess with RE.gsub() and friends. Meh, it works. |
@@ -551,22 +637,50 @@ function Writer.link(lab, url, tit) | |||
551 | local u = string.sub(ur, 1, f4 - 1) | 637 | local u = string.sub(ur, 1, f4 - 1) |
552 | ur = u | 638 | ur = u |
553 | end | 639 | end |
640 | local md | ||
641 | if ('fos' == string.sub(p, 9, 11)) or ('Fos' == string.sub(p, 1, 3)) then | ||
642 | md = readMdMd('Foswiki/' .. ur .. '.md', {}) | ||
643 | else | ||
644 | md = readMdMd('PmWiki/' .. string.gsub(ur, '%.', '/', 1) .. '.md', {}) | ||
645 | end | ||
646 | if (nil ~= md) and (nil ~= md.realURL) then url = md.realURL end | ||
647 | |||
648 | local xlnk = xLinks[string.gsub(ur, '%..*', '', 1)] | ||
554 | if nil == Context.path then | 649 | if nil == Context.path then |
555 | url = string.gsub(ur, '%.', '/', 1) | 650 | url = string.gsub(ur, '%.', '/', 1) |
556 | else | 651 | else |
557 | local xlnk = xLinks[string.gsub(ur, '%..*', '', 1)] | 652 | if nil == xlnk then xlnk = string.gsub(string.gsub(ur, '%..*', '', 1), '/.*', '', 1) end |
558 | if nil == xlnk then xlnk = string.gsub(ur, '%..*', '', 1) end | ||
559 | if '' ~= Context.path then xlnk = linkFrom(Context.path, xlnk) end | 653 | if '' ~= Context.path then xlnk = linkFrom(Context.path, xlnk) end |
560 | if '/' == string.sub(xlnk, 1, 1) then xlnk = string.sub(xlnk, 2) end | 654 | if '/' == string.sub(xlnk, 1, 1) then xlnk = string.sub(xlnk, 2) end |
561 | if ('' ~= xlnk) and ('/' ~= string.sub(xlnk, -1)) then xlnk = xlnk .. '/' end | 655 | if ('' ~= xlnk) and ('/' ~= string.sub(xlnk, -1)) then xlnk = xlnk .. '/' end |
562 | if 'DUNNO/' == xlnk then print('OOPS! Page not found - ' .. Context.path .. ' / ' .. Context.bit .. ' ' .. url .. ' -> ' .. xlnk .. ' ' .. string.gsub(ur, '.*%.', '', 1) .. '.HTML') end | 656 | if 'DUNNO/' == xlnk then print('OOPS! page not found - @' .. Context.path .. ' / ' .. Context.bit .. '\t' .. url .. ' -> ' .. xlnk .. ' ' .. string.gsub(ur, '.*%.', '', 1) .. '.HTML') end |
563 | url = xlnk .. string.gsub(ur, '.*%.', '', 1) .. '.HTML' | 657 | end |
658 | -- if (nil ~= md) and (nil ~= md.realURL) then url = md.realURL | ||
659 | -- else | ||
660 | url = xlnk .. string.gsub(ur, '.*%.', '', 1) | ||
661 | -- end | ||
662 | if 'PmWiki/uploads/' == p then | ||
663 | url = '../' .. p .. string.gsub(ur, '%.', '.', 1) | ||
664 | elseif 'Foswiki/pub/' == p then | ||
665 | url = '../' .. p .. ur | ||
666 | else | ||
667 | url = url .. '.HTML' | ||
564 | end | 668 | end |
565 | end | 669 | end |
566 | end | 670 | end |
567 | end | 671 | end |
568 | return OgWriterLink(lab, url, tit) | 672 | return url |
673 | end | ||
674 | |||
675 | local OgWriterLink = Writer.link -- So we can call the original from within mine, we are just changing the URL. | ||
676 | function Writer.link(lab, url, tit) | ||
677 | return OgWriterLink(lab, lunaLinky(url), tit) | ||
569 | end | 678 | end |
679 | local OgWriterImage = Writer.image | ||
680 | function Writer.image(lab, url, tit) | ||
681 | return OgWriterImage(lab, lunaLinky(url), tit) | ||
682 | end | ||
683 | |||
570 | local Parse = Lunamark.reader.markdown.new(Writer, LunamarkOpts) | 684 | local Parse = Lunamark.reader.markdown.new(Writer, LunamarkOpts) |
571 | 685 | ||
572 | 686 | ||