Module:User:Victar/links-list

This module needs documentation.
Please document this module by describing its purpose and usage on the documentation page.

local export = {}
local m_languages = require("Module:languages")

function export.show(frame)

	local list_with_holes = { list = true, allow_holes = true }
	local params = {
		["columns"] = {type = "number"},
		["inline"] = {type = "boolean", default = false},
		["limit"] = {type = "number"}
	}
	
	local frame_args = require("Module:parameters").process(frame.args, params)
	
	params = {
		[1] = { required = true, default = "en" },
		[2] = { required = true, list = true, allow_holes = true },
		["accel"] = {},
		["alt"] = list_with_holes,
		["g"] = list_with_holes,
		["t"] = list_with_holes,
		["id"] = list_with_holes,
		["lit"] = list_with_holes,
		["pos"] = list_with_holes,
		["gloss"] = { alias_of = "t" },
		["tr"] = list_with_holes,
		["ts"] = list_with_holes,
		["sc"] = {},
		["columns"] = {type = "number"},
		["inline"] = {type = "boolean", default = false},
		["limit"] = {type = "number"}
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local terms
	if args[2].maxindex > 0 then
		terms = args[2]
	else
		terms = {"beer and skittles", "beer belly", "beer-bust", "beer can", "beered-up", "beer garden", "beer goggles", "beer gut", "beer hall", "beerily", "beerish", "beerless", "beer mat", "beer muscles", "beer o'clock", "beer parlour", "beerstone", "beery", "bock beer", "champagne taste on a beer budget", "craft beer", "cry in one's beer", "ginger beer", "keg beer", "ice beer", "near beer", "root beer", "small beer", "spruce beer"}
		terms.maxindex = #terms
	end
	local sc = args["sc"]
	
	local columns = frame_args["columns"] or args["columns"]
	local inline = frame_args["inline"] or args["inline"]
	local limit =  frame_args["limit"] or args["limit"]
	
	local lang = args[1]
	lang = m_languages.getByCode(lang) or m_languages.err(lang, "lang")
	
	if sc then
		sc = require("Module:scripts").getByCode(sc) or error("The script code \"" .. sc .. "\" is not valid.")
	end
	
	local links = {}
	local maxindex = {}
	
	if limit then
		maxindex = limit
	else
		maxindex = math.max(terms.maxindex, args.tr.maxindex, args.ts.maxindex)
	end
	
	local link_data = {}
	for i = 1, maxindex do
		link_data[i] = {
			lang = lang,
			sc = sc,
			term = terms[i],
			accel = args.accel,
			alt = args.alt[i],
			g = args.g[i],
			id = args.id[i],
			lit = args.lit[i],
			pos = args.pos[i],
			gloss = args.t[i],
			tr = args.tr[i],
			ts = args.ts[i],
		}
	end
	
	-- Assume no non-BMP characters for now.
	local compare = require "Module:collation".make_compare_func(lang)
	table.sort(link_data,
		function (a, b)
			if a.term and b.term then
				return compare(a.term, b.term)
			else
				return a.term ~= nil
			end
		end)
	
	local links = {}
	local full_link = require "Module:links".full_link
	for i = 1, maxindex do
		links[i] = full_link(link_data[i])
	end
	
	local class = {}
	
	if inline then
		columns = false
		table.insert(class, "horizontal")
	end
	
	if columns == 2 then
		table.insert(class, "column-count-2")
	elseif columns == 3 then
		table.insert(class, "column-count-3")
	end
		
	local links = table.concat(links, "</li>\n<li>")
	
	local class = table.concat(class, " ")
	
	return "<ul class=\"links-list " .. class .. "\">\n<li>" .. links .. "</li>\n</ul>"
	
end

return export