Module:Sandbox/Md gilbert/ListItem

Revision as of 21:02, 23 October 2022 by imported>Legoktm (Replace Module:No globals with require( "strict" ))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Sandbox/Md gilbert/ListItem/doc

require('strict')

local p = {}

--[[
This module is intended to be invoked by the ListMaster module. Depending on the style
passed to the parent, will parse and display the list item as a table row, subsection, etc.

Possible values that could be used for a members table:
  name, role, member_since, role, status, etc
Possible values that could be used for a tasks table:
  title, notes, page, owner, created, due, completed, priority, remaining, etc
  
Values can be passed in that aren't displayed, allowing for more robust storage
of metadata for each item (ie, any arbitrary value can be passed in that aren't
intended to be displayed, but instead facilitate machine-trackable data).
--]]
function p.printItem(frame)
	local parent_frame = frame:getParent()
	local style = parent_frame.args.style
	local display = parent_frame.args.display

	local res = ""
	if style == "table" then
		res = "|- \n"
		for col in string.gmatch(display, "[^,]+") do
			-- Validate the current field, make sure we have the desired value,
			-- setting to an empty string if the arg doesn't exist.
			if frame.args[col] == nil then
				frame.args[col] = ""
			end
			-- Then display, passing to our local functions if the argument
			-- is one for which we have a specialized display function
			if p["display_" .. col] ~= nil then
				res = res .. "| " .. p["display_" .. col]( frame.args[col] ) .. "\n"
			else
				res = res .. "| " .. frame.args[col] .. "\n"
			end
		end
	elseif style == "section" then
		local title = "ListItem"
		if frame.args.name ~= nil then
			title = frame.args.name
		elseif frame.args.title ~= nil then
			title = frame.args.title
		end
		res = "==" .. title .. "==\n"
		-- Then go through each of the display sections and add to res
		for col in string.gmatch(display, "[^,]+") do
			-- Validate the current field, make sure we have the desired value,
			-- setting to an empty string if the arg doesn't exist.
			if frame.args[col] == nil then
				frame.args[col] = ""
			end
			-- Then display each value, passing to our local functions if they exist.
			if p["display_" .. col] ~= nil then
				res = res .. "'''" .. p.firstToUpper(col) .. "''': " .. p["display_" .. col]( frame.args[col] )
			else
				res = res .. "'''" .. p.firstToUpper(col) .. "''': " .. frame.args[col]
			end
		end
	end

	return res
end

function p.display_name(name)
	return "[[User:" .. name .. "|" .. name .. "]] • [[User_talk:" .. name .. "|Talk]] • [[Special:Contributions/" .. name .. "|Contribs]]"
end

function p.firstToUpper(str)
    return (str:gsub("^%l", string.upper))
end

return p