Module:Template input parse: Difference between revisions
Jump to navigation
Jump to search
m Cleared todo: since tip's purpose is to parse template input, the absence of input should just be an empty table, not nil |
Merged fuzzy-ratio-parsing back into original function, so it's enabled by passing TRUE for the last arg |
||
Line 52: | Line 52: | ||
-- Helper function | -- Helper function | ||
-- Parses a pair of numeric values | -- Parses a pair of numeric values | ||
function p.parse_numeric_pair(unparsed, delimiter) | -- Passing in TRUE for is_fuzzy allows singular values to be parsed as ratios | ||
-- whose denominator is 1; default is FALSE | |||
function p.parse_numeric_pair(unparsed, delimiter, is_fuzzy) | |||
local parsed = {} | local parsed = {} | ||
local delimiter = delimiter or ':' | local delimiter = delimiter or ':' | ||
local is_fuzzy = is_fuzzy or false | |||
local expr = '([^' .. delimiter .. ']+)' | local expr = '([^' .. delimiter .. ']+)' | ||
for entry in string.gmatch(unparsed, expr) do | for entry in string.gmatch(unparsed, expr) do | ||
Line 63: | Line 66: | ||
if #parsed == 2 then | if #parsed == 2 then | ||
return parsed | return parsed | ||
elseif #parsed == 1 and is_fuzzy then | |||
return { parsed[1], 1 } | |||
else | else | ||
return nil | return nil | ||
Line 75: | Line 80: | ||
-- as the intended use is parsing ratios a/b, so input must be formatted as such | -- as the intended use is parsing ratios a/b, so input must be formatted as such | ||
-- "a/b; c/d; e/f" | -- "a/b; c/d; e/f" | ||
function p.parse_numeric_pairs(unparsed, delimiter1, delimiter2) | -- Passing in TRUE for is_fuzzy allows singular values to be parsed as ratios | ||
-- whose denominator is 1; defualt is FALSE. | |||
function p.parse_numeric_pairs(unparsed, delimiter1, delimiter2, is_fuzzy) | |||
local delimiter1 = delimiter1 or ";" | local delimiter1 = delimiter1 or ";" | ||
local delimiter2 = delimiter2 or "/" | local delimiter2 = delimiter2 or "/" | ||
local is_fuzzy = is_fuzzy or false | |||
-- Split the string of unparsed pairs | -- Split the string of unparsed pairs | ||
local parsed = p.parse_entries(unparsed, delimiter1) | local parsed = p.parse_entries(unparsed, delimiter1) | ||
Line 83: | Line 91: | ||
local pairs_ = {} | local pairs_ = {} | ||
for i = 1, #parsed do | for i = 1, #parsed do | ||
local pair = p.parse_numeric_pair(parsed[i], delimiter2 | local pair = p.parse_numeric_pair(parsed[i], delimiter2, is_fuzzy) | ||
if pair ~= nil and #pair == 2 then | if pair ~= nil and #pair == 2 then | ||
table.insert(pairs_, pair) | table.insert(pairs_, pair) | ||
Line 150: | Line 118: | ||
end | end | ||
function p.tester() | function p.tester() | ||
return p.parse_numeric_pairs("2.5.7/6.11/6.aesrdnt.17/12", ".", "/", true) | |||
return p. | |||
end | end | ||
return p | return p |
Revision as of 04:49, 24 September 2024
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
-- Passing in TRUE for is_fuzzy allows singular values to be parsed as ratios
-- whose denominator is 1; default is FALSE
function p.parse_numeric_pair(unparsed, delimiter, is_fuzzy)
local parsed = {}
local delimiter = delimiter or ':'
local is_fuzzy = is_fuzzy or false
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
elseif #parsed == 1 and is_fuzzy then
return { parsed[1], 1 }
else
return nil
end
end
-- Helper function
-- Parses a list of numeric pairs
-- Delimiter between each entry can be anything; default is semicolon; this
-- allows for entry of subgroup entries
-- Delimiter between the pair's two values can be anything; default is slash
-- as the intended use is parsing ratios a/b, so input must be formatted as such
-- "a/b; c/d; e/f"
-- Passing in TRUE for is_fuzzy allows singular values to be parsed as ratios
-- whose denominator is 1; defualt is FALSE.
function p.parse_numeric_pairs(unparsed, delimiter1, delimiter2, is_fuzzy)
local delimiter1 = delimiter1 or ";"
local delimiter2 = delimiter2 or "/"
local is_fuzzy = is_fuzzy or false
-- Split the string of unparsed pairs
local parsed = p.parse_entries(unparsed, delimiter1)
-- Then tokenize the tokens into numeric pairs
local pairs_ = {}
for i = 1, #parsed do
local pair = p.parse_numeric_pair(parsed[i], delimiter2, is_fuzzy)
if pair ~= nil and #pair == 2 then
table.insert(pairs_, 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_ = {}
--local num_pairs = 0
for i = 1, #parsed do
local pair = p.parse_pair(parsed[i])
if pair ~= nil and #pair == 2 then
pairs_[pair[1]] = pair[2]
--num_pairs = num_pairs + 1
end
end
return pairs_
end
function p.tester()
return p.parse_numeric_pairs("2.5.7/6.11/6.aesrdnt.17/12", ".", "/", true)
end
return p