317
редагувань
starcitizen>Alistair3149 (Add support for `require("strict")`) |
м (56 ревізій: Template:Infobox_Species from StarCitizenTools) |
||
| (Не показані 13 проміжних версій 2 користувачів) | |||
| Рядок 16: | Рядок 16: | ||
local builtins = { | local builtins = { | ||
-- ["libraryUtil"] = "mw:Special:MyLanguage/Extension:Scribunto/Lua reference manual#libraryUtil", | --[[ | ||
["strict"] = "mw:Special:MyLanguage/Extension:Scribunto/Lua reference manual#strict", | ["libraryUtil"] = { | ||
link = "mw:Special:MyLanguage/Extension:Scribunto/Lua reference manual#libraryUtil", | |||
categories = {}, | |||
} | |||
]] | |||
["strict"] = { | |||
link = "mw:Special:MyLanguage/Extension:Scribunto/Lua reference manual#strict", | |||
categories = { "Strict mode modules" }, | |||
}, | |||
}; | }; | ||
| Рядок 70: | Рядок 78: | ||
local builtin = builtins[name]; | local builtin = builtins[name]; | ||
if builtin then | if builtin then | ||
return builtin .. "|" .. name; | return builtin.link .. "|" .. name, builtin; | ||
end | end | ||
end | end | ||
| Рядок 96: | Рядок 104: | ||
---@return string[] Sequence of strings | ---@return string[] Sequence of strings | ||
local function getDynamicRequireList( query ) | local function getDynamicRequireList( query ) | ||
local isDynamic = true; | |||
if query:find( '%.%.' ) then | if query:find( '%.%.' ) then | ||
query = mw.text.split( query, '..', true ) | query = mw.text.split( query, '..', true ) | ||
| Рядок 102: | Рядок 112: | ||
query = table.concat( query ) | query = table.concat( query ) | ||
else | else | ||
_, query = query:match( '(["\'])(.-)%1' ) | local _; _, query = query:match( '(["\'])(.-)%1' ) | ||
query = query:gsub( '%%%a', '%%' ) | |||
if query == nil then | |||
return {}, isDynamic | |||
end | |||
local replacements; | |||
query, replacements = query:gsub( '%%%a', '%%' ) | |||
if replacements == 0 then | |||
isDynamic = false; | |||
end | |||
end | end | ||
query = query:gsub( '^[Mm]odule:', '' ) | query = query:gsub( '^[Mm]odule:', '' ) | ||
if query:find( '^[Dd]ata/' ) then | |||
return { 'Module:' .. query }, isDynamic; -- This format will later be used by formatDynamicQueryLink() | |||
end | |||
if dynamicRequireListQueryCache[ query ] then | if dynamicRequireListQueryCache[ query ] then | ||
return dynamicRequireListQueryCache[ query ] | return dynamicRequireListQueryCache[ query ], isDynamic; | ||
end | end | ||
| Рядок 130: | Рядок 153: | ||
dynamicRequireListQueryCache[ query ] = list | dynamicRequireListQueryCache[ query ] = list | ||
return list | return list, isDynamic; | ||
end | end | ||
| Рядок 136: | Рядок 159: | ||
---@param moduleName string | ---@param moduleName string | ||
---@param searchForUsedTemplates boolean | ---@param searchForUsedTemplates boolean | ||
---@return string[], string[], string[] | ---@return string[], string[], string[], string[] | ||
local function getRequireList( moduleName, searchForUsedTemplates ) | local function getRequireList( moduleName, searchForUsedTemplates ) | ||
local content = mw.title.new( moduleName ):getContent() | local content = mw.title.new( moduleName ):getContent() | ||
| Рядок 144: | Рядок 167: | ||
local dynamicRequirelist = arr{} | local dynamicRequirelist = arr{} | ||
local dynamicLoadDataList = arr{} | local dynamicLoadDataList = arr{} | ||
local extraCategories = arr{} | |||
assert( content ~= nil, string.format( '%s does not exist', moduleName ) ) | assert( content ~= nil, string.format( '%s does not exist', moduleName ) ) | ||
| Рядок 158: | Рядок 182: | ||
end | end | ||
elseif match ~= '' then | elseif match ~= '' then | ||
match = formatModuleName( match, true ) | local builtin; | ||
match, builtin = formatModuleName( match, true ) | |||
table.insert( requireList, match ) | table.insert( requireList, match ) | ||
if builtin then | |||
local builtinCategories = builtin.categories; | |||
if type(builtinCategories) == "table" then | |||
for _, x in ipairs(builtinCategories) do | |||
table.insert(extraCategories, x); | |||
end | |||
end | |||
end | |||
end | end | ||
end | end | ||
| Рядок 195: | Рядок 229: | ||
match = mw.text.trim( match ) | match = mw.text.trim( match ) | ||
if func == 'require' | local dynList, isDynamic; | ||
if func == 'require' then | |||
dynList, isDynamic = getDynamicRequireList(match); | |||
if (isDynamic == false and #dynList == 1) then | |||
table.insert(requireList, dynList[1]); | |||
else for _, x in ipairs(dynList) do | |||
table.insert( dynamicRequirelist, x ) | table.insert( dynamicRequirelist, x ) | ||
end end | |||
elseif func == 'mw.loadData' then | |||
dynList, isDynamic = getDynamicRequireList(match); | |||
if (isDynamic == false and #dynList == 1) then | |||
table.insert(loadDataList, dynList[1]); | |||
else for _, x in ipairs(dynList) do | |||
table.insert( dynamicLoadDataList, x ) | |||
end end | |||
end | end | ||
end | end | ||
| Рядок 248: | Рядок 293: | ||
loadDataList = loadDataList:unique() | loadDataList = loadDataList:unique() | ||
usedTemplateList = usedTemplateList:unique() | usedTemplateList = usedTemplateList:unique() | ||
extraCategories = extraCategories:unique() | |||
table.sort( requireList ) | table.sort( requireList ) | ||
table.sort( loadDataList ) | table.sort( loadDataList ) | ||
table.sort( usedTemplateList ) | table.sort( usedTemplateList ) | ||
table.sort( extraCategories ) | |||
return requireList, loadDataList, usedTemplateList | return requireList, loadDataList, usedTemplateList, extraCategories | ||
end | end | ||
| Рядок 277: | Рядок 324: | ||
return invokeList | return invokeList | ||
end | end | ||
| Рядок 310: | Рядок 330: | ||
---@return string | ---@return string | ||
local function messageBoxUnused( pageName, addCategories ) | local function messageBoxUnused( pageName, addCategories ) | ||
local mbox = require( 'Module:Mbox' )._mbox | |||
local category = addCategories and '[[Category:Unused modules]]' or '' | |||
return mbox( | |||
'This module is unused.', | |||
string.format( 'This module is neither invoked by a template nor required/loaded by another module. If this is in error, make sure to add <code>{{[[Template:Documentation|Documentation]]}}</code>/<code>{{[[Template:No documentation|No documentation]]}}</code> to the calling template\'s or parent\'s module documentation.', | |||
pageName | |||
), | |||
{ icon = 'WikimediaUI-Alert.svg' } | |||
) .. category | |||
end | end | ||
| Рядок 582: | Рядок 591: | ||
) | ) | ||
table.insert( res, mHatnote._hatnote( msg, { icon='WikimediaUI-Code.svg' } ) ) | table.insert( res, mHatnote._hatnote( msg, { icon='WikimediaUI-Code.svg' } ) ) | ||
end | end | ||
| Рядок 641: | Рядок 626: | ||
moduleIsUsed = true -- Don't show sandbox modules as unused | moduleIsUsed = true -- Don't show sandbox modules as unused | ||
end | end | ||
if currentPageName:find( '^Template:' ) then | if currentPageName:find( '^Template:' ) then | ||
local ok, invokeList = pcall( getInvokeCallList, currentPageName ) | local ok, invokeList = pcall( getInvokeCallList, currentPageName ) | ||
if ok then | if ok then | ||
return formatInvokeCallList( currentPageName, addCategories, invokeList | return formatInvokeCallList( currentPageName, addCategories, invokeList ) | ||
else | else | ||
return userError(invokeList) | return userError(invokeList) | ||
| Рядок 672: | Рядок 655: | ||
} ) | } ) | ||
local requireList, loadDataList, usedTemplateList = getRequireList | local requireList, loadDataList, usedTemplateList, extraCategories; | ||
do | |||
local ok; | |||
ok, requireList, loadDataList, usedTemplateList, extraCategories = pcall(getRequireList, currentPageName, true); | |||
if not ok then | |||
return userError(requireList); | |||
end | |||
end | |||
requireList = arr.map( requireList, function ( moduleName ) | requireList = arr.map( requireList, function ( moduleName ) | ||
| Рядок 705: | Рядок 695: | ||
table.insert( res, formatUsedTemplatesList( currentPageName, addCategories, usedTemplateList ) ) | table.insert( res, formatUsedTemplatesList( currentPageName, addCategories, usedTemplateList ) ) | ||
table.insert( res, formatRequiredByList( currentPageName, addCategories, whatModulesLinkHere ) ) | table.insert( res, formatRequiredByList( currentPageName, addCategories, whatModulesLinkHere ) ) | ||
if addCategories then | |||
extraCategories = arr.map(extraCategories, function(categoryName) | |||
return "[[Category:" .. categoryName .. "]]"; | |||
end); | |||
table.insert(res, table.concat(extraCategories)); | |||
end | |||
if not moduleIsUsed then | if not moduleIsUsed then | ||