gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
75.06k stars 7.48k forks source link

replaceRE only works for individual words not the whole string #2490

Closed maxmilton closed 8 years ago

maxmilton commented 8 years ago

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:

<picture> <img src="/example.jpg" alt="Example"> </picture> ... more text....

(Obviously this is simplified, but there's enough code as an example)

layouts/_default/summary.html:

{{ .Summary | replaceRE "<picture>.*</picture>" "" | safeHTML }}

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 out p, 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?

maxmilton commented 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.

moorereason commented 8 years ago

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.

maxmilton commented 8 years ago

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

maxmilton commented 8 years ago

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.

moorereason commented 8 years ago

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 }}
maxmilton commented 8 years ago

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:

github-actions[bot] commented 2 years ago

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.