Jump to content

Module:Wikidebate

From Wikiversity

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

local wikidebate = {}

function wikidebate.isTrue( proposition )
	proposition = mw.text.trim( proposition )
	local title = mw.title.new( 'Wikidebate/' .. proposition )
	if title.exists then
		local text = title:getContent()
		for objection in string.gmatch( text, '{%b{}}' ) do
			if string.match( objection, '{{[^|}]-|(.+)}}' ) then
				if wikidebate.isSound( objection ) then
					return false
				end
			end
		end
		return true
	end
	return true
end

function wikidebate.isSound( objection )
	local premises = string.match( objection, '{{[^|}]-|(.+)}}' )
	if not premises then return end
	for premise in mw.text.gsplit( premises, '|' ) do
		if not wikidebate.isTrue( premise ) then
			return false
		end
	end
	return true
end

function wikidebate.objection( frame )

	local premises = {}
	for key, value in pairs( frame:getParent().args ) do premises[ key ] = value end
	for key, value in pairs( frame.args ) do premises[ key ] = value end

	local t = mw.html.create( 'table' )

	local td1, td2, td3, tr
	for key, premise in pairs( premises ) do
		premise = mw.text.trim( premise )
		td1 = mw.html.create( 'td' ):addClass( 'wikidebate-number' ):wikitext( key )
		td2 = mw.html.create( 'td' ):wikitext( '[[Wikidebate/' .. premise .. '|' .. premise .. ']]' )
		td3 = mw.html.create( 'td' ):addClass( 'wikidebate-status' )
		if wikidebate.isTrue( premise ) then
			td3:wikitext( 'TRUE' ):addClass( 'wikidebate-true' )
		else
			td3:wikitext( 'FALSE' ):addClass( 'wikidebate-false' )
		end
		tr = mw.html.create( 'tr' ):node( td1 ):node( td2 ):node( td3 )
		t:node( tr )
	end

	-- Add conclusion
	local conclusion = frame:preprocess( '[[{{FULLPAGENAME}}|{{SUBPAGENAME}}]] is FALSE' )
	td1 = mw.html.create( 'td' ):addClass( 'wikidebate-number' ):wikitext( '∴' )
	td2 = mw.html.create( 'td' ):wikitext( conclusion )
	td3 = mw.html.create( 'td' ):addClass( 'wikidebate-status' )
	tr = mw.html.create( 'tr' ):node( td1 ):node( td2 ):node( td3 )
	t:node( tr )

	local div = mw.html.create( 'div' ):addClass( 'wikidebate-objection' ):node( t )
	return div
end

return wikidebate