Module:Str find word/report explained

-- 17-4-2023 working with /sandbox now.-- not fit for mainspace in any form (uncontrolled effects while dev/sandbox) 23-3-2023require('strict')local sReportType = nillocal reportSep = '|'local str = require('Module:String')local yesno = require('Module:Yesno')local br = '
'local tMSG = -- Explain options (=report info), interprets parameter explain=-- returns true/false/'testcases'-- explain=true => show report in Preview-- explain=testcases => WHEN in ns:template: AND subpage = '/testcases' THEN show permanently-- Format string in tag -- replaces whitespace by single nbsp (keep untrimmed ws visible) local function inCode(s) if s == nil then return '' end s = string.gsub(s, '%s+', '&nbsp;') return '<code>' .. s .. ''end

-- Use mono font-family (from: Template:Mono)local function inMono(s) if s

nil then s = end return '

' .. s .. ''end

-- Formats table (array) using concat-- replace space by nbsp (keep untrimmed sp)-- in monospace font-familylocal function formatTablelist(t) if t

nil then return '<>' end

local s = s = table.concat(t, reportSep) s = mw.text.decode(string.gsub(s, '%s+', ' ')) s = '<' .. inMono(s) .. '>' return send

-- Make tResults.sRESULTstring returnstring into flat text (while keeping image name etc)-- for reporting only-- issue: somehow an is not :-ised (showing in text)local function xpWikicodePlain(sReturnString)local plain = require('Module:Plain text')

if sReturnString

nil or sReturnString

then return end

sReturnString = mw.text.killMarkers(sReturnString) sReturnString = mw.text.decode(sReturnString) sReturnString :gsub('%<%/? *div[^%>]*%>', ) :gsub('%<%/? *span[^%>]*%>', ) :gsub('%[%[%s*[Ff][Ii][Ll][Ee]%s*:', '', ) --strip out piped link text :gsub('([^%[])%^%[%][^%]]-%s', '%1') --strip out external link text :gsub('^%^%[%][^%]]-%s', ) --strip out external link text :gsub('[%[%]]', ) --then strip out remaining [and ] :gsub("?", "") --not stripping out ' gives correct output for bolded text in quotes :gsub('----+', ) --remove ---- lines :gsub("%s+", " ") --strip redundant spaces :gsub("^%s+", "") --strip leading :gsub("%s+$", "") --and trailing spaces

if mw.ustring.len(sReturnString) > 200 then sReturnString = mw.ustring.sub(sReturnString, 1, 200) .. ' ...' end return sReturnStringend

-- List the input arguments-- could be normalised newArgs-- no nils expectedlocal function xpListArguments(tOrigA) local sList = 'Arguments: ' for k, v in pairs(tOrigA) do sList = sList .. ' |' .. k .. '=' .. v end table.insert(tMSG, sList) returnend

-- One or both wordsets (sourcewords / to-find-words) is empty, so not check to do at all.local function xpNoWords(tArgs, tWork, tResults) table.insert(tMSG, 'no words to check:') if (#tWork.SOURCEwords

0) then table.insert(tMSG, 'No words in |source: ' .. inCode(tArgs.source)) end if (#tWork.ANDwords + #tWork.ORwords

0) then local sWords sWords = mw.text.trim((tArgs.andString or ) .. ' ' .. (tArgs.orString or )) table.insert(tMSG, 'No words to find ' .. '(|word= |andwords= |orwords=): ' .. inCode(sWords)) end returnend

---

-- Build top three lines of the reporttablelocal function xpBuildTopreport(tArgs, tWork, tResults)local aye = ''local nay = ''local msglocal tTitle =

-- report line 1. Title local title title = '

Preview report ' .. '{{Str find word/sbox}} ' .. 'explain=' .. inMono(tostring(tArgs.explain)) table.insert(tTitle, 1, title)

-- report line 2. Result (T/F) msg = 'RESULT: ' if tResults.resultALL then msg = msg .. '(words found in source) TRUE : <' .. tResults.sRESULTstring .. '>' .. aye else msg = msg .. ' FALSE' .. nay end table.insert(tTitle, 2, msg)

-- report line 3. Return value (Yes/No value) msg = 'RETURN VALUE ' if tResults.resultALL then -- True if (tArgs.yes

nil) then -- default = return tResults.sRESULTstring msg = msg .. '(default) |out-true= : ' .. inCode(tResults.sRESULTstring) else msg = msg .. '|out-true= ' .. inCode(xpWikicodePlain(tArgs.yes)) end else -- False msg = msg .. '|out-false= '.. inCode(xpWikicodePlain(tArgs.no)) end table.insert(tTitle, 3, msg)

return tTitleend

local function xpBuildWorkreport(tArgs, tWork, tResults)local tReport = tReport = --- todo usde xpListArguments return tReportend

local function xpBuildMsgreport(tArgs, tWork, tResults)local tReport = local msg

-- (bottom lines): settings & process steps results msg = 'MSGs: #todo>' .. #tMSG table.insert(tReport, msg)

-- SEP separator msg = 'SEP: >' .. inCode(tArgs.sep) .. '< [' .. tArgs.sep_pattern .. '] SEP-OUT: >' .. inCode(tArgs.out_sep) .. '<' table.insert(tReport, msg)

-- CASE: case-sensitive? local msg = if yesno(tArgs.case)

true then msg = 'Case-sensitive: true (A≠a)' else msg = 'Case-sensitive: false (A=a)' end table.insert(tReport, msg)

-- BOOLEANS: read as booleans? table.insert(tReport, 'Read booleans: ' .. tostring(yesno(tArgs.booleans))) return tReportend

-- Build report tables, exported: -- top table: title & results, 3 lines-- bottom table has processing results-- including messages like noWords (already in the table)local function xpBuildReport(tArgs, tWork, tResults)local tTopreport = local tWorkreport = local tMsgreport = local msg

-- Three top title rows tTopreport = xpBuildTopreport(tArgs, tWork, tResults) tWorkreport = xpBuildWorkreport(tArgs, tWork, tResults) tMsgreport = xpBuildMsgreport(tArgs, tWork, tResults) end

-- Add single message to report, bottom half (usually in debugging)local function xpMessage(sMsg) table.insert(tMSG, sMsg or ) returnend

-- Format return box (the Preview presentation)local function xpPresent(tArgs, tWork, tResults)local tTopreport = local tWorkreport = local tMsgreport = -- msg lineslocal divInTop = br .. '

'local divInWorks = '

'local divInMsg = '

'local divEnd = '

'

local tTopreport = xpBuildTopreport(tArgs, tWork, tResults)local tWorkreport = xpBuildWorkreport(tArgs, tWork, tResults)local tMsgreport = xpBuildMsgreport(tArgs, tWork, tResults)

local reportBox reportBox = divInTop .. table.concat(tTopreport, br) .. divEnd .. divInWorks .. table.concat(tWorkreport, br) .. divEnd .. divInMsg .. table.concat(tMsgreport, br) .. divEnd return reportBox end

-- Exported functionsreturn