Module:Guide

local p = {} local h = {}

local utilsArg = require("Module:UtilsArg") local utilsError = require("Module:UtilsError") local utilsString = require("Module:UtilsString") local utilsTable = require("Module:UtilsTable")

local Constants = mw.loadData("Module:Constants/Data") local Data = mw.loadData("Module:Guide/Data")

function p.eGuide(frame) return h.printGuide(frame, "eGuide", Data.eGuides) end

function p.Guide(frame) return h.printGuide(frame, "Guide", Data.guides) end

function h.printGuide(frame, template, guideData) local args, err = utilsArg.parse(frame:getParent.args, p.Templates[template]) local game, publisher, omitPublisher = args.game, args.publisher, args.omitPublisher local categories = err and err.categoryText or "" if utilsString.isBlank(game) then return utilsError.error("game required"), categories elseif utilsString.isBlank(publisher) then return utilsError.error("publisher required"), categories end local gameGuides = guideData[game] local guide = gameGuides and gameGuides[publisher] if gameGuides == nil then categories = categories.."" return utilsError.error("invalid game guide", string.format("Invalid game . For a list of supported game guides, see Template:%s.", game, template)), categories elseif guide == nil then categories = categories.."" return utilsError.error("invalid game guide", string.format("Guide publisher  is invalid for game  . For a list of supported game guides, see Template:%s.", publisher, game, template)), categories end local result = p.link(guide.guide) if not omitPublisher then result = result .. " by "..guide.by.."" end return result, categories end -- Strips (eGuide) from the link text if the page name ends with it function p.link(guidePage) local endMarker = "endMarker" local index = string.find(guidePage..endMarker, " %(eGuide%)"..endMarker) local linkText = guidePage if index ~= nil then linkText = string.sub(guidePage, 1, index-1) end return string.format("%s", guidePage, linkText) end

function p.Data(frame) local utilsLayout = require("Module:UtilsLayout") local template = frame:getParent:getTitle if template == "Template:Guide/Documentation" then return p.guideTable("Guide", "guides") elseif template == "Template:EGuide/Documentation" then return p.guideTable("eGuide", "eGuides") else return utilsLayout.tabs({			{				label = "Guides",				content = p.guideTable("Guide", "guides"),			},			{				label = "eGuides",				content = p.guideTable("eGuide", "eGuides")			}		}) end end function p.guideTable(template, guideType) local Franchise = require("Module:Franchise") local utilsLayout = require("Module:UtilsLayout") local games = Franchise.enum({ includeGroups = true }) local rows = {} for i, game in ipairs(games) do		local gameGuides = Data[guideType][game] or {} gameGuides = utilsTable.keys(gameGuides) table.sort(gameGuides) local gameRows = utilsTable.map(gameGuides, function(guide)			local input = string.format(" ", template, game, guide)			local output = mw.getCurrentFrame:expandTemplate({ title = template, args = {game, guide} })			return {input, output}		end) rows = utilsTable.concat(rows, gameRows) end local wikitable = utilsLayout.table({		headers = {"Input", "Output"},		rows = rows,	}) return wikitable end

local params = { [1] = {		name = "game", type = "string", required = true, desc = "A game abbreviation. See .", trim = true, nilIfEmpty = true, },	[2] = {		name = "publisher", type = "string", required = true, desc = "A publisher abbreviation. See .", trim = true, nilIfEmpty = true, },	[3] = {		name = "omitPublisher", type = "boolean", desc = "If present, the author/publisher link is omitted and only the magazine link is shown.", canOmit = true, }, } p.Templates = { ["Guide"] = { purpose = "Formatting links to guides.", params = params, examples = { {"ALttP", "Nintendo"}, {"ALttP&FS", "Nintendo"}, {"BotW", "Piggyback", "-"}, {				desc = "Error handling", args = {"OoT3D", "invalid publisher"}, },			{"invalid game", "Nintendo"}, {"OoT3D", ""}, {"", ""},		}	},	["eGuide"] = { purpose = "Formatting links to eGuides.", params = params, examples = { {"ST", "Prima"}, {"BotW", "Piggyback", "-"}, {				desc = "Error handling", args = {"OoT3D", "invalid publisher"}, },			{"invalid game", "Nintendo"}, {"OoT3D", ""}, {"", ""},		},	}, }

p.Schemas = { Data = { type = "record", required = true, properties = { {				_id = "#guides", name = "eGuides", required = true, type = "map", keyPlaceholder = "game", keys = { type = "string" }, values = { type = "map", keyPlaceholder = "guideInput", keys = { type = "string" }, values = { type = "record", properties = { {								name = "guide", type = "string", required = true, desc = "The wiki page for the guide.", },							{								name = "by", type = "string", required = true, desc = "The wiki page for the company or team that authored the guide.", },						},					},				},			},			{				name = "guides", _ref = "#guides", },		},	}, }

return p