Module:Catalog lookup link/sandbox: Difference between revisions

Content deleted Content added
positional url-access parameters
sync from live;
Line 16:
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.)
 
 
new parameters that support access icons:
|allowed_icons= – comma-separated list of keywords: free, limited, registration, subscription, none, all (default; 'all' implied when this parameter empty or omitted)
the icons specified in the following parameters are checked agains the list in |allowed-icons=; not in the list? not displayed
|url-access-all= – applies specified icon to all items in the list; accepted keywords: free, limited, registration, subscription;
|url-accessn= – applies specified icon to item n of the list (the nth positional parameter); accepted keywords: free, limited, registration, subscription;
 
]]
Line 21 ⟶ 28:
require('Module:No globals');
local getArgs = require ('Module:Arguments').getArgs;
local lock_icons = { --icon classes are defined in Module:Citation/CS1/styles.css
['free'] = {'[[File:Lockcs1-green.svg|9px|link=|alt=Freelylock-free', accessible|'Freely accessible]]'},
['registration'] = {'[[File:Lockcs1-bluelock-alt-2.svg|9px|link=|alt=Free registration', required|'Free registration required]]'},
['limited'] = {'[[File:Lockcs1-bluelock-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:Lockcs1-redlock-alt.svg|9px|link=|alt=Paid subscription', required|'Paid subscription required]]'},
}
 
local p = {};
 
 
Line 38 ⟶ 43:
 
local function is_set( var )
return not (var and== nil or mw.ustring.match(var, == '%S')) and true or false;
end
 
Line 65 ⟶ 70:
return table.concat ({label, postfix, ' '}); -- assemble the complete label
else
return postfix or ''; -- no space after postfix if no label
end
end
 
 
--[[--------------------------< I C O N _ I N D E X _ G E T >--------------------------------------------------
 
returns index into lock_icons[] if value assigned to |url-access= or |url-access-all= is a valid icon selector
(free, limited, registration, subscription)
 
icon selection may be limited to a subset of the icons with:
|allow_icons=<comma-separated list of allowed icons>
<comma-separated list of allowed icons> may be any of the keywords: free, limited, registration, subscription, none, all
 
keyword 'all' is default condition; 'all' is implied when |allowed=icons= is empty or omitted
 
keyword 'none' for use with identifiers where icons are inappropriate (isbn, issn, oclc)
 
Templates using this module should set:
|allow_icons=free for most identifiers;
|allow_icons=none for isbn, issn, oclc, etc
 
|url-access= is alias of |url-access1=
 
]]
 
local function icon_index_get (args, k)
local p = {}icon;
local param_name = (1 == k and is_set (args['url-access']) and 'url-access') or table.concat ({'url-access', k}); -- make an enumerated parameter name
 
if is_set (args['url-access-all']) and lock_icons[args['url-access-all']] then -- if set and valid
icon = args['url-access-all']; -- tentatively
 
elseif is_set (args[param_name]) and lock_icons[args[param_name]] then -- if set and valid
icon = args[param_name]; -- tentatively
 
else
return nil; -- neither |url-access-all= nor |url-accessn= set so return nil
end
 
if args['allow_icons'] and args['allow_icons']:find ('none') then -- if 'none' keyword is present
return nil; -- icons display not allowed
end
 
if not is_set (args['allow_icons']) or args['allow_icons']:find ('all') or args['allow_icons']:find (icon) then --if all allowed or specified icon is allowed
return icon; -- return selected icon as index into icon table
end
end
Line 72 ⟶ 122:
--[[--------------------------< M A I N >----------------------------------------------------------------------
 
Template entrypoint to this module; arguments come primarily from the parent frame though in templates that use
this module, |allowed-icons= is typically set, if needed, in the {{#invoke:}}.
 
]]
 
local function p.main (frame)
local args = getArgs (frame);
local out_text = '';
Line 90 ⟶ 142:
local list_separator = args['list-separator'] or ', ';
local leadout_postfix = args['leadout-postfix'] or ' ';
local access_index;
local list_leadout;
 
local access_indexicon_index;
if is_set (args['list-leadout']) then
list_leadout = table.concat ({
mw.ustring.match(mw.ustring.subgsub (args['list-leadout'],1,1 '^(%a)', '[ %a]1'), -- andinsert 'leading 'space orif '',first character is a letter
args['list-leadout'],
leadout_postfix,
});
Line 104 ⟶ 156:
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
Line 119 ⟶ 171:
']' -- close ext link markup
});
 
icon_index = icon_index_get (args, k); -- set if icon specified and allowed for this item; nil else
access_index = table.concat ({'url-access', k}); -- make an enumerated index
if access_iconicon_index then
local access_icon;
if is_set (args[access_index]) then -- if set
access_icon = lock_icons[args[access_index]]
end
if not access_icon then -- if lock_icons[access_icon]not set
access_icon = lock_icons[args['url-access']] -- use default
end
if access_icon then
item = table.concat ({ -- add access icon markup to this item
'<span class="plainlinks">', -- thisopen the opening span tag; icon classes are linkdefined isin plainModule:Citation/CS1/styles.css
access_iconlock_icons[icon_index][1], -- add the appropriate lock icon class
'</span>" title="', -- and close the spanstitle attribute
lock_icons[icon_index][2], -- for an appropriate tool tip
'">', -- close the opening span tag
item,
'</span style="padding-left:0.15em;">', -- and replacedclose withthe span
access_icon, -- the appropriate icon
'</span>', -- and close the spans
'</span>'
});
end
Line 160 ⟶ 206:
end
 
return p{main = main};