Module:Card table/archive/Render

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

local StringBuffer = require( 'Module:StringBuffer' );

local DataTable    = require( 'Module:Card table/Data table' );
local DataSections = require( 'Module:Card table/Data sections' );

local CARD_BACK = 'Back-EN.png';

--------------------
-- Helper functions:
--------------------
-- mw functions:
local mwHtmlCreate = mw.html.create;

--------------------
-- Render functions:
--------------------
--[[Doc
@function renderErrors
@parameter {InfoClass} instance
--]]
local function renderErrors( instance )
	return tostring(
		mwHtmlCreate( 'div' )
			:addClass( 'card-table__errors' )
			:tag( 'ul' )
				:node(
					instance:dumpErrors( function( err )
						return tostring(
							mwHtmlCreate( 'li' )
								:tag( 'strong' )
									:wikitext( err )
								:done()
							:allDone()
						);
					end )
				)
			:done()
		:allDone()
	);
end

--[[Doc
@function renderCategories
@parameter {InfoClass} instance
--]]
local function renderCategories( instance )
	return tostring(
		mwHtmlCreate( 'div' )
			:addClass( 'card-table__categories' )
			:wikitext( instance:dumpCategories() )
		:allDone()
	);
end

--[[Doc
@function renderHeader
]]
local function renderHeader( header )
	return tostring(
		mwHtmlCreate( 'div' )
			:addClass( 'card-table__header' ) -- .heading
			:addAttr( 'id', header:getId() )
			--:tag( 'div' )
				:node( header:getContent() )
			--:done()
		:allDone()
	);
end

--[[Doc
@function renderCaption
]]
local function renderCaption( caption )
	return tostring(
		mwHtmlCreate( 'div' )
			:addClass( 'card-table__caption' ) -- .above
			:addAttr( 'id', caption:getId() )
			:node( caption:getContent() )
		:done()
	);
end

--[[Doc
@function renderImage
]]
local function renderImage()
	-- TODO
	return '[[File:' .. _args.image.main .. ']]';
end

--[[Doc
@function renderFooter
]]
local function renderFooter( footer )
	return tostring(
		mwHtmlCreate( 'div' )
			:addClass( 'card-table__footer' ) -- .below
			:node( footer:getContent() )
		:done()
	);
end

--[[Doc
@function render
@parameter {InfoClass} instance The instance of what is to be rendered.
@paramter {Card table/Input} args The input for the card table to render. 
]]
local function render( instance, args )
	
	local dataTable = DataTable();

	for row in args:Rows():values() do
		dataTable:addRow( row );
	end

	local sections = DataSections();

	for section in args:Sections():values() do
		sections:addSection( section );
	end

	local cardTable = mwHtmlCreate( 'div' )
		:attr( 'id', 'card-table' )
		:addClass( 'card-table' )
	;

	for class in args:Classes():values() do
		cardTable:addClass( class );
	end

	cardTable
		:node( renderHeader( args:Header() ) )

		:node( renderCaption( args:Caption() ) )

		:tag( 'div' )
			:addClass( 'card-table__columns' ) --card-table-columns
			:node( renderImage( args:Image() ) ) -- TODO
			--:tag( 'div' )
			--	:addClass( 'card-table__columns__image' ) -- imagecolumn

			:tag( 'div' )
				:addClass( 'card-table__columns__data' )
				:node( dataTable:render() )
			:done()
		:done() -- Close .card-table__columns

		:node( renderFooter( args:Footer() ) )

	:allDone();

	local content = StringBuffer()
		:add( renderErrors( instance ) )
		:add( tostring( cardTable ) )
		:add( sections:render() )
		:add( renderCategories( instance ) )
		:flush( '\n' )
	;

	return content:toString();
end

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