Module:Template input parse: Difference between revisions
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 |