Module:Rulesbox

local p = {}

-- To edit the list of resources in its raw form, go to the page named below. content = require( 'Module:Rulesbox/Content' ) is_rtl = require( 'Module:Is rtl' ) lang = mw.getCurrentFrame:preprocess('') ModuleMsg = require( 'Module:ModuleMsg' ) msg = ModuleMsg:get_msgs('Template:I18n/Rulesbox', lang)

function get_translation(details) -- Get translated version of a list entry --	-- Usage: --  details: table containing the entry to translate --	-- Return table: translated entry (with English as fallback) if details.unique_id == nil or lang == 'en' then -- This system assumes that a unique ID is assigned. return details end

for k, v in pairs(details) do if msg['content-' .. details.unique_id .. '-' .. k] ~= nil then details[k] = msg['content-' .. details.unique_id .. '-' .. k]		end end

return details

end

function get_directionality -- Should something be left-aligned or right-aligned? if is_rtl[lang] == true then return 'right' end return 'left' end

function build_entry(frame, details) -- Builds an individual entry on a Resource Center view --	-- Usage: --  frame: The frame object --  details: a table with keys header, description, contact, related, --           category, audiences (header is required) --	-- Return string: wikitext details = get_translation(details) template_args = { header = details.header, ['contact-label'] = msg['editor-field-contact'], ['related-pages-label'] = msg['editor-field-relatedpages'], }	if details.community ~= nil then template_args.community = details.community end if details.unique_id ~= nil then template_args.unique_id = details.unique_id end if details.description ~= nil then template_args.description = details.description end if details.contact ~= nil then template_args.contact = details.contact end if details.related ~= nil then template_args.related = details.related end

entrycontent = frame:expandTemplate{ title = 'Rulesbox listing', args = template_args }	return entrycontent end

function build_category(frame, entries, category_name) -- Helper function for building audience views and standalone category views --	-- Usage: --  frame: The frame object --  entries: table, containing the content tables --  category_name: string, category to build a list for --	-- Return string: wikitext categorycontent = '' for _, details in ipairs(entries) do categorycontent = categorycontent .. build_entry(frame, details) end categorycontent = categorycontent .. '\n\n ' return '' .. categorycontent .. ' ' end

function p.category(frame) -- Builds a category-level view (but without the section header) --	-- Takes one parameter: string of the category name to filter against --	-- Returns wikitext for _, value in ipairs(frame.args) do		category = value end filtered_entries = {} for _, entry in ipairs(content) do		if entry.category == category then table.insert(filtered_entries, entry) end end return build_category(frame, filtered_entries, category) end

function p.audience(frame) -- Builds an audience-level view --	-- Takes one parameter: string of the audience name to filter against --	-- Returns wikitext audiencecontent = '' for _, audience in ipairs(frame.args) do		-- In principle there should only be one argument but we still need to -- iterate like this. category_tables = { {name = 'Test cat 1', content = {}}, }		-- `content` is a table of tables. Lua doesn't have arrays but if it did you -- could think of it like an array of tables. Or an array of objects. An -- array of associative arrays? --		-- This first pass re-organizes the `content` table from a flat table to one -- divided into the different categories for _, entry in ipairs(content) do			allow_this_one = false if audience == 'all' then allow_this_one = true else for __, entry_audience in ipairs(entry.audiences) do					if entry_audience == audience then allow_this_one = true end end end

if allow_this_one == true then entry_is_assigned = false for index, blob in ipairs(category_tables) do					if blob.name == entry.category then entry_is_assigned = true table.insert(category_tables[index]['content'], entry) break end end if entry_is_assigned == false then table.insert(category_tables, {name = entry.category, content = {entry}}) end end end

for index, blob in ipairs(category_tables) do			if #blob.content > 0 then msgname = 'category-' .. string.gsub(string.lower(blob.name), ' ', '-') if msg[msgname] ~= nil then relevant_msg = msg[msgname] else relevant_msg = 'error' end audiencecontent = audiencecontent .. '' .. relevant_msg .. ' '				audiencecontent = audiencecontent .. build_category(frame, blob.content, blob.name) end end end

return audiencecontent end

return p