'On ' .. os.date('%A') .. ' Jane goes to ' .. Locations[os.date('%a')] .. '.'
Using a little ingenuity this task could get a lot simpler.
'On {LongDay} Janes goes to {{ShortDay}Location}.'
This is something called Nested Expressions. This could be an insurmountable task, but thankfully it's not. As long as we follow some rules we can use one of Lua's built in abilities for this.
The most important rule we need to follow is that our expression must begin and end with different characters. For example, let's use the curly brackets (i.e. '{Expression}'). If both characters were the same Lua wouldn't be able to tell the difference between the beginning and the end.
Now we need to define a function for our needs. Yes, we really do need a function for this. Since we need to follow the same steps for each nested expression we will need to call the function inside itself.
Code: Select all
function nested(line)
-- Do Something Here
end
Code: Select all
function nested(line)
return (line:gsub('%b{}', function(input)
-- Do Something Here
end))
end
Code: Select all
function nested(line)
return (line:gsub('%b{}', function(input)
local newline = nested(input:match('^{(.-)}$'))
--Do Something Here
end))
end
You can do whatever you want to your expression, just be sure you return something. It is important to remember to use the newline variable in whatever you do from this point on. This is the variable that has all of the nested expressions already evaluated.
Code: Select all
function nested(line)
return (line:gsub('%b{}', function(input)
local newline = nested(input:match('^{(.-)}$'))
if newline ~= '' then
return --Do Something Here
else
return string.format('{%s}', newline)
end
end))
end
Code: Select all
local nested = function(line, self)
return (line:gsub('%b{}', function(input)
local newline = self(input:match('^{(.-)}$'), self)
if newline ~= '' then
return --Do Something Here
else
return string.format('{%s}', newline)
end
end))
end
SomeString = nested('{Variable{AnotherVariable}}', nested)
Here's something extremely simple. We're just going to see what Jane is doing today.
Code: Select all
Variables = {
LongDay = os.date('%A'),
ShortDay = os.date('%a'),
SunLocation = 'Church',
MonLocation = 'Grocery Store',
TueLocation = 'the Salon',
WedLocation = 'a PTA meeting',
ThuLocation = 'a concert',
FriLocation = 'the bar',
SatLocation = 'the beach',
}
function nested(line)
return (line:gsub('%b{}', function(input)
local newline = nested(input:match('^{(.-)}$'))
if Variables[newline] then
return Variables[newline]
else
return string.format('{%s}', newline)
end
end))
end
SomeString = nested('On {LongDay} Janes goes to {{ShortDay}Location}.')
Code: Select all
function Replace(input)
return (input:gsub('(%b[])', function(line)
local newline = Replace(line:match('^%[(.-)]$'))
local typ, name = newline:match('(.)(.+)')
-- Establish an string to return in case we encounter an error
local ErrorString = ('[%s]'):format(newline)
-- Make allowance for escaped expression
if newline:match('^%*(.-)%*$') then
return newline:gsub('^%*(.-)%*$', '[%1]')
-- Measures / Meters
elseif typ == '&' then
-- Make allowance for section variables
local section = name:match('([^:]+)')
-- Check if Measure / Meter exists
if SKIN:GetMeasure(section) or SKIN:GetMeter(section) then
-- Use existing function for maximum flexibility
return SKIN:ReplaceVariables(('[%s]'):format(name))
-- Measure / Meter does not exist
else
return ErrorString
end
-- Variable
elseif typ == '#' then
return SKIN:GetVariable(name) or ErrorString
-- Did not define anything we are looking for
else
return ErrorString
end
end
))
end
SomeLine = Replace('[&SomeMeasure[#SomeVariable[&SomeMeter:X]]]')
Code: Select all
function SomeFunction(InputExpression, FuncTbl)
local nested = function(line, self)
return (line:gsub('%b{}', function(input)
local newline = self(input:match('^{(.-)}$'), self)
if FuncTbl[newline] then
return FuncTbl[newline]
else
return string.format('{%s}', newline)
end
end))
end
return nested(InputExpression, nested)
end
Variables = {
LongDay = os.date('%A'),
ShortDay = os.date('%a'),
SunLocation = 'Church',
MonLocation = 'Grocery Store',
TueLocation = 'the Salon',
WedLocation = 'a PTA meeting',
ThuLocation = 'a concert',
FriLocation = 'the bar',
SatLocation = 'the beach',
}
SomeString = SomeFunction('On {LongDay} Janes goes to {{ShortDay}Location}.', Variables)