Module:Infobox story

From Sekaipedia
Revision as of 13:42, 11 February 2022 by YBamY (talk | contribs) (added key unit and involved unit)

To generate {{Infobox story}}, invoke using the main function.


--------------------------
-- Module for [[Template:Infobox story]]
------------------------
local getArgs   = require('Module:Arguments').getArgs
local builder   = require('Module:InfoboxBuilder new')
local constants = require('Module:Constants')
local utils     = require('Module:Utilities')
local cicon     = require('Module:Character icon')._main

local p = {}
local categories = ""

local function splitStringWithDelim(delim)
	return function(str)
		if str == nil then
			return nil
		end
		
		local list = mw.text.split(str, delim)
		for i=1,#list do
			list[i] = mw.text.trim(list[i])
		end
		
		return list
	end
end

local function formatImage(image_name)
	return string.format("[[File:%s|220px]]", image_name)
end

local function formatList(list)
	if #list < 2 then
		return list[1]
	end
	
	local ul = mw.html.create('ul')
	
	for i=1,#list do
		ul:tag('li')
			:wikitext(list[i])
	end
	
	return tostring(ul)
end

local function formatCharacters(characters)
	local resArr = {}
	for i,v in ipairs(characters) do
		table.insert(resArr, cicon({ v, size = '30px' }))
	end
	
	return table.concat(resArr, " ")
end


function p.main(frame)
	local args = getArgs(frame)
	local infobox = builder.new()
	
	infobox:setName('Infobox story')
		:setParams{
			{ name = 'story name', default = mw.title.getCurrentTitle().text },
			{ name = 'type', pFunc = string.lower },
			{ name = 'image', dFunc = formatImage },
			{ name = 'japanese' },
			{ name = 'romaji' },
			{ name = 'chapters' },
			{ name = 'key unit', pFunc = splitStringWithDelim(';'), dFunc = formatList },
			{ name = 'involved unit', pFunc = splitStringWithDelim(';'), dFunc = formatList },
			{ name = 'character', pFunc = splitStringWithDelim(','),dFunc = formatCharacters },
			{ name = 'translator', pFunc = splitStringWithDelim(','), dFunc = formatList },
			{ name = 'proofreader', pFunc = splitStringWithDelim(','), dFunc = formatList },
			{ name = 'source', pFunc = splitStringWithDelim(','), dFunc = formatList }
		}
		:setArgs(args)
		:processArgs()
		:setCategoryMap({
			['type'] = {
				['main']  = 'Main stories',
				['event'] = 'Event stories',
			},
		})


	infobox
		:addHeader({ tag = 'argth', content = 'story name' })
		:addImage({
			{ tag = 'argtd', content = 'image' },
		})
		:addRow(
			{
				{ tag = 'th', content = 'Japanese' },
				{ tag = 'argtd', content = 'japanese' }
			},
			{ hideIfEmpty = { 'japanese' } }
		)
		:addRow(
			{
				{ tag = 'th', content = 'Romaji' },
				{ tag = 'argtd', content = 'romaji' }
			},
			{ hideIfEmpty = { 'romaji' } }
		)
		:addHeader({ tag = 'th', content = 'Story Information' }, { subheader = true })
		:addRow({
			{ tag = 'th', content = 'Chapters' },
			{ tag = 'argtd', content = 'chapters' }
		})
		:addRow(
			{
				{ tag = 'th', content = 'Key Unit' },
				{ tag = 'argtd', content = 'key unit' }
			},
			{ hideIfEmpty = { 'key unit' } }
		)
		:addRow({
			{ tag = 'th', content = 'Involved Unit(s)' },
			{ tag = 'argtd', content = 'involved unit' }
		})
		:addRow({
			{ tag = 'th', content = 'Character(s)' },
			{ tag = 'argtd', content = 'character' }
		})
		:addHeader({ tag = 'th', content = 'Translation Information' }, { subheader = true })
		:addRow(
			{
				{ tag = 'th', content = 'Translator(s)' },
				{ tag = 'argtd', content = 'translator' }
			},
			{ hideIfEmpty = { 'translator' } }
		)
		:addRow(
			{
				{ tag = 'th', content = 'Proofreader(s)' },
				{ tag = 'argtd', content = 'proofreader' }
			},
			{ hideIfEmpty = { 'proofreader' } }
		)
		:addRow({
			{ tag = 'th', content = 'Source(s)' },
			{ tag = 'argtd', content = 'source' }
		})
	
	if mw.title.getCurrentTitle().namespace == 0 then
		categories = "[[Category:Stories]]" .. infobox:getCategories()
	end

	return infobox:tostring() .. categories
end

return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.