Module:links/templates

local export = {}
local m_debug = require("Module:debug")
local m_languages = require("Module:languages")
local m_links = require("Module:links")
local m_params = require("Module:parameters")
local m_params_data = mw.loadData("Module:parameters/data")["links/templates"]
local m_scripts = require("Module:scripts")
local m_script_utils = require("Module:script utilities")

--[=[
	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 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 = m_params.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 = m_languages.getByCode(lang, 1, true)
	sc = sc and m_scripts.getByCode(sc, "sc") or lang:findBestScript(alt or term)
	
	-- Forward the information to full_link
	return m_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 = m_params.process(frame:getParent().args, m_params_data.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 = m_languages.getByCode(lang, 1, true)
	sc = sc and m_scripts.getByCode(sc, "sc") or lang:findBestScript(alt or term)
	
	local id = args["id"]
	
	term = m_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 = m_params.process(frame:getParent().args, m_params_data.def_t)
	local lang = m_languages.getByCode("en")
	local sc = m_scripts.getByCode("Latn")
	
	local text = m_links.embedded_language_links(
		{
			term = args[1],
			lang = lang,
			sc = sc
		},
		true
	)
	return m_script_utils.tag_text(text, lang, sc)
end


function export.linkify_t(frame)
	local args = m_params.process(frame:getParent().args, m_params_data.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 = m_params.process(frame:getParent().args, m_params_data.section_link_t)
	
	return m_links.section_link(args[1])
end

function export.language_name_link_t(frame)
	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 = m_params.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 = m_languages.getByCode(lang, 1, true)
	
	if sc then
		sc = m_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 .. " " ..
			m_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