Module:Pagelist: Difference between revisions

From Zoophilia Wiki
Jump to navigationJump to search
meta>Mr. Stradivarius
create a replacement for Template:Pagelist
 
Ookami (talk | contribs)
m 7 revisions imported
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
local p = {}
local p = {}


-- Get the page object. This will return the page object for the page
local separators = {
-- specified, or nil if there are errors in the title, if the
dot = true,
-- expensive function count has been exceeded, or if the page was not
pipe = true,
-- specified.
comma = true,
function getPageObject( page )
['tpt-languages'] = true
    if not page then
}
        return nil
 
    end
local function getSeparator(sep)
    -- Get the page object, passing the function through pcall
if type(sep) ~= 'string' then
    -- in case we are over the expensive function count limit.
return nil
    local noError, pageObject = pcall(mw.title.new, page)
end
    if not noError then
if separators[sep] then
        return nil
return mw.message.new(sep .. '-separator'):plain()
    else
else
        return pageObject
return sep
    end
end
end
end


-- Process the separator parameter.
local function generateLink(page, nspace, delim, endDelim)
local function getSeparator( sep )
if not page then
    if sep and type(sep) == 'string' then
return nil
        if sep == 'dot'  
end
            or sep =='pipe'
local pagename = mw.title.new(page)
            or sep == 'comma'
if not pagename then
            or sep == 'tpt-languages' then
-- Default to the args we were passed if our page
            return mw.message.new( sep .. '-separator' ):plain()
-- object was nil.
        else
pagename = page
            return sep
else
        end
pagename = pagename.text
    else
end
        return nil
delim = delim or ''
    end
endDelim = endDelim or delim
nspace = nspace or ''
if nspace == 'all' then
nspace = ''
pagename = page
end
local outStr = mw.ustring.gsub(  
string.format(
'%s[[:%s:%s|%s]]%s',
delim, nspace, pagename, page, endDelim
),
':+',
':'
)
return outStr
end
end


local function generateLink( page, nspace, delim, edelim )
function p._main(args)
    if not page then
local t = {}
        return nil
local separator = getSeparator(args.separator)
    end
local conjunction = getSeparator(args.conjunction)
    local pagename = getPageObject( page )
for i, v in ipairs(args) do
    if not pagename then
table.insert(t, generateLink(
        -- Default to the args we were passed if our page
v, args.nspace, args.delim, args.edelim
        -- object was nil.
))
        pagename = page
end
    else
return mw.text.listToText(t, separator, conjunction)
        pagename = pagename.text
    end
    delim = delim or ''
    edelim = edelim or delim
    nspace = nspace or mw.title.getCurrentTitle().nsText
    return mw.ustring.format(  
        '%s[[:%s:%s|%s]]%s',
        delim, nspace, pagename, page, edelim
    )
end
end


local function _main( args )
function p.main(frame)
    local t = {}
local origArgs = require('Module:Arguments').getArgs(frame, {
    local separator = getSeparator( args.separator )
trim = false,
    local conjunction = getSeparator( args.conjunction )
removeBlanks = false,
    for i, v in ipairs( args ) do
wrappers = 'Template:Pagelist'
        table.insert( t, generateLink(
})
            v, args.nspace, args.delim, args.edelim
 
        ) )
-- Process integer args. Allow for explicit positional arguments that are
    end
-- specified out of order, e.g. {{br separated entries|3=entry3}}.
    return mw.text.listToText( t, separator, conjunction )
-- After processing, the args can be accessed accurately from ipairs.
local args = {}
for k, v in pairs(origArgs) do
if type(k) == 'number' and
k >= 1 and
math.floor(k) == k and
string.match(v, '%S') then -- Remove blank or whitespace values.
table.insert(args, k)
end
end
table.sort(args)
for i, v in ipairs(args) do
args[i] = origArgs[v]
-- Trim whitespace.
if type(args[i]) == 'string' then
args[i] = mw.text.trim(args[i])
end
end
 
-- Get old named args. We don't need to remove blank values
-- as for the nspace and edelim parameters the behaviour is different
-- depending on whether the parameters are blank or absent, and for
-- the delim parameter the default should be the blank string anyway.
args.delim = origArgs.delim
args.edelim = origArgs.edelim
args.nspace = origArgs.nspace
 
-- Get new named args, "separator" and "conjunction", and strip blank values.
if origArgs.separator and origArgs.separator ~= '' then
args.separator = origArgs.separator
end
if origArgs.conjunction and origArgs.conjunction ~= '' then
args.conjunction = origArgs.conjunction
end
 
return p._main(args)
end
end


function p.main( frame )
    local origArgs
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. If the invoking template passed any arguments,
        -- use them. Otherwise, use the arguments that were passed into the template.
        origArgs = frame:getParent().args
        for k, v in pairs( frame.args ) do
            origArgs = frame.args
            break
        end
    else       
        -- We're being called from another module or from the debug console, so assume
        -- the arguments are passed in directly.
        origArgs = frame
    end
   
    -- Process integer args. Allow for explicit positional arguments that are
    -- specified out of order, e.g. {{br separated entries|3=entry3}}.
    -- After processing, the args can be accessed accurately from ipairs.
    local args = {}
    for k, v in pairs( origArgs ) do
        if type( k ) == 'number' and
            k >= 1 and
            math.floor( k ) == k and
            mw.ustring.match( v, '%S' ) then -- Remove blank or whitespace values.
            table.insert( args, k )
        end
    end
    table.sort( args )
    for i, v in ipairs( args ) do
        args[ i ] = origArgs[ v ]
    end
   
    -- Get old named args. We don't need to remove blank values
    -- as for the nspace and edelim parameters the behaviour is different
    -- depending on whether the parameters are blank or absent, and for
    -- the delim parameter the default should be the blank string anyway.
    args.delim = origArgs.delim
    args.edelim = origArgs.edelim
    args.nspace = origArgs.nspace
   
    -- Get new named args, "separator" and "conjunction", and strip blank values.
    if origArgs.separator and origArgs.separator ~= '' then
        args.separator = origArgs.separator
    end
    if origArgs.conjunction and origArgs.conjunction ~= '' then
        args.conjunction = origArgs.conjunction
    end
    return _main( args )
end
return p
return p

Latest revision as of 04:46, 3 September 2020

Documentation for this module may be created at Module:Pagelist/doc

local p = {}

local separators = {
	dot = true,
	pipe = true,
	comma = true,
	['tpt-languages'] = true
}

local function getSeparator(sep)
	if type(sep) ~= 'string' then
		return nil
	end
	if separators[sep] then
		return mw.message.new(sep .. '-separator'):plain()
	else
		return sep
	end
end

local function generateLink(page, nspace, delim, endDelim)
	if not page then
		return nil
	end
	local pagename = mw.title.new(page)
	if not pagename then
		-- Default to the args we were passed if our page
		-- object was nil.
		pagename = page
	else
		pagename = pagename.text
	end
	delim = delim or ''
	endDelim = endDelim or delim
	nspace = nspace or ''
	if nspace == 'all' then
		nspace = ''
		pagename = page
	end
	local outStr = mw.ustring.gsub( 
		string.format( 
			'%s[[:%s:%s|%s]]%s',
			delim, nspace, pagename, page, endDelim
		),
		 ':+',
		 ':' 
	)
	return outStr
end

function p._main(args)
	local t = {}
	local separator = getSeparator(args.separator)
	local conjunction = getSeparator(args.conjunction)
	for i, v in ipairs(args) do
		table.insert(t, generateLink(
			v, args.nspace, args.delim, args.edelim
		))
	end
	return mw.text.listToText(t, separator, conjunction)
end

function p.main(frame)
	local origArgs = require('Module:Arguments').getArgs(frame, {
		trim = false,
		removeBlanks = false,
		wrappers = 'Template:Pagelist'
	})

	-- Process integer args. Allow for explicit positional arguments that are
	-- specified out of order, e.g. {{br separated entries|3=entry3}}.
	-- After processing, the args can be accessed accurately from ipairs.
	local args = {}
	for k, v in pairs(origArgs) do
		if type(k) == 'number' and 
			k >= 1 and
			math.floor(k) == k and
			string.match(v, '%S') then -- Remove blank or whitespace values.
			table.insert(args, k)
		end
	end
	table.sort(args)
	for i, v in ipairs(args) do
		args[i] = origArgs[v]
		-- Trim whitespace.
		if type(args[i]) == 'string' then
			args[i] = mw.text.trim(args[i])
		end
	end

	-- Get old named args. We don't need to remove blank values
	-- as for the nspace and edelim parameters the behaviour is different
	-- depending on whether the parameters are blank or absent, and for
	-- the delim parameter the default should be the blank string anyway.
	args.delim = origArgs.delim
	args.edelim = origArgs.edelim
	args.nspace = origArgs.nspace

	-- Get new named args, "separator" and "conjunction", and strip blank values.
	if origArgs.separator and origArgs.separator ~= '' then
		args.separator = origArgs.separator
	end
	if origArgs.conjunction and origArgs.conjunction ~= '' then
		args.conjunction = origArgs.conjunction
	end

	return p._main(args)
end

return p