Module:Sandbox/Ahecht/Catalog lookup link

This is an old revision of this page, as edited by Ahecht (talk | contribs) at 23:29, 16 July 2018 (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...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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.ustring.match(v,'%S') then args[k] = mw.text.trim(v) end
	end
	
	local output = ''
	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
						output = '[[' .. args['article-link'] .. '|' .. (args['article-name'] or args['article-link']) .. ']]'
					else
						output = args['article-name']
					end
					output = output .. (args['article-postfix'] or '') .. ' '
				else
					output = (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
						output = output .. ' '
					end
					output = output .. args['list-leadout'] .. args['leadout-postfix'] .. ' '
				else -- if not last item or list-leadout isn't specified
					output = output .. (args['list-separator'] or ', ')
				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 .. ']'
			end
			output = output .. item
			i = i + 1
		end
	end
	
	return output
end

return p