Hi! I’ve been working on a custom markup system to add extra richtext tags; one feature of this is being able to pass properties e.g.,
-- Built in <font> tag
<font size='50' face='Michroma'>
-- My custom <blink> tag
<blink color1='rgb(255, 0, 0)' color2='rgb(0, 255, 0)' threshold='0.5' speed='3'>
Ripping property values is easy to do using string patterns (see: String Patterns)
-======================================
Example:
local tag = "<font size='50' face='Michroma'>"
local sizePattern = "size='(%d+)'"
local facePattern = "face='(%a+)'"
local size = tag:match(sizePattern)
local face = tag:match(facePattern)
print(size, face)
-- Output: 50 Michroma
Nice!
-======================================
Lets try with my more complex tag…
local tag = "<blink color1='rgb(255, 0, 0)' color2='rgb(0, 255, 0)' threshold='0.5' speed='3'>"
local color1Pattern = "color1='rgb((%d+),%s?(%d+),%s?(%+d)'" -- %s? allows 0 or 1 whitespace
local color2Pattern = "color2='rgb((%d+),%s?(%d+),%s?(%+d)'"
local thresholdPattern = "threshold='(%d+)'"
local speedPattern = "speed='(%d+)'"
local color1 = {tag:match(color1Pattern)}
local color2 = {tag:match(color2Pattern)}
local threshold = tag:match(thresholdPattern)
local speed = tag:match(speedPattern)
print(color1, color2, threshold, speed)
-- Output:{} {} nil 3
That output is not what we expected…
In
tag
, the color properties and threshold property are using (
)
.
which are Magic Characters
-======================================
We can create a fix for the color/rgb issue by using curly brackets instead. I am however a bit stumped as to how to handle decimal represenation in threshold='0.5'
- How about typing a scientific notation? Unfortunately, 0.5 == 5e-1.
-
is a Magic Character - We can escape the
.
by instead using%.
- but then this is in the format%d%p%d
Would greatly appreciate some thoughts/ideas here!