Module:links: Difference between revisions

From Laenkea
Jump to navigation Jump to search
No edit summary
No edit summary
Line 12: Line 12:
local text = args["text"]
local text = args["text"]
local pos = args["pos"]
local pos = args["pos"]
local noname = args["noname"]
local language = require("Module:languages").get_by_code(args["code"])
local language = require("Module:languages").get_by_code(args["code"])
local link = ""
local link = ""
if link_type == "mention" then
if link_type == "mention" and (not noname) then
if type(language.link) == "string" then
if type(language.link) == "string" then
link = link .. "[[" .. language.link .. "|" .. language.name .. "]] "
link = link .. "[[" .. language.link .. "|" .. language.name .. "]] "
Line 57: Line 58:
["display"] = args[3] or args["d"] or args["display"],
["display"] = args[3] or args["d"] or args["display"],
["text"] = args[4] or args["t"] or args["text"],
["text"] = args[4] or args["t"] or args["text"],
["pos"] = args["pos"]
["pos"] = args["pos"],
["noname"] = args["noname"]
}
}
end
end

Revision as of 19:54, 5 August 2023

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 noname = args["noname"]
	local language = require("Module:languages").get_by_code(args["code"])
	local link = ""
	if link_type == "mention" and (not noname) 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"],
		["noname"] = args["noname"]
	}
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