More actions
suggestion by dinoguy1000 |
adding a fmt parameter |
||
| Line 13: | Line 13: | ||
-- str: String to parse | -- str: String to parse | ||
-- delim: String to use to join the book names (default '\n') | -- delim: String to use to join the book names (default '\n') | ||
function p._appearancesList(str, delim) | function p._appearancesList(str, fmt, delim) | ||
if not str then | if not str then | ||
return '{{error|Please specify the <code>str</code> or first unnamed param.}}' | return '{{error|Please specify the <code>str</code> or first unnamed param.}}' | ||
end | end | ||
fmt = fmt or '%s' | |||
delim = delim or '\n' | delim = delim or '\n' | ||
local names = mw.text.split(str, ' ', true) | local names = mw.text.split(str, ' ', true) | ||
| Line 23: | Line 24: | ||
local full = abbrs[v] | local full = abbrs[v] | ||
if full then | if full then | ||
names[i] = full | names[i] = string.format(fmt, full) | ||
else | else | ||
return '{{error|Unrecognized abbreviation <code>' .. abbr .. '</code>}}' | return '{{error|Unrecognized abbreviation <code>' .. abbr .. '</code>}}' | ||
| Line 50: | Line 51: | ||
local args = frame == mw.getCurrentFrame() and frame:getParent().args or frame | local args = frame == mw.getCurrentFrame() and frame:getParent().args or frame | ||
local str = args.str or args[1] | local str = args.str or args[1] | ||
local delim = args.delim or args[ | local fmt = args.fmt or args[2] or "''[[%s]]''" | ||
local delim = args.delim or args[3] or '<br>' | |||
-- debugging stuff | -- 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)) | ||
Revision as of 20:16, 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, fmt, delim)
if not str then
return '{{error|Please specify the <code>str</code> or first unnamed param.}}'
end
fmt = fmt or '%s'
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] = string.format(fmt, 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 = frame == mw.getCurrentFrame() and frame:getParent().args or frame
local str = args.str or args[1]
local fmt = args.fmt or args[2] or "''[[%s]]''"
local delim = args.delim or args[3] or '<br>'
-- debugging stuff
return p._dump(frame) .. '<br>\n' .. p._dump(frame:getParent()) .. '<br>\n' .. frame:preprocess(p._appearancesList(str, delim))
end
return p