Module:Transcript

local p = {} local h = {}

local TransclusionArguments = require("Module:Transclusion Arguments") local utilsArg = require("Module:UtilsArg") local utilsCargo = require("Module:UtilsCargo") local utilsLayout = require("Module:UtilsLayout") local utilsVar = require("Module:UtilsVar")

local CATEGORY_INVALID_ARGS = require("Module:Constants/category/invalidArgs") local VAR_TRANSCRIPT = "Template:Transcript/"

function h.warn(msg) local utilsError = require("Module:UtilsError") utilsError.warn(msg) end

function h.error(errMsg, warnMsg) local utilsError = require("Module:UtilsError") return utilsError.error(errMsg, warnMsg) end

function p.Copy(frame) local args, err = utilsArg.parse(frame:getParent.args, p.Templates["Transcript Copy"]) local categories = err and err.categoryText or "" if not args.fromPage or not args.storedAs then return categories end local transcript = h.fetchTranscript(args.storedAs, args.fromPage) if not transcript then local err = h.error("Data not found", string.format("No transcript named  is stored on page %s", args.storedAs, args.fromPage)) categories = categories.."" return err, categories else TransclusionArguments.store({			module = "Module:Transcript",			isValid = true,			args = {				fromPage = args.fromPage,				storedAs = args.storedAs,			},		}) return p.print(transcript, false), categories end end

function p.Main(frame) local args, err = utilsArg.parse(frame:getParent.args, p.Templates.Transcript) local categories = err and err.categoryText or "" if not args.text then return categories end

if args.storeAs then local cachedTranscript = utilsVar.get(VAR_TRANSCRIPT..args.storeAs) if cachedTranscript then h.warn(string.format("A transcript named  is already stored on this page. Please rename this transcript.", args.storeAs)) categories = categories.."" else utilsVar.set(VAR_TRANSCRIPT..args.storeAs, args.text) end frame:callParserFunction({			name = "#cargo_store:_table=Transcripts",			args = {				name = args.storeAs,				text = args.text,			}		}) end

return p.print(args.text, true), categories end

function h.fetchTranscript(name, page) local results = utilsCargo.query("Transcripts", "text", {		where = utilsCargo.allOf({ _pageName = page, name = name, })	})	local text = results[1] and results[1].text text = text and "\n"..text.."\n" -- re-add leading and trailing space stripped by Cargo return text end

function p.Pages(frame) local args, err = utilsArg.parse(frame:getParent.args, p.Templates["Transcript Pages"]) local categories = err and err.categoryText or "" local tabData = {} for i, page in ipairs(args.pages or {}) do		local pageTranscript = p.print(page.page, true) table.insert(tabData, {			label = args.entry.." "..i,			content = pageTranscript		}) end return utilsLayout.tabs(tabData) end

function p.print(text, blockquote) local transcript = mw.getCurrentFrame:extensionTag("poem", text) if blockquote then return ''..transcript.." " else return transcript end end

p.Templates = { Transcript = { format = "block", description = "Displays transcripts of written texts and song lyrics.", purpose = "Displays transcripts of in-game written texts and song lyrics. For multi-page books, use Template:Transcript Pages.", params = { [1] = {				inline = true, name = "text", required = true, type = "content", desc = "In-game text with original formatting—line breaks, paragraphs, indentation, etc.", },			storeAs = { inline = true, type = "string", desc = "If present, the transcript is stored in the Transcripts Cargo table under that name. Other pages can retrieve the transcript using Template:Transcript Copy.", trim = true, nilIfEmpty = true, },		}	},	["Transcript Copy"] = { purpose = "Retrieves transcripts that were stored on other pages using Template:Transcript.", paramOrder = {"fromPage", "storedAs"}, params = { fromPage = { required = true, type = "wiki-page-name", desc = "The name of the wiki page containing the transcript to be copied.", trim = true, nilIfEmpty = true, },			storedAs = { required = true, type = "string", desc = "The name of the transcript to be copied, corresponding to the  parameter of Template:Transcript.", },		},		examples = { vertical = true, {				fromPage = "Template:Transcript", storedAs = "Kashiko", },			{				desc = "Error: Invalid transcript name", args = { fromPage = "Template:Transcript", storedAs = "foo", },			},			{				desc = "Error: Missing parameters", args = { storedAs = "Kashiko", },			},		},	},	["Transcript Pages"] = { format = "block", purpose = "This template is a variation of Template:Transcript that displays transcripts of in-game Books with multiple pages.", categories = {"Formatting Templates"}, repeatedGroup = { name = "pages", params = {"page"}, },		params = { entry = { type = "content", desc = "The word to use in the numbered tab labels.", default = "Page", },			page = { required = true, type = "content", desc = "The in-game text of a book page with original formatting—line breaks, paragraphs, indentation, etc.", },		},	}, }

return p