Module:Catalog lookup link: Difference between revisions
From Zoophilia Wiki
Jump to navigationJump to search
meta>Ahecht Copy from sandbox. Various formatting improvements to match existing template. |
meta>Trappist the monk enumerated access icon parameter support |
||
Line 21: | Line 21: | ||
require('Module:No globals'); | require('Module:No globals'); | ||
local getArgs = require ('Module:Arguments').getArgs; | local getArgs = require ('Module:Arguments').getArgs; | ||
local lock_icons = { | |||
['free'] = '[[File:Lock-green.svg|9px|link=|alt=Freely accessible|Freely accessible]]', | |||
['registration'] = '[[File:Lock-blue-alt-2.svg|9px|link=|alt=Free registration required|Free registration required]]', | |||
['limited'] = '[[File:Lock-blue-alt-2.svg|9px|link=|alt=Free access subject to limited trial, subscription normally required|Free access subject to limited trial, subscription normally required]]', | |||
['subscription'] = '[[File:Lock-red-alt.svg|9px|link=|alt=Paid subscription required|Paid subscription required]]', | |||
} | |||
local p = {}; | local p = {}; | ||
Line 36: | Line 42: | ||
--[=[-------------------------< M A K E _ | --[=[-------------------------< M A K E _ L A B E L >---------------------------------------------------------- | ||
Makes a | Makes a wikilinked or plain text label from arguments; when both link and display text is provided, makes a | ||
link is provided, | wikilink in the form [[L|D]]; if only link is provided, makes a wikilinked label in the form [[L]]; if only display | ||
empty string. | is provided, makes a plain-text label; if neither are provided makes a label from postfix, returns an empty string else. | ||
]=] | ]=] | ||
local function | local function make_label (link, display, postfix) | ||
local label = ''; | |||
if is_set (link) then | if is_set (link) then | ||
if is_set (display) then | if is_set (display) then | ||
label = table.concat ({'[[', link, '|', display, ']]'}); -- make [[L|D]] wikilinked label | |||
else | else | ||
label = table.concat ({'[[', link, ']]'}); -- make [[L]] wikilinked label | |||
end | end | ||
elseif is_set (display) then | elseif is_set (display) then | ||
return | label = display; -- plain-text label | ||
end | |||
if is_set (label) or is_set (postfix) then -- label can be empty string here | |||
return table.concat ({label, postfix, ' '}); -- assemble the complete label | |||
else | else | ||
return '' | return ''; -- no inputs so return empty string for concatenation | ||
end | end | ||
end | end | ||
Line 68: | Line 78: | ||
local args = getArgs (frame); | local args = getArgs (frame); | ||
local out_text = ''; | local out_text = ''; | ||
if is_set(args[1]) then | if is_set(args[1]) then | ||
local result = {}; | local result = {}; | ||
local | local label; | ||
local article_postfix = args['article-postfix'] or ''; | local article_postfix = args['article-postfix'] or ''; | ||
Line 80: | Line 90: | ||
local list_separator = args['list-separator'] or ', '; | local list_separator = args['list-separator'] or ', '; | ||
local leadout_postfix = args['leadout-postfix'] or ' '; | local leadout_postfix = args['leadout-postfix'] or ' '; | ||
local list_leadout; | local list_leadout; | ||
local access_index; | |||
if is_set (args['list-leadout']) then | if is_set (args['list-leadout']) then | ||
Line 93: | Line 104: | ||
end | end | ||
label = make_label (args['article-link'], args['article-name'], article_postfix); | |||
for k, item in ipairs (args) do -- for each of the positional parameters | for k, item in ipairs (args) do -- for each of the positional parameters | ||
item = mw.text.trim (item); -- remove extraneous whitespace | item = mw.text.trim (item); -- remove extraneous whitespace | ||
Line 114: | Line 120: | ||
']' -- close ext link markup | ']' -- close ext link markup | ||
}); | }); | ||
access_index = table.concat ({'url-access', k}); -- make an enumerated index | |||
if is_set (args[access_index]) and lock_icons[args[access_index]] then -- if set and valid | |||
item = table.concat ({ -- add access icon markup to this item | |||
'<span class="plainlinks">', -- this link is plain | |||
item, | |||
'<span style="padding-left:0.15em;">', -- and replaced with | |||
lock_icons[args[access_index]], -- the appropriate icon | |||
'</span>', -- and close the spans | |||
'</span>' | |||
}); | |||
end | |||
else | else | ||
item = table.concat ({ -- create an unlinked item | item = table.concat ({ -- create an unlinked item | ||
Line 127: | Line 144: | ||
if is_set (args['list-leadout']) then | if is_set (args['list-leadout']) then | ||
out_text = table.concat ({ | out_text = table.concat ({label, mw.text.listToText (result, list_separator, list_leadout)}); | ||
else | else | ||
out_text = table.concat ({ | out_text = table.concat ({label, table.concat (result, list_separator)}); | ||
end | end | ||
end | |||
end --is_set (args[1]) | |||
return out_text | return out_text |
Revision as of 12:26, 18 July 2018
Documentation for this module may be created at Module:Catalog lookup link/doc
--[[
|1=, |2=, |3=, |4=, |5=, |6=, |7=, |8=, |9=: Optional unnamed parameters for 0 to 9 items to be listed.
Whitespace is trimmed off both ends and the strings are urlencoded as if they were query strings.
|article-link=: Optional Wikipedia article name to link to.
|article-name=: Optional alternative text to be displayed for |article-link= link in front of catalog link.
If not specified, |article-link= is used for display as well. If both parameters are not specified, the prefix is omitted completely.
|article-postfix=: Optional symbol to be displayed after article name or link (f.e. ":"; omitted, if not defined).
|link-prefix=: Optional prefix portion of url to external catalog item(s).
|link-postfix=: Optional postfix portion of url to external catalog item(s).
|item-prefix=: Optional text displayed in front of each external link (omitted, if not defined)
|item-postfix=: Optional text displayed immediately after each external link (omitted, if not defined)
|list-separator=: Optional alternative separator displayed between list items (default: ", ", if not specified). Whitespace must be encoded.
|list-leadout=: Optional alternative leadout text displayed between the last two list items (f.e. "and", "or", "as well as", etc., default is the |list-separator= or ", ".)
|leadout-postfix=: Optional alternative postfix text of the leadout (see |list-leadout=) displayed between the last two list items.
This gets added in front of the last list item instead of the default whitespace which is added without this parameter.
This may be necessary if |list-separator= is used not only to define the list separator but also parts of the item prefix
(except for the first one). (At present, this is used only to cope with format oddities of the {{MR}} template.)
]]
require('Module:No globals');
local getArgs = require ('Module:Arguments').getArgs;
local lock_icons = {
['free'] = '[[File:Lock-green.svg|9px|link=|alt=Freely accessible|Freely accessible]]',
['registration'] = '[[File:Lock-blue-alt-2.svg|9px|link=|alt=Free registration required|Free registration required]]',
['limited'] = '[[File:Lock-blue-alt-2.svg|9px|link=|alt=Free access subject to limited trial, subscription normally required|Free access subject to limited trial, subscription normally required]]',
['subscription'] = '[[File:Lock-red-alt.svg|9px|link=|alt=Paid subscription required|Paid subscription required]]',
}
local p = {};
--[[--------------------------< I S _ S E T >------------------------------------------------------------------
Returns true if argument is set; false otherwise. Argument is 'set' when it exists (not nil) or when it is not an empty string.
]]
local function is_set( var )
return (var and mw.ustring.match(var,'%S')) and true or false;
end
--[=[-------------------------< M A K E _ L A B E L >----------------------------------------------------------
Makes a wikilinked or plain text label from arguments; when both link and display text is provided, makes a
wikilink in the form [[L|D]]; if only link is provided, makes a wikilinked label in the form [[L]]; if only display
is provided, makes a plain-text label; if neither are provided makes a label from postfix, returns an empty string else.
]=]
local function make_label (link, display, postfix)
local label = '';
if is_set (link) then
if is_set (display) then
label = table.concat ({'[[', link, '|', display, ']]'}); -- make [[L|D]] wikilinked label
else
label = table.concat ({'[[', link, ']]'}); -- make [[L]] wikilinked label
end
elseif is_set (display) then
label = display; -- plain-text label
end
if is_set (label) or is_set (postfix) then -- label can be empty string here
return table.concat ({label, postfix, ' '}); -- assemble the complete label
else
return ''; -- no inputs so return empty string for concatenation
end
end
--[[--------------------------< M A I N >----------------------------------------------------------------------
]]
function p.main (frame)
local args = getArgs (frame);
local out_text = '';
if is_set(args[1]) then
local result = {};
local label;
local article_postfix = args['article-postfix'] or '';
local link_prefix = args['link-prefix'] or '';
local link_postfix = args['link-postfix'] or '';
local item_prefix = args['item-prefix'] or '';
local item_postfix = args['item-postfix'] or '';
local list_separator = args['list-separator'] or ', ';
local leadout_postfix = args['leadout-postfix'] or ' ';
local list_leadout;
local access_index;
if is_set (args['list-leadout']) then
list_leadout = table.concat ({
mw.ustring.match(mw.ustring.sub(args['list-leadout'],1,1), '[%a]') and ' ' or '',
args['list-leadout'],
leadout_postfix,
});
else
list_leadout = '';
end
label = make_label (args['article-link'], args['article-name'], article_postfix);
for k, item in ipairs (args) do -- for each of the positional parameters
item = mw.text.trim (item); -- remove extraneous whitespace
if is_set (link_prefix) then -- if there is link prefix...
item = table.concat ({ -- create an external link item
'[', -- open ext link markup
link_prefix, -- url prefix
mw.uri.encode (item), -- item is part of url
link_postfix, -- url postfix
' ', -- required space between url and label
item_prefix, -- label prefix
item, -- item as label
item_postfix, -- item postfix
']' -- close ext link markup
});
access_index = table.concat ({'url-access', k}); -- make an enumerated index
if is_set (args[access_index]) and lock_icons[args[access_index]] then -- if set and valid
item = table.concat ({ -- add access icon markup to this item
'<span class="plainlinks">', -- this link is plain
item,
'<span style="padding-left:0.15em;">', -- and replaced with
lock_icons[args[access_index]], -- the appropriate icon
'</span>', -- and close the spans
'</span>'
});
end
else
item = table.concat ({ -- create an unlinked item
item_prefix, -- label prefix
item, -- item as label
item_postfix, -- item postfix
});
end
table.insert (result, item); -- add the item to the result list
end
if is_set (args['list-leadout']) then
out_text = table.concat ({label, mw.text.listToText (result, list_separator, list_leadout)});
else
out_text = table.concat ({label, table.concat (result, list_separator)});
end
end --is_set (args[1])
return out_text
end
return p;