Difference between revisions of "Module:Sandbox/Ahecht/Catalog lookup link"

From blackwiki
Jump to navigation Jump to search
blackwiki>Ahecht
(Created page with 'local p = {} function p.main(frame) local args = {} -- discard empty parameters and trim whitespace for k, v in frame:getParent().args do if v and mw.ust...')
 
blackwiki>Ahecht
(add lock icon)
Line 3: Line 3:
 
function p.main(frame)
 
function p.main(frame)
 
local args = {}
 
local args = {}
 +
local out = ''
 +
local defaultSeparator = ', '
 +
local lockIcon = '<span style="padding-left: 0.15em;">[[File:Lock-green.svg|9px|link=|alt=Freely accessible|Freely accessible]]</span>'
 
 
 
-- discard empty parameters and trim whitespace
 
-- discard empty parameters and trim whitespace
Line 9: Line 12:
 
end
 
end
 
 
local output = ''
 
 
if args[1] then
 
if args[1] then
 
local i = 1
 
local i = 1
Line 17: Line 19:
 
if args['article-link'] or args['article-name'] then
 
if args['article-link'] or args['article-name'] then
 
if args['article-link'] then
 
if args['article-link'] then
output = '[[' .. args['article-link'] .. '|' .. (args['article-name'] or args['article-link']) .. ']]'
+
out = '[[' .. args['article-link'] .. '|' .. (args['article-name'] or args['article-link']) .. ']]'
 
else
 
else
output = args['article-name']
+
out = args['article-name']
 
end
 
end
output = output .. (args['article-postfix'] or '') .. ' '
+
out = out .. (args['article-postfix'] or '') .. ' '
 
else
 
else
output = (args['article-postfix'] or '')
+
out = (args['article-postfix'] or '')
 
end
 
end
 
else -- otherwise, add separator
 
else -- otherwise, add separator
 
if ( (not args[i+1]) and args['list-leadout'] ) then -- if last item
 
if ( (not args[i+1]) and args['list-leadout'] ) then -- if last item
 
if mw.ustring.match(mw.ustring.sub(args['list-leadout'],1,1), '[%a]') then
 
if mw.ustring.match(mw.ustring.sub(args['list-leadout'],1,1), '[%a]') then
output = output .. ' '
+
out = out .. ' '
 
end
 
end
output = output .. args['list-leadout'] .. args['leadout-postfix'] .. ' '
+
out = out .. args['list-leadout'] .. args['leadout-postfix'] .. ' '
 
else -- if not last item or list-leadout isn't specified
 
else -- if not last item or list-leadout isn't specified
output = output .. (args['list-separator'] or ', ')
+
out = out .. (args['list-separator'] or defaultSeparator)
 
end
 
end
 
end
 
end
Line 40: Line 42:
 
if args['link-prefix'] then
 
if args['link-prefix'] then
 
item = '[' .. args['link-prefix'] .. mw.uri.encode(args[i]) .. (args['link-postfix'] or '') .. ' '.. item .. ']'
 
item = '[' .. args['link-prefix'] .. mw.uri.encode(args[i]) .. (args['link-postfix'] or '') .. ' '.. item .. ']'
 +
if args['lock-icon'] then
 +
item = item .. lockIcon
 +
end
 
end
 
end
output = output .. item
+
out = out .. item
 
i = i + 1
 
i = i + 1
 
end
 
end
 
end
 
end
 
 
return output
+
if args['lock-icon'] or args['plain-links'] then
 +
out = '<span class="plainlinks">' .. out .. '</span>'
 +
end
 +
 +
return out
 
end
 
end
  
 
return p
 
return p

Revision as of 23:36, 16 July 2018

Documentation for this module may be created at Module:Sandbox/Ahecht/Catalog lookup link/doc

local p = {}

function p.main(frame)
	local args = {}
	local out = ''
	local defaultSeparator = ', '
	local lockIcon = '<span style="padding-left: 0.15em;">[[File:Lock-green.svg|9px|link=|alt=Freely accessible|Freely accessible]]</span>'
	
	-- discard empty parameters and trim whitespace
	for k, v in frame:getParent().args do
		if v and mw.ustring.match(v,'%S') then args[k] = mw.text.trim(v) end
	end
	
	if args[1] then
		local i = 1
		while args[i] do
			
			if i == 1 then -- if first item, add article link
				if args['article-link'] or args['article-name'] then
					if args['article-link'] then
						out = '[[' .. args['article-link'] .. '|' .. (args['article-name'] or args['article-link']) .. ']]'
					else
						out = args['article-name']
					end
					out = out .. (args['article-postfix'] or '') .. ' '
				else
					out = (args['article-postfix'] or '')
				end
			else -- otherwise, add separator
				if ( (not args[i+1]) and args['list-leadout'] ) then -- if last item
					if mw.ustring.match(mw.ustring.sub(args['list-leadout'],1,1), '[%a]') then
						out = out .. ' '
					end
					out = out .. args['list-leadout'] .. args['leadout-postfix'] .. ' '
				else -- if not last item or list-leadout isn't specified
					out = out .. (args['list-separator'] or defaultSeparator)
				end
			end
			
			-- generate link
			local item = (args['item-prefix'] or '') .. args[i] .. (args['item-postfix'] or '')
			if args['link-prefix'] then
				item = '[' .. args['link-prefix'] .. mw.uri.encode(args[i]) .. (args['link-postfix'] or '') .. ' '.. item .. ']'
				if args['lock-icon'] then
					item = item .. lockIcon
				end
			end
			out = out .. item
			i = i + 1
		end
	end
	
	if args['lock-icon'] or args['plain-links'] then
		out = '<span class="plainlinks">' .. out .. '</span>'
	end
	
	return out
end

return p