模块:Sig2User
此模块的文档可以在模块:Sig2User/doc创建
-- Module:Sig2User
-- Made with ♥ by zhmoe:User:Leranjun
--CC-BY-NC-SA 3.0 From zhmoe
-- This module takes a signature and returns the username found in the signature.
-- For efficiency reasons, the module first looks for the "common" namespace aliases
-- before obtaining all aliases from the mw.site.namespaces table.
local p = {}
local getArgs = require("Module:Arguments").getArgs
local function isempty(s)
return not s or s == ""
end
local function throw(e, suppress)
return suppress and ("Error: " .. e) or error(e)
end
local function toComp(v)
local r =
mw.ustring.gsub(
mw.ustring.gsub(v, "%s", "[ _]"),
"(%%?)(.)",
function(percent, letter)
if percent ~= "" or not letter:match("%a") then -- do not use ustring!
-- if the '%' matched, or `letter` is not a letter, return "as is"
return percent .. letter
else
-- else, return a case-insensitive character class of the matched letter
return mw.ustring.format("[%s%s]", mw.ustring.lower(letter), mw.ustring.upper(letter))
end
end
)
return r
end
local function findFromTable(s, prefixes, lang)
local r = nil
for _, v in pairs(prefixes) do
pattern = "%[%[:?" .. toComp(v) .. "([^|/]+)|?.-/?.-%]%]"
r = mw.ustring.match(s, pattern)
if r then
return lang:ucfirst(r)
end
end
end
local function patternExtend()
local r = {}
local NS_USER = mw.site.namespaces[2]["aliases"]
local NS_USER_TALK = mw.site.namespaces[3]["aliases"]
local NS_SPECIAL = mw.site.namespaces[-1]["aliases"]
for _, v in pairs(NS_USER) do
table.insert(r, v .. ":")
end
for _, v in pairs(NS_USER_TALK) do
table.insert(r, v .. ":")
end
for _, v in pairs(NS_SPECIAL) do
table.insert(r, v .. ":Contributions/")
table.insert(r, v .. ":用[户戶][贡貢][献獻]/")
end
return r
end
function p._main(args)
local s = args[1]
local suppress = args["suppress"]
if isempty(s) then
return throw("Empty string", suppress)
end
-- Common namespace aliases
local COMMON_PREFIX = {
"User:",
"U:",
"User talk:",
"Special:Contributions/",
"Special:用[户戶][贡貢][献獻]/"
}
local lang = mw.language.getContentLanguage()
local r = findFromTable(s, COMMON_PREFIX, lang)
if r then
return r
end
-- All namespace aliases
r = findFromTable(s, patternExtend(), lang)
if r then
return r
end
-- Nothing found :(
return throw("Invalid signature", suppress)
end
function p.main(frame)
return p._main(getArgs(frame))
end
return p