全站通知:
模块:Name
刷
历
编
跳到导航
跳到搜索
local utils = require("Module:Utils")
local en_name_lower_to_id = mw.loadData("Module:Items/en/name/ids")
local zh_name_to_id = mw.loadData("Module:Items/zh/name/ids")
local zh_id_to_name = mw.loadData("Module:Items/zh/ids/name")
local en_id_to_name = mw.loadData("Module:Items/en/ids/name")
local p = {}
local overrides = mw.loadData("Module:Name/data/overrides")
function p.getName(input)
local text = utils.getArg(input)
if not text then return "" end
text = text:lower():gsub("_", " ")
if overrides[text] then
return overrides[text]
end
local id = en_name_lower_to_id[text]
return id and zh_id_to_name[id] or ""
end
function p.getDefaultName(input)
local text = utils.getArg(input)
if not text then return "" end
local id = zh_name_to_id[text]
return id and en_id_to_name[id] or ""
end
local function fileExists(filename)
local url = mw.getCurrentFrame():callParserFunction('filepath', filename)
return url ~= ''
end
local function getEnglishName(text)
if not text or text == "" then
return ""
end
local pagesModule = require("Module:Pages")
local fakeFrame = {
args = {[1] = text}
}
return pagesModule.getEnglishName(fakeFrame) or ""
end
function p.render(frame)
local args = frame.args
if frame == mw.getCurrentFrame() then
args = require('Module:Arguments').getArgs(frame, {
wrappers = 'Template:Name'
})
end
local inputName = args[1] or ""
local description = args[2] or ""
local size = args.size or "24"
local link = args.link
local alt = args.alt
local class = args.class
local imageName = inputName
if not fileExists(inputName .. ".png") then
local englishName = getEnglishName(inputName)
if englishName and englishName ~= "" and fileExists(englishName .. ".png") then
imageName = englishName
else
imageName = "Blank icon"
end
end
local linkText
if link then
linkText = "[[" .. link .. "]]"
elseif alt then
linkText = "[[" .. alt .. "]]"
else
local chineseName = p.getName(inputName)
if chineseName and chineseName ~= "" then
linkText = "[[" .. chineseName .. "]]"
else
linkText = "[[" .. inputName .. "]]"
end
end
local cssClass = "nametemplate"
if class == "inline" then
cssClass = cssClass .. "inline"
end
local result = string.format(
'<span class="%s">[[File:%s.png|%spx|link=]] %s%s</span>',
cssClass,
imageName,
size,
linkText,
description ~= "" and "(" .. description .. ")" or ""
)
return result
end
function p.debug()
local frame = mw.getCurrentFrame()
return frame:callParserFunction('filepath', 'Example1.jpg') == ''
end
function p.debug2()
local results = {}
for key, _ in pairs(overrides) do
local tmp = p.getName(key)
if tmp ~= "" then
tmp = key .. '=' ..tmp
table.insert(results, tmp)
end
end
return table.concat(results, "\n")
end
return p