Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use % as escape character in a template #451

Closed
dargueta opened this issue Apr 7, 2023 · 3 comments
Closed

Can't use % as escape character in a template #451

dargueta opened this issue Apr 7, 2023 · 3 comments

Comments

@dargueta
Copy link
Contributor

dargueta commented Apr 7, 2023

Suppose we have the following code:

tpl = [[
% for k, v in pairs(values) do
$(k) = $(v)
% end
]]

text = template.substitute(
    tpl,
    {
         _escape = '%',
         _parent = _G,
        values = {a = 1, b = 2, c = 3}
    }
)
print(text)

It appears the escape has no effect:

% for k, v in pairs(tbl) do
 = 
% end

If we use something else, like !, we get the expected result:

c = 3
a = 1
b = 2

I think we can fix this by passing plain=true to the strfind() calls here, e.g.

-- Instead of this:
local _, e, lua = strfind(chunk,esc_pat1, s)

-- Do this:
local _, e, lua = strfind(chunk,esc_pat1, s, 1, true)

Environment:
OS: Ubuntu 22.04
Lua: 5.4.4
Penlight: 1.13.1

@alerque
Copy link
Member

alerque commented Apr 8, 2023

Can you send a PR for this? I think you fix looks like it will be correct, but we'll want to see some test results, which a PR should show us.

@dargueta
Copy link
Contributor Author

dargueta commented Apr 8, 2023

Done: #452

After a bit of tinkering I realized I needed to use string escaping rather than a literal search so that the inline escape would work as well.

@dargueta
Copy link
Contributor Author

Fixed by #452

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants