Module:Main page

From Tensura Wiki
Revision as of 10:55, 25 September 2026 by imported>Merge (merge.py)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:Main page/doc

local trim = mw.text.trim
local p = {}

function p.main(frame)
	
	-- DEBUG
	-- local frame = mw.getCurrentFrame()
	-- frame.args['desktop'] = '\nwelcome welcome welcome\nabout about links\n'
	-- frame.args['tablet'] = '\nwelcome welcome\nabout links\n'
	-- frame.args['mobile'] = '\nwelcome\nabout\nlinks\n'
	-- END DEBUG

	local sitename = frame:preprocess(mw.message.new('mainpage'):plain())
	
	-- parse the arguments into CSS variables that contain legal syntax for grid-template-areas
	local desktop = "--main-page-layout--desktop: '" .. string.gsub(trim(frame.args['desktop']), '\n', "' '") .. "';"
	local tablet =  "--main-page-layout--tablet: '"  .. string.gsub(trim(frame.args['tablet' ]), '\n', "' '") .. "';"
	local mobile =  "--main-page-layout--mobile: '"  .. string.gsub(trim(frame.args['mobile' ]), '\n', "' '") .. "';"
	-- grid-template-columns
	local desktop_cols = trim(string.gsub(frame.args['desktop-columns'] or '', ';', ''))
	local  tablet_cols = trim(string.gsub(frame.args[ 'tablet-columns'] or '', ';', ''))
	local  mobile_cols = trim(string.gsub(frame.args[ 'mobile-columns'] or '', ';', ''))
	if desktop_cols ~= '' then
		desktop = desktop .. '--main-page-layout-columns--desktop: '.. desktop_cols ..';'
	end
	if tablet_cols ~= '' then
		tablet = tablet .. '--main-page-layout-columns--tablet: '.. tablet_cols ..';'
	end
	if mobile_cols ~= '' then
		mobile = mobile .. '--main-page-layout-columns--mobile: '.. mobile_cols ..';'
	end

	local boxes = {}
	
	-- helper function for the next part, checks if an array-like table contains a value
	function contains(t, entry)
		for _,value in pairs(t) do
			if value == entry then
				return true
			end
		end
		return false
	end
	
	-- add every box referenced in the layout rules once
	-- we check all 3 layouts so that typos or other errors will clearly surface
	for _,value in pairs(mw.text.split(mw.text.trim(frame.args['desktop'] .. '\n' .. frame.args['tablet'] .. '\n' .. frame.args['mobile']), '%s')) do
		if not (value == "" or contains(boxes, value)) then
			table.insert(boxes, value)
		end
	end
	
	-- mw.logObject(boxes)
	
	-- start our mp-container wrapper, and add our variables from earlier as inline styles to declare them
	-- the sitename is added to the dataset so it's easily accessible by mp-edit-links.js and it doesn't need to make its own API call
	local output = '<div id="mp-container" style="' .. desktop .. tablet .. mobile .. '" data-sitename="' .. sitename .. '">'
	
	-- loop through boxes and add the relevant main page subpages into the output
	for _,box in pairs(boxes) do
		if mw.title.new(sitename .. '/' .. box).exists then
			output = output .. frame:expandTemplate{ title =  ':' .. sitename .. '/' .. box}
		else
			output = output .. frame:expandTemplate{ title = 'Main page box/missing', args = { box } } -- See [[Template:Main page box/missing]]
		end
	end
	
	output = output .. '</div>'
	
	return output
end

return p