Module:Sandbox/Md gilbert/ListMaster

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/ListMaster/doc

require('strict')

local p = {}

--[[
Will first validate, normalize, and then print tabular data according to the style
named in the arguments list. Validation will remove any malformed submodule
transclusions to ensure one bad entry doesn't break the entire display. Normalization
ensures that the desired key-value pairs exist (still working through the details on
how this should be implemented.  Display is self-explanatory.

The goal of this module is to provide a more resilient means of supporting both
human- and machine-readable and writable data.

Usage to print a members' table, for instance: 
{{#invoke:Sandbox/Md gilbert/ListMaster|printTable|style=[table|section]|display=[...]|
  {{#name=name1|role=role1|member_since=date1}}
  {{#name=name2|role=role2|member_since=date2}}
  {{#name=name3|role=role3|member_since=date3}}
  {{#name=name4|role=role4|member_since=date4}}
}}
--]]

function p.printTable(frame)
	-- Ensure the style is supported, defaults to table
	if frame.args.style ~= "table" and frame.args.style ~= "section" then
		frame.args.style = "table"
	end
	-- Ensure the display argument exists, return error otherwise
	if frame.args.display == nil then
		return "'display' argument is required in ListMaster invocation"
	end

	-- Display either table or section, other styles can be added in the future
	local res = ""
	if frame.args.style == "table" then
		-- Print the table header
		res = res .. "{| class='wikitable' style='border-spacing: 10px; border-collapse: collapse;' \n"
		res = res .. "|- \n"
		for col in string.gmatch(frame.args.display, "[^,]+") do
			res = res .. "! " .. p.firstToUpper(col) .. "\n"
		end
	elseif frame.args.style == "section" then
		
	end
	
	-- Then iterate over all the submodule items
	for k,v in ipairs(frame.args) do
		for mod in string.gmatch(v, "{{[^}}]+}}") do
			-- Add in the invocation text
			mod = "{{#invoke:Sandbox/Md gilbert/ListItem|printItem|" .. string.sub(mod, 4)
			-- And process the module
			res = res .. frame:preprocess( mod ) .. "\n"
		end
	end
	
	if frame.args.style == "table" then
		res = res .. "|}\n"
	elseif frame.args.style == "section" then
		
	end
	
	--return "<nowiki>" .. res .. "</nowiki>"
	return res
end

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

return p