Module:Explode String: Difference between revisions

From Valheim Wiki
Created page with "local p = {} p.loop = function(frame) local stage = frame.args.stage local rows = {} -- the actual looping part for value in mw.text.gsplit(stage, ',') do table.insert(r..."
 
No edit summary
 
(18 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local currentFrame
p.loop = function(frame)
local trim = mw.text.trim
local stage = frame.args.stage
 
local rows = {}
---Holds the arguments from the template call.
-- the actual looping part
local args_table
for value in mw.text.gsplit(stage, ',') do
 
table.insert(rows, '|' .. value)
local function toList(input, linkSuffix, hasLink)
    if not linkSuffix then
        linkSuffix = ''
    else
        linkSuffix = ' '..linkSuffix
    end
 
-- Turns Fish,Kraken into
-- <ul>
--  <li>[[Fish (Creature) | Fish)]]</li>
--  <li>[[Kraken (Creature) | Kraken)]]</li>
-- </ul>
    local ret = '<ul class="infobox-ul">'
    for str in string.gmatch(input, '([^,]+)') do
        if hasLink == 'no' then
            ret = ret .. '<li>' .. str .. '</li>'
        else
            ret = ret .. '<li>[[' .. str .. linkSuffix .. '|' .. str .. ']]</li>'
        end
    end
    ret = ret .. '</ul>'
 
    return ret
end
 
---Return a trimmed version of the value of the template parameter with the specified `key`.
---Return `nil` if the parameter is empty or unset.
---@param key string|number
---@return string|nil
local function getArg(key)
local value = args_table[key]
if not value then
return nil
end
 
value = trim(value)
if value == '' then
return nil
end
end
 
return ' {|\n' .. table.concat(rows, '\n|-\n') .. '\n|}'
return value
end
end


-----------------------------------------------------------------
local p = {}
-- main return object
p.loop = function(frame)
return {
args_table = frame:getParent().args


go = function(frame, args)
    local ret  = ''
string = getArg('string')
    ret = ret..(getArg('linksuffix') or '?')..(getArg('input') or 'Z')..(getArg('string') or '8')
linksuffix = getArg('linksuffix') or ''


-- output
    mw.log(ret)
return string..linksuffix..'test'


end,
    return toList(getArg('input') or '', getArg('linksuffix') or '', getArg('link') or '')
end


}
return p

Latest revision as of 23:07, 23 February 2023

Documentation for this module may be created at Module:Explode String/doc

local currentFrame
local trim = mw.text.trim

---Holds the arguments from the template call.
local args_table

local function toList(input, linkSuffix, hasLink)
    if not linkSuffix then
        linkSuffix = ''
    else
        linkSuffix = ' '..linkSuffix
    end

-- Turns Fish,Kraken into
-- <ul>
--  <li>[[Fish (Creature) | Fish)]]</li>
--  <li>[[Kraken (Creature) | Kraken)]]</li>
-- </ul>
    local ret = '<ul class="infobox-ul">'
    for str in string.gmatch(input, '([^,]+)') do
        if hasLink == 'no' then
            ret = ret .. '<li>' .. str .. '</li>'
        else
            ret = ret .. '<li>[[' .. str .. linkSuffix .. '|' .. str .. ']]</li>'
        end
    end
    ret = ret .. '</ul>'

    return ret
end

---Return a trimmed version of the value of the template parameter with the specified `key`.
---Return `nil` if the parameter is empty or unset.
---@param key string|number
---@return string|nil
local function getArg(key)
	local value = args_table[key]
	if not value then
		return nil
	end

	value = trim(value)
	if value == '' then
		return nil
	end

	return value
end

local p = {}
p.loop = function(frame)
	args_table = frame:getParent().args

    local ret  = ''
    ret = ret..(getArg('linksuffix') or '?')..(getArg('input') or 'Z')..(getArg('string') or '8')

    mw.log(ret)

    return toList(getArg('input') or '', getArg('linksuffix') or '', getArg('link') or '')
end

return p