logo

Module:Utilities是什么意思_Module:Utilities读音|解释_Module:Utilities同义词|反义词

Module:Utilities

该模块导出各种通用功能,可供其他模块使用。

pattern_escape

pattern_escape(text)

跳脱匹配模式(Lua的正则表达式版本)中使用的魔法字符。比如,"^$()%.[]*+-?" 变成 "%^%$%(%)%%%.%[%]%*%+%-%?"

format_categories

format_categories(categories, lang, sort_key, sort_base, force_output)

形成一个分类名称的列表(表格)。输出是一个由所有类别组成的字符串,每个类别都使用了[[Category:...]],并添加了给定的排序键。如果命名空间不是主命名空间、附录命名空间或重构命名空间,输出将是一个空字符串,除非给出force_output。如果没有给定排序键:

  1. 默认的排序键是通过使用sort_base(如果有给定的话)或当前的子页面名称,以及去除开头的连字符(以便后缀可以在没有键的情况下进行排序)而生成的。
  2. 如果给定的语言有排序键,则其将会被用来创建一个遵循该语言规则的排序键。

template_categorize

{{#invoke:utilities|template_categorize}}

这个函数使用于 {{categorize}}{{catlangname}}{{catlangcode}} 等模板。

catfix

这个函数添加了一个“catfix”,它被用于特定语言的分类页面,为所有条目名称添加语言属性,通常是文字类(script classes)。语言属性和文字类的添加使条目名称显示得更好(使用MediaWiki:Common.css中指定的特定语言或脚本样式),这对于在浏览器中没有一致字体支持的非英语语言尤其重要。

语言属性是为所有语言添加的,但脚本类只为在其数据文件中列出文字的语言添加,或者为在Module:utilities/datacatfix_script列表中列出一个默认脚本的语言添加。有些语言显然有一个默认文字,但在他们的数据文件中仍有其他文字,因此需要指定他们的默认文字。其他语言则没有默认文字。

  • 塞尔维亚-克罗地亚语经常以拉丁字母和西里尔字母两种文字书写。因为使用两种文字,所以塞尔维亚-克罗地亚语不能在其分类页面的条目中使用文字类别,因为一次只能指定一种文字类别。
  • 俄语通常用西里尔文字(Cyrl)书写,但盲文(Brai)也列于其数据文件中。因此,俄语需要在catfix_script列表中加入一个条目,这样Cyrl(西里尔字母)文字类就会被应用于其分类页面中的条目。

要查看一种语言所列出的文字,请见Module:languages,并使用搜索框找到该语言的数据文件。要想知道一个文字代码的含义,请在Module:scripts/data中搜索该文字代码。


local export = {}

local data = mw.loadData("Module:utilities/data")
local notneeded = data.notneeded
local neededhassubpage = data.neededhassubpage

-- A helper function to escape magic characters in a string
-- Magic characters: ^$()%.[]*+-?
function export.pattern_escape(text)
	if type(text) == "table" then
		text = text.args[1]
	end
	text = mw.ustring.gsub(text, "([%^$()%%.%[%]*+%-?])", "%%%1")
	return text
end

function export.plain_gsub(text, pattern, replacement)
	local invoked = false
	
	if type(text) == "table" then
		invoked = true
		
		if text.args then
			local frame = text
			
			local params = {
				[1] = {},
				[2] = {},
				[3] = { allow_empty = true },
			}
			
			local args = require("Module:parameters").process(frame.args, params)
			
			text = args[1]
			pattern = args[2]
			replacement = args[3]
		else
			error("If the first argument to plain_gsub is a table, it should be a frame object.")
		end
	else
		if not ( type(pattern) == "string" or type(pattern) == "number" ) then
			error("The second argument to plain_gsub should be a string or a number.")
		end
		
		if not ( type(replacement) == "string" or type(replacement) == "number" ) then
			error("The third argument to plain_gsub should be a string or a number.")
		end
	end
	
	pattern = export.pattern_escape(pattern)
	
	if invoked then
		text = mw.ustring.gsub(text, pattern, replacement)
		return text
	else
		return mw.ustring.gsub(text, pattern, replacement)
	end
end

--[[
Format the categories with the appropriate sort key. CATEGORIES is a list of
categories.
	-- LANG is an object encapsulating a language; if nil, the object for
	   language code 'und' (undetermined) will be used.
	-- SORT_KEY is placed in the category invocation, and indicates how the
	   page will sort in the respective category. Normally this should be nil,
	   and a default sort key based on the subpage name (the part after the
	   colon) will be used.
	-- SORT_BASE lets you override the default sort key used when SORT_KEY is
	   nil. Normally, this should be nil, and a language-specific default sort
	   key is computed from the subpage name (e.g. for Russian this converts
	   Cyrillic ё to a string consisting of Cyrillic е followed by U+10FFFF,
	   so that effectively ё sorts after е instead of the default Wikimedia
	   sort, which (I think) is based on Unicode sort order and puts ё after я,
	   the last letter of the Cyrillic alphabet.
	-- FORCE_OUTPUT forces normal output in all namespaces. Normally, nothing
	   is output if the page isn't in the main, Appendix:, Reconstruction: or
	   Citations: namespaces.
]]
export.format_categories = require("Module:utilities/format_categories")

-- Used by {{categorize}}
function export.template_categorize(frame)
	local NAMESPACE = mw.title.getCurrentTitle().nsText
	local format = frame.args["format"]
	local args = frame:getParent().args
	
	local langcode = args[1]; if langcode == "" then langcode = nil end
	local sort_key = args["sort"]; if sort_key == "" then sort_key = nil end
	local no_prefix_fix = args["no_prefix_fix"]
	local categories = {}
	
	if not langcode then
		if NAMESPACE == "Template" then return "" end
		error("Language code has not been specified. Please pass parameter 1 to the template.")
	end
	
	local lang = require("Module:languages").getByCode(langcode)
	
	if not lang then
		if NAMESPACE == "Template" then return "" end
		error("The language code \"" .. langcode .. "\" is not valid.")
	end
	
	local prefix = ""
	
	local i = 2
	local cat = args[i]
	if format == "pos" then
		prefix = lang:getCanonicalName() .. ""
		while cat do
			if cat ~= "" then
				-- 如果cat里有'的'就把prefix插到最后一个'的'字的后面,否则把prefix插到cat的前面
				rindex_de = mw.ustring.find(cat, '的[^的]*$')
				if rindex_de and not no_prefix_fix then
					table.insert(categories, mw.ustring.sub(cat, 1, rindex_de) .. prefix .. mw.ustring.sub(cat, rindex_de + 1))
				else
					table.insert(categories, prefix .. cat)
				end
			end
			
			i = i + 1
			cat = args[i]
		end
	else
		if format == "topic" then
			prefix = lang:getCanonicalName() .. " "
		end
		
		while cat do
			if cat ~= "" then
				table.insert(categories, prefix .. cat)
			end
			i = i + 1
			cat = args[i]
		end
	end
		
	
	return export.format_categories(categories, lang, sort_key)
end

function export.catfix(lang, sc)
	if not lang then
		require("Module:debug").track("catfix/no lang")
		return nil
	elseif type(lang) ~= "table" then
		require("Module:debug").track("catfix/lang not table")
		return nil
	end
	local canonicalName = lang:getCanonicalName() or error('The first argument to the function "catfix" should be a language object from Module:languages.')
	
	if sc and not sc.getCode then
		error('The second argument to the function "catfix" should be a script object from Module:scripts.')
	end
	
	-- To add script classes to links on pages created by category boilerplate templates.
	if not sc then
		sc = data.catfix_scripts[lang:getCode()]
		if sc then
			sc = require("Module:scripts").getByCode(sc)
		end
	end
	
	return "<span id=\"catfix\" style=\"display:none;\" class=\"CATFIX-" .. mw.uri.anchorEncode(canonicalName) .. "\">" ..
		require("Module:script utilities").tag_text("&nbsp;", lang, sc, nil) ..
		"</span>"
end

function export.catfix_template(frame)
	local params = {
		[1] = {},
		[2] = { alias_of = "sc" },
		["sc"] = {},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local lang = require("Module:languages").getByCode(args[1]) or require("Module:languages").err(args[1], 1)
	
	local sc = args.sc
	if sc then
		sc = require("Module:scripts").getByCode(sc) or error('The script code "' .. sc .. '", provided in the second parameter, is not valid.')
	end
	
	return export.catfix(lang, sc)
end

-- Not exporting because it is not used yet.
local function getDateTense(frame) 
	local name_num_mapping = {["January"] = 1, ["February"] = 2, ["March"] = 3, ["April"] = 4, ["May"] = 5, ["June"] = 6, 
		["July"] = 7, ["August"] = 8, ["September"] = 9, ["October"] = 10, ["November"] = 11, ["December"] = 12, 
		[1] = 1, [2] = 2, [3] = 3, [4] = 4, [5] = 5, [6] = 6, [7] = 7, [8] = 8, [9] = 9, [10] = 10, [11] = 11, [12] = 12}
	local month = name_num_mapping[frame.args[2]]
	local date = os.time({year = frame.args[1], day = frame.args[3], month = month})
	local today = os.time() -- 12 AM/PM
	local diff = os.difftime(date, today)
	local daylength = 24 * 3600
	
	if diff < -daylength / 2 then return "past"
	else 
		if diff > daylength / 2  then return "future"
		else return "present" end
	end
end

function export.make_id(lang, str)
	--[[	If called with invoke, first argument is a frame object.
			If called by a module, first argument is a language object. ]]
	local invoked = false
	
	if type(lang) == "table" then
		if lang.args then
			invoked = true
			
			local frame = lang
			
			local params = {
				[1] = {},
				[2] = {},
			}
			
			local args = require("Module:parameters").process(frame:getParent().args, params)
			
			local langCode = args[1]
			str = args[2]
			
			local m_languages = require("Module:languages")
			
			lang = m_languages.getByCode(langCode) or m_languages.err(langCode, 1)
		elseif not lang.getCanonicalName then
			error("The first argument to make_id should be a language object.")
		end
	end

	if not ( type(str) == "string" or type(str) == "number" ) then
		error("The second argument to make_id should be a string or a number.")
	end
	
	local id = require("Module:senseid").anchor(lang, str)
	
	if invoked then
		return '<li class="senseid" id="' .. id .. '">'
	else
		return id
	end
end

return export