Module:Main: Difference between revisions
Jump to navigation
Jump to search
JumbledLimes (talk | contribs) m (The Way of Sumo uses roman numerals) |
PhantomCaleb (talk | contribs) (support pipe separators like Template:List so that Template:, is not needed) |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local utilsArg = require("Module:UtilsArg") | local utilsArg = require("Module:UtilsArg") | ||
Line 8: | Line 6: | ||
local utilsString = require("Module:UtilsString") | local utilsString = require("Module:UtilsString") | ||
local utilsTable = require("Module:UtilsTable") | local utilsTable = require("Module:UtilsTable") | ||
local CATEGORY_INVALID_ARGS = "[[Category:"..require("Module:Constants/category/invalidArgs").."]]" | |||
function p.Main(frame) | function p.Main(frame) | ||
Line 13: | Line 13: | ||
local categories = err and err.categoryText or "" | local categories = err and err.categoryText or "" | ||
local pages = args.pages | |||
if pages == nil or #pages == 0 then | |||
return "", categories | return "", categories | ||
elseif #pages == 1 then -- assume list items are delimited by commas instead of pipes | |||
pages = utilsString.split(pages[1]) | |||
end | end | ||
pages = utilsTable.map(pages, p.formatListItem) | |||
local pagesOld = pages | |||
pages = utilsTable.compact(pages) -- remove nils | pages = utilsTable.compact(pages) -- remove nils | ||
if # | if #pagesOld ~= #pages then | ||
utilsError.warn(string.format("Page list cannot have blank entries: <code>%s</code>", frame:getParent().args[1])) | utilsError.warn(string.format("Page list cannot have blank entries: <code>%s</code>", frame:getParent().args[1])) | ||
categories = categories.. | categories = categories..CATEGORY_INVALID_ARGS | ||
end | end | ||
local text = #pages > 1 and "Main articles: " or "Main article: " | local text = #pages > 1 and "Main articles: " or "Main article: " | ||
local pageList = text..mw.text.listToText(pages) | local pageList = text..mw.text.listToText(pages) | ||
local | local hatnote = frame:expandTemplate({ | ||
title = "Hatnote", | |||
args = {pageList}, | |||
}) | |||
return | return hatnote, categories | ||
end | end | ||
Line 50: | Line 55: | ||
["Main"] = { | ["Main"] = { | ||
purpose = "To be placed under a heading when that section's topic has its own page or pages.", | purpose = "To be placed under a heading when that section's topic has its own page or pages.", | ||
categories = {"Formatting | categories = {"Formatting templates"}, | ||
params = { | params = { | ||
[ | ["..."] = { | ||
name = "pages", | name = "pages", | ||
required = true, | required = true, | ||
desc = " | desc = "Article names", | ||
trim = true, | trim = true, | ||
nilIfEmpty = true, | nilIfEmpty = true, | ||
}, | }, | ||
}, | }, | ||
} | } | ||
} | } | ||
return p | return p |
Latest revision as of 14:48, 21 July 2023
This is the main module for the following templates:
Not to be confused with Module:Main Page.
local p = {}
local utilsArg = require("Module:UtilsArg")
local utilsError = require("Module:UtilsError")
local utilsMarkup = require("Module:UtilsMarkup")
local utilsString = require("Module:UtilsString")
local utilsTable = require("Module:UtilsTable")
local CATEGORY_INVALID_ARGS = "[[Category:"..require("Module:Constants/category/invalidArgs").."]]"
function p.Main(frame)
local args, err = utilsArg.parse(frame:getParent().args, p.Templates.Main)
local categories = err and err.categoryText or ""
local pages = args.pages
if pages == nil or #pages == 0 then
return "", categories
elseif #pages == 1 then -- assume list items are delimited by commas instead of pipes
pages = utilsString.split(pages[1])
end
pages = utilsTable.map(pages, p.formatListItem)
local pagesOld = pages
pages = utilsTable.compact(pages) -- remove nils
if #pagesOld ~= #pages then
utilsError.warn(string.format("Page list cannot have blank entries: <code>%s</code>", frame:getParent().args[1]))
categories = categories..CATEGORY_INVALID_ARGS
end
local text = #pages > 1 and "Main articles: " or "Main article: "
local pageList = text..mw.text.listToText(pages)
local hatnote = frame:expandTemplate({
title = "Hatnote",
args = {pageList},
})
return hatnote, categories
end
function p.formatListItem(item)
item = mw.text.decode(item)
if utilsString.isBlank(item) then
return nil
elseif utilsMarkup.containsLink(item) then
return item
elseif string.find(item, "#") then
local linkDisplay = string.gsub(item, "#", " § ")
return string.format("[[%s|%s]]", item, linkDisplay)
else
return string.format("[[%s]]", item)
end
end
p.Templates = {
["Main"] = {
purpose = "To be placed under a heading when that section's topic has its own page or pages.",
categories = {"Formatting templates"},
params = {
["..."] = {
name = "pages",
required = true,
desc = "Article names",
trim = true,
nilIfEmpty = true,
},
},
}
}
return p