Modul:script utilities

Üdvözlöm, Ön a Modul:script utilities szó jelentését keresi. A DICTIOUS-ban nem csak a Modul:script utilities szó összes szótári jelentését megtalálod, hanem megismerheted az etimológiáját, a jellemzőit és azt is, hogyan kell a Modul:script utilities szót egyes és többes számban mondani. Minden, amit a Modul:script utilities szóról tudni kell, itt található. A Modul:script utilities szó meghatározása segít abban, hogy pontosabban és helyesebben fogalmazz, amikor beszélsz vagy írsz. AModul:script utilities és más szavak definíciójának ismerete gazdagítja a szókincsedet, és több és jobb nyelvi forráshoz juttat.

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

lang_t

{{#invoke:script utilities|lang_t}}

This is used by {{lang}} to wrap portions of text in a language tag. See there for more information.

tag_text

tag_text(text, lang, sc, face, class)

Wraps the given text in HTML tags with appropriate CSS classes (see WT:CSS) for the language and script. This 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>.
"hypothetical"
The text is wrapped in <span class="hypothetical-star">*</span><i class="(sc) hypothetical" lang="(lang)">...</i>.
"bold"
The text is wrapped in <b class="(sc)" lang="(lang)">...</b>.
nil
The text is wrapped in <span class="(sc)" lang="(lang)">...</span>.

The optional class parameter can be used to specify an additional CSS class to be added to the tag.

tag_translit

tag_translit(translit, lang, kind, attributes)

Tags the transliteration for given text translit and language lang. It will add the language, script subtag (as defined in BCP 47 2.2.3) and dir (directional) attributes as needed.

The optional kind parameter can be one of the following:

"term"
tag transliteration for {{mention}}
"usex"
tag transliteration for {{usex}}
"head"
tag transliteration for {{head}}
"default"
default

The optional attributes parameter is used to specify additional HTML attributes for the tag.

request_script

request_script(lang, sc)

Generates a request to provide a term in its native script, if it is missing. This is used by the {{rfscript}} template as well as by the functions in Module:links.

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

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

template_rfscript

{{#invoke:script utilities|template_rfscript}}

This is used by {{rfscript}}. See there for more information.

See also


local export = {}

--[=[
	Modules used:
	]
	]
	] (only when id's present)
	] (only when hyphens in Korean text or spaces in vertical text)
	]
	]
	]
	]
]=]

function export.is_Latin_script(sc)
	-- Latn, Latf, Latinx, pjt-Latn
	return sc:getCode():find("Lat") and true or false
end

-- Used by ]
function export.lang_t(frame)
	params = {
		 = {},
		 = { allow_empty = true, default = "" },
		 = {},
		 = {},
		 = {},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	local NAMESPACE = mw.title.getCurrentTitle().nsText
	
	local lang = args or (NAMESPACE == "Sablon" 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
	
	local sc = args
	sc = (sc and (require("Module:scripts").getByCode(sc) or error("The script code \"" .. sc .. "\" is not valid.")) or nil)
	
	local face = args
	
	return export.tag_text(text, lang, sc, face, class)
end

-- Ustring turns on the codepoint-aware string matching. The basic string function
-- should be used for simple sequences of characters, Ustring function for
-- sets – .
local function trackPattern(text, pattern, tracking, ustring)
	local find = ustring and mw.ustring.find or string.find
	if pattern and find(text, pattern) then
		require("Module:debug/track")("script/" .. tracking)
	end
end

local function track(text, lang, sc)
	local U = mw.ustring.char
	
	if lang and text then
		local langCode = lang:getCode()
		
		-- ]
		if langCode == "ang" then
			local decomposed = mw.ustring.toNFD(text)
			local acute = U(0x301)
			
			trackPattern(decomposed, acute, "ang/acute")
		
		--[=[
		]
		]
		]
		]
			ϑ, ϰ, ϱ, ϕ should generally be replaced with θ, κ, ρ, φ.
		]=]
		elseif langCode == "el" or langCode == "grc" then
			trackPattern(text, "ϑ", "Greek/wrong-theta")
			trackPattern(text, "ϰ", "Greek/wrong-kappa")
			trackPattern(text, "ϱ", "Greek/wrong-rho")
			trackPattern(text, "ϕ", "Greek/wrong-phi")
		
			--[=[
			]
			]
			]
				When spacing coronis and spacing smooth breathing are used as apostrophes, 
				they should be replaced with right single quotation marks (’).
			]=]
			if langCode == "grc" then
				trackPattern(text, U(0x1FBD), "Ancient Greek/spacing-coronis")
				trackPattern(text, U(0x1FBF), "Ancient Greek/spacing-smooth-breathing")
				trackPattern(text, "", "Ancient Greek/wrong-apostrophe", true)
			end
		
		-- ]
		elseif langCode == "ru" then
			local decomposed = mw.ustring.toNFD(text)
			
			trackPattern(decomposed, U(0x300), "Russian/grave-accent")
		
		-- ]
		elseif langCode == "bo" then
			trackPattern(text, "$", "Tibetan/trailing-punctuation", true)
			trackPattern(text, "%]%]$", "Tibetan/trailing-punctuation", true)

		--[=[
		]
		]
		]
		]=]
		elseif langCode == "th" then
			trackPattern(text, "เ".."เ", "Thai/broken-ae")
			trackPattern(text, "ํ?า", "Thai/broken-am", true)
			trackPattern(text, "า", "Thai/wrong-rue-lue", true)

		--[=[
		]
		]
		]
		]
		]
		]=]
		elseif langCode == "lo" then
			trackPattern(text, "ເ".."ເ", "Lao/broken-ae")
			trackPattern(text, "ໍ?າ", "Lao/broken-am", true)
			trackPattern(text, "ຫນ", "Lao/possible-broken-ho-no")
			trackPattern(text, "ຫມ", "Lao/possible-broken-ho-mo")
			trackPattern(text, "ຫລ", "Lao/possible-broken-ho-lo")

		--[=[
		]
		]
		]=]
		elseif langCode == "khb" then
			trackPattern(text, "ᦵ".."ᦵ", "Lü/broken-ae")
			trackPattern(text, "", "Lü/possible-wrong-sequence", true)
		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:getDirection() == "down" and text:find(" ") then
		text = require("Module:munge_text")(text, function(txt)
			-- having extra parentheses makes sure only the first return value gets through
			return (txt:gsub(" +", "<br>"))
		end)
	end

	-- Hack Korean script text to remove hyphens.
	-- This should be handled in a more general fashion, but needs to
	-- be efficient by not doing anything if no hyphens are present, and currently this is the only
	-- language needing such processing.
	if sc:getCode() == "Kore" and text:find("%-") then
		text = require("Module:munge_text")(text, function(txt)
			-- having extra parentheses makes sure only the first return value gets through
			return (txt:gsub("%-", ""))
		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(...)
		local output = {}
		if id then
			table.insert(output, 'id="' .. require("Module:senseid").anchor(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
	-- ]
		require("Module:debug/track")("script-utilities/face/hypothetical")
	end
	
	local data = mw.loadData("Module:script utilities/data").faces
	
	local post = ""
	if sc:getDirection() == "rtl" and (face == "translation" or mw.ustring.find(text, "%p$")) then
		post = "&lrm;"
	end
	
	-- Add a script wrapper
	if data then
		return ( data.prefix or "" ) .. '<' .. data.tag .. ' ' .. tag_attr(data.class) .. '>' .. text .. '</' .. data.tag .. '>' .. post
	else
		error('Invalid script face "' .. face .. '".')
	end
end

function export.tag_translit(translit, lang, kind, attributes, is_manual)
	if type(lang) == "table" then
		lang = lang.getCode and lang:getCode()
			or error("Second argument to tag_translit should be a language code or language object.")
	end
	
	local data = mw.loadData("Module:script utilities/data").translit
	
	local opening_tag = {}
	
	table.insert(opening_tag, data.tag)
	if lang == "ja" then
		table.insert(opening_tag, 'class="' .. (data.classes and data.classes .. " " or "") .. (is_manual and "manual-tr " or "") .. 'tr"')
	else
		table.insert(opening_tag, 'lang="' .. lang .. '-Latn"')
		table.insert(opening_tag, 'class="' .. (data.classes and data.classes .. " " or "") .. (is_manual and "manual-tr " 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

function export.tag_transcription(transcription, 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 = mw.loadData("Module:script utilities/data").transcription
	
	local opening_tag = {}
	
	table.insert(opening_tag, data.tag)
	if lang == "ja" then
		table.insert(opening_tag, 'class="' .. (data.classes and data.classes .. " " or "") .. 'ts"')
	else
		table.insert(opening_tag, 'lang="' .. lang .. '-Latn"')
		table.insert(opening_tag, 'class="' .. (data.classes and data.classes .. " " or "") .. 'ts Latn"')
	end
	
	if data.dir then
		table.insert(opening_tag, 'dir="' .. data.dir .. '"')
	end
	
	table.insert(opening_tag, attributes)
	
	return "<" .. table.concat(opening_tag, " ") .. ">" .. transcription .. "</" .. data.tag .. ">"	
end

-- Add a notice to request the native script of a word
function export.request_script(lang, sc, usex, nocat, sort_key)
	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
	end
	
	-- Is the script known?
	if sc then
		-- If the script is Latin, return nothing.
		if export.is_Latin_script(sc) then
			return ""
		end
		
		if sc:getCode() ~= scripts: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 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 export.is_Latin_script(val) then
				has_nonlatin = true
				break
			end
		end
		
		-- If there are no non-Latin scripts, return nothing.
		if not has_nonlatin then
			return ""
		end
	end
	
	local category
	
	if usex then
		category = "Requests for " .. cat_script .. " script in " .. lang:getCanonicalName() .. " usage examples"
	else
		category = "Requests for " .. cat_script .. " script for " .. lang:getCanonicalName() .. " terms"
	end
	
	return "<small></small>" ..
		(nocat and "" or require("Module:utilities/format_categories")({category}, lang, sort_key))
end

function export.template_rfscript(frame)
	params = {
		 = { required = true, default = "und" },
		 = {},
		 = { type = "boolean" },
		 = { type = "boolean" },
		 = {},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local lang = require("Module:languages").getByCode(args, 1)
	local sc = args.sc and require("Module:scripts").getByCode(args.sc, true)

	local ret = export.request_script(lang, sc, args.usex, args.nocat, args.sort)
	
	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 export.checkScript(text, scriptCode, result)
	local scriptObject = require("Module:scripts").getByCode(scriptCode)
	
	if not scriptObject then
		error('The script code "' .. scriptCode .. '" is not recognized.')
	end
	
	local originalText = text
	
	-- Remove non-letter characters.
	text = mw.ustring.gsub(text, "", "")
	
	-- Remove all characters of the script in question.
	text = mw.ustring.gsub(text, "", "")
	
	if text ~= "" then
		if type(result) == "string" then
			error(result)
		else
			error('The text "' .. originalText .. '" contains the letters "' .. text .. '" that do not belong to the ' .. scriptObject:getDisplayForm() .. '.', 2)
		end
	end
end

return export