Module:User:Erutuon/usex

local export = {}

local function remove(t, key) t.maxindex = (t.maxindex or #t) - 1 return table.remove(t, key) end

local tinsert = table.insert

local function insert(t, pos, val) local maxindex = t.maxindex or require("Module:table/sandbox").maxIndex(t) if val ~= nil or pos <= maxindex then t.maxindex = maxindex + 1 tinsert(t, pos, val) end end

function export.format_suffixusex(terms, lang, sc, alts, transliterations, glosses) local full_link = require("Module:links").full_link local links = {} for i = 1, math.max(terms.maxindex, alts.maxindex, glosses.maxindex) do		links[i] = full_link({ lang = lang, sc = sc, term = terms[i], alt = alts[i], gloss = glosses[i] }, "term") end local resulting_form = table.remove(links, #links) local lrm = sc:getDirection == "rtl" and "&lrm;" or "" return table.concat(links, " + " .. lrm) .. " &rarr; " .. lrm .. resulting_form end

function export.suffixusex(frame) local list_with_holes = { list = true, allow_holes = true } local params = { [1] = { list = true, required = true, allow_holes = true }, sc = {}, alt = list_with_holes, altsuf = {}, tr = list_with_holes, t = { list = true, allow_holes = true, alias_of = "gloss" }, gloss = list_with_holes, }	local args = require("Module:parameters").process(frame:getParent.args, params) local lang = remove(args[1], 1) local title = mw.title.getCurrentTitle local namespace = title.nsText if namespace == "Template" then lang = lang or "en" if not args[1][1] then args[1][1] = "write" args[1][2] = "-er" args[1][3] = "writer" args[1].maxindex = 3 end elseif namespace == "" or namespace == "Reconstruction" or namespace == "User" then -- suffix should title local pos = args[1].maxindex insert(args[1], pos, title.text) insert(args.alt, pos, args.altsuf) insert(args.gloss, pos, nil) insert(args.tr, pos, nil) end -- Check for a gap in the numbered parameters. local gaps for i = 1, args[1].maxindex do		if args[1][i] == nil then gaps = gaps or {} table.insert(gaps, i)		end end if gaps then local agreement = gaps[2] and "s " or " " error("Empty parameter" .. agreement			.. (gaps[2] and table.concat( -- Add 1 to gap number, because removing language parameter demoted all numbered parameters by 1.					require("Module:fun").map( function (gap) return gap + 1 end, gaps),					", ") or gaps[1] + 1)			.. ".") end lang = require("Module:languages").getByCode(lang) or require("Module:languages").err(lang, 1) local sc = args.sc sc = sc and (require("Module:scripts") or error("The script code " .. sc .. " is invalid")) or lang:findBestScript(			args[1][1] or error("Parameter 2 is required.")) return export.format_suffixusex(args[1], lang, sc, args.alt, args.tr, args.gloss) end

return export