(创建页面,内容为“local p = {}; states = require('Module:State/List'); local getArgs = require('Module:Arguments').getArgs function p.state_name(frame) local args = getArgs(frame) return p._state_name(args) end function p._state_name(args) id = tonumber(args[1]) if not states[id] then return "<span style=\"color: red;\">Module:State/List does not have state:</span>" else return states[id].name end end function p.state_names(frame)…”) |
无编辑摘要 标签:已被回退 |
||
第1行: | 第1行: | ||
local p = {} | local p = {} | ||
states = require('Module:State/List') | -- 导入州名数据模块 | ||
local states = require('Module:State/List') | |||
-- 导入参数处理模块 | |||
local getArgs = require('Module:Arguments').getArgs | local getArgs = require('Module:Arguments').getArgs | ||
-- 根据ID获取州名(包括name2和name) | |||
function p._state_name(args) | function p._state_name(args) | ||
id = tonumber(args[1]) | local id = tonumber(args[1]) | ||
if not states[id] then | if not states[id] then | ||
return "<span style=\"color: red;\">[[Module:State/List]] does not have state:</span>" | return "<span style=\"color: red;\">[[Module:State/List]] does not have state:</span>" | ||
else | else | ||
local name2 = states[id].name2 or "" | |||
local name = states[id].name or "" | |||
-- 返回name2和name | |||
return name2 .. " (" .. name .. ")" | |||
end | end | ||
end | end | ||
-- 根据多个ID获取州名列表(包括name2和name) | |||
function p._state_names(args) | function p._state_names(args) | ||
output = {} | local output = {} | ||
for i, j in pairs(args) do | for i, j in pairs(args) do | ||
if type(i) == "number" then | if type(i) == "number" then | ||
第28行: | 第28行: | ||
return "<span style=\"color: red;\">[[Module:State/List]] does not have state: " .. j .. "</span>" | return "<span style=\"color: red;\">[[Module:State/List]] does not have state: " .. j .. "</span>" | ||
end | end | ||
-- 返回name2和name | |||
local name2 = states[tonumber(j)].name2 or "" | |||
local name = states[tonumber(j)].name or "" | |||
if not args.nob then | if not args.nob then | ||
name = string.format("'''%s'''", name) | |||
end | end | ||
if not args.noid then | if not args.noid then | ||
name = name .. string.format(" ''(%s)''", j) | |||
end | end | ||
output[i] = | output[i] = name2 .. " (" .. name .. ")" | ||
end | end | ||
end | end | ||
-- 根据参数中的分隔符进行分隔 | |||
if args.sep then | if args.sep then | ||
sep = args.sep:gsub("\\n", "\n"):gsub("_", " ") | local sep = args.sep:gsub("\\n", "\n"):gsub("_", " ") | ||
return table.concat(output, sep) | |||
elseif #output == 1 then | elseif #output == 1 then | ||
return output[1] | |||
elseif #output == 2 then | elseif #output == 2 then | ||
return output[1] .. " and " .. output[2] | |||
else | else | ||
return table.concat(output, ", ", 1, #output-1) .. ", and " .. output[#output] | |||
end | end | ||
end | end | ||
-- 导出模块 | |||
return p | return p |
2024年5月13日 (一) 21:31的版本
可在Module:State/doc创建此模块的帮助文档
local p = {} -- 导入州名数据模块 local states = require('Module:State/List') -- 导入参数处理模块 local getArgs = require('Module:Arguments').getArgs -- 根据ID获取州名(包括name2和name) function p._state_name(args) local id = tonumber(args[1]) if not states[id] then return "<span style=\"color: red;\">[[Module:State/List]] does not have state:</span>" else local name2 = states[id].name2 or "" local name = states[id].name or "" -- 返回name2和name return name2 .. " (" .. name .. ")" end end -- 根据多个ID获取州名列表(包括name2和name) function p._state_names(args) local output = {} for i, j in pairs(args) do if type(i) == "number" then if not states[tonumber(j)] then return "<span style=\"color: red;\">[[Module:State/List]] does not have state: " .. j .. "</span>" end -- 返回name2和name local name2 = states[tonumber(j)].name2 or "" local name = states[tonumber(j)].name or "" if not args.nob then name = string.format("'''%s'''", name) end if not args.noid then name = name .. string.format(" ''(%s)''", j) end output[i] = name2 .. " (" .. name .. ")" end end -- 根据参数中的分隔符进行分隔 if args.sep then local sep = args.sep:gsub("\\n", "\n"):gsub("_", " ") return table.concat(output, sep) elseif #output == 1 then return output[1] elseif #output == 2 then return output[1] .. " and " .. output[2] else return table.concat(output, ", ", 1, #output-1) .. ", and " .. output[#output] end end -- 导出模块 return p