Module:Letter

local p = {}

local utilsArg = require("Module:UtilsArg") local utilsMarkup = require("Module:UtilsMarkup") local utilsTable = require("Module:UtilsTable") local subpageList = require("Module:Subpage List")

local dataTree = subpageList.main("Data:Letters")

function p.Letter(frame) local args, err = utilsArg.parse(frame:getParent.args, p.Templates.Letter) if err then return utilsMarkup.categories(err.categories) end return p.letter(args) end

function p.letter(args) local output = "" local data = p.getRawData(args) data = p.formatData(data) output = output .. p.applyDisplayOptions(data, args.display) return output end

function p.getRawData(args) local dataPageName = dataTree .children[args.game] .children[args.letterId] .value local dataPage = mw.title.new(dataPageName) return dataPage:getContent end

function p.formatData(rawData) local lines = mw.text.split(rawData, '\n') local formattedText = '' for i, line in ipairs(lines) do		local formattedLine = string.gsub(line, "^%s", " ") --leading space repeat local rep formattedLine, rep = string.gsub(formattedLine, "%s%s", " ") until (rep == 0) if (line ~= '' and i ~= #lines) then formattedLine = formattedLine .. ' '		end formattedText = formattedText .. formattedLine .. '\n' end return mw.getCurrentFrame:preprocess(formattedText) end

function p.applyDisplayOptions(text, displayOptions) if not displayOptions then return text end local tags = { blockquote = {'"', '"'}, collapsed = {' ', ' '}, italics = {', '}, nowrap = {' ', '  '} }	local displayText = text for _, option in pairs(displayOptions) do displayText = tags[option][1] .. '\n' .. displayText .. '\n' ..tags[option][2] end return displayText end

p.Templates = { Letter = { purpose = "This template outputs the full text of NaN Letters with in-game coloration and spacing.", usesData = "Data:Letters", params = { [1] = {				name = "game", required = true, enum = utilsTable.merge({}, utilsTable.keys(dataTree.children), {					reference = "Data:Letters"				}), desc = "The standard initialism for the game.", },			[2] = {				name = "letterId", required = true, enumDependsOn = "game", enum = function(game) local lettersForGame = dataTree.children[game] or {} local letterIds = utilsTable.keys(lettersForGame.children or {}) letterIds.reference = "Data:Letters/" .. game .. "" return letterIds end, desc = "The letter's unique identifier.", },			display = { placeholder = "option1, option2, ..., optionN", split = true, enum = {"blockquote", "italics", "collapsed", "nowrap"}, desc = "Multiple options can be combined together in any order to change how the letter text is displayed. See examples below.", },		},		paramOrder = {1, 2, "display"}, format = "inline", examples = { vertical = true, {"TWW", "Kashiko", display= "blockquote, italics, nowrap"}, {"TWW", "Beedle's Shop Ship", display= "italics, collapsed"}, {"invalid", "args"}, },	}, }

return p