વિભાગ:લિપિના ઉપયોગીતા

This module provides access to Module:લિપિઓ from templates, so that they can make use of the information stored there. It also provides a number of functions that can be used by other modules.

{{#invoke:script utilities|lookup_script|(script code)|(item to look up)|(index)}}

Queries information about a script code. This is analogous to lookup_language in Module:ભાષાના ઉપયોગીતા and works the same way, but looks up scripts instead. See there for a more thorough description.

{{#invoke:script utilities|script_exists|(script code)}}

Check whether a script code exists and is valid. It will return "1" if the script code exists, and the empty string "" if it does not.

This is rarely needed, because a script error will result when someone uses a code that is not valid, so you do not need this just to check for errors. However, in case you need to decide different actions based on whether a certain parameter is a script code or something else, this function can be useful.

tag_text(text, lang, sc, face)

Wraps the given text in HTML tags with appropriate CSS classes (see WT:CSS) for the language and script. It replaces the function of the લિપિ સંકેતના ઢાંચાઓ (specifically the {{લિપિ સહાયક}} template) and is required for all non-English text on Wiktionary.

The actual tags and CSS classes that are added are determined by the face parameter. It can be one of the following:

"term"
The text is wrapped in <i class="(sc) mention" lang="(lang)">...</i>.
"head"
The text is wrapped in <strong class="(sc) headword" lang="(lang)">...</strong>.
"bold"
The text is wrapped in <b class="(sc)" lang="(lang)">...</b>.
nil
The text is wrapped in <span class="(sc)" lang="(lang)">...</span>.

transliterate(text, lang, sc)

Generates a transliteration (romanization) from the given text into Latin script, using the transliteration rules of the given language. This will use the module specified with the translit_module setting for the language in Module:ભાષાઓ. If no transliteration module is available, or if the transliteration fails in some other way, nil is returned.

request_script(lang, sc)

Generates a request to provide a term in its native script, if it is missing. This is used by the {{લિપિની માગણી}} template as well as by the functions in Module:કડીઓ.

The function will add entries to one of the subcategories of Category:Terms needing native script by language, and do several checks on the given language and script. In particular:

  • If the script was given, a subcategory named "needing (script) script" is added, but only if the language has more than one script. Otherwise, the main "needing native script" category is used.
  • Nothing is added at all if the language has no scripts other than Latin and its varieties.

{{#invoke:લિપિના ઉપયોગીતા|template_rfscript}}

This is used by {{લિપિની માગણી}}. See there for more information.


local export = {}

-- Implements [[Template:lang]]
function export.lang_t(frame)
	local args = frame:getParent().args
	NAMESPACE = mw.title.getCurrentTitle().nsText
	
	local lang = args[1]; if lang == "" then lang = nil end
	local text = args[2] or ""
	local sc = args["લિપિ"]; if sc == "" then sc = nil end
	local face = args["મોઢું"]; if face == "" then face = nil end
	
	lang = lang or (NAMESPACE == "ઢાંચો" and "und") or error("Language code has not been specified. Please pass parameter 1 to the template.")
	lang = require("Module:ભાષાઓ").getByCode(lang) or error("The language code \"" .. lang .. "\" is not valid.")
	
	if sc then
		sc = require("Module:લિપિઓ").getByCode(sc) or error("The script code \"" .. sc .. "\" is not valid.")
	else
		sc = require("Module:લિપિઓ").findBestScript(text, lang)
	end
	
	return export.tag_text(text, lang, sc, face)
end

-- Wrap text in the appropriate HTML tags with language and script class.
function export.tag_text(text, lang, sc, face)
	-- Add a script wrapper
	if face == "શબ્દ" then
		return '<i class="' .. sc:getCode() .. ' mention" ભાષા="' .. lang:getCode() .. '">' .. text .. '</i>'
	elseif face == "માથું" then
		return '<strong class="' .. sc:getCode() .. ' headword" ભાષા="' .. lang:getCode() .. '">' .. text .. '</strong>'
	elseif face == "ઘાટા" then
		return '<b class="' .. sc:getCode() .. '" ભાષા="' .. lang:getCode() .. '">' .. text .. '</b>'
	elseif face == nil then
		return '<span class="' .. sc:getCode() .. '" ભાષા="' .. lang:getCode() .. '">' .. text .. '</span>'
	else
		error("Invalid script face \"" .. face .. "\".")
	end
end

-- Add a notice to request the native script of a word
function export.request_script(lang, sc)
	local scripts = lang:getScripts()
	
	-- By default, request for "native" script
	local cat_script = "દેશી"
	local disp_script = "લિપિ"
	
	-- If the script was not specified, and the language has only one script, use that.
	if not sc and #scripts == 1 then
		sc = scripts[1]
	end
	
	-- Is the script known?
	if sc then
		-- If the script is Gujarati, return nothing.
		if is_Gujarati_script(sc) then
			return ""
		end
		
		if sc:getCode() ~= scripts[1]:getCode() then
			disp_script = sc:getCanonicalName()
		end
		
		-- The category needs to be specific to script only if there is chance
		-- of ambiguity. This occurs when lang=und, or when the language has
		-- multiple scripts.
		if lang:getCode() == "und" or scripts[2] then
			cat_script = sc:getCanonicalName()
		end
	else
		-- The script is not known.
		-- Does the language have at least one non-Latin script in its list?
		local has_nonlatin = false
		
		for i, val in ipairs(scripts) do
			if not is_Gujarati_script(val) then
				has_nonlatin = true
				break
			end
		end
		
		-- If there are non-Latin scripts, return nothing.
		if not has_nongujarati then
			return ""
		end
	end
	
	local category = ""
	
	if mw.title.getCurrentTitle().nsText ~= "ઢાંચો" then
		category = "[[શ્રેણી:" .. lang:getCanonicalName() .. " terms needing " .. cat_script .. " લિપિ]]"
	end
	
	return "<small>[" .. disp_script .. "?]</small>" .. category
end

function export.template_rfscript(frame)
	local args = frame.args
	local lang = args[1] or error("The first parameter (language code) has not been given")
	local sc = args["લિપિ"]; if sc == "" then sc = nil end
	lang = require("Module:ભાષાઓ").getByCode(lang) or error("The language code \"" .. lang .. "\" is not valid.")
	sc = (sc and (require("Module:લિપિઓ").getByCode(sc) or error("The script code \"" .. sc .. "\" is not valid.")) or nil)
	
	local ret = export.request_script(lang, sc)
	
	if ret == "" then
		error("આ ભાષા ગુજરાતી લિપિમાં લખેલી છે. તળપદું લિપિની જરૂરી નથી.")
	else
		return ret
	end
end

function is_Gujarati_script(sc)
	return (sc:getCode():find("Guja", nil, true))
end

return export