Module:DependencyList: Difference between revisions
mNo edit summary |
mNo edit summary |
||
| (7 intermediate revisions by the same user not shown) | |||
| Line 13: | Line 13: | ||
--- Used in case 'require( varName )' is found. Attempts to find a string value stored in 'varName'. | --- Used in case 'require( varName )' is found. Attempts to find a string value stored in 'varName'. | ||
---@param content string | ---@param content string The content of the module to search in | ||
---@param varName string | ---@param varName string | ||
---@return string | ---@return string | ||
local function substVarValue( content, varName ) | local function substVarValue( content, varName ) | ||
local res = content:match( varName .. '%s*=%s*(%b""%s-%.*)' ) or content:match( varName .. "%s*=%s*(%b''%s-%.*)" ) or '' | |||
return | if res:find( '^(["\'])[Mm]odule:[%S]+%1' ) and not res:find( '%.%.' ) and not res:find( '%%%a' ) then | ||
return mw.text.trim( res ) | |||
else | else | ||
local | return '' | ||
end | |||
end | |||
---@param capture string | |||
---@param content string The content of the module to search in | |||
---@return string | |||
local function extractModuleName( capture, content ) | |||
capture = capture:gsub( '^%(%s*(.-)%s*%)$', '%1' ) | |||
if capture:find( '^(["\']).-%1$' ) then -- Check if it is already a pure string | |||
return capture | |||
elseif capture:find( '^[%a_][%w_]*$' ) then -- Check if if is a single variable | |||
return substVarValue( content, capture ) | |||
end | end | ||
return capture | |||
end | |||
---@param str string | |||
---@return string | |||
local function formatModuleName( str ) | |||
return (str:gsub( '^([\'\"])(.-)%1$', function(_, x) return x end ) -- Only remove quotes at start and end of string if both are the same type | |||
:gsub( '_', ' ' ) | |||
:gsub( '^.', string.upper ) | |||
:gsub( ':(.)', function(x) return ':'..x:upper() end )) | |||
end | end | ||
| Line 32: | Line 52: | ||
--- Will return a list of pages which satisfy this pattern where 'isTheBest' can take any value. | --- Will return a list of pages which satisfy this pattern where 'isTheBest' can take any value. | ||
---@param query string | ---@param query string | ||
---@return string[] | ---@return string[] Sequence of strings | ||
local function getDynamicRequireList( query ) | local function getDynamicRequireList( query ) | ||
query = mw.text.split( query, '..', true ) | if query:find( '%.%.' ) then | ||
query = mw.text.split( query, '..', true ) | |||
query = enum.map( query, function(x) return mw.text.trim(x) end ) | |||
query = enum.map( query, function(x) return (x:match('^[\'\"](.-)[\'\"]$') or '%') end ) | |||
query = table.concat( query ) | |||
else | |||
_, query = query:match( '(["\'])(.-)%1' ) | |||
query = query:gsub( '%%%a', '%%' ) | |||
end | |||
query = query:gsub( '^[Mm]odule:', '' ) | query = query:gsub( '^[Mm]odule:', '' ) | ||
if query: | if query:find( '^[Ee]xchange/' ) or query:find( '^[Dd]ata/' ) then | ||
return { 'Module:' .. query } -- This format will later be used by formatDynamicQueryLink() | return { 'Module:' .. query } -- This format will later be used by formatDynamicQueryLink() | ||
end | end | ||
| Line 51: | Line 76: | ||
namespace = 'Module', | namespace = 'Module', | ||
titlematch = query, | titlematch = query, | ||
nottitlematch = '%/doc | nottitlematch = '%/doc|'..query..'/%', | ||
distinct = 'strict', | distinct = 'strict', | ||
ignorecase = true, | ignorecase = true, | ||
| Line 94: | Line 119: | ||
end | end | ||
for match in dualGmatch( content, 'require%s*%( | for match in dualGmatch( content, 'require%s*(%b())', 'require%s*((["\'])%s*[Mm]odule:.-%2)' ) do | ||
match = mw.text.trim( match ) | match = mw.text.trim( match ) | ||
match = | match = extractModuleName( match, content ) | ||
if match:find( '%.%.' ) then | if match:find( '%.%.' ) or match:find( '%%%a' ) then | ||
for _, x in ipairs( getDynamicRequireList( match ) ) do | for _, x in ipairs( getDynamicRequireList( match ) ) do | ||
table.insert( dynamicRequirelist, x ) | table.insert( dynamicRequirelist, x ) | ||
end | end | ||
elseif match ~= '' then | elseif match ~= '' then | ||
match = match | match = formatModuleName( match ) | ||
if match == ' | if match == 'LibraryUtil' then | ||
match = 'Module:LibraryUtil' | match = 'Module:LibraryUtil' | ||
end | end | ||
| Line 113: | Line 138: | ||
end | end | ||
for match in dualGmatch( content, 'mw%.loadData%s*%( | for match in dualGmatch( content, 'mw%.loadData%s*(%b())', 'mw%.loadData%s*((["\'])%s*[Mm]odule:.-%2)' ) do | ||
match = mw.text.trim( match ) | match = mw.text.trim( match ) | ||
match = | match = extractModuleName( match, content ) | ||
if match:find( '%.%.' ) then | if match:find( '%.%.' ) or match:find( '%%%a' ) then | ||
for _, x in ipairs( getDynamicRequireList( match ) ) do | for _, x in ipairs( getDynamicRequireList( match ) ) do | ||
table.insert( dynamicLoadDataList, x ) | table.insert( dynamicLoadDataList, x ) | ||
end | end | ||
elseif match ~= '' then | elseif match ~= '' then | ||
match = match | match = formatModuleName( match ) | ||
table.insert( loadDataList, match ) | table.insert( loadDataList, match ) | ||
end | end | ||
| Line 172: | Line 197: | ||
end | end | ||
else | else | ||
if | if name:match( '^%u+$' ) or name == '!' then | ||
table.insert( usedTemplateList, name ) -- Probably a magic word | table.insert( usedTemplateList, name ) -- Probably a magic word | ||
else | else | ||
| Line 195: | Line 220: | ||
return requireList, loadDataList, usedTemplateList | return requireList, loadDataList, usedTemplateList | ||
end | end | ||
| Line 214: | Line 232: | ||
for moduleName, funcName in string.gmatch( content, '{{[{|safeubt:}]-#[Ii]nvoke:([^|]+)|([^}|]+)[^}]*}}' ) do | for moduleName, funcName in string.gmatch( content, '{{[{|safeubt:}]-#[Ii]nvoke:([^|]+)|([^}|]+)[^}]*}}' ) do | ||
moduleName = string.format( 'Module:%s', mw.text.trim( moduleName ) ) | |||
moduleName = string.format( 'Module:%s', moduleName ) | moduleName = formatModuleName( moduleName ) | ||
moduleName = moduleName | funcName = mw.text.trim( funcName ) | ||
table.insert( invokeList, {moduleName=moduleName, funcName=funcName} ) | table.insert( invokeList, {moduleName=moduleName, funcName=funcName} ) | ||
end | end | ||
| Line 229: | Line 247: | ||
---@param addCategories boolean | ---@param addCategories boolean | ||
---@return string | ---@return string | ||
local function collapseList( list, id, listType ) | local function collapseList( list, id, listType ) | ||
local text = string.format( '%d %s', #list, listType ) | local text = string.format( '%d %s', #list, listType ) | ||
| Line 296: | Line 296: | ||
for _, item in ipairs( invokeList ) do | for _, item in ipairs( invokeList ) do | ||
table.insert( res, string.format( | table.insert( res, string.format( | ||
"<div class='seealso'>'''''%s''' invokes function '''%s''' in [[%s]] using [[ | "<div class='seealso'>'''''%s''' invokes function '''%s''' in [[%s]] using [[GSWiki:Lua|Lua]]''.</div>", | ||
templateName, | templateName, | ||
item.funcName, | item.funcName, | ||
| Line 316: | Line 316: | ||
local function formatInvokedByList( moduleName, addCategories, whatLinksHere ) | local function formatInvokedByList( moduleName, addCategories, whatLinksHere ) | ||
local templateData = enum.map( whatLinksHere, function(x) return {templateName=x, invokeList=getInvokeCallList(x)} end ) | local templateData = enum.map( whatLinksHere, function(x) return {templateName=x, invokeList=getInvokeCallList(x)} end ) | ||
templateData = enum.filter( templateData, function(x) return enum.any( x.invokeList, function(y) return y.moduleName==moduleName end ) end ) | templateData = enum.filter( templateData, function(x) | ||
return enum.any( x.invokeList, function(y) | |||
return y.moduleName:lower() == moduleName:lower() | |||
end ) | |||
end ) | |||
local invokedByList = {} | local invokedByList = {} | ||
| Line 365: | Line 369: | ||
local requiredByList = enum.map( childModuleData, function ( item ) | local requiredByList = enum.map( childModuleData, function ( item ) | ||
if enum.any( item.requireList, function(x) return x==moduleName end ) then | if enum.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 ) | ||
| Line 375: | Line 379: | ||
local loadedByList = enum.map( childModuleData, function ( item ) | local loadedByList = enum.map( childModuleData, function ( item ) | ||
if enum.any( item.loadDataList, function(x) return x==moduleName end ) then | if enum.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 ) | ||
| Line 502: | Line 506: | ||
local title = mw.title.getCurrentTitle() | local title = mw.title.getCurrentTitle() | ||
-- Leave early if not in module, template or calculator namespace or if module is part of exchange or data groups | |||
if param.is_empty( currentPageName ) and ( | if param.is_empty( currentPageName ) and ( | ||
( not enum.contains( {'Module', 'Template', 'Calculator'}, title.nsText ) ) or | |||
( title.nsText == 'Module' and ( enum.contains( {'Exchange', 'Exchange historical', 'Data'}, title.text:match( '^(.-)/' ) ) ) ) | |||
) 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 = formatModuleName( 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 ) ) | ||
| Line 531: | Line 536: | ||
namespace = 'Module', | namespace = 'Module', | ||
linksto = currentPageName, | linksto = currentPageName, | ||
nottitlematch = '%/doc| | nottitlematch = '%/doc|Exchange/%|Exchange historical/%|Data/%|' .. currentPageName:gsub( 'Module:', '' ), | ||
distinct = 'strict', | distinct = 'strict', | ||
ignorecase = true, | ignorecase = true, | ||
| Line 558: | Line 563: | ||
usedTemplateList = enum.map( usedTemplateList, function( templateName ) | usedTemplateList = enum.map( usedTemplateList, function( templateName ) | ||
if | if string.find( templateName, ':' ) then -- Real templates are prefixed by a namespace, magic words are not | ||
return '[['..templateName..']]' | return '[['..templateName..']]' | ||
else | else | ||
| Line 572: | Line 577: | ||
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 ) ) | ||
return table.concat( res ) | return table.concat( res ) | ||