Jump to content

Mòideal:ListMaster

O Wiktionary

Documentation for this module may be created at Mòideal: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: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
 
 	-- If we don't have any list items (ie, this is a stub that was just created), don't display anything
 	local not_empty = nil
 	for k,v in ipairs(frame.args) do
 		if string.find(v, "{{[^}}]+}}") ~= nil then not_empty = 1 end
 	end
 	if not_empty == nil then 
 		return "<div style='color: #A4A4A4; font-style: italic;'>Empty [[Module:ListMaster|ListMaster]] list</div>"
 	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: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