Drop enum in favor of arr
starcitizen>Alistair3149 Немає опису редагування |
starcitizen>Alistair3149 (Drop enum in favor of arr) |
||
| Рядок 2: | Рядок 2: | ||
local p = {} | local p = {} | ||
local libraryUtil = require( 'libraryUtil' ) | local libraryUtil = require( 'libraryUtil' ) | ||
local yn = require( 'Module:Yesno' ) | local yn = require( 'Module:Yesno' ) | ||
local param = require( 'Module:Paramtest' ) | local param = require( 'Module:Paramtest' ) | ||
| Рядок 61: | Рядок 60: | ||
return module | return module | ||
end | |||
local function dualGmatch( str, pat1, pat2 ) | |||
local f1 = string.gmatch( str, pat1 ) | |||
local f2 = string.gmatch( str, pat2 ) | |||
return function() | |||
return f1() or f2() | |||
end | |||
end | end | ||
| Рядок 70: | Рядок 77: | ||
if query:find( '%.%.' ) then | if query:find( '%.%.' ) then | ||
query = mw.text.split( query, '..', true ) | query = mw.text.split( query, '..', true ) | ||
query = | query = arr.map( query, function(x) return mw.text.trim(x) end ) | ||
query = | query = arr.map( query, function(x) return (x:match('^[\'\"](.-)[\'\"]$') or '%') end ) | ||
query = table.concat( query ) | query = table.concat( query ) | ||
else | else | ||
| Рядок 78: | Рядок 85: | ||
end | end | ||
query = query:gsub( '^[Mm]odule:', '' ) | query = query:gsub( '^[Mm]odule:', '' ) | ||
if dynamicRequireListQueryCache[ query ] then | if dynamicRequireListQueryCache[ query ] then | ||
| Рядок 124: | Рядок 127: | ||
content = content:gsub( '%-%-%[(=-)%[.-%]%1%]', '' ):gsub( '%-%-[^\n]*', '' ) -- Strip comments | content = content:gsub( '%-%-%[(=-)%[.-%]%1%]', '' ):gsub( '%-%-[^\n]*', '' ) -- Strip comments | ||
for match in dualGmatch( content, 'require%s*(%b())', 'require%s*((["\'])%s*[Mm]odule:.-%2)' ) do | for match in dualGmatch( content, 'require%s*(%b())', 'require%s*((["\'])%s*[Mm]odule:.-%2)' ) do | ||
| Рядок 143: | Рядок 138: | ||
elseif match ~= '' then | elseif match ~= '' then | ||
match = formatModuleName( match ) | match = formatModuleName( match ) | ||
table.insert( requireList, match ) | table.insert( requireList, match ) | ||
end | end | ||
| Рядок 205: | Рядок 195: | ||
if name:find( ':' ) then | if name:find( ':' ) then | ||
local ns = name:match( '^(.-):' ) | local ns = name:match( '^(.-):' ) | ||
if | if arr.contains( {'', 'template', 'user'}, ns:lower() ) then | ||
table.insert( usedTemplateList, name ) | table.insert( usedTemplateList, name ) | ||
elseif ns == ns:upper() then | elseif ns == ns:upper() then | ||
| Рядок 222: | Рядок 212: | ||
end | end | ||
requireList = requireList .. dynamicRequirelist:reject( loadDataList ) | |||
requireList = requireList:unique() | |||
requireList = | loadDataList = loadDataList .. dynamicLoadDataList:reject( requireList ) | ||
loadDataList = loadDataList:unique() | |||
loadDataList = | usedTemplateList = usedTemplateList:unique() | ||
loadDataList = | |||
usedTemplateList = | |||
table.sort( requireList ) | table.sort( requireList ) | ||
table.sort( loadDataList ) | table.sort( loadDataList ) | ||
| Рядок 243: | Рядок 231: | ||
local invokeList = {} | local invokeList = {} | ||
assert | assert( content ~= nil, string.format( '%s does not exist', templateName ) ) | ||
for moduleName, funcName in string.gmatch( content, '{{[{|safeubt:}]-#[Ii]nvoke:([^|]+)|([^}|]+)[^}]*}}' ) do | for moduleName, funcName in string.gmatch( content, '{{[{|safeubt:}]-#[Ii]nvoke:([^|]+)|([^}|]+)[^}]*}}' ) do | ||
moduleName = formatModuleName( moduleName ) | moduleName = formatModuleName( moduleName ) | ||
funcName = mw.text.trim( funcName ) | funcName = mw.text.trim( funcName ) | ||
if string.find( funcName, '^{{{' ) then | |||
funcName = funcName .. '}}}' | |||
end | |||
table.insert( invokeList, {moduleName=moduleName, funcName=funcName} ) | table.insert( invokeList, {moduleName=moduleName, funcName=funcName} ) | ||
end | end | ||
invokeList = | invokeList = arr.unique( invokeList, function(x) return x.moduleName..x.funcName end ) | ||
table.sort( invokeList, function(x, y) return x.moduleName..x.funcName < y.moduleName..y.funcName end ) | table.sort( invokeList, function(x, y) return x.moduleName..x.funcName < y.moduleName..y.funcName end ) | ||
| Рядок 277: | Рядок 267: | ||
end | end | ||
styleList = | styleList = arr.unique( styleList, function(x) return x.styleName end ) | ||
table.sort( styleList, function(x, y) return x.styleName < y.styleName end ) | table.sort( styleList, function(x, y) return x.styleName < y.styleName end ) | ||
| Рядок 312: | Рядок 302: | ||
local text = string.format( '%d %s', #list, listType ) | local text = string.format( '%d %s', #list, listType ) | ||
local button = '<span>' .. text .. ':</span>' | local button = '<span>' .. text .. ':</span>' | ||
list = | list = arr.map( list, function(x) return '\n# '..x end ) | ||
local content = '\n'..table.concat( list )..'\n\n' | local content = '\n'..table.concat( list )..'\n\n' | ||
| Рядок 377: | Рядок 367: | ||
---@return string | ---@return string | ||
local function formatInvokedByList( moduleName, addCategories, whatLinksHere ) | local function formatInvokedByList( moduleName, addCategories, whatLinksHere ) | ||
local templateData = | local function lcfirst( str ) | ||
templateData = | return string.gsub( str, '^[Mm]odule:.', string.lower ) | ||
return | end | ||
return y.moduleName | |||
local templateData = arr.map( whatLinksHere, function(x) return {templateName=x, invokeList=getInvokeCallList(x)} end ) | |||
templateData = arr.filter( templateData, function(x) | |||
return arr.any( x.invokeList, function(y) | |||
return lcfirst(y.moduleName) == lcfirst(moduleName) | |||
end ) | end ) | ||
end ) | end ) | ||
| Рядок 425: | Рядок 419: | ||
---@return string | ---@return string | ||
local function formatRequiredByList( moduleName, addCategories, whatLinksHere ) | local function formatRequiredByList( moduleName, addCategories, whatLinksHere ) | ||
local childModuleData = | local childModuleData = arr.map( whatLinksHere, function ( title ) | ||
local requireList, loadDataList = getRequireList( title ) | local requireList, loadDataList = getRequireList( title ) | ||
return {name=title, requireList=requireList, loadDataList=loadDataList} | return {name=title, requireList=requireList, loadDataList=loadDataList} | ||
end ) | end ) | ||
local requiredByList = | local requiredByList = arr.map( childModuleData, function ( item ) | ||
if | if arr.any( item.requireList, function(x) return x:lower()==moduleName:lower() end ) then | ||
if item.name:find( '%%' ) then | if item.name:find( '%%' ) then | ||
return formatDynamicQueryLink( item.name ) | return formatDynamicQueryLink( item.name ) | ||
| Рядок 440: | Рядок 434: | ||
end ) | end ) | ||
local loadedByList = | local loadedByList = arr.map( childModuleData, function ( item ) | ||
if | if arr.any( item.loadDataList, function(x) return x:lower()==moduleName:lower() end ) then | ||
if item.name:find( '%%' ) then | if item.name:find( '%%' ) then | ||
return formatDynamicQueryLink( item.name ) | return formatDynamicQueryLink( item.name ) | ||
| Рядок 495: | Рядок 489: | ||
if #requireList > COLLAPSE_LIST_LENGTH_THRESHOLD then | if #requireList > COLLAPSE_LIST_LENGTH_THRESHOLD then | ||
requireList = collapseList( requireList, 'require', 'modules' ) | |||
end | end | ||
| Рядок 591: | Рядок 585: | ||
local title = mw.title.getCurrentTitle() | local title = mw.title.getCurrentTitle() | ||
-- Leave early if not in module | -- Leave early if not in module or template namespace or if module is part of exchange or data groups | ||
if param.is_empty( currentPageName ) and | if param.is_empty( currentPageName ) and ( not arr.contains( {'Module', 'Template'}, title.nsText ) ) then | ||
return '' | return '' | ||
end | end | ||
currentPageName = param.default_to( currentPageName, title.fullText ) | currentPageName = param.default_to( currentPageName, title.fullText ) | ||
currentPageName = string.gsub( currentPageName, '/[Dd]oc$', '' ) | currentPageName = string.gsub( currentPageName, '/[Dd]oc$', '' ) | ||
currentPageName = | currentPageName = formatPageName( currentPageName ) | ||
addCategories = yn( param.default_to( addCategories, title.subpageText~='doc' ) ) | addCategories = yn( param.default_to( addCategories, title.subpageText~='doc' ) ) | ||
moduleIsUsed = yn( param.default_to( isUsed, false ) ) | moduleIsUsed = yn( param.default_to( isUsed, false ) ) | ||
if title.text: | if title.text:lower():find( 'sandbox' ) then | ||
moduleIsUsed = true -- Don't show sandbox modules as unused | moduleIsUsed = true -- Don't show sandbox modules as unused | ||
end | end | ||
| Рядок 636: | Рядок 627: | ||
local requireList, loadDataList, usedTemplateList = getRequireList( currentPageName, true ) | local requireList, loadDataList, usedTemplateList = getRequireList( currentPageName, true ) | ||
requireList = | requireList = arr.map( requireList, function ( moduleName ) | ||
if moduleName:find( '%%' ) then | if moduleName:find( '%%' ) then | ||
return formatDynamicQueryLink( moduleName ) | return formatDynamicQueryLink( moduleName ) | ||
| Рядок 644: | Рядок 635: | ||
end ) | end ) | ||
loadDataList = | loadDataList = arr.map( loadDataList, function ( moduleName ) | ||
if moduleName:find( '%%' ) then | if moduleName:find( '%%' ) then | ||
return formatDynamicQueryLink( moduleName ) | return formatDynamicQueryLink( moduleName ) | ||
| Рядок 652: | Рядок 643: | ||
end ) | end ) | ||
usedTemplateList = | usedTemplateList = arr.map( usedTemplateList, function( templateName ) | ||
if string.find( templateName, ':' ) then -- Real templates are prefixed by a namespace, magic words are not | if string.find( templateName, ':' ) then -- Real templates are prefixed by a namespace, magic words are not | ||
return '[['..templateName..']]' | return '[['..templateName..']]' | ||