Module:Template input parse: Difference between revisions

From Xenharmonic Wiki
Jump to navigation Jump to search
Ganaram inukshuk (talk | contribs)
Added variant function that returns split numeric values
Ganaram inukshuk (talk | contribs)
Added functions to parse numeric pairs
Line 48: Line 48:
return nil
return nil
end
end
end
-- Helper function
-- Parses a pair of numeric values
function p.parse_numeric_pair(unparsed, delimiter)
local parsed = {}
local delimiter = delimiter or ':'
local expr = '([^' .. delimiter .. ']+)'
for entry in string.gmatch(unparsed, expr) do
local trimmed = entry:gsub("^%s*(.-)%s*$", "%1")
local numeric = tonumber(trimmed)
table.insert(parsed, numeric) -- Add to array
end
if #parsed == 2 then
return parsed
else
return nil
end
end
-- Helper function
-- Parses a list of numeric pairs
-- Delimiter between each pair must be a semicolon
-- Delimiter between the pair's two values can be anything; default is slash
-- as the intended use is parsing ratios a/b
function p.parse_numeric_pairs(unparsed, delimiter)
local delimiter = delimiter or "/"
-- Split the string of unparsed pairs
local parsed = p.parse_entries(unparsed)
-- Then tokenize the tokens into numeric pairs
local pairs_ = {}
for i = 1, #parsed do
local pair = p.parse_numeric_pair(parsed[i], delimiter)
if pair ~= nil and #pair == 2 then
table.insert(pair)
end
end
return pairs_
end
end



Revision as of 18:46, 22 August 2024

Module documentation[view] [edit] [history] [purge]
Todo: add documentation

local p = {}

-- Helper module for various modules that require entry of multiple labels/values

-- Helper function
-- Parses entries from a delimited string and returns them in an array
-- Default delimiter is semicolon (;)
function p.parse_entries(unparsed, delimiter)
	local parsed = {}
	local delimiter = delimiter or ';'
	local expr = '([^' .. delimiter .. ']+)'
	for entry in string.gmatch(unparsed, expr) do
		local trimmed = entry:gsub("^%s*(.-)%s*$", "%1")
		table.insert(parsed, trimmed)		-- Add to array
	end
	return parsed
end

-- Helper function
-- Parses entries from a delimited string and returns them in an array
-- Values entered are convereted into numbers rather than strings.
-- Default delimiter is semicolon (;)
function p.parse_numeric_entries(unparsed, delimiter)
	local parsed = {}
	local delimiter = delimiter or ';'
	local expr = '([^' .. delimiter .. ']+)'
	for entry in string.gmatch(unparsed, expr) do
		local trimmed = entry:gsub("^%s*(.-)%s*$", "%1")
		local numeric = tonumber(trimmed)
		table.insert(parsed, numeric)		-- Add to array
	end
	return parsed
end

-- Helper function
-- Parses pairs of values separated by a delimiter; default is colon (:)
function p.parse_pair(unparsed, delimiter)
	local parsed = {}
	local delimiter = delimiter or ':'
	local expr = '([^' .. delimiter .. ']+)'
	for entry in string.gmatch(unparsed, expr) do
		local trimmed = entry:gsub("^%s*(.-)%s*$", "%1")
		table.insert(parsed, trimmed)		-- Add to array
	end
	if #parsed == 2 then
		return parsed
	else
		return nil
	end
end

-- Helper function
-- Parses a pair of numeric values
function p.parse_numeric_pair(unparsed, delimiter)
	local parsed = {}
	local delimiter = delimiter or ':'
	local expr = '([^' .. delimiter .. ']+)'
	for entry in string.gmatch(unparsed, expr) do
		local trimmed = entry:gsub("^%s*(.-)%s*$", "%1")
		local numeric = tonumber(trimmed)
		table.insert(parsed, numeric)		-- Add to array
	end
	if #parsed == 2 then
		return parsed
	else
		return nil
	end
end

-- Helper function
-- Parses a list of numeric pairs
-- Delimiter between each pair must be a semicolon
-- Delimiter between the pair's two values can be anything; default is slash
-- as the intended use is parsing ratios a/b
function p.parse_numeric_pairs(unparsed, delimiter)
	local delimiter = delimiter or "/"
	-- Split the string of unparsed pairs
	local parsed = p.parse_entries(unparsed)
	-- Then tokenize the tokens into numeric pairs
	local pairs_ = {}
	for i = 1, #parsed do
		local pair = p.parse_numeric_pair(parsed[i], delimiter)
		if pair ~= nil and #pair == 2 then
			table.insert(pair)
		end
	end
	return pairs_
end

-- Helper function
-- Parses key-value pairs separated by a semicolon
-- Pairs themselves are separated using a colon
function p.parse_kv_pairs(unparsed)
	-- Split the string of unparsed pairs
	local parsed = p.parse_entries(unparsed)
	-- Then tokenize the tokens into key-value pairs
	local pairs_ = {}
	for i = 1, #parsed do
		local pair = p.parse_pair(parsed[i])
		if pair ~= nil and #pair == 2 then
			pairs_[pair[1]] = pair[2]
		end
	end
	return pairs_
end

return p