Content deleted Content added
No edit summary |
Undid revision 1147915801 by Lemondoge (talk): oh dear. I checked with testcases - don't know how this goofed |
||
(66 intermediate revisions by 4 users not shown) | |||
Line 1:
require('strict')
local p = {}
local getArgs = require('Module:Arguments').getArgs
local str = require('Module:String')
local yesno = require('Module:Yesno')
local
local iMaxWords =
local warningIMaxWordsReached = nil
local xpLitWordCount = 0
local report -- to be initinated when explain needed
-- Initialise the /report subpage.
-- only invoked when
local function initReport()
report = require('Module:
end
-- Turn "A" into "A" etc. asap
-- and reduce multi-spaces (including nbsp etc.) into single space
local function decodeUnicode(str)
Line 33 ⟶ 32:
-- step 1: when case-insensitive, turn string into lowercase
-- step 2: read & remove Literals ("..")
-- step 3:
-- step 4: when booleans=T, change boolean words into true/false (module:yesno rules)
-- all words returned are trimmed, TODO and all ws into single-plainspace?
-- only T/F words are edited, other words remain, untouched
-- return the table (
▲local function buildWordTableCSV(newA, sWordlist)
local wordTable = {}
local hitWord = ''
Line 46 ⟶ 44:
-- Step 1: case-sensitive
if yesno(
sWordlist = string.lower(sWordlist)
end
Line 53 ⟶ 51:
-- then remove them from the string:
-- replaced by single comma; idle & keeps word separation
--- if yesno(
local _, sCount
_, sCount = mw.ustring.gsub(sWordlist, '"', '')
Line 79 ⟶ 78:
-- Step 3: parse comma-delimited words
hitCount = 0
sWordlist = tArgs.sep .. sWordlist .. tArgs.sep
local eSep
eSep = escape_word(tArgs.sep)
local patstring = '%f[^' .. eSep .. '][^' .. eSep .. ']+%f[' .. eSep .. ']'
report.xpMessage('1.eSep: ' .. eSep) -- dev
report.xpMessage('2.pattern: ' .. patstring) -- dev
end▼
while hitCount <= iMaxWords do
hitCount = hitCount + 1
hitWord = str._match(
hitWord = mw.text.trim(hitWord)
if hitWord ==
-- no more words found in the string
break
Line 91 ⟶ 99:
end
if hitCount > iMaxWords then
warningIMaxWordsReached = 'Max number of words (' .. tostring(iMaxWords) .. ') reached
.. ' (' .. mw.ustring.sub(mw.text.trim(sWordlist), 1, 90) .. ' ...). '
end
-- Step 4: when read booleans, converse
-- todo: check parameter here not elsewhere
▲ if newA.booleans then
if tArgs.booleans then -- TODO if Yesno(tArgs.booleans) ...
local sBool
for i, v in ipairs(wordTable) do
Line 153 ⟶ 163:
bAND = true
end
return bAND, tHits
end
Line 191 ⟶ 201:
-- Determine the requested return value (string).
-- sYeslist is the _main return value (logically defined value)
-- this function applies
-- note: yes='' implies: blank return value
-- note: no parameter yes= (that is, yes=nil) implies: by default, return the sYeslist
local function yesnoReturnstring(
if sYeslist == '' then -- False
return
else -- True
if
return sYeslist
else -- some |yes= value is entered, could be ''
return
end
end
Line 215 ⟶ 225:
-- explain=true => show report in Preview
-- explain=testcases => WHEN in ns: template: or user: AND subpage = '/testcases' THEN show permanently
local function checkExplain(
return false -- never. 22Mar2023 checkExplain(newArgs)
▲ if yesno(newA.explain, true) then
return true▼
▲ end
end
Line 245 ⟶ 241:
-- 5. based on T or F status, the return value (string) is established and returned
-- note 1: each return value (yes=.., no=..) can be '' (nulstring)
function p._main(
local sourceWordTable = {}
local andWordTable = {}
Line 257 ⟶ 253:
local sYeslist = ''
sourceWordTable =
andWordTable =
orWordTable =
if (#sourceWordTable == 0) or (#andWordTable + #orWordTable == 0) then
--
resultALL = false
if yesno(
report.xpNoWords(
end
else
Line 277 ⟶ 273:
-- concat the sYeslist (= all hit words; from 2 tables)
if bANDresult then
sYeslist = sYeslist .. table.concat(tANDhits,
end
if #tORhits > 0 then
if #tANDhits > 0 then
sYeslist = sYeslist ..
end
sYeslist = sYeslist .. table.concat(tORhits,
end
end
if yesno(
if
if (
report.xpYesNoBothBlank()
end
Line 297 ⟶ 293:
report.xpMessage(warningIMaxWordsReached)
end
report.xpBuildReport(
bANDresult, andWordTable, tANDhits,
bORresult, orWordTable, tORhits,
sYeslist, xpLitWordCount)
end
return yesnoReturnstring(
-- set wordt separator
local function setSep(sSep)
if sSep == nil then return defaultSep end
local msg = ''
-- todo what with {{!}}
local newSep = defaultSep
newSep = sSep
sSep = decodeUnicode(sSep)
if string.match(sSep, '[%s%w%d]') ~= nil then -- not ok
msg = 'Irregular characters in sep: ' .. sSep
newSep = defaultSep
newSep = string.sub(sSep, 1, 1)
if newSep == '' then --- ???
newSep = defaultSep
end
local function concatAndLists(s1, s2, newSep)
local tLists = {} -- working table: both s1 and s2 to concat
table.insert(tLists, s1)
table.insert(tLists, s2)
return table.concat(tLists,
end
local function parseArgs(origArgs)
local newArgs = {}
newArgs['sep'] = setSep(origArgs['sep']) -- do first, needed below
newArgs['source'] = decodeUnicode(origArgs['s'] or origArgs['source'] or '')
newArgs['andString'] = decodeUnicode(concatAndLists(
origArgs['w'] or origArgs['word'] or nil,
origArgs['andw'] or origArgs['andwords'] or nil
newArgs.sSep)
)
newArgs['orString'] = decodeUnicode(origArgs['orw'] or origArgs['orwords'] or '')
-- boolean options: catch both parameters, also handle nil & nonsense input values:
newArgs['case'] = yesno(origArgs['case'] or origArgs['casesensitive'] or
newArgs['booleans'] = yesno(origArgs['bool'] or origArgs['booleans'] or false, false) -- defaults to False
newArgs['literals'] = yesno(origArgs['literals'] or origArgs['lit'] or true, true) -- defaults to True
newArgs['yes'] = origArgs['yes'] or nil -- nil; default so return sYeslist; keep '' as legal input & return value
newArgs['no'] = origArgs['no'] or ''
newArgs['explain'] =
newArgs.explain = false -- never. 22Mar2023 checkExplain(newArgs)
return newArgs
Line 335 ⟶ 355:
local origArgs = getArgs(frame)
local sReturn = ''
local
if yesno(
initReport()
report.xpListArguments(origArgs)
end
sReturn = p._main(
if warningIMaxWordsReached ~=nil then
Line 350 ⟶ 370:
end
if yesno(
return sReturn .. report.xpPresent(
else
return sReturn
|