Closed maxmilton closed 8 years ago
P.s. if anyone is running into a similar issue, this is the current workaround I'm using:
{{ .Summary | replaceRE "(<picture>)" "<!--$1" | replaceRE "(</picture>)" "$1-->" | safeHTML }}
Just commenting out the picture
tag is the best I can do but it's less than ideal.
replaceRE
should work as you expect, but you have to use grouping to do a replacement.
{{ .Summary | replaceRE "(<picture>.*</picture>)" "" | safeHTML }}
Please close this issue if that fixes the problem for you.
Thanks for your response @moorereason . Unfortunately that didn't solve the problem; it's still not matching the whole string.
In case a real world example is better, here's the article I'm working with:
https://github.com/MaxMilton/MaxMilton.com/blob/master/content/blog/100-days.md
and this is the img shortcode (this generates the <picture>
):
https://github.com/MaxMilton/MaxMilton.com/blob/master/themes/mm/layouts/shortcodes/img.html
and here's the layout:
https://github.com/MaxMilton/MaxMilton.com/blob/master/themes/mm/layouts/_default/summary.html
For debugging if I use {{ .Summary | replaceRE ".*" "test" }}
I would expect it to match the whole .Summary string like this:
test
Instead, this is what I get now:
test test test test test test test test test test test test test test test test test test test test test test test test test test test test
There's multiple matches and there's spaces between each one.
Well, I learned something new today. By default, the Go .
pattern doesn't match newlines. You have to pass the s
flag to the group. See regexp/syntax.
{{ .Summary | replaceRE "(?s:<picture>.*</picture>)" "" | safeHTML }}
Oh wow, that's interesting. Thank you for linking to the regexp/syntax documentation, it's actually super useful. I'm surprised I hadn't read it before :sweat_smile:
This is the correct solution so I'm closing the issue.
P.s. @moorereason thank you very much for contributing replaceRE
in the first place, it's so handy! :sparkling_heart:
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
I'm trying to use replaceRE to replace a large block of markup but it seems to only work on a per word basis. Here's an example:
Sample summary string:
(Obviously this is simplified, but there's enough code as an example)
layouts/_default/summary.html:
I want to remove the
picture
tag and everything inside it, however, my replaceRE does not match anything. I can match individual words but not a whole block of text like what I'm trying above. I'd rather not use{{ .Summary | plainify | safeHTML }}
because it strips outp
,ul
, etc.I didn't notice anything in the documentation specifying strings are automatically broken up into parts. Or am I going about this the wrong way?