Module:Sandbox/ProcrastinatingReader/Dbm: Difference between revisions

Content deleted Content added
add summary
assert position in string
 
(4 intermediate revisions by the same user not shown)
Line 2:
local getArgs = require('Module:Arguments').getArgs
 
-- ConstructReturns a table ofwith CSDsthe andkeys anyof argumentsthe passed intable as perits CSDvalues
local function getTableKeys(tbl)
local keys = {}
for k,_ in pairs(tbl) do
keys[#keys + 1] = k
end
 
table.sort(keys)
return keys
end
 
-- Construct a table of the tagged CSDs and any arguments passed in for each CSD
-- Example format: |G6|rationale{{=}}G6 rationale|xfd|G7|rationale{{=}}G7 rationale
local function constructCsdTable(args)
local tbl = {}
local current = '' -- Code of current CSD being processed. Its value will be the last CSD-like param read (eg 'G6')
local keys = {}
local currentsize = ''0
for k,v in pairs(args) do
if string.match(v, '^[GgAaFfCcUuRrTtPp]%d%d?$') then -- Begin collecting args for a new CSD
current, size = string.lower(v), 0
table.insert(keys, tbl[current)] = {['size'] = size}
else -- Add the arg to the CSD being processed (ie, the corresponding CSD)
tbl[current] = {}
else
local arg = mw.text.split(v, '=')
tbl[current][string.lower(arg[1])] = arg[2]
size = size + 1 -- Increment the 'size', ie number of params passed for a particular CSD
tbl[current]['size'] = {}size
end
end
return {keys, tbl}
end
 
-- Build the Db-meta wrapper template
local function build(frame, args)
local passThruArgs = {'rationale', 'xfd', 'user', 'blanked', 'url', 'url2', 'url3', 'source', 'article', '___location', 'filename', 'criterion'} -- Arguments we'll pass through unchanged to the Db-meta wrapper
local constructedcsdTable = constructCsdTable(args)
local csdscsdKeys = constructed[1]getTableKeys(csdTable)
--if true then return mw.dumpObject(csdTable) end
local csdList = constructed[2]
 
local requestedReasonsbuiltCsdTemplates = {}
local summary = ""
-- Loop through CSDs (eg G1, G2)
forlocal k,vn in= pairs(csdList) do0
for k,v in pairs(csdTable) do
local csdArgs = {
n = n + else1
local csdArgstemplateArgs = {
['raw'] = 'yes',
['nocat'] = 'true' -- for test purposes
}
summary-- Add =to summary ..the string.upper(k)name of .the CSD. (#vPlus >open 0brackets andif we've ('got orextra '')params
summary = summary .. string.upper(k) .. (v['size'] > 0 and ' (' or '')
-- add values to templateArgs (and summary) in any custom params we got for this CSD, eg Rationale, URL
for _,arg in ipairs(passThruArgs) do
if v[arg] then
csdArgstemplateArgs[arg] = v[arg]
summary = summary .. arg .. ': ' .. v[arg] .. (#v['size'] > _ and ', ' or '')
end
end
summary-- =Close summarybrackets .if we had custom params. (#vAdd >a 0comma andif we')'ve orgot '')another CSD
summary = summary .. (v['size'] > 0 and ')' or '') .. (#csdKeys > n and ', ' or '')
-- Save the built template
table.insert(requestedReasonsbuiltCsdTemplates, frame:expandTemplate{ title = 'Db-'..k, args = csdArgstemplateArgs })
end
-- Construct Db-meta wrapper, composed of all the built CSD templates
local dbTemplate = frame:expandTemplate{ title = 'Db-meta', args = {
[1] = '&#32;for the following reasons:</b><ul><li>'..table.concat(requestedReasonsbuiltCsdTemplates, '</li><li>') .. '</li></ul> <b>The page may be deleted under any criterion that is valid',
['criterion'] = 'NA',
['temp'] = 'Db-notice-multiple',
['temp2'] = '|'..table.concat(csdscsdKeys, '|'),
['summary'] = 'Multiple reasons: ' .. summary,
-- forward given params to Db-meta
['bot'] = args['bot'] or '',
['blanked'] = args['blanked'] or (csdListcsdTable['g10'] and 'yes') or '',
['divblank'] = args['divblank'] or (csdListcsdTable['g10'] and 'yes') or '',
['help'] = args['help'] or '',
}}
 
return dbTemplate
end
 
-- Exports
 
function p.main(frame)
local args = getArgs(frame)
Line 75 ⟶ 94:
end
 
-- Test
function p.dump(frame)
return mw.dumpObject(mw.text.unstripNoWiki(frame.args[1]))