Module:links/templates: Difference between revisions

From Linguifex
Jump to navigation Jump to search
No edit summary
Tag: Manual revert
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- Prevent substitution.
if mw.isSubsting() then
return require("Module:unsubst")
end
local export = {}
local links_module = "Module:links"
local process_params = require("Module:parameters").process
local process_params = require("Module:parameters").process
local remove = table.remove
local remove = table.remove
local upper = require("Module:string utilities").upper
local upper = require("Module:string utilities").upper
local export = {}


--[=[
--[=[
Line 11: Line 17:
[[Module:scripts]]
[[Module:scripts]]
[[Module:parameters]]
[[Module:parameters]]
[[Module:debug]]
]=]
]=]


Line 24: Line 31:
local alias_of_4 = {alias_of = 4}
local alias_of_4 = {alias_of = 4}
local boolean = {type = "boolean"}
local boolean = {type = "boolean"}
local plain = {}
local params = {
local params = {
[1] = {required = true, type = "language", default = "und"},
[1] = {required = true, type = "language", default = "und"},
[2] = plain,
[2] = true,
[3] = plain,
[3] = true,
[4] = plain,
[4] = true,
["g"] = {list = true},
["g"] = {list = true},
["gloss"] = alias_of_4,
["gloss"] = alias_of_4,
["id"] = plain,
["id"] = true,
["lit"] = plain,
["lit"] = true,
["pos"] = plain,
["pos"] = true,
["sc"] = {type = "script"},
["sc"] = {type = "script"},
["t"] = alias_of_4,
["t"] = alias_of_4,
["tr"] = plain,
["tr"] = true,
["ts"] = plain,
["ts"] = true,
["accel-form"] = plain,
["accel-form"] = true,
["accel-translit"] = plain,
["accel-translit"] = true,
["accel-lemma"] = plain,
["accel-lemma"] = true,
["accel-lemma-translit"] = plain,
["accel-lemma-translit"] = true,
["accel-gender"] = plain,
["accel-gender"] = true,
["accel-nostore"] = boolean,
["accel-nostore"] = boolean,
["name"] = boolean,
}
}
if iargs.compat then
if iargs.compat then
Line 53: Line 58:
end
end
if iargs.langname then
if iargs.langname then
params["w"] = {type = "boolean", default = true}
params["w"] = boolean
end
end
return process_params(frame:getParent().args, params), iargs
return process_params(frame:getParent().args, params), iargs
end
end
-- Used in [[Template:l]] and [[Template:mn]].
-- Used in [[Template:l]] and [[Template:m]].
function export.l_term_t(frame)
function export.l_term_t(frame)
local args, iargs = get_args(frame)
local args, iargs = get_args(frame)
Line 72: Line 77:
end
end
local langname = (not args.name or iargs.langname) and (
local langname = iargs.langname and (
args.w and lang:makeWikipediaLink() or
args.w and lang:makeWikipediaLink() or
lang:getCanonicalName()
lang:getCanonicalName()
Line 81: Line 86:
end
end
-- Forward the information to full_link
-- Forward the information to full_link
return (langname and langname .. " " or "") .. require("Module:links").full_link(
return (langname and langname .. " " or "") .. require(links_module).full_link(
{
{
lang = lang,
lang = lang,
Line 95: Line 100:
pos = args["pos"],
pos = args["pos"],
lit = args["lit"],
lit = args["lit"],
to_wikt = iargs["to_wikt"],
accel = args["accel-form"] and {
accel = args["accel-form"] and {
form = args["accel-form"],
form = args["accel-form"],
Line 107: Line 111:
iargs.face,
iargs.face,
not iargs.notself
not iargs.notself
)
end
-- Used in [[Template:link-annotations]].
function export.l_annotations_t(frame)
local args, iargs = get_args(frame)
-- Forward the information to format_link_annotations
return require(links_module).format_link_annotations(
{
lang = args[1],
tr = { args["tr"] },
ts = { args["ts"] },
genders = args["g"],
pos = args["pos"],
lit = args["lit"]
},
iargs.face
)
)
end
end
Line 114: Line 136:
do
do
local function get_args(frame)
local function get_args(frame)
local plain = {}
return process_params(frame:getParent().args, {
return process_params(frame:getParent().args, {
[1] = {required = true, type = "language", default = "und"},
[1] = {required = true, type = "language", default = "und"},
[2] = {allow_empty = true},
[2] = {allow_empty = true},
[3] = plain,
[3] = true,
["id"] = plain,
["id"] = true,
["sc"] = {type = "script"},
["sc"] = {type = "script"},
})
})
Line 131: Line 152:
term = term ~= "" and term or nil
term = term ~= "" and term or nil
return require("Module:links").language_link{
return require(links_module).language_link{
lang = lang,
lang = lang,
sc = sc,
sc = sc,
Line 149: Line 170:
[1] = {required = true, default = ""},
[1] = {required = true, default = ""},
})
})
 
local lang = require("Module:languages").getByCode("en")
local face = frame.args.face or "non-gloss"
local sc = require("Module:scripts").getByCode("Latn")
 
local ret = require("Module:script utilities").tag_definition(require(links_module).embedded_language_links{
return require("Module:script utilities").tag_text(require("Module:links").embedded_language_links{
term = args[1],
term = args[1],
lang = lang,
lang = require("Module:languages").getByCode("en"),
sc = sc
sc = require("Module:scripts").getByCode("Latn")
}, lang, sc)
}, face)
 
if face == "non-gloss" then
return ret
end
return '<span class="mention-gloss-paren">(</span>' .. ret .. '<span class="mention-gloss-paren">)</span>'
end
end


Line 170: Line 195:
if args[1] == "" or args[1]:find("[[", nil, true) then
if args[1] == "" or args[1]:find("[[", nil, true) then
return args[1]
return args[1]
else
return "[[" .. args[1] .. "]]"
end
end
return "[[" .. args[1] .. "]]"
end
end


Line 178: Line 202:
local args = process_params(frame:getParent().args, {
local args = process_params(frame:getParent().args, {
[1] = {required = true},
[1] = {required = true},
[2] = true,
["lang"] = {type = "language", default = "en"},
["lang"] = {type = "language", default = "en"},
})
})
Line 183: Line 208:
local term = args[1]
local term = args[1]
return require("Module:links").full_link{
return require(links_module).full_link{
lang = args.lang,
lang = args.lang,
term = term,
term = term,
alt = term:gsub("^.[\128-\191]*", upper)
alt = term:gsub("^.[\128-\191]*", upper) .. (args[2] or "")
}
}
end
end
Line 195: Line 220:
})
})
return require("Module:links").section_link(args[1])
return require(links_module).section_link(args[1])
end
end


return export
return export

Latest revision as of 23:09, 1 July 2025

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

-- Prevent substitution.
if mw.isSubsting() then
	return require("Module:unsubst")
end

local export = {}

local links_module = "Module:links"
local process_params = require("Module:parameters").process
local remove = table.remove
local upper = require("Module:string utilities").upper

--[=[
	Modules used:
	[[Module:links]]
	[[Module:languages]]
	[[Module:scripts]]
	[[Module:parameters]]
	[[Module:debug]]
]=]

do
	local function get_args(frame)
		-- `compat` is a compatibility mode for {{term}}.
		-- If given a nonempty value, the function uses lang= to specify the
		-- language, and all the positional parameters shift one number lower.
		local iargs = frame.args
		iargs.compat = iargs.compat and iargs.compat ~= ""
		iargs.langname = iargs.langname and iargs.langname ~= ""
		iargs.notself = iargs.notself and iargs.notself ~= ""
		local alias_of_4 = {alias_of = 4}
		local boolean = {type = "boolean"}
		local params = {
			[1] = {required = true, type = "language", default = "und"},
			[2] = true,
			[3] = true,
			[4] = true,
			["g"] = {list = true},
			["gloss"] = alias_of_4,
			["id"] = true,
			["lit"] = true,
			["pos"] = true,
			["sc"] = {type = "script"},
			["t"] = alias_of_4,
			["tr"] = true,
			["ts"] = true,
			["accel-form"] = true,
			["accel-translit"] = true,
			["accel-lemma"] = true,
			["accel-lemma-translit"] = true,
			["accel-gender"] = true,
			["accel-nostore"] = boolean,
		}
		if iargs.compat then
			params["lang"] = {type = "language", default = "und"}
			remove(params, 1)
			alias_of_4.alias_of = 3
		end
		if iargs.langname then
			params["w"] = boolean
		end
		return process_params(frame:getParent().args, params), iargs
	end
	
	-- Used in [[Template:l]] and [[Template:m]].
	function export.l_term_t(frame)
		local args, iargs = get_args(frame)
		local compat = iargs.compat
		local lang = args[compat and "lang" or 1]
		
		local term = args[(compat and 1 or 2)]
		local alt = args[(compat and 2 or 3)]
		term = term ~= "" and term or nil
		
		if not term and not alt and iargs.demo then
			term = iargs.demo
		end
		
		local langname = iargs.langname and (
			args.w and lang:makeWikipediaLink() or
			lang:getCanonicalName()
		) or nil
		
		if langname and term == "-" then
			return langname
		end
		-- Forward the information to full_link
		return (langname and langname .. " " or "") .. require(links_module).full_link(
			{
				lang = lang,
				sc = args.sc,
				track_sc = true,
				term = term,
				alt = alt,
				gloss = args[4],
				id = args["id"],
				tr = args["tr"],
				ts = args["ts"],
				genders = args["g"],
				pos = args["pos"],
				lit = args["lit"],
				accel = args["accel-form"] and {
					form = args["accel-form"],
					translit = args["accel-translit"],
					lemma = args["accel-lemma"],
					lemma_translit = args["accel-lemma-translit"],
					gender = args["accel-gender"],
					nostore = args["accel-nostore"],
				} or nil
			},
			iargs.face,
			not iargs.notself
		)
	end
	
	-- Used in [[Template:link-annotations]].
	function export.l_annotations_t(frame)
		local args, iargs = get_args(frame)
		
		-- Forward the information to format_link_annotations
		return require(links_module).format_link_annotations(
			{
				lang = args[1],
				tr = { args["tr"] },
				ts = { args["ts"] },
				genders = args["g"],
				pos = args["pos"],
				lit = args["lit"]
			},
			iargs.face
		)
	end
end

-- Used in [[Template:ll]].
do
	local function get_args(frame)
		return process_params(frame:getParent().args, {
			[1] = {required = true, type = "language", default = "und"},
			[2] = {allow_empty = true},
			[3] = true,
			["id"] = true,
			["sc"] = {type = "script"},
		})
	end
	
	function export.ll(frame)
		local args = get_args(frame)
		local lang = args[1]
		local sc = args["sc"]
		local term = args[2]
		term = term ~= "" and term or nil
		
		return require(links_module).language_link{
			lang = lang,
			sc = sc,
			term = term,
			alt = args[3],
			id = args.id
		} or "<small>[Term?]</small>" ..
			require("Module:utilities").format_categories(
				{lang:getFullName() .. " term requests"},
				lang, "-", nil, nil, sc
			)
	end
end

function export.def_t(frame)
	local args = process_params(frame:getParent().args, {
		[1] = {required = true, default = ""},
	})

	local face = frame.args.face or "non-gloss"

	local ret = require("Module:script utilities").tag_definition(require(links_module).embedded_language_links{
		term = args[1],
		lang = require("Module:languages").getByCode("en"),
		sc = require("Module:scripts").getByCode("Latn")
	}, face)

	if face == "non-gloss" then
		return ret
	end
	return '<span class="mention-gloss-paren">(</span>' .. ret .. '<span class="mention-gloss-paren">)</span>'
end


function export.linkify_t(frame)
	local args = process_params(frame:getParent().args, {
		[1] = {required = true, default = ""},
	})
	
	args[1] = mw.text.trim(args[1])
	
	if args[1] == "" or args[1]:find("[[", nil, true) then
		return args[1]
	end
	return "[[" .. args[1] .. "]]"
end

function export.cap_t(frame)
	local args = process_params(frame:getParent().args, {
		[1] = {required = true},
		[2] = true,
		["lang"] = {type = "language", default = "en"},
	})
	
	local term = args[1]
	
	return require(links_module).full_link{
		lang = args.lang,
		term = term,
		alt = term:gsub("^.[\128-\191]*", upper) .. (args[2] or "")
	}
end

function export.section_link_t(frame)
	local args = process_params(frame:getParent().args, {
		[1] = {},
	})
	
	return require(links_module).section_link(args[1])
end

return export