Module:Template input parse: Difference between revisions
mNo edit summary |
Added fuzzy option for parsing a string of ratios (entries that are a single value are interpreted as a ratio with denominator 1); changed parse-numeric-pairs functions to have options to change both delimiters (between entries and within entries) |
||
Line 73: | Line 73: | ||
-- Helper function | -- Helper function | ||
-- Parses a list of numeric pairs | -- Parses a list of numeric pairs | ||
-- Delimiter between each | -- 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 | -- 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 | -- 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, | function p.parse_numeric_pairs(unparsed, delimiter1, delimiter2) | ||
local | local delimiter1 = delimiter1 or ";" | ||
local delimiter2 = delimiter2 or "/" | |||
-- Split the string of unparsed pairs | -- Split the string of unparsed pairs | ||
local parsed = p.parse_entries(unparsed) | 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) | |||
if pair ~= nil and #pair == 2 then | |||
table.insert(pairs_, pair) | |||
end | |||
end | |||
return pairs_ | |||
end | |||
-- Helper function | |||
-- A less strict version of parse-numeric-pairs that parses singular values | |||
-- as a ratio with denominator 1. | |||
function p.parse_numeric_pair_fuzzy(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 | |||
elseif #parsed == 1 then | |||
return { parsed[1], 1 } | |||
else | |||
return nil | |||
end | |||
end | |||
-- Helper function | |||
-- Parses a list of numeric pairs, where if values are not ratios, they're | |||
-- interpreted as ratios with denominator 1. | |||
function p.parse_numeric_pairs_fuzzy(unparsed, delimiter1, delimiter2) | |||
local delimiter1 = delimiter1 or ";" | |||
local delimiter2 = delimiter2 or "/" | |||
-- Split the string of unparsed pairs | |||
local parsed = p.parse_entries(unparsed, delimiter1) | |||
-- Then tokenize the tokens into numeric pairs | -- Then tokenize the tokens into numeric pairs | ||
local pairs_ = {} | local pairs_ = {} | ||
for i = 1, #parsed do | for i = 1, #parsed do | ||
local pair = p. | local pair = p.parse_numeric_pair_fuzzy(parsed[i], delimiter2) | ||
if pair ~= nil and #pair == 2 then | if pair ~= nil and #pair == 2 then | ||
table.insert(pairs_, pair) | table.insert(pairs_, pair) | ||
Line 109: | Line 151: | ||
end | end | ||
return pairs_ | return pairs_ | ||
end | |||
function p.tester() | |||
return p.parse_numeric_pairs_fuzzy("2.5.7/6.11/6", ".", "/") | |||
end | end | ||
return p | return p |