Module:JCW

local p = {} local mArguments = require('Module:Arguments') local TableTools = require('Module:TableTools')

local function stripToNil(text) -- If text is a non-empty string, return its trimmed content. -- Otherwise, return nothing (text is an empty string or is not a string). if type(text) == 'string' then return text:match('(%S.-)%s*$') end end

local function getDoiArgs(args) -- Return a list of |doiN=xxx parameter values, omitting blanks and skipped N's. -- LATER Should expand this to work with each exported function. local indices = {} for k, v in pairs(args) do		if stripToNil(v) and type(k) == 'string' then local n = tonumber(k:match('^doi(%d+)$')) if n then table.insert(indices, n)			end end end table.sort(indices) local parms = {} if stripToNil(args.doi) then if args.doi1 then error('Use doi or doi1, not both', 0) end parms[1] = args.doi end for _, n in ipairs(indices) do table.insert(parms, args['doi' .. n]) end return parms end

local Searcher Searcher = { link = function (self, doi) return ' '	end, new = function (options) options = options or {} local doiText = options.linkdoi and 'doi' or 'doi' return setmetatable({doiText = doiText}, Searcher) end } Searcher.__index = Searcher

function p.selected(frame) local n = mArguments.getArgs(frame, {parentOnly = true}) local note = n.note local wantSource = { ['User:JL-Bot/Questionable.cfg'] = true, }	--local abbrev = {BLJ = 'BLJ', BLJU='BLJU', BLP='BLP', BLPU='BLPU', SPJJ = 'SPJJ', SPJP = 'SPJP', DOAJ='Lying about DOAJ', UCG='Removed from UGC' QW ='QW',	DEPS ='DEPS', URF ='URF'} local abbrev = {Bohannon = 'Bohannon', BLJ = 'BLJ', BLJU= 'BLJU', BLP = 'BLP', BLPU = 'BLPU', SPJJ ='SPJJ', SPJP ='SPJP', DOAJ = 'DOAJ', QW='QW', DEPS ='DEPS', UCG = 'UCG', URF = 'URF', Unknown = 'Unknown'} local source if wantSource[mw.title.getCurrentTitle.fullText] then source = n.source or 'Unknown' source = abbrev[source] or source end local sourcenote if note and source then sourcenote = string.format(' (%s) &#91;%s&#93;', source, note) elseif source then sourcenote = string.format(' (%s)', source) elseif note then sourcenote = string.format(' &#91;%s&#93;', note) else sourcenote = '' end local listDoi, nrDoi = {}, 0 local function addDoi(doi, i)		if doi then nrDoi = nrDoi + 1 if i and listDoi[i] then table.insert(listDoi, i, doi) else listDoi[nrDoi] = doi end end end local rows = {} for i, v in ipairs(TableTools.compressSparseArray(n)) do		if i == 1 then rows[1] = { string.format('*%s%s', v, sourcenote) } elseif v:match('^10%.%d+$') then addDoi(v) else table.insert(rows, string.format('**%s', v)) end end if not rows[1] then error('Need at least one target parameter', 0) end for _, param in ipairs({ 'imprint', 'parent' }) do		for i = 1, 10 do local arg = n[param .. i]			if arg then table.insert(rows[1], string.format("%s", arg)) end end end if n.doi and n.doi1 then error('Use doi or doi1, not both', 0) end for i = 1, 10 do		local doi if i == 1 then doi = n.doi or n.doi1 else doi = n['doi' .. i]		end addDoi(doi, i)	end local search = Searcher.new for i = 1, nrDoi do		local doi = listDoi[i] table.insert(rows,			'**' .. search:link(doi)		) end rows[1] = table.concat(rows[1], ' / ') return table.concat(rows, '\n') end

function p.doilist(frame) local doiList = getDoiArgs(frame:getParent.args) if not doiList[1] then return '' end local search = Searcher.new({linkdoi = true}) for i, doi in ipairs(doiList) do		doiList[i] = search:link(doi) end return " |  " .. table.concat(doiList, ' &middot; ') .. -- middot is double bold " " end

function p.exclude(frame) local n = mArguments.getArgs(frame, {parentOnly = true}) local length = TableTools.length(n) local text = string.format('*%s', n[1] or '') n[1] = nil for _, v in ipairs(TableTools.compressSparseArray(n)) do		if length > 1 then text = text .. string.format(" ≠ %s", v)		end end return text end

function p.pattern(frame) local rows = {} for i, v in ipairs(frame:getParent.args) do		v = mw.text.trim(v) if v == '' then rows[i] = '' elseif i == 1 then rows[i] = string.format('*%s', v)		else rows[i] = string.format("\n** ", v				:gsub("%.%*", ".* ")				:gsub("!", "! ")				:gsub("'", "&rsquo;")			) end end if not rows[1] then error('pattern requires parameter 1', 0) end return table.concat(rows) end

return p