This documentation is transcluded from Module:ArticleList/doc. Changes can be proposed in the talk page.
This module is unused.
This module is neither invoked by a template nor required/loaded by another module. If this is in error, make sure to add
{{Documentation}}
/{{No documentation}}
to the calling template's or parent's module documentation.Function list |
---|
L 19 — getFirstImage L 95 — createTableHeader L 106 — createTableFooter L 115 — createTableRow L 134 — methodTable.render L 146 — methodTable.registerProperty L 150 — methodTable.registerProperties L 156 — methodTable.addArticle L 160 — methodTable.addArticles L 166 — methodTable.addArticlesForCategory L 172 — methodTable.addPropertyDisplayFunction L 176 — methodTable.addAlternativeHeader L 180 — ArticleList.new L 191 — ArticleList.fromArgs |
This template automatically generates a list of articles and their data based on Semantic MediaWiki properties.
Example usage
The following code is used to create the List of Coaches. It displays all articles in Category:Carriages. The columns are specified via the respective property names as anonymous parameters.
{{ArticleList
|category=Carriages
|Title
|Image
|Coupled With
|Status
|Spawn Points
|Seats
|Doors
|Gangway Doors
}}
No description.
Parameter | Description | Type | Status | |
---|---|---|---|---|
Category | category | The category that contains the articles to be displayed.
| Line | required |
local SMWUtil = require("Module:SMWUtil")
local getStatus = require("Module:Status").get
local ArticleList = {}
local methodTable = {}
local metatable = {
__index = methodTable,
__tostring = function(self)
return self:render()
end
}
local imageSize = "310px"
--- Returns the first image in a gallery, or the image if only an image is provided
--- @param input string
--- @return string
local function getFirstImage(input)
if input == nil then
return nil
end
if input == "" then
return "Placeholder.png"
end
if not string.find(input, ";") then
input = string.gsub(input, "^File:", "")
return input -- only an image
end
local images = mw.text.split(input, ";")
local firstImage = images[1]
local image = mw.text.split(firstImage, "=")[2]
image = string.gsub(image, "^File:", "")
return image
end
local display = function(data, context)
if data == "" then
return "N/A"
end
return data
end
local propertyDisplayFunctions = {
["Title"] = function(data, context)
local title = data
if title == "" then
return nil
end
local url = "[[" .. context.pageName .. "|" .. title .. "]]"
return url
end,
["Image"] = function(data, context)
local image = getFirstImage(data)
return "[[File:" .. image .. "|" .. imageSize .. "]]"
end,
["Status"] = function(data, context)
local statusText, statusClass = getStatus(display(data))
local statusDescription = SMWUtil.get(context.frame, context.pageName, "Status Description")
local hoverElement = string.format("<span class=\"smw-highlighter smwttinline\" data-state=\"inline\">%s<span class=\"smwttcontent\">%s</span></span>", statusText, statusDescription)
if statusDescription == "" then
hoverElement = nil
end
return " class=\"status-cell " .. statusClass .. "\" |" .. (hoverElement or statusText)
end,
["Signal Box"] = function(data, context)
if data ~= "" then
return data
end
local signalBoxes = SMWUtil.askForPages(
context.frame,
"[[Category:Signal Boxes]] [[Crossings Controlled::"
.. context.pageName .. "]] [[Concept:InGame]]"
)
return display(signalBoxes, context)
end,
}
local alternativeHeaders = {
["Title"] = "Name"
}
--- Creates a table header with the given headers
---@param headers table the headers to be added to the table
---@return string
local function createTableHeader(headers)
local str = "{| class=\"wikitable sortable\"\n"
for _, header in ipairs(headers) do
local headerText = alternativeHeaders[header] or header
str = str .. "!" .. headerText .. "\n"
end
return str
end
--- Creates a table footer
---@return string
local function createTableFooter()
return "|}"
end
--- Creates a table row with the given properties
---@param frame table the frame object
---@param title string the title of the page
---@param properties table the properties to be added to the table
---@return string
local function createTableRow(frame, title, properties)
local str = "|-\n"
for _, property in ipairs(properties) do
local value = SMWUtil.get(frame, title, property)
local displayFunction = propertyDisplayFunctions[property] or display
local displayValue = displayFunction(value, {
pageName = title,
frame = frame
})
if property == "Title" and displayValue == nil then
return ""
end
str = str .. "|" .. displayValue .. "\n"
end
return str
end
-- PUBLIC FUNCTIONS
function methodTable.render(self, frame)
local output = createTableHeader(self.properties)
for _, article in ipairs(self.articles) do
output = output .. createTableRow(frame, article, self.properties)
end
output = output .. createTableFooter()
return output
end
function methodTable.registerProperty(self, property)
table.insert(self.properties, property)
end
function methodTable.registerProperties(self, properties)
for _, property in ipairs(properties) do
methodTable.registerProperty(self, property)
end
end
function methodTable.addArticle(self, article)
table.insert(self.articles, article)
end
function methodTable.addArticles(self, articles)
for _, article in ipairs(articles) do
methodTable.addArticle(self, article)
end
end
function methodTable.addArticlesForCategory(self, categoryName)
local query = SMWUtil.createQueryString(categoryName)
local articles = SMWUtil.getPages(mw.getCurrentFrame(), query)
methodTable.addArticles(self, articles)
end
function methodTable.addPropertyDisplayFunction(self, property, displayFunction)
self.propertyDisplayFunctions[property] = displayFunction
end
function methodTable.addAlternativeHeader(self, property, header)
self.alternativeHeaders[property] = header
end
function ArticleList.new()
local instance = {
properties = {},
articles = {},
propertyDisplayFunctions = {},
alternativeHeaders = {}
}
setmetatable(instance, metatable)
return instance
end
function ArticleList.fromArgs(frame)
local instance = ArticleList:new()
-- lazy load module
local args = require("Module:Arguments").getArgs(frame)
local i = 1
while args[i] do
instance:registerProperty(args[i])
i = i + 1
end
local category = args.category
instance:addArticlesForCategory(category)
return instance:render(frame)
end
return ArticleList