Module:links

Revision as of 19:41, 5 August 2023 by Maria (talk | contribs)

Documentation for this module may be created at Module:links/documentation

local export = {}

function export.make_link(args)
	if args["link_type"] ~= "mention" and args["link_type"] ~= "list" then
		error("Link type must be specified")
	end
	if not args["code"]  then
		error("Language code must be specified")
	end
	local word = args["word"]
	local display = args["display"]
	local text = args["text"]
	local pos = args["pos"]
	local language = require("Module:languages").get_by_code(args["code"])
	local link = ""
	if link_type == "mention" then
		if type(language.link) == "string" then
			link = link .. "[[" .. language.link .. "|" .. language.name .. "]] "
		else
			link = link .. language.name .. " "
		end
	end
	local link_format = (link_type == "mention" and "''") or ""
	local link_proto = (language.proto and ("Appendix:" .. string.gsub(language.name, " ", "_") .. "/")) or ""
	local link_display_proto = (language.proto and "*") or ""
	local link_display = display or word
	link = link .. link_format .. "[[" .. link_proto .. word .. "#" .. string.gsub(language.name, " ", "_") .. "|" .. link_display_proto .. link_display .. "]]" .. link_format
	if type(text) == "string" or type(pos) == "string" then
		link = link .. " ("
		if type(text) == "string" then
			link = link .. "“" .. text .. "”" .. ((type(pos) == "string" and ", ") or "")
		end
		if type(pos) == "string" then
			link = link .. pos
		end
		link = link .. ")"
	end
	return link
end

function export.make_list_link(args)
	args["link_type"] = "list"
	return export.make_link(args)
end

function export.make_mention_link(args)
	args["link_type"] = "mention"
	return export.make_link(args, "mention")
end

local function make_from_frame(frame, link_type)
	local args = (frame:getParent() and frame:getParent().args) or frame.args
	return export.make_link{
		["link_type"] = link_type,
		["code"] = args[1],
		["word"] = args[2],
		["display"] = args[3] or args["d"] or args["display"],
		["text"] = args[4] or args["t"] or args["text"],
		["pos"] = args["pos"]
	}
end

function export.list(frame)
	return make_from_frame(frame, "list")
end

function export.mention(frame)
	return make_from_frame(frame, "mention")
end

return export