Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Book appearances: Difference between revisions

From Wings of Fire Wiki
No edit summary
No edit summary
Line 18: Line 18:
end
end
delim = delim or '\n'
delim = delim or '\n'
local res = '';
local names = mw.text.split(str, ' ', true)
-- iterate over substrings
-- iterate over substrings
for abbr in mw.text.gsplit(str, ' ', true) do
for i, v in ipairs(names) do
local full = abbrs[abbr]
local full = abbrs[v]
if full then
if full then
res = res .. full .. delim -- concat to result string
names[i] = full
else
else
return '{{error|Unrecognized abbreviation <code>' .. abbr .. '</code>}}'
return '{{error|Unrecognized abbreviation <code>' .. abbr .. '</code>}}'
end
end
end
end
return res
return table.concat(names, delim)
end
end


Line 57: Line 57:
local str = args.str or args[1]
local str = args.str or args[1]
local delim = args.delim or args[2]
local delim = args.delim or args[2]
-- debugging stuff
return p._dump(frame) .. '<br>\n' .. p._dump(frame:getParent()) .. '<br>\n' .. frame:preprocess(p._appearancesList(str, delim))
return p._dump(frame) .. '<br>\n' .. p._dump(frame:getParent()) .. '<br>\n' .. frame:preprocess(p._appearancesList(str, delim))
end
end


return p
return p

Revision as of 06:57, 18 April 2025

Documentation for this module may be created at Module:Book appearances/doc

-- local string = require("Module:String")

local p = {}

local abbrs = {
	["TDP"] = "The Dragonet Prophecy",
	["TLH"] = "The Lost Heir",
	["THK"] = "The Hidden Kingdom",
	["TDS"] = "The Dark Secret",
	["TBN"] = "The Brightest Night",
}

-- str: String to parse
-- delim: String to use to join the book names (default '\n')
function p._appearancesList(str, delim)
	if not str then 
		return '{{error|Please specify the <code>str</code> or first unnamed param.}}'
	end
	delim = delim or '\n'
	local names = mw.text.split(str, ' ', true)
	-- iterate over substrings
	for i, v in ipairs(names) do
		local full = abbrs[v]
		if full then
			names[i] = full
		else
			return '{{error|Unrecognized abbreviation <code>' .. abbr .. '</code>}}'
		end
	end
	return table.concat(names, delim)
end

-- for debugging -- https://stackoverflow.com/a/27028488
function p._dump(o)
   if type(o) == 'table' then
      local s = '{ '
      for k,v in pairs(o) do
         if type(k) ~= 'number' then k = '"'..k..'"' end
         s = s .. '['..k..'] = ' .. p._dump(v) .. ','
      end
      return s .. '} '
   else
      return tostring(o)
   end
end


-- wrapper for _appearancesList, untested
function p.appearancesList(frame)
	local args
	-- if parent frame's args are not empty
	if frame:getParent() ~= nil and next(frame:getParent().args) ~= nil then
		args = frame:getParent().args
	else
		args = frame.args
	end
	local str = args.str or args[1]
	local delim = args.delim or args[2]
	-- debugging stuff
	return p._dump(frame) .. '<br>\n' .. p._dump(frame:getParent()) .. '<br>\n' .. frame:preprocess(p._appearancesList(str, delim))
end

return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.