Module:script utilities: difference between revisions

From Wiktionary, the free dictionary
Jump to navigation Jump to search
Content deleted Content added
removing lang attribute and script class "Latn" for Japanese transliterations, which sometimes include both Kana and Latin ... this needs a better solution, though
track incorrect Ancient Greek characters
Line 27: Line 27:
return export.tag_text(text, lang, sc, face, class)
return export.tag_text(text, lang, sc, face, class)
end

local function trackChar(text, character, tracking)
if character and mw.ustring.find(text, character) then
require("Module:debug").track("script/" .. tracking)
end
end
end


local function track(text, lang, sc)
local function track(text, lang, sc)
if lang and lang:getCode() == "ang" then
if lang and text then
-- [[Special:WhatLinksHere/Template:tracking/script/ang/acute]]
local decomposed = mw.ustring.toNFD(text)
if lang:getCode() == "ang" then
local acute = mw.ustring.char(0x301)
local decomposed = mw.ustring.toNFD(text)
local acute = mw.ustring.char(0x301)
trackChar(text, acute, "ang/acute")
end
--[=[
if mw.ustring.find(decomposed, acute) then
[[Special:WhatLinksHere/Template:tracking/script/Greek/wrong-phi]]
require("Module:debug").track("script/ang/acute")
[[Special:WhatLinksHere/Template:tracking/script/Greek/wrong-theta]]
[[Special:WhatLinksHere/Template:tracking/script/Greek/wrong-kappa]]
[[Special:WhatLinksHere/Template:tracking/script/Greek/wrong-rho]]
]=]
if lang:getCode() == "el" or lang:getCode() == "grc" then
trackChar(text, "ϑ", "Greek/wrong-theta")
trackChar(text, "ϰ", "Greek/wrong-kappa")
trackChar(text, "ϱ", "Greek/wrong-rho")
trackChar(text, "ϕ", "Greek/wrong-phi")
end
end
end
end

Revision as of 00:01, 21 May 2017

This module provides access to Module:scripts 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.

Data is found in Module:script utilities/data.

Exported functions

export.lang_t

function export.lang_t(frame)

This function lacks documentation. Please add a description of its usages, inputs and outputs, or its difference from similar functions, or make it local to remove it from the function list.

export.tag_text

function export.tag_text(text, lang, sc, face, class, id)

This function lacks documentation. Please add a description of its usages, inputs and outputs, or its difference from similar functions, or make it local to remove it from the function list.

export.tag_translit

function export.tag_translit(translit, lang, kind, attributes)

This function lacks documentation. Please add a description of its usages, inputs and outputs, or its difference from similar functions, or make it local to remove it from the function list.

export.request_script

function export.request_script(lang, sc)

This function lacks documentation. Please add a description of its usages, inputs and outputs, or its difference from similar functions, or make it local to remove it from the function list.

export.template_rfscript

function export.template_rfscript(frame)

This function lacks documentation. Please add a description of its usages, inputs and outputs, or its difference from similar functions, or make it local to remove it from the function list.

is_Latin_script

function is_Latin_script(sc)

This function lacks documentation. Please add a description of its usages, inputs and outputs, or its difference from similar functions, or make it local to remove it from the function list.

See also


local export = {}

local m_data = mw.loadData("Module:script utilities/data")

-- Used by [[Template:lang]]
function export.lang_t(frame)
	params = {
		[1] = {},
		[2] = { allow_empty = true, default = "" },
		["sc"] = {},
		["face"] = {},
		["class"] = {},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	local NAMESPACE = mw.title.getCurrentTitle().nsText
	
	local lang = args[1] or (NAMESPACE == "Template" and "und") or error("Language code has not been specified. Please pass parameter 1 to the template.")
	lang = require("Module:languages").getByCode(lang) or require("Module:languages").err(lang, 1)
	
	local text = args[2]
	
	local sc = args["sc"]
	sc = (sc and (require("Module:scripts").getByCode(sc) or error("The script code \"" .. sc .. "\" is not valid.")) or nil)
	
	local face = args["face"]
	
	return export.tag_text(text, lang, sc, face, class)
end

local function trackChar(text, character, tracking)
	if character and mw.ustring.find(text, character) then
		require("Module:debug").track("script/" .. tracking)
	end
end

local function track(text, lang, sc)
	if lang and text then
		-- [[Special:WhatLinksHere/Template:tracking/script/ang/acute]]
		if lang:getCode() == "ang" then
			local decomposed = mw.ustring.toNFD(text)
			local acute = mw.ustring.char(0x301)
			
			trackChar(text, acute, "ang/acute")
		end
		
		--[=[
		[[Special:WhatLinksHere/Template:tracking/script/Greek/wrong-phi]]
		[[Special:WhatLinksHere/Template:tracking/script/Greek/wrong-theta]]
		[[Special:WhatLinksHere/Template:tracking/script/Greek/wrong-kappa]]
		[[Special:WhatLinksHere/Template:tracking/script/Greek/wrong-rho]]
		]=]
		if lang:getCode() == "el" or lang:getCode() == "grc" then
			trackChar(text, "ϑ", "Greek/wrong-theta")
			trackChar(text, "ϰ", "Greek/wrong-kappa")
			trackChar(text, "ϱ", "Greek/wrong-rho")
			trackChar(text, "ϕ", "Greek/wrong-phi")
		end
	end
end

-- Wrap text in the appropriate HTML tags with language and script class.
function export.tag_text(text, lang, sc, face, class, id)
	if not sc then
		sc = require("Module:scripts").findBestScript(text, lang)
	end
	
	track(text, lang, sc)
		
	-- Replace space characters with newlines in Mongolian-script text, which is written top-to-bottom.
	if sc and sc:getDirection() == "down" then
		--[[	First, escape targets of wikilinks and HTML tags,
				which should not have their spaces
				replaced with line breaks. ]]
		local escaped = {}
		
		local i = 1
		for link_target in mw.ustring.gmatch(text, "%[%[([^|]+|)") do
			escaped[i] = link_target
			text = mw.ustring.gsub(text, link_target, "$" .. i)
			i = i + 1
		end
		
		for HTML_tag in mw.ustring.gmatch(text, "<[^>]+>") do
			escaped[i] = HTML_tag
			text = mw.ustring.gsub(text, HTML_tag, "$" .. i)
			i = i + 1
		end
		
		text = mw.ustring.gsub(
			text,
			" +",
			"<br>"
		)
		
		-- Unescape whatever was escaped.
		text = mw.ustring.gsub(
			text,
			"$(%d)",
			function(a)
				a = tonumber(a)
				return escaped[a]
			end
		)
	end

	if sc:getCode() == "Imag" then
		face = nil
	end

	local function class_attr(classes)
		table.insert(classes, 1, sc:getCode())
		if class and class ~= '' then
			table.insert(classes, class)
		end
		return 'class="' .. table.concat(classes, ' ') .. '"'
	end
	
	local function tag_attr(...)
		output = {}
		if id then
			table.insert(output, 'id="' .. require("Module:utilities").make_id(lang, id) .. '"')
		end
		
		table.insert(output, class_attr({...}) )
		
		if lang then
			table.insert(output, 'lang="' .. lang:getCode() .. '"')
		end
		
		return table.concat(output, " ")
	end
	
	if face == "hypothetical" then
	-- [[Special:WhatLinksHere/Template:tracking/script-utilities/face/hypothetical]]
		require("Module:debug").track("script-utilities/face/hypothetical")
	end
	
	local data
	if face then
		data = m_data.faces[face]
	else
		data = m_data.faces["nil"]
	end
	
	-- Add a script wrapper
	if data then
		return ( data.prefix or "" ) .. '<' .. data.tag .. ' ' .. tag_attr(data.class) .. '>' .. text .. '</' .. data.tag .. '>'
	else
		error('Invalid script face "' .. face .. '".')
	end
end

function export.tag_translit(translit, lang, kind, attributes)
	if type(lang) == "table" then
		lang = lang.getCode and lang:getCode()
			or error("Third argument to tag_translit should be a language code or language object.")
	end
	
	local data = m_data.translit[kind or "default"]
	
	local opening_tag = {}
	
	table.insert(opening_tag, data.tag)
	if lang == "ja" then
		table.insert(opening_tag, 'class="' .. (data.classes and data.classes .. " " or "") .. 'tr"')
	else
		table.insert(opening_tag, 'lang="' .. lang .. '-Latn"')
		table.insert(opening_tag, 'class="' .. (data.classes and data.classes .. " " or "") .. 'tr Latn"')
	end
	
	if data.dir then
		table.insert(opening_tag, 'dir="' .. data.dir .. '"')
	end
	
	table.insert(opening_tag, attributes)
	
	return "<" .. table.concat(opening_tag, " ") .. ">" .. translit .. "</" .. data.tag .. ">"
end

-- Add a notice to request the native script of a word
function export.request_script(lang, sc)
	local scripts = lang.getScripts and lang:getScripts() or error('The language "' .. lang:getCode() .. '" does not have the method getScripts. It may be unwritten.')
	
	-- By default, request for "native" script
	local cat_script = "native"
	local disp_script = "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 Latin, return nothing.
		if is_Latin_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_Latin_script(val) then
				has_nonlatin = true
				break
			end
		end
		
		-- If there are non-Latin scripts, return nothing.
		if not has_nonlatin then
			return ""
		end
	end
	
	local category = ""
	
	if mw.title.getCurrentTitle().nsText ~= "Template" then
		category = "[[Category:" .. lang:getCanonicalName() .. " terms needing " .. cat_script .. " script]]"
	end
	
	return "<small>[" .. disp_script .. " needed]</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["sc"]; if sc == "" then sc = nil end
	lang = require("Module:languages").getByCode(lang) or error("The language code \"" .. lang .. "\" is not valid.")
	sc = (sc and (require("Module:scripts").getByCode(sc) or error("The script code \"" .. sc .. "\" is not valid.")) or nil)
	
	local ret = export.request_script(lang, sc)
	
	if ret == "" then
		error("This language is written in the Latin alphabet. It does not need a native script.")
	else
		return ret
	end
end

function is_Latin_script(sc)
	return (sc:getCode():find("Latn", nil, true)) or sc:getCode() == "Latinx"
end

return export