Module:links/templates: Difference between revisions

From Linguifex
Jump to navigation Jump to search
No edit summary
No edit summary
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
local process_params = require("Module:parameters").process
local remove = table.remove
local upper = require("Module:string utilities").upper
local export = {}
local export = {}


Line 9: Line 13:
]=]
]=]


-- Used in [[Template:l]] and [[Template:m]].
do
function export.l_term_t(frame)
local function get_args(frame)
local face = frame.args["face"]
-- `compat` is a compatibility mode for {{term}}.
local allowSelfLink = frame.args["notself"]; allowSelfLink = not allowSelfLink or allowSelfLink == ""
-- If given a nonempty value, the function uses lang= to specify the
-- language, and all the positional parameters shift one number lower.
local params = {
local iargs = frame.args
[1] = {required = true},
iargs.compat = iargs.compat and iargs.compat ~= ""
[2] = {},
iargs.langname = iargs.langname and iargs.langname ~= ""
[3] = {},
iargs.notself = iargs.notself and iargs.notself ~= ""
[4] = {alias_of = "gloss"},
local alias_of_4 = {alias_of = 4}
["accel-form"] = {},
local boolean = {type = "boolean"}
["accel-translit"] = {},
local plain = {}
["accel-lemma"] = {},
local params = {
["accel-lemma-translit"] = {},
[1] = {required = true, type = "language", default = "und"},
["accel-gender"] = {},
[2] = plain,
["accel-nostore"] = {type = "boolean"},
[3] = plain,
["g"] = {list = true},
[4] = plain,
["gloss"] = {},
["g"] = {list = true},
["id"] = {},
["gloss"] = alias_of_4,
["lit"] = {},
["id"] = plain,
["pos"] = {},
["lit"] = plain,
["t"] = {alias_of = "gloss"},
["pos"] = plain,
["tr"] = {},
["sc"] = {type = "script"},
["ts"] = {},
["t"] = alias_of_4,
["sc"] = {},
["tr"] = plain,
["w"] = {type = "boolean", default = true},
["ts"] = plain,
}
["accel-form"] = plain,
["accel-translit"] = plain,
-- Compatibility mode for {{term}}.
["accel-lemma"] = plain,
-- If given a nonempty value, the function uses lang= to specify the
["accel-lemma-translit"] = plain,
-- language, and all the positional parameters shift one number lower.
["accel-gender"] = plain,
local compat = (frame.args["compat"] or "") ~= ""
["accel-nostore"] = boolean,
["name"] = boolean,
if compat then
["to_wikt"] = boolean,
params["lang"] = {},
}
table.remove(params, 1)
if iargs.compat then
end
params["lang"] = {type = "language", default = "und"}
remove(params, 1)
local args = require("Module:parameters").process(frame:getParent().args, params)
alias_of_4.alias_of = 3
end
local lang = args[compat and "lang" or 1]
if iargs.langname then
params["w"] = {type = "boolean", default = true}
lang = lang or "und"
end
local sc = args["sc"]
return process_params(frame:getParent().args, params), iargs
local term = args[(compat and 1 or 2)]
local alt = args[(compat and 2 or 3)]
-- Check parameters
lang = require("Module:languages").getByCode(lang, 1, "allow etym")
lang = require("Module:languages").getNonEtymological(lang)
if sc then
sc = require("Module:scripts").getByCode(sc, "sc")
end
if not term and not alt and frame.args["demo"] then
term = frame.args["demo"]
end
end
local language_name = args.w and "[[" .. lang:getCanonicalName() .. "]]" or lang:getCanonicalName()
-- Used in [[Template:l]] and [[Template:mn]].
function export.l_term_t(frame)
if term == "-" then
local args, iargs = get_args(frame)
return language_name
local compat = iargs.compat
else
local lang = args[compat and "lang" or 1]
return language_name .. " " ..
require("Module:links").full_link(  
local term = args[(compat and 1 or 2)]
{
local alt = args[(compat and 2 or 3)]
lang = lang,  
term = term ~= "" and term or nil
sc = sc,  
term = term,
if not term and not alt and iargs.demo then
alt = alt,  
term = iargs.demo
id = args["id"],  
end
tr = args["tr"],
ts = args["ts"],
local langname = (not args.name or iargs.langname) and (
genders = args["g"],  
args.w and lang:makeWikipediaLink() or
gloss = args["gloss"],
lang:getCanonicalName()
pos = args["pos"],  
) or nil
lit = args["lit"],
nocont = not frame.args["from_l"],
if langname and term == "-" then
accel = args["accel-form"] and {
return langname
form = args["accel-form"],
end
translit = args["accel-translit"],
-- Forward the information to full_link
lemma = args["accel-lemma"],
return (langname and langname .. " " or "") .. require("Module:links").full_link(
lemma_translit = args["accel-lemma-translit"],
{
gender = args["accel-gender"],
lang = lang,
nostore = args["accel-nostore"],
sc = args.sc,
} or nil,
track_sc = true,
},
term = term,
face,
alt = alt,
allowSelfLink
gloss = args[4],
)
id = args["id"],
tr = args["tr"],
ts = args["ts"],
genders = args["g"],
pos = args["pos"],
lit = args["lit"],
to_wikt = args["to_wikt"],
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
end
end
end


-- Used in [[Template:ll]].
-- Used in [[Template:ll]].
function export.ll(frame)
do
local params = {
local function get_args(frame)
[1] = { required = true },
local plain = {}
[2] = { allow_empty = true },
return process_params(frame:getParent().args, {
[3] = {},
[1] = {required = true, type = "language", default = "und"},
["notself"] = { type = "boolean", default = false },
[2] = {allow_empty = true},
["id"] = {},
[3] = plain,
}
["id"] = plain,
local args = require("Module:parameters").process(frame:getParent().args, params)
["sc"] = {type = "script"},
local allowSelfLink = not args["notself"]
})
local lang = args[1]
lang = require("Module:languages").getByCode(lang, 1, "allow etym")
lang = require("Module:languages").getNonEtymological(lang)
 
local text = args[2]
local alt = args[3]
if text == "" then
return alt or ""
end
end
local id = args["id"]
function export.ll(frame)
local args = get_args(frame)
return require("Module:links").language_link(
local lang = args[1]
{
local sc = args["sc"]
term = text,
local term = args[2]
alt = alt,
term = term ~= "" and term or nil
return require("Module:links").language_link{
lang = lang,
lang = lang,
id = id
sc = sc,
},
term = term,
allowSelfLink
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
end


function export.def_t(frame)
function export.def_t(frame)
local params = {
local args = process_params(frame:getParent().args, {
[1] = {required = true, default = ""},
[1] = {required = true, default = ""},
}
})
local args = require("Module:parameters").process(frame:getParent().args, params)
local lang = require("Module:languages").getByCode("en")
local sc = require("Module:scripts").getByCode("Latn")
return require("Module:links").english_links(args[1])
return require("Module:script utilities").tag_text(require("Module:links").embedded_language_links{
term = args[1],
lang = lang,
sc = sc
}, lang, sc)
end
end




function export.linkify_t(frame)
function export.linkify_t(frame)
local params = {
local args = process_params(frame:getParent().args, {
[1] = {required = true, default = ""},
[1] = {required = true, default = ""},
}
})
local args = require("Module:parameters").process(frame:getParent().args, params)
args[1] = mw.text.trim(args[1])
args[1] = mw.text.trim(args[1])
Line 166: Line 176:
end
end


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


function export.language_name_link_t(frame)
function export.section_link_t(frame)
local face = frame.args["face"]
local args = process_params(frame:getParent().args, {
local allowSelfLink = frame.args["notself"]; allowSelfLink = not allowSelfLink or allowSelfLink == ""
[1] = {},
})
local params = {
return require("Module:links").section_link(args[1])
[1] = {required = true},
[2] = {},
[3] = {},
[4] = {alias_of = "gloss"},
["g"] = {list = true},
["gloss"] = {},
["id"] = {},
["lit"] = {},
["pos"] = {},
["t"] = {alias_of = "gloss"},
["tr"] = {},
["ts"] = {},
["sc"] = {},
["w"] = { type = "boolean", default = true },
}
-- 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 compat = (frame.args["compat"] or "") ~= ""
if compat then
params["lang"] = {},
table.remove(params, 1)
end
local args = require("Module:parameters").process(frame:getParent().args, params)
local lang = args[compat and "lang" or 1]
lang = lang or "und"
local sc = args["sc"]
local term = args[(compat and 1 or 2)]
local alt = args[(compat and 2 or 3)]
-- Check parameters
lang = require("Module:languages").getByCode(lang, 1, "allow etym", true)
local non_etym_lang = require("Module:languages").getNonEtymological(lang)
if sc then
sc = require("Module:scripts").getByCode(sc, "sc")
end
 
if not term and not alt and frame.args["demo"] then
term = frame.args["demo"]
end
--[[
Add a language name, linked to Wikipedia if the Wikipedia parameter is set to true.
Forward the information to full_link.
]]
local language_name = args.w and "[[" .. lang:getCanonicalName() .. "]]" or lang:getCanonicalName()
if term == "-" then
return language_name
else
return language_name .. " " ..
require("Module:links").full_link(  
{
lang = non_etym_lang,
sc = sc,
term = term,
alt = alt,
id = args["id"],
tr = args["tr"],
ts = args["ts"],
genders = args["g"],
gloss = args["gloss"],
pos = args["pos"],
lit = args["lit"]
},
face,
allowSelfLink
)
end
end
end


return export
return export

Latest revision as of 20:30, 22 November 2024

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

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

local export = {}

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

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 plain = {}
		local params = {
			[1] = {required = true, type = "language", default = "und"},
			[2] = plain,
			[3] = plain,
			[4] = plain,
			["g"] = {list = true},
			["gloss"] = alias_of_4,
			["id"] = plain,
			["lit"] = plain,
			["pos"] = plain,
			["sc"] = {type = "script"},
			["t"] = alias_of_4,
			["tr"] = plain,
			["ts"] = plain,
			["accel-form"] = plain,
			["accel-translit"] = plain,
			["accel-lemma"] = plain,
			["accel-lemma-translit"] = plain,
			["accel-gender"] = plain,
			["accel-nostore"] = boolean,
			["name"] = boolean,
			["to_wikt"] = 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"] = {type = "boolean", default = true}
		end
		return process_params(frame:getParent().args, params), iargs
	end
	
	-- Used in [[Template:l]] and [[Template:mn]].
	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 = (not args.name or 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("Module:links").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"],
				to_wikt = args["to_wikt"],
				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
end

-- Used in [[Template:ll]].
do
	local function get_args(frame)
		local plain = {}
		return process_params(frame:getParent().args, {
			[1] = {required = true, type = "language", default = "und"},
			[2] = {allow_empty = true},
			[3] = plain,
			["id"] = plain,
			["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("Module:links").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 lang = require("Module:languages").getByCode("en")
	local sc = require("Module:scripts").getByCode("Latn")
	
	return require("Module:script utilities").tag_text(require("Module:links").embedded_language_links{
		term = args[1],
		lang = lang,
		sc = sc
	}, lang, sc)
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]
	else
		return "[[" .. args[1] .. "]]"
	end
end

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

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

return export