Module:Card table/archive/Data sections

From Yugipedia
Jump to: navigation, search
-- <pre>
--[=[Doc
@module Card table/Data section
@description 
@author [[User:Becasita]]
@contact [[User talk:Becasita]]
]=]

--------------------
-- Helper functions:
--------------------
--[[Doc
@function makeHeader
]]
local function makeHeader( title )
	return ( '\n== %s ==\n' ):format( title );
end

--[[Doc
@function renderSection
]]
local function renderSection( section ) -- don't forget section.options
	return mw.html.create( 'div' ) -- or <section>
		:addClass( 'data-sections__section' )
		:wikitext( makeHeader( section.header ) )
		--:tag( 'div' )
		--	:addClass( 'data-sections__section__content' )
			:node( section.content )
		--:done()
	:allDone();
end

-------------
-- The class:
-------------
--[[Doc
@class DataSections
@description Creates a new HTML responsive table to hold data.
@exportable
]]
local DataSections = {};
DataSections.__index = DataSections;
DataSections.__class = DataSections;

--[[Doc
@function DataSections new
@description Initializes an instance of `DataSections`.
@returns {DataSections} A new data table.
]]
function DataSections.new() -- TODO: needs name?
	local data = {
		_sections = {},
	};

	return setmetatable( data, DataSections );
end

--[[Doc
@function DataSections addSection
@description Adds a new section to the data section container.
@returns {DataSections} `self`
]]
function DataSections:addSection( section )
	if ( section.header or section.content ) then
		section.options = section.options or {};

		table.insert( self._sections, section );
	end

	return self;
end

--[[Doc
@function DataSections render
@description Renders the sections.
@returns {string} Wikitext for the sections.
]]
function DataSections:render()
	local s = mw.html.create( 'div' )
		:addClass( 'data-sections' )
	;

	for index, section in ipairs( self._sections ) do
		s:node( renderSection( section ) );
	end

	s:allDone();

	return tostring( s );
end

----------
-- Return:
----------
--[[Doc
@exports The `DataSections` class.
]]
return DataSections;
-- </pre>