Clean up and add support for TemplateStyles
starcitizen>Alistair3149 Немає опису редагування |
starcitizen>Alistair3149 (Clean up and add support for TemplateStyles) |
||
| Рядок 191: | Рядок 191: | ||
if name:find( ':' ) then | if name:find( ':' ) then | ||
local ns = name:match( '^(.-):' ) | local ns = name:match( '^(.-):' ) | ||
if enum.contains( {'', 'template | if enum.contains( {'', 'template', 'user'}, ns:lower() ) then | ||
table.insert( usedTemplateList, name ) | table.insert( usedTemplateList, name ) | ||
elseif ns == ns:upper() then | elseif ns == ns:upper() then | ||
| Рядок 242: | Рядок 242: | ||
return invokeList | return invokeList | ||
end | |||
--- Returns a list with TemplateStyles found on page 'templateName'. | |||
---@param templateName string | |||
---@return table<string>[] | |||
local function getTemplateStylesList( templateName ) | |||
local content = mw.title.new( templateName ):getContent() | |||
local styleList = {} | |||
assert( param.has_content( content ), string.format( '%s does not exist', templateName ) ) | |||
for styleName in string.gmatch( content, '<templatestyles src="(.+)"' ) do | |||
if string.find( styleName, ':', 1, true ) then | |||
styleName = mw.text.trim( styleName ) | |||
else | |||
styleName = string.format( 'Template:%s', mw.text.trim( styleName ) ) | |||
end | |||
styleName = formatModuleName( styleName ) | |||
table.insert( styleList, {styleName=styleName} ) | |||
end | |||
styleList = enum.unique( styleList, function(x) return x.styleName end ) | |||
table.sort( styleList, function(x, y) return x.styleName < y.styleName end ) | |||
return styleList | |||
end | end | ||
| Рядок 335: | Рядок 360: | ||
---@param moduleName string | ---@param moduleName string | ||
---@param addCategories boolean | ---@param addCategories boolean | ||
---@param whatLinksHere string @A list generated by a dpl of pages in the Template | ---@param whatLinksHere string @A list generated by a dpl of pages in the Template namespace which link to moduleName. | ||
---@return string | ---@return string | ||
local function formatInvokedByList( moduleName, addCategories, whatLinksHere ) | local function formatInvokedByList( moduleName, addCategories, whatLinksHere ) | ||
| Рядок 509: | Рядок 534: | ||
templateName | templateName | ||
) ) | ) ) | ||
end | |||
return table.concat( res ) | |||
end | |||
---@param templateName string | |||
---@param addCategories boolean | |||
---@param styleList table<string>[] @This is the list returned by getTemplateStylesList() | |||
---@return string | |||
local function formatTemplateStylesList( templateName, addCategories, styleList ) | |||
local category = addCategories and '[[Category:Templates using TemplateStyles]]' or '' | |||
local res = {} | |||
for _, item in ipairs( styleList ) do | |||
table.insert( res, string.format( | |||
"<div role='note' class='hatnote'><div class=hatnote-container navigation-not-searchable'><span class='hatnote-icon metadata'>[[File:WikimediaUI-Code.svg|14px|link=]]</span>'''%s''' invokes [[%s]] using [[Star Citizen:TemplateStyles|TemplateStyles]].</div></div>", | |||
templateName, | |||
item.styleName | |||
) ) | |||
end | |||
if #styleList > 0 then | |||
table.insert( res, category ) | |||
end | end | ||
| Рядок 529: | Рядок 577: | ||
local title = mw.title.getCurrentTitle() | local title = mw.title.getCurrentTitle() | ||
-- Leave early if not in module, template | -- Leave early if not in module, 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 enum.contains( {'Module', 'Template | ( not enum.contains( {'Module', 'Template'}, title.nsText ) ) or | ||
( title.nsText == 'Module' and ( enum.contains( {'Exchange', 'Exchange historical', 'Data'}, title.text:match( '^(.-)/' ) ) ) ) | ( title.nsText == 'Module' and ( enum.contains( {'Exchange', 'Exchange historical', 'Data'}, title.text:match( '^(.-)/' ) ) ) ) | ||
) then | ) then | ||
| Рядок 547: | Рядок 595: | ||
end | end | ||
if currentPageName:find( '^Template | if currentPageName:find( '^Template:' ) then | ||
local invokeList = getInvokeCallList( currentPageName ) | local invokeList = getInvokeCallList( currentPageName ) | ||
return formatInvokeCallList( currentPageName, addCategories, invokeList ) | local styleList = getTemplateStylesList( currentPageName ) | ||
return formatInvokeCallList( currentPageName, addCategories, invokeList ) .. formatTemplateStylesList( currentPageName, addCategories, styleList ) | |||
end | end | ||