Perform list substitution only on word boundaries

When performing list substitution, only replace a list name when it is
surrounded by whitespace or expected punctuation characters. Lua
patterns don't have a notion of this-or-that patterns e.g. (^|abc), so
we have 3 versions of the substitution depending on whether he list name
occurs in the beginning, middle, or end of a string.

This fixes #197.
This commit is contained in:
Mark Stemm 2017-06-30 15:03:33 -07:00
parent 42e50356cf
commit f1b44da90c

View File

@ -325,7 +325,12 @@ end
function compiler.compile_filter(name, source, macro_defs, list_defs) function compiler.compile_filter(name, source, macro_defs, list_defs)
for name, items in pairs(list_defs) do for name, items in pairs(list_defs) do
source = string.gsub(source, name, table.concat(items, ", ")) local begin_name_pat = "^("..name..")([%s(),=])"
local mid_name_pat = "([%s(),=])("..name..")([%s(),=])"
local end_name_pat = "([%s(),=])("..name..")$"
source = string.gsub(source, begin_name_pat, table.concat(items, ", ").."%2")
source = string.gsub(source, mid_name_pat, "%1"..table.concat(items, ", ").."%3")
source = string.gsub(source, end_name_pat, "%1"..table.concat(items, ", "))
end end
local ast, error_msg = parser.parse_filter(source) local ast, error_msg = parser.parse_filter(source)