Module:Links/templates: Difference between revisions

From The Languages of David J. Peterson
Jump to navigation Jump to search
mNo edit summary
(Undo revision 517538 by Juelos (talk))
Tag: Undo
 
(3 intermediate revisions by 2 users not shown)
Line 12: Line 12:
-- Used in [[Template:l]] and [[Template:m]].
-- Used in [[Template:l]] and [[Template:m]].
function export.l_term_t(frame)
function export.l_term_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 face = frame.args["face"]
local allowSelfLink = frame.args["notself"]; allowSelfLink = not allowSelfLink or allowSelfLink == ""
local allowSelfLink = frame.args["notself"]; allowSelfLink = not allowSelfLink or allowSelfLink == ""
local params = {
local params = m_params_data.l_term_t
[1] = {required = true},
[2] = {},
[3] = {},
[4] = {alias_of = "gloss"},
["accel"] = {},
["g"] = {list = true},
["gloss"] = {},
["id"] = {},
["lit"] = {},
["pos"] = {},
["t"] = {alias_of = "gloss"},
["tr"] = {},
["ts"] = {},
["sc"] = {},
}
-- Compatibility mode for {{term}}.
-- Compatibility mode for {{term}}.
Line 36: Line 24:
-- language, and all the positional parameters shift one number lower.
-- language, and all the positional parameters shift one number lower.
local compat = (frame.args["compat"] or "") ~= ""
local compat = (frame.args["compat"] or "") ~= ""
if compat then
if compat then
params["lang"] = {},
params = m_params_data.l_term_t_compat
table.remove(params, 1)
end
end
Line 48: Line 34:
-- Tracking for missing language or und
-- Tracking for missing language or und
if not lang then
if not lang then
require("Module:debug").track("link/no lang")
m_debug.track("link/no lang")
elseif lang == "und" then
elseif lang == "und" then
require("Module:debug").track("link/und")
m_debug.track("link/und")
end
end
Line 58: Line 44:
local term = args[(compat and 1 or 2)]
local term = args[(compat and 1 or 2)]
local alt = args[(compat and 2 or 3)]
local alt = args[(compat and 2 or 3)]
if term == "" then term = nil end
-- Check parameters
lang = require("Module:languages").getByCode(lang, 1, true)
lang = require("Module:languages").getByCode(lang) or
sc = sc and require("Module:scripts").getByCode(sc, "sc") or lang:findBestScript(alt or term)
require("Module:languages").err(lang, 1)
if sc then
sc = require("Module:scripts").getByCode(sc) or error("The script code \"" .. sc .. "\" is not valid.")
end
if not term and not alt and frame.args["demo"] then
term = frame.args["demo"]
end
-- Forward the information to full_link
-- Forward the information to full_link
return require("Module:links").full_link(  
return require("Module:links").full_link(
{
{
lang = lang,  
lang = lang,
sc = sc,  
sc = sc,
term = term.."-".."-"..tostring(compat).."-"..tostring(allowSelfLink),
term = term,
alt = alt,  
alt = alt,
id = args["id"],  
gloss = args[4],
id = args["id"],
tr = args["tr"],
tr = args["tr"],
ts = args["ts"],
ts = args["ts"],
genders = args["g"],
genders = args["g"],
gloss = args["gloss"],  
pos = args["pos"],
pos = args["pos"],  
lit = args["lit"],
lit = args["lit"],
accel = args["accel"] and string.gsub(args["accel"], "_", "|"), -- To allow use of | in templates
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,
face,
Line 94: Line 79:
-- Used in [[Template:ll]].
-- Used in [[Template:ll]].
function export.ll(frame)
function export.ll(frame)
local params = {
local args = require("Module:parameters").process(frame:getParent().args, mw.loadData("Module:parameters/data")["links/templates"].ll)
[1] = { required = true },
[2] = { allow_empty = true },
[3] = {},
["notself"] = { type = "boolean", default = false },
["id"] = {},
}
local args = require("Module:parameters").process(frame:getParent().args, params)
local allowSelfLink = not args["notself"]
local allowSelfLink = not args["notself"]
local lang = args[1]
local lang = args[1]
lang = require("Module:languages").getByCode(lang) or require("Module:languages").err(lang, 1)
local sc = args["sc"]
local term = args[2]
local alt = args[3]
if term == "" then term = nil end
local text = args[2]
lang = require("Module:languages").getByCode(lang, 1, true)
local alt = args[3]
sc = sc and require("Module:scripts").getByCode(sc, "sc") or lang:findBestScript(alt or term)
if text == "" then
return alt or ""
end
local id = args["id"]
local id = args["id"]
return require("Module:links").language_link(
term = require("Module:links").language_link(
{
{
term = text,
lang = lang,
sc = sc,
term = term,
alt = alt,
alt = alt,
lang = lang,
id = id
id = id
},
},
allowSelfLink
allowSelfLink
)
)
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


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




function export.linkify_t(frame)
function export.linkify_t(frame)
local params = {
local args = require("Module:parameters").process(frame:getParent().args, mw.loadData("Module:parameters/data")["links/templates"].linkify_t)
[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 154: Line 145:


function export.section_link_t(frame)
function export.section_link_t(frame)
local params = {
local args = require("Module:parameters").process(frame:getParent().args, mw.loadData("Module:parameters/data")["links/templates"].section_link_t)
[1] = {},
}
local args = require("Module:parameters").process(frame:getParent().args, params)
return require("Module:links").section_link(args[1])
return require("Module:links").section_link(args[1])
Line 164: Line 151:


function export.language_name_link_t(frame)
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 face = frame.args["face"]
local allowSelfLink = frame.args["notself"]; allowSelfLink = not allowSelfLink or allowSelfLink == ""
local allowSelfLink = frame.args["notself"]; allowSelfLink = not allowSelfLink or allowSelfLink == ""
local params = {
local params = m_params_data.language_name_link_t
[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 = false },
}
-- Compatibility mode for {{term}}.
-- Compatibility mode for {{term}}.
Line 188: Line 163:
-- language, and all the positional parameters shift one number lower.
-- language, and all the positional parameters shift one number lower.
local compat = (frame.args["compat"] or "") ~= ""
local compat = (frame.args["compat"] or "") ~= ""
if compat then
if compat then
params["lang"] = {},
params = m_params_data.language_name_link_t_compat
table.remove(params, 1)
end
end
Line 200: Line 173:
-- Tracking for missing language or und
-- Tracking for missing language or und
if not lang then
if not lang then
require("Module:debug").track("link/no lang")
m_debug.track("link/no lang")
elseif lang == "und" then
elseif lang == "und" then
require("Module:debug").track("link/und")
m_debug.track("link/und")
end
end
Line 212: Line 185:
-- Check parameters
-- Check parameters
lang = require("Module:languages").getByCode(lang) or
lang = require("Module:languages").getByCode(lang, 1, true)
require("Module:languages").err(lang, 1)
if sc then
if sc then
sc = require("Module:scripts").getByCode(sc) or error("The script code \"" .. sc .. "\" is not valid.")
sc = require("Module:scripts").getByCode(sc, "sc")
end
end
 
if not term and not alt and frame.args["demo"] then
if not term and not alt and frame.args["demo"] then
term = frame.args["demo"]
term = frame.args["demo"]
Line 232: Line 205:
else
else
return language_name .. " " ..
return language_name .. " " ..
require("Module:links").full_link(  
require("Module:links").full_link(
{
{
lang = lang,  
lang = lang,
sc = sc,  
sc = sc,
term = term,  
term = term,
alt = alt,  
alt = alt,
id = args["id"],  
gloss = args[4],
tr = args["tr"],  
id = args["id"],
ts = args["ts"],  
tr = args["tr"],
genders = args["g"],
ts = args["ts"],
gloss = args["gloss"],  
genders = args["g"],
pos = args["pos"],  
pos = args["pos"],
lit = args["lit"]
lit = args["lit"]
},
},
Line 250: Line 223:
)
)
end
end
end
function export.light_link_t(frame)
local params = {
[1] = { required = true },
[2] = { required = true },
[3] = {},
["sc"] = {},
["langname"] = { required = true },
}
local args = frame:getParent().args
for key, value in pairs (params) do
if value.required then
if not args[key] then
error('Parameter "' .. key .. '" is required.')
end
end
end
return require("Module:links").light_link{ 
langCode = args[1],
term = args[2],
alt = args[3],
scCode = args.sc or "Latn", 
langName = args.langname
}
end
end


return export
return export

Latest revision as of 00:30, 10 May 2024

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

local export = {}

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

-- Used in [[Template:l]] and [[Template:m]].
function export.l_term_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.l_term_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.l_term_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)]
	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

-- Used in [[Template:ll]].
function export.ll(frame)
	local args = require("Module:parameters").process(frame:getParent().args, mw.loadData("Module:parameters/data")["links/templates"].ll)
	local allowSelfLink = not args["notself"]
	
	local lang = args[1]
	local sc = args["sc"]
	local term = args[2]
	local alt = args[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)
	
	local id = args["id"]
	
	term = require("Module:links").language_link(
		{
			lang = lang,
			sc = sc,
			term = term,
			alt = alt,
			id = id
		},
		allowSelfLink
	)
	
	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

function export.def_t(frame)
	local args = require("Module:parameters").process(frame:getParent().args, mw.loadData("Module:parameters/data")["links/templates"].def_t)
	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
		},
		true
	)
	return require("Module:script utilities").tag_text(text, lang, sc)
end


function export.linkify_t(frame)
	local args = require("Module:parameters").process(frame:getParent().args, mw.loadData("Module:parameters/data")["links/templates"].linkify_t)
	
	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 = require("Module:parameters").process(frame:getParent().args, mw.loadData("Module:parameters/data")["links/templates"].section_link_t)
	
	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

return export