Module:Infobox Book

From the RuneScape Wiki, the wiki for all things RuneScape
Jump to navigation Jump to search
Module documentation
This documentation is transcluded from Module:Infobox Book/doc. [edit] [history] [purge]
This module does not have any documentation. Please consider adding documentation at Module:Infobox Book/doc. [edit]
Module:Infobox Book's function main is invoked by Template:Infobox Book.
Module:Infobox Book requires Module:Infobox.
Module:Infobox Book requires Module:Mainonly.
Module:Infobox Book requires Module:Yesno.
Function list
L 12 — p.main
L 100 — yesnodisp
L 112 — addcategories
L 113 — is_def
-- <nowiki>
--------------------------
-- Module for [[Template:Infobox Book]]
------------------------
local p = {}

-- "imports"
local onmain = require('Module:Mainonly').on_main
local infobox = require('Module:Infobox')
local yesno = require('Module:Yesno')

function p.main(frame)
	local args = frame:getParent().args
	local ret = infobox.new(args)

	ret:defineParams{
		{ name = 'name', func = 'name' },
		{ name = 'vanchor', func = { name = 'has_content', params = { 'version' }, flag = 'p' } },
		{ name = 'image', func = 'image' },
		{ name = 'aka', func = 'has_content' },
		{ name = 'author', func = 'has_content' },
		{ name = 'quest', func = 'has_content' },
		{ name = 'readable', func = 'has_content' },
		{ name = 'readabledisp', func = { name = yesnodisp, params = { 'readable' }, flag = { 'd' } } },
		{ name = 'bookcase', func = 'has_content' },
		{ name = 'bookcasedisp', func = { name = yesnodisp, params = { 'bookcase' }, flag = { 'd' } } },
		{ name = 'description', func = 'has_content' },
	}

	ret:setMaxButtons(4)
	ret:create()
	ret:cleanParams()
	ret:customButtonPlacement(true)
	
	ret:defineLinks({ hide = true})

	ret:defineName('Infobox Book')
	ret:addClass('infobox-book left-info')

	ret:addButtonsCaption()

	-- PARAMETER: name
	ret:addRow{
		{ tag = 'argh', content = 'name', colspan = '10', class = 'infobox-header' }
	}
	ret:pad(2)

	-- PARAMETER: aka
	if ret:paramDefined('aka') then
		ret:addRow{
			{ tag = 'th', content = 'Alternative titles', colspan = '4' },
			{ tag = 'argd', content = 'aka', colspan = '6' }
		}
	end
	
	-- PARAMETER: author
	ret:addRow{
		{ tag = 'th', content = 'Author', colspan = '4' },
		{ tag = 'argd', content = 'author', colspan = '6' }
	}
	
	-- PARAMETER: quest
	if ret:paramDefined('quest') then
		ret:addRow{
			{ tag = 'th', content = 'Quest', colspan = '4' },
			{ tag = 'argd', content = 'quest', colspan = '6' }
		}
	end
	
	-- PARAMETER: readable
	ret:addRow{
		{ tag = 'th', content = 'Readable', colspan = '4' },
		{ tag = 'argd', content = 'readabledisp', colspan = '6' }
	}
	
	-- PARAMETER: bookcase
	ret:addRow{
		{ tag = 'th', content = 'Bookcase', colspan = '4' },
		{ tag = 'argd', content = 'bookcasedisp', colspan = '6' }
	}
	
	-- PARAMETER: description
	if ret:paramDefined('description') then
		ret:addRow{
			{ tag = 'th', content = 'Description', colspan = '4' },
			{ tag = 'argd', content = 'description', colspan = '6' }
		}
	end
	ret:pad(2)
	
	ret:finish()
	if onmain() then
		local a1 = ret:param('all')
		local a2 = ret:categoryData()
		ret:wikitext(addcategories(a1, a2))
	end
	return ret:tostring()
end

function yesnodisp(arg)
	if infobox.isDefined(arg) then
		if yesno(arg) == false then
			return '[[File:X mark.svg|20px|link=]]'
		else
			return '[[File:Yes check.svg|20px|link=]]'
		end
	else
		return nil
	end
end

function addcategories(args, catargs)
	function is_def(x, y)
		y = y or 'd'
		if infobox.isDefined(x) then
			return infobox.isDefined(x[y])
		end
		return false
	end
	
	local ret = { 'Texts and tomes' }
	
	local cat_map = {
		-- Added if the parameter has content
		defined = {
		},
		-- Added if the parameter has no content
		notdefined = {
		},
	}
	
	-- Run and add mapped categories
	for n, v in pairs(cat_map.defined) do
		if catargs[n] and catargs[n].one_defined then
			table.insert(ret,v)
		end
	end
	for n, v in pairs(cat_map.notdefined) do
		if catargs[n] and catargs[n].all_defined == false then
			table.insert(ret,v)
		end
	end
	
	-- Add the associated category if the parameter is valued at yes
	if args['bookcase'] then
		if yesno(args['bookcase'].d or '') then
			table.insert(ret, 'Books that can be stored in a player-owned house bookcase')
		end
	end
	
	-- Add the associated category if the parameter is valued at yes
	if args['readable'] then
		if not yesno(args['readable'].d or '') then
			table.insert(ret, 'Unreadable')
		end
	end
	
	-- combine table and format category wikicode
	for i, v in ipairs(ret) do
		if (v ~= '') then
			ret[i] = string.format('[[Category:%s]]', v)
		end
	end

	return table.concat(ret, '')
end

return p
-- </nowiki>