Module:Infobox story: Difference between revisions

From Sekaipedia
Content added Content deleted
m (make translator and proofreader hidden if empty)
(using new builder)
Line 3: Line 3:
------------------------
------------------------
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs
local builder = require('Module:InfoboxBuilder')
local builder = require('Module:InfoboxBuilder new')
local constants = require('Module:Constants')
local constants = require('Module:Constants')
local utils = require('Module:Utilities')
local utils = require('Module:Utilities')
Line 10: Line 10:
local p = {}
local p = {}
local categories = ""
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)
local function formatImage(image_name)
Line 15: Line 30:
end
end


local function formatSplit(val)
local function formatList(list)
if val then
if #list < 2 then
return list[1]
local split_vals = utils.split(val, ",")
for i,v in ipairs(split_vals) do
split_vals[i] = utils.trim(v)
end
return table.concat(split_vals, "<br>")
end
end
local ul = mw.html.create('ul')
return nil
end
for i=1,#list do

ul:tag('li')
local function formatUnits(units)
:wikitext(list[i])
if units and units ~= '' then
local split_units = mw.text.split(units, ',')
local res_list = {}
for i,v in ipairs(split_units) do
local icon = icons({ v, size = '20px' })
if icon then
table.insert(res_list, string.format(
"%s [[%s]]",
icon,
constants.get_unit(v)
))
end
end
return table.concat(res_list, '<br>')
end
end
return nil
return tostring(ul)
end
end


Line 59: Line 54:
return table.concat(resArr, " ")
return table.concat(resArr, " ")
end
end





Line 67: Line 61:
infobox:setName('Infobox story')
infobox:setName('Infobox story')
:setWidth("330px")
:setParams{
:setParams{
{ name = 'story name', default = mw.title.getCurrentTitle().text },
{ name = 'story name', default = mw.title.getCurrentTitle().text },
{ name = 'image', func = formatImage },
{ name = 'type', pFunc = string.lower },
{ name = 'image', dFunc = formatImage },
{ name = 'japanese' },
{ name = 'japanese' },
{ name = 'romaji' },
{ name = 'romaji' },
{ name = 'chapters' },
{ name = 'chapters' },
{ name = 'unit', func = formatUnits },
{ name = 'unit', pFunc = splitStringWithDelim(';'), dFunc = formatList },
{ name = 'character', func = formatCharacters },
{ name = 'character', pFunc = splitStringWithDelim(','),dFunc = formatList },
{ name = 'translator', func = formatSplit },
{ name = 'translator', pFunc = splitStringWithDelim(','), dFunc = formatList },
{ name = 'proofreader', func = formatSplit },
{ name = 'proofreader', pFunc = splitStringWithDelim(','), dFunc = formatList },
{ name = 'source', func = formatSplit }
{ name = 'source', pFunc = splitStringWithDelim(','), dFunc = formatList }
}
}
:setArgs(args)
:setArgs(args)
:processArgs()
:setCategoryMap({
['type'] = {
['main'] = 'Main stories',
['event'] = 'Event stories',
},
})



infobox
infobox
Line 89: Line 91:
:addRow(
:addRow(
{
{
{ tag = 'th', content = 'Japanese', colspan = 12 },
{ tag = 'th', content = 'Japanese' },
{ tag = 'argtd', content = 'japanese', colspan = 18 }
{ tag = 'argtd', content = 'japanese' }
},
},
{ hideIfEmpty = { 'japanese' } }
{ hideIfEmpty = { 'japanese' } }
Line 96: Line 98:
:addRow(
:addRow(
{
{
{ tag = 'th', content = 'Romaji', colspan = 12 },
{ tag = 'th', content = 'Romaji' },
{ tag = 'argtd', content = 'romaji', colspan = 18 }
{ tag = 'argtd', content = 'romaji' }
},
},
{ hideIfEmpty = { 'romaji' } }
{ hideIfEmpty = { 'romaji' } }
Line 103: Line 105:
:addHeader({ tag = 'th', content = 'Story Information' }, { subheader = true })
:addHeader({ tag = 'th', content = 'Story Information' }, { subheader = true })
:addRow({
:addRow({
{ tag = 'th', content = 'Chapters', colspan = 12 },
{ tag = 'th', content = 'Chapters' },
{ tag = 'argtd', content = 'chapters', colspan = 18 }
{ tag = 'argtd', content = 'chapters' }
})
})
:addRow({
:addRow({
{ tag = 'th', content = 'Unit(s)', colspan = 12 },
{ tag = 'th', content = 'Unit(s)' },
{ tag = 'argtd', content = 'unit', colspan = 18 }
{ tag = 'argtd', content = 'unit' }
})
})
:addRow({
:addRow({
{ tag = 'th', content = 'Character(s)', colspan = 12 },
{ tag = 'th', content = 'Character(s)' },
{ tag = 'argtd', content = 'character', colspan = 18 }
{ tag = 'argtd', content = 'character' }
})
})
:addHeader({ tag = 'th', content = 'Translation Information' }, { subheader = true })
:addHeader({ tag = 'th', content = 'Translation Information' }, { subheader = true })
:addRow(
:addRow(
{
{
{ tag = 'th', content = 'Translator(s)', colspan = 12 },
{ tag = 'th', content = 'Translator(s)' },
{ tag = 'argtd', content = 'translator', colspan = 18 }
{ tag = 'argtd', content = 'translator' }
},
},
{ hideIfEmpty = { 'translator' } }
{ hideIfEmpty = { 'translator' } }
Line 124: Line 126:
:addRow(
:addRow(
{
{
{ tag = 'th', content = 'Proofreader(s)', colspan = 12 },
{ tag = 'th', content = 'Proofreader(s)' },
{ tag = 'argtd', content = 'proofreader', colspan = 18 }
{ tag = 'argtd', content = 'proofreader' }
},
},
{ hideIfEmpty = { 'proofreader' } }
{ hideIfEmpty = { 'proofreader' } }
)
)
:addRow({
:addRow({
{ tag = 'th', content = 'Source(s)', colspan = 12 },
{ tag = 'th', content = 'Source(s)' },
{ tag = 'argtd', content = 'source', colspan = 18 }
{ tag = 'argtd', content = 'source' }
})
})
if mw.title.getCurrentTitle().namespace == 0 then
if mw.title.getCurrentTitle().namespace == 0 then
categories = "[[Category:Stories]]" .. categories
categories = "[[Category:Stories]]" .. infobox:getCategories()
local procArgs = infobox:getArgs('processed')
local rawArgs = infobox:getArgs('raw')
if rawArgs['type'] then
if rawArgs['type'] == 'main' then
categories = categories .. '[[Category:Main stories]]'
elseif rawArgs['type'] == 'event' then
categories = categories .. '[[Category:Event stories]]'
elseif rawArgs['type'] == 'side' then
categories = categories .. '[[Category:Side stories]]'
end
end
end
end



Revision as of 07:03, 8 February 2022

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 icons     = require('Module:Icons')._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 characterArr = utils.split(characters, ',')
	local resArr = {}
	for i,v in ipairs(characterArr) do
		table.insert(resArr, icons({ 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 = 'unit', pFunc = splitStringWithDelim(';'), dFunc = formatList },
			{ name = 'character', pFunc = splitStringWithDelim(','),dFunc = formatList },
			{ 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 = 'Unit(s)' },
			{ tag = 'argtd', content = '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.