Jump to content

Module:Life Guide: Difference between revisions

From The Petit Planet Wiki
ReisuDesign (talk | contribs)
Created page with "local p = {} local lib = require('Module:Feature') local checkExists = require('Module:Exists').checkExists local getTemplateArgs = require('Module:Parser').getTemplateArgs -- Template:Tutorial function p.Tutorial(frame, args) if args == nil then args = require('Module:Arguments').getArgs(frame, { parentFirst = true }) end local html = mw.html.create() if args[1] and type(args[1]) == 'string' then for page in lib.gsplit(args[1], ';', { removeEmpty = true }..."
 
ReisuDesign (talk | contribs)
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 4: Line 4:
local getTemplateArgs = require('Module:Parser').getTemplateArgs
local getTemplateArgs = require('Module:Parser').getTemplateArgs


-- [[Template:Tutorial]]
-- [[Template:Life Guide]]
function p.Tutorial(frame, args)
function p.LifeGuide(frame, args)
if args == nil then
if args == nil then
args = require('Module:Arguments').getArgs(frame, { parentFirst = true })
args = require('Module:Arguments').getArgs(frame, { parentFirst = true })
end
end
local title = mw.title.getCurrentTitle()
local standalone = not args.table and title.rootText == 'Life Guide'
local html = mw.html.create()
local html = mw.html.create()
if args[1] and type(args[1]) == 'string' then
if args[1] and type(args[1]) == 'string' then
for page in lib.gsplit(args[1], ';', { removeEmpty = true }) do
for page in lib.gsplit(args[1], ';', { removeEmpty = true }) do
local tutorialArgs = getTemplateArgs('Tutorial/' .. page, { only = 'Tutorial' })
local lifeguideArgs = getTemplateArgs('Life Guide/' .. page, { only = 'Life Guide' })
if lib.isEmpty(tutorialArgs) then
if lib.isEmpty(lifeguideArgs) then
html:tag('strong'):addClass('error'):wikitext('The page [[Tutorial/', page, ']] is not a valid tutorial.')
html:tag('strong'):addClass('error'):wikitext('The page [[Life Guide/', page, ']] is not a valid lifeguide.')
else
else
html
html
:tag('div'):addClass('tutorial')
:tag('div')
:addClass('lifeguide')
:addClass(standalone and 'lifeguide-standalone' or '')
:tag('dl')
:tag('dl')
:tag('dt'):wikitext(tutorialArgs.title or page)
:tag('dt'):wikitext(lifeguideArgs.title or page)
:tag('dd'):node(p._TutorialFormat(tutorialArgs))
:tag('dd'):node(p._LifeGuideFormat(lifeguideArgs))
end
end
end
end
else
else
html
html
:tag('div'):addClass('tutorial')
:tag('div')
:node(p._TutorialFormat(args))
:addClass('lifeguide')
:addClass(standalone and 'lifeguide-standalone' or '')
:node(p._LifeGuideFormat(args))
end
end
return frame:preprocess(tostring(html))
return frame:preprocess(tostring(html))
end
end


function p._TutorialFormat(args)
function p._LifeGuideFormat(args)
local out = mw.html.create('')
local out = mw.html.create('')
local title = mw.title.getCurrentTitle()
local title = mw.title.getCurrentTitle()
if args.about then
if args.about then
args.about = lib.split(args.about, ';')
args.about = lib.split(args.about, ';')
if title.rootText == 'Tutorial' then
if title.rootText == 'Life Guide' then
out:tag('i'):wikitext(require('Module:Hatnote').main(args.about))
out:tag('i'):wikitext(require('Module:Hatnote').main(args.about))
end
end
Line 43: Line 50:
local size = args.size or '200px'
local size = args.size or '200px'
if (title.rootText == 'Tutorial') then
if (title.rootText == 'Life Guide') then
size = '400px'
size = '400px'
end
end
Line 53: Line 60:
local text = args['text' .. tostring(I)]
local text = args['text' .. tostring(I)]
if (image or text) then
if (image or text) then
local row = out:tag('div'):addClass('tutorial-detail')
local row = out:tag('div'):addClass('lifeguide-detail')
row:wikitext('[[File:', (image or 'Empty.png'), '|', size, ']]')
row:wikitext('[[File:', (image or 'Empty.png'), '|', size, ']]')
row:tag('div'):wikitext((text or '<wbr>'))
row:tag('div'):wikitext((text or '<wbr>'))
Line 65: Line 72:
-- add categories if correct page
-- add categories if correct page
local subpage = args.title or title.subpageText
local subpage = args.title or title.subpageText
if (not args['table'] and title.baseText == 'Tutorial' and (subpage ~= nil and not lib.inArray({ 'Elements', 'Enemies', 'System', 'Adventure', 'Event', 'Quest', 'Uncategorized' }, subpage))) then
if (not args['table'] and title.baseText == 'Life Guide' and (subpage ~= nil and not lib.inArray({ 'Elements', 'Enemies', 'System', 'Adventure', 'Event', 'Quest', 'Uncategorized' }, subpage))) then
local cat = mw.html.create()
local cat = mw.html.create()
cat:wikitext('[[Category:Tutorials|', subpage, ']]')
cat:wikitext('[[Category:Life Guides|', subpage, ']]')
if args['sort'] then
if args['sort'] then
cat:wikitext('[[Category:Tutorial|', string.format('%05d', args['sort']), ']]')
cat:wikitext('[[Category:Life Guide|', string.format('%05d', args['sort']), ']]')
else
else
cat:wikitext('[[Category:Tutorial]]')
cat:wikitext('[[Category:Life Guide]]')
end
end
if lib.inArray({ 'Elements', 'Enemies', 'System', 'Adventure' }, args['type']) then
if lib.inArray({ 'Elements', 'Enemies', 'System', 'Adventure' }, args['type']) then
cat:wikitext('[[Category:', args['type'], ' Tutorials|', subpage, ']]')
cat:wikitext('[[Category:', args['type'], ' Life Guides|', subpage, ']]')
if not args['sort'] then
if not args['sort'] then
cat:wikitext('[[Category:Tutorial Missing Sort]]')
cat:wikitext('[[Category:Life Guide Missing Sort]]')
end
end
elseif args['type'] == 'Event' or args['type'] == 'Quest' then
elseif args['type'] == 'Event' or args['type'] == 'Quest' then
cat:wikitext('[[Category:', args['type'], ' Tutorials|', subpage, ']]')
cat:wikitext('[[Category:', args['type'], ' Life Guides|', subpage, ']]')
else
else
cat:wikitext('[[Category:Uncategorized Tutorials|', subpage, ']]')
cat:wikitext('[[Category:Uncategorized Life Guides|', subpage, ']]')
end
end
if args.about then
if args.about then
for _, v in pairs(args.about) do
for _, v in pairs(args.about) do
cat:wikitext('[[Category:', v, ' Tutorials]]')
cat:wikitext('[[Category:', v, ' Life Guides]]')
end
end
end
end
Line 96: Line 103:
while (args['image' .. tostring(I)] and exists) do
while (args['image' .. tostring(I)] and exists) do
if not checkExists('File:' .. args['image' .. tostring(I)]) then
if not checkExists('File:' .. args['image' .. tostring(I)]) then
cat:wikitext('[[Category:Tutorial Missing Image]]')
cat:wikitext('[[Category:Life Guide Missing Image]]')
exists = false;
exists = false;
end
end
Line 109: Line 116:
end
end


-- [[Template:Tutorials by Category Table]]
-- [[Template:Life Guides by Category Table]]
function p.Table(frame)
function p.Table(frame)
local args = require('Module:Arguments').getArgs(frame, { parentFirst = true })
local args = require('Module:Arguments').getArgs(frame, { parentFirst = true })
local result = mw.html.create('div'):addClass('tutorial-table')
local result = mw.html.create('div'):addClass('lifeguide-table')
local title = mw.title.getCurrentTitle().text
local title = mw.title.getCurrentTitle().text
if lib.isEmpty(args.pages) then
if lib.isEmpty(args.pages) then
result:tag('span'):wikitext('No [[Tutorial]]s ', (args[1] and 'that match the category selection' or 'are in ' .. title), '.')
result:tag('span'):wikitext('No [[Life Guide]]s ', (args[1] and 'that match the category selection' or 'are in ' .. title), '.')
else
else
local resultTable = mw.html.create('table'):addClass('article-table')
local resultTable = mw.html.create('table'):addClass('article-table')
local count = 0
local count = 0
for tutorialPage in lib.gsplit(args.pages, ';;;', { noTrim = true, removeEmpty = true }) do
for lifeguidePage in lib.gsplit(args.pages, ';;;', { noTrim = true, removeEmpty = true }) do
local tutorialArgs = getTemplateArgs(tutorialPage, { only = 'Tutorial' })
local lifeguideArgs = getTemplateArgs(lifeguidePage, { only = 'Life Guide' })
for key, value in pairs(tutorialArgs) do
for key, value in pairs(lifeguideArgs) do
if value == '' then tutorialArgs[key] = nil end
if value == '' then lifeguideArgs[key] = nil end
end
end
tutorialArgs.PAGENAME = tutorialPage
lifeguideArgs.PAGENAME = lifeguidePage
resultTable:node(p._TableRow(frame, tutorialArgs))
resultTable:node(p._TableRow(frame, lifeguideArgs))
count = count + 1
count = count + 1
end
end
local header = result:tag('div'):addClass('tutorial-header')
local header = result:tag('div'):addClass('lifeguide-header')
if lib.isNotEmpty(args.header) then
if lib.isNotEmpty(args.header) then
header:wikitext(tostring(tostring(args.header):gsub('%%PAGES%%', count)))
header:wikitext(tostring(tostring(args.header):gsub('%%PAGES%%', count)))
Line 140: Line 147:
count,
count,
"'''",
"'''",
' [[Tutorial]]',
' [[Life Guide]]',
(count == 1 and ' ' or 's '),
(count == 1 and ' ' or 's '),
(args[1] and ('that match' .. (count == 1 and 'es' or '') .. ' the category selection:') or ' are in ' .. title)
(args[1] and ('that match' .. (count == 1 and 'es' or '') .. ' the category selection:') or ' are in ' .. title)
Line 166: Line 173:
local row = out:tag('tr'):tag('td')
local row = out:tag('tr'):tag('td')
args['table'] = true
args['table'] = true
row:node(p.Tutorial(frame, args))
row:node(p.LifeGuide(frame, args))
end
end
return out
return out

Latest revision as of 09:34, 27 September 2026

Module documentation

This module implements {{Tutorial}} and {{Tutorials by Category Table}}.

local p = {}
local lib = require('Module:Feature')
local checkExists = require('Module:Exists').checkExists
local getTemplateArgs = require('Module:Parser').getTemplateArgs

-- [[Template:Life Guide]]
function p.LifeGuide(frame, args)
	if args == nil then
		args = require('Module:Arguments').getArgs(frame, { parentFirst = true })
	end
	
	local title = mw.title.getCurrentTitle()
	local standalone = not args.table and title.rootText == 'Life Guide'
	
	local html = mw.html.create()
	if args[1] and type(args[1]) == 'string' then
		for page in lib.gsplit(args[1], ';', { removeEmpty = true }) do
			local lifeguideArgs = getTemplateArgs('Life Guide/' .. page, { only = 'Life Guide' })
			if lib.isEmpty(lifeguideArgs) then
				html:tag('strong'):addClass('error'):wikitext('The page [[Life Guide/', page, ']] is not a valid lifeguide.')
			else
				html
					:tag('div')
						:addClass('lifeguide')
						:addClass(standalone and 'lifeguide-standalone' or '')
						:tag('dl')
							:tag('dt'):wikitext(lifeguideArgs.title or page)
							:tag('dd'):node(p._LifeGuideFormat(lifeguideArgs))
			end
		end
	else
		html
			:tag('div')
				:addClass('lifeguide')
				:addClass(standalone and 'lifeguide-standalone' or '')
				:node(p._LifeGuideFormat(args))
	end
	return frame:preprocess(tostring(html))
end

function p._LifeGuideFormat(args)
	local out = mw.html.create('')
	local title = mw.title.getCurrentTitle()
	if args.about then
		args.about = lib.split(args.about, ';')
		if title.rootText == 'Life Guide' then
			out:tag('i'):wikitext(require('Module:Hatnote').main(args.about))
		end
	end
	
	local size = args.size or '200px'
	if (title.rootText == 'Life Guide') then
		size = '400px'
	end
	
	-- create format
	local I = 1
	while (args['image' .. tostring(I)] or args['text' .. tostring(I)]) do
		local image = args['image' .. tostring(I)]
		local text = args['text' .. tostring(I)]
		if (image or text) then
			local row = out:tag('div'):addClass('lifeguide-detail')
			row:wikitext('[[File:', (image or 'Empty.png'), '|', size, ']]')
			row:tag('div'):wikitext((text or '<wbr>'))
			if (args['image' .. tostring(I+1)] or args['text' .. tostring(I+1)]) then
				row:tag('hr'):addClass('mobile-only')
			end
		end
		I = I + 1
	end
	
	-- add categories if correct page
	local subpage = args.title or title.subpageText
	if (not args['table'] and title.baseText == 'Life Guide' and (subpage ~= nil and not lib.inArray({ 'Elements', 'Enemies', 'System', 'Adventure', 'Event', 'Quest', 'Uncategorized' }, subpage))) then
		local cat = mw.html.create()
		cat:wikitext('[[Category:Life Guides|', subpage, ']]')
		
		if args['sort'] then
			cat:wikitext('[[Category:Life Guide|', string.format('%05d', args['sort']), ']]')
		else
			cat:wikitext('[[Category:Life Guide]]')
		end
		
		if lib.inArray({ 'Elements', 'Enemies', 'System', 'Adventure' }, args['type']) then
			cat:wikitext('[[Category:', args['type'], ' Life Guides|', subpage, ']]')
			if not args['sort'] then
				cat:wikitext('[[Category:Life Guide Missing Sort]]')
			end
		elseif args['type'] == 'Event' or args['type'] == 'Quest' then
			cat:wikitext('[[Category:', args['type'], ' Life Guides|', subpage, ']]')
		else
			cat:wikitext('[[Category:Uncategorized Life Guides|', subpage, ']]')
		end
		
		if args.about then
			for _, v in pairs(args.about) do
				cat:wikitext('[[Category:', v, ' Life Guides]]')
			end
		end
		
		local I = 1
		local exists = true
		while (args['image' .. tostring(I)] and exists) do
			if not checkExists('File:' .. args['image' .. tostring(I)]) then
				cat:wikitext('[[Category:Life Guide Missing Image]]')
				exists = false;
			end
			I = I + 1
		end
			
		if not args.nocat then
			out:node(require('Module:Namespace detect')._main{main=cat, module=cat})
		end
	end
	return out
end

-- [[Template:Life Guides by Category Table]]
function p.Table(frame)
	local args = require('Module:Arguments').getArgs(frame, { parentFirst = true })
	local result = mw.html.create('div'):addClass('lifeguide-table')
	local title = mw.title.getCurrentTitle().text
	
	if lib.isEmpty(args.pages) then
		result:tag('span'):wikitext('No [[Life Guide]]s ', (args[1] and 'that match the category selection' or 'are in ' .. title), '.')
	else
		local resultTable = mw.html.create('table'):addClass('article-table')
		local count = 0
		for lifeguidePage in lib.gsplit(args.pages, ';;;', { noTrim = true, removeEmpty = true }) do
			local lifeguideArgs = getTemplateArgs(lifeguidePage, { only = 'Life Guide' })
			for key, value in pairs(lifeguideArgs) do
				if value == '' then lifeguideArgs[key] = nil end	
			end
			lifeguideArgs.PAGENAME = lifeguidePage
			resultTable:node(p._TableRow(frame, lifeguideArgs))
			count = count + 1
		end
		
		local header = result:tag('div'):addClass('lifeguide-header')
		if lib.isNotEmpty(args.header) then
			header:wikitext(tostring(tostring(args.header):gsub('%%PAGES%%', count)))
		else
			header:wikitext(
				'There ',
				(count == 1 and 'is ' or 'are '),
				"'''",
				count,
				"'''",
				' [[Life Guide]]',
				(count == 1 and ' ' or 's '),
				(args[1] and ('that match' .. (count == 1 and 'es' or '') .. ' the category selection:') or ' are in ' .. title)
			)
		end
		result:node(resultTable)
	end
	
	return frame:preprocess(tostring(result))
end

function p._TableRow(frame, args)
	local out = mw.html.create('')
	
	if args.title then
		local header = out:tag('tr'):tag('th')
		header:wikitext('[[', args.PAGENAME, '|', args.title, ']]')
		if args.subtitle then
			header:wikitext(' - ', args.subtitle)
		end
		if args.icon then
			header:wikitext(' [[File:', args.icon, '|25px|link=]]')
		end
		
		local row = out:tag('tr'):tag('td')
		args['table'] = true
		row:node(p.LifeGuide(frame, args))
	end
	return out
end

return p