模块:Utils
来自星露谷物语扩展百科
更多操作
local p = {}
function p.getArg(input, index)
if not index then index = 1 end
if type(input) == "string" then return input end
local result = input[index] or (input.args and input.args[index]) or input:getParent().args[index] or ""
return result
end
function p.hasChinese(str)
return type(str) == 'string' and
string.match(str, "[\228-\233][\128-\191][\128-\191]") ~= nil
end
function p.LazyLoad(moduleName, toLowerCase)
toLowerCase = toLowerCase or false
local loaded = nil
local function doLoad()
if loaded == nil then
loaded = mw.loadData(moduleName)
if toLowerCase then
local lowerCaseLoaded = {}
for key, value in pairs(loaded) do
lowerCaseLoaded[string.lower(key)] = value
end
loaded = lowerCaseLoaded
end
end
return loaded
end
local meta = {}
meta.__index = function(_table, key) return doLoad()[key] end
meta.__pairs = function(_table) return pairs(doLoad()) end
meta.__ipairs = function(_table) return ipairs(doLoad()) end
local ret = {}
setmetatable(ret, meta)
return ret
end
function p.ExpandTemplate(title, args)
return mw.getCurrentFrame():expandTemplate{title = title, args = args}
end
function p.fileExists(filename)
local url = mw.getCurrentFrame():callParserFunction('filepath', filename)
return url ~= ''
end
function p.normalizeKey(text)
if not text then return nil end
return text:lower():gsub("_", " ")
end
p.lazyload = p.LazyLoad
p.lazyLoad = p.LazyLoad
p.expandTemplate = p.ExpandTemplate
return p