Toggle menu
209
928
189
6.3K
Dovedale Railway Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.
Module documentation[view][edit][history][purge]
This documentation is transcluded from Module:Infobox/doc. Changes can be proposed in the talk page.
Function list
L 7 — table.contains
L 20 — p.fromArgs

This module provides a wrapper around Module:InfoboxNeue which allows creating SMW-based infobox templates using only wikitext.


local getArgs = require("Module:Arguments").getArgs
local getStatus = require("Module:Status").get
local SMWUtil = require("Module:SMWUtil")

local p = {}

function table.contains(table, element)
  for _, value in pairs(table) do
    if value == element then
      return true
    end
  end
  return false
end

--- Create an Infobox from args using Module:InfoboxNeue
---
--- @param frame table
--- @return string
function p.fromArgs(frame)
	local infobox = require("Module:InfoboxNeue"):new()
	local args = getArgs(frame)

	local image = args.image
	if image then
		infobox:renderImageOrGallery(image)
	end

	local status = args.status
    local statusDescription = args.status_description

    local captionTitle = args.caption_title
    local caption = args.caption
    local captionClass = args.caption_class

    if status then
        local text, class = getStatus(status)
        infobox:renderIndicator( {
            data = text,
            desc = statusDescription,
            class = class
        } )
    else
    	-- render the caption indicator only if there was no status provided
    	infobox:renderIndicator( {
        	data = captionTitle,
        	desc = caption,
        	class = captionClass
    	} )
	end

	local title = args.title or mw.title.getCurrentTitle().text
	local subtitle = args.subtitle

	local smwIdentifier = frame:preprocess("{{FULLPAGENAME}}") .. ((args.use_subobject == "true" and ("#" .. title)) or (""))

    infobox:renderHeader( {
        title = title,
        subtitle = subtitle,
    } )

    local rootSection = {}
    local sections = {}
    local sectionsInOrder = {}
    local properties = {}

	for i = 1, 50, 1 do
		local propertyName = args["property" .. i]
		if propertyName then
            table.insert(properties, propertyName)

			local section = args["section" .. i]
            local label = args["label" .. i] or propertyName

            -- whether to print the data using smw
            local printout = args["printout" .. i] == "true"

            local data = nil

            if printout then
                data = SMWUtil.get(frame, smwIdentifier, propertyName)
                -- TODO add params to configure printouts, like e.g. the unit
            else
                -- TODO this is not optimal for performance, implement a way to get all param names in a single query
                local paramName = SMWUtil.get(frame, "Property:" .. propertyName, "Parameter Name")
                data = args[paramName]
            end

			if not section then
                table.insert(rootSection, infobox:renderItem{
                    label = label,
                    data = data
                })
			else
                if not table.contains(sectionsInOrder, section) then
                    -- insert section to preserve the order of all sections
                    table.insert(sectionsInOrder, section)
                end

                local sectionContent = sections[section] or {}

                -- these arguments are usually per item, we use section defaults here
                -- these can be overridden for each item
                local row = args["row" .. i] or args["sectionRow" .. section] or "false"
                local spaceBetween = args["spaceBetween" .. i] or args["sectionSpaceBetween" .. section] or "false"

                table.insert(sectionContent, infobox:renderItem{
                    label = label,
                    data = data,
                    row = (row == "true"),
                    spacebetween = (spaceBetween == "true")
                })

                sections[section] = sectionContent
			end
		end
	end

	if #rootSection ~= 0 then
        local rootSectionCols = tonumber(args.root_section_cols) or 2
        infobox:renderSection( {
            content = table.concat(rootSection),
            col = rootSectionCols
        } )
	end

	for _, sectionKey in ipairs(sectionsInOrder) do
        local sectionContent = sections[sectionKey] or {}
        local cols = args["sectionCols" .. sectionKey] or 1
        local sectionName = args["sectionTitle" .. sectionKey]
        infobox:renderSection( {
            content = table.concat(sectionContent),
            col = cols,
            title = sectionName
        } )
	end

	SMWUtil.declareInfoboxProperties(frame, properties)

	return infobox:renderInfobox(args)
end

return p
🍪 Yum Yum Yum! Cookies help us better deliver our services. By using our site, you agree to our use of cookies.