Module:Links/templates: Difference between revisions

From The Languages of David J. Peterson
Jump to navigation Jump to search
No edit summary
No edit summary
Tag: Reverted
Line 1: Line 1:
local process_params = require("Module:parameters").process
local remove = table.remove
local export = {}
local export = {}


Line 10: Line 13:
]=]
]=]


-- Used in [[Template:l]] and [[Template:m]].
do
function export.l_term_t(frame)
local function get_args(frame)
local m_debug = require("Module:debug")
-- `compat` is a compatibility mode for {{term}}.
local m_params_data = mw.loadData("Module:parameters/data")["links/templates"]
-- If given a nonempty value, the function uses lang= to specify the
-- language, and all the positional parameters shift one number lower.
local face = frame.args["face"]
local iargs = frame.args
local allowSelfLink = frame.args["notself"]; allowSelfLink = not allowSelfLink or allowSelfLink == ""
iargs.compat = iargs.compat and iargs.compat ~= ""
iargs.langname = iargs.langname and iargs.langname ~= ""
local params = m_params_data.l_term_t
iargs.notself = iargs.notself and iargs.notself ~= ""
local alias_of_4 = {alias_of = 4}
-- Compatibility mode for {{term}}.
local boolean_default_false = {type = "boolean", default = false}
-- If given a nonempty value, the function uses lang= to specify the
local plain = {}
-- language, and all the positional parameters shift one number lower.
local params = {
local compat = (frame.args["compat"] or "") ~= ""
[1] = {required = true, type = "language", etym_lang = true, default = "und"},
if compat then
[2] = plain,
params = m_params_data.l_term_t_compat
[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"] = {type = "boolean"},
}
if iargs.compat then
params["lang"] = {type = "language", etym_lang = true, default = "und"}
remove(params, 1)
alias_of_4.alias_of = 3
end
if iargs.langname then
params["w"] = boolean_default_false
end
return process_params(frame:getParent().args, params), iargs
end
end
local args = require("Module:parameters").process(frame:getParent().args, params)
-- Used in [[Template:l]] and [[Template:m]].
function export.l_term_t(frame)
local lang = args[compat and "lang" or 1]
local m_debug = require("Module:debug")
-- Tracking for missing language or und
local args, iargs = get_args(frame)
if not lang then
local compat = iargs.compat
m_debug.track("link/no lang")
local lang = args[compat and "lang" or 1]
elseif lang == "und" then
m_debug.track("link/und")
-- Tracking for und.
if not compat and lang:getCode() == "und" then
m_debug.track("link/und")
end
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("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"],
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
lang = lang or "und"
local sc = args["sc"]
local term = args[(compat and 1 or 2)]
local alt = args[(compat and 2 or 3)]
if term == "" then term = nil end
lang = require("Module:languages").getByCode(lang, 1, true)
sc = sc and require("Module:scripts").getByCode(sc, "sc") or lang:findBestScript(alt or term)
-- Forward the information to full_link
return require("Module:links").full_link(
{
lang = lang,
sc = sc,
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,
},
face,
allowSelfLink
)
end
end


-- Used in [[Template:ll]].
-- Used in [[Template:ll]].
function export.ll(frame)
do
local args = require("Module:parameters").process(frame:getParent().args, mw.loadData("Module:parameters/data")["links/templates"].ll)
local function get_args(frame)
local allowSelfLink = not args["notself"]
local plain = {}
return process_params(frame:getParent().args, {
local lang = args[1]
[1] = {required = true, type = "language", etym_lang = true, default = "und"},
local sc = args["sc"]
[2] = {allow_empty = true},
local term = args[2]
[3] = plain,
local alt = args[3]
["id"] = plain,
if term == "" then term = nil end
["sc"] = {type = "script"},
})
lang = require("Module:languages").getByCode(lang, 1, true)
end
sc = sc and require("Module:scripts").getByCode(sc, "sc") or lang:findBestScript(alt or term)
local id = args["id"]
term = require("Module:links").language_link(
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,
lang = lang,
sc = sc,
sc = sc,
term = term,
term = term,
alt = alt,
alt = args[3],
id = id
id = args.id
},
} or "<small>[Term?]</small>" ..
allowSelfLink
require("Module:utilities").format_categories(
)
{lang:getFullName() .. " term requests"},
lang, "-", nil, nil, sc
if term then
)
return term
else
term = "<small>[Term?]</small>"
if mw.title.getCurrentTitle().nsText ~= "Template" then
term = term .. require("Module:utilities").format_categories({lang:getNonEtymologicalName() .. " term requests"}, lang, "-", nil, nil, sc)
end
return term
end
end
end
end


function export.def_t(frame)
function export.def_t(frame)
local args = require("Module:parameters").process(frame:getParent().args, mw.loadData("Module:parameters/data")["links/templates"].def_t)
local args = process_params(frame:getParent().args, {
[1] = {required = true, default = ""},
})
local lang = require("Module:languages").getByCode("en")
local lang = require("Module:languages").getByCode("en")
local sc = require("Module:scripts").getByCode("Latn")
local sc = require("Module:scripts").getByCode("Latn")
local text = require("Module:links").embedded_language_links(
local text = require("Module:links").embedded_language_links{
{
term = args[1],
term = args[1],
lang = lang,
lang = lang,
sc = sc
sc = sc
}
},
true
)
return require("Module:script utilities").tag_text(text, lang, sc)
return require("Module:script utilities").tag_text(text, lang, sc)
end
end
Line 133: Line 168:


function export.linkify_t(frame)
function export.linkify_t(frame)
local args = require("Module:parameters").process(frame:getParent().args, mw.loadData("Module:parameters/data")["links/templates"].linkify_t)
local args = process_params(frame:getParent().args, {
[1] = {required = true, default = ""},
})
args[1] = mw.text.trim(args[1])
args[1] = mw.text.trim(args[1])
Line 145: Line 182:


function export.section_link_t(frame)
function export.section_link_t(frame)
local args = require("Module:parameters").process(frame:getParent().args, mw.loadData("Module:parameters/data")["links/templates"].section_link_t)
local args = process_params(frame:getParent().args, {
[1] = {},
})
return require("Module:links").section_link(args[1])
return require("Module:links").section_link(args[1])
end
function export.language_name_link_t(frame)
local m_debug = require("Module:debug")
local m_params_data = mw.loadData("Module:parameters/data")["links/templates"]
local face = frame.args["face"]
local allowSelfLink = frame.args["notself"]; allowSelfLink = not allowSelfLink or allowSelfLink == ""
local params = m_params_data.language_name_link_t
-- 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 = m_params_data.language_name_link_t_compat
end
local args = require("Module:parameters").process(frame:getParent().args, params)
local lang = args[compat and "lang" or 1]
-- Tracking for missing language or und
if not lang then
m_debug.track("link/no lang")
elseif lang == "und" then
m_debug.track("link/und")
end
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, true)
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:makeWikipediaLink() or lang:getCanonicalName()
if term == "-" then
return language_name
else
return language_name .. " " ..
require("Module:links").full_link(
{
lang = lang,
sc = sc,
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"]
},
face,
allowSelfLink
)
end
end
end


return export
return export

Revision as of 00:29, 10 May 2024

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

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

local export = {}

--[=[
	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_default_false = {type = "boolean", default = false}
		local plain = {}
		local params = {
			[1] = {required = true, type = "language", etym_lang = true, 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"] = {type = "boolean"},
		}
		if iargs.compat then
			params["lang"] = {type = "language", etym_lang = true, default = "und"}
			remove(params, 1)
			alias_of_4.alias_of = 3
		end
		if iargs.langname then
			params["w"] = boolean_default_false
		end
		return process_params(frame:getParent().args, params), iargs
	end
	
	-- Used in [[Template:l]] and [[Template:m]].
	function export.l_term_t(frame)
		local m_debug = require("Module:debug")
		
		local args, iargs = get_args(frame)
		local compat = iargs.compat
		local lang = args[compat and "lang" or 1]
		
		-- Tracking for und.
		if not compat and lang:getCode() == "und" then
			m_debug.track("link/und")
		end
		
		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("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"],
				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", etym_lang = true, 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")
	
	local text = require("Module:links").embedded_language_links{
		term = args[1],
		lang = lang,
		sc = sc
	}
	return require("Module:script utilities").tag_text(text, 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.section_link_t(frame)
	local args = process_params(frame:getParent().args, {
		[1] = {},
	})
	
	return require("Module:links").section_link(args[1])
end

return export