Module:Ref

From Halopedia, the Halo wiki

Revision as of 16:55, April 25, 2021 by Dab1001 (talk | contribs)
vde
Lua Module Documentation

This module provides the backend implementations of the Ref templates.

Public Functions

ref.ref

ref.list

ref.generic

Private Functions

makeRef

makeGenericRef

makeReuse

makeList

Code

A copy of the code for this module follows:


local utils = require( 'Module:Utils' )

--------------------------------------------------------------------------------
------------------------------- PRIVATE FUNCTIONS ------------------------------
--------------------------------------------------------------------------------

function makeRef( frame, id, group, text )
	if text == nil or text == '' then
		return utils.error(
			'No reference text was specified!',
			'ref',
			'Pages with empty citations' )
	end
	text = tostring( text )
	local args = {}
	if id ~= nil and id ~= '' then
		args.name = tostring( id )
	end
	if group ~= nil and group ~= '' then
		args.group = tostring( group )
	end
	
	return frame:extensionTag( 'ref', text, args )
end

function makeGenericRef( frame, id, group )
	return 'TODO: Implement this'
end

function makeReuse( frame, id, group )
	local args = {}
	if id == nil or id == '' then
		return utils.error(
			'No id was specified for ref reuse!',
			'ref',
			'Pages containing missing template parameter errors' )
	else
		args.name = tostring( id )
	end
	if group ~= nil and group ~= '' then
		args.group = tostring( group )
	end
	return frame:extensionTag( 'ref', '', args )
end

function makeList( frame, group, colNum, colWidth, colGap )
	local warnings = ''
	
	local args = {}
	if group ~= nil and group ~= '' then
		args.group = tostring( group )
	end
	local wikitext = frame:extensionTag( 'references', '', args )
	
	local html = mw.html.create( 'div' )
	if colNum ~= nil and colNum ~= '' then
		if tonumber( colNum ) == nil then
			warnings = warnings .. utils.warning(
				'Column amount must be a number! Instead, it was: "' .. colNum .. '"!',
				'ref list',
				'Pages containing invalid template parameter warnings' )
		else
			colNum = tostring( tonumber( colNum ) )
			html = html
				:attr( '-moz-column-count', colNum )
				:attr( '-webkit-column-count', colNum )
				:attr( 'column-count', colNum )
		end
	end
	if colWidth ~= nil and colWidth ~= '' then
		colWidth = tostring( colWidth )
		if string.find( colWidth, '[:;"\']') then
			warnings = warnings .. utils.warning(
				'Column width cannot contain any of the following characters: \' " ; :',
				'ref list',
				'Pages containing invalid template parameter warnings' )
		else
			html = html
				:attr( '-moz-column-width', colWidth )
				:attr( '-webkit-column-width', colWidth )
				:attr( 'column-width', colWidth )
		end
	end
	if colGap ~= nil and colGap ~= '' then
		colGap = tostring( colGap )
		if string.find( colGap, '[:;"\']') then
			warnings = warnings .. utils.warning(
				'Column gap cannot contain any of the following characters: \' " ; :',
				'ref list',
				'Pages containing invalid template parameter warnings' )
		else
			html = html
				:attr( '-moz-column-gap', colGap )
				:attr( '-webkit-column-gap', colGap )
				:attr( 'column-gap', colGap )
		end
	end
	
	local html = html
		:wikitext( wikitext )
		:allDone()
	
	return tostring( html ) .. warnings
end

--------------------------------------------------------------------------------
------------------------------- PUBLIC FUNCTIONS -------------------------------
--------------------------------------------------------------------------------

local ref = {}

function ref.ref( frame )
	local templateFrame = frame:getParent()
	local id = templateFrame.args['Id'] or templateFrame.args['Name']
	local group = templateFrame.args['Group']
	local text = templateFrame.args[1] or templateFrame.args['Text']
	return makeRef( frame, text, id, group )
end

function ref.list( frame )
	local templateFrame = frame:getParent()
	local group = templateFrame.args['Group']
	local colNum = templateFrame.args['Columns']
	local colWidth = templateFrame.args['ColWidth']
	local colGap = templateFrame.args['ColGap']
	return makeList( frame, group, colNum, colWidth, colGap )
end

function ref.generic( frame )
	return 'TODO: Implement this'
end

return ref