In a Discourse forum support thread titled ‘Replace backslash escape only in old posts after import,’ user fhe reports that imported posts containing \% are being incorrectly rendered as % due to markdown processing. They seek a way to either exclude \% from processing or replace it with \\% only in imported posts. schungx suggests using rake posts:remap for global replacements, but fhe finds that regex lookbehinds (needed to avoid replacing \\%) aren’t supported by the database layer. pfaffman recommends using the Rails console with date-based post filtering, prompting fhe to request an example. After experimenting, fhe shares a Ruby snippet targeting posts by ID but notes it doesn’t work until pfaffman suggests adding post.save. Eventually, fhe succeeds by adjusting escape sequences: raw = post.raw.gsub(/(?<=[^\])\\%/, '\\%'), noting single vs. double quotes behave differently with escapes. Mittineague explains this behavior, clarifying how single quotes preserve literal escapes while double quotes process them. pfaffman expresses satisfaction that their initial nudge helped. The thread resolves with a working Rails console solution for selectively fixing escaped percent signs in imported posts.
I’ve imported a forum where we made heavy use of this combination of characters \%
which is now reduced to the percent sign due to markdown (?) processing
% (I’ve also used the backslash here, but omitted the code ticks)
Is there a way to
exclude the \% combination from being processed in a special way, or
to replace the \% combination to \\% but only for post-import posts?
However, when looking at the code, it seems like regexp is also supported.
So something like
rake posts:remap["(?<=[^\\])\\%","\\\\%","regex"]
should replace all occurrences of \% which are not preceded by another \. But it seems this regexp is then passed along to the database layer which doesn’t support lookbehinds.
This is powerful stuff Seems like I’m slowly learning Ruby here.
I’ve thought of something like
imported_posts = Post.where("id < 117108")
imported_posts.find_each do |post|
raw = post.raw.gsub(/(?<=[^\\])\\%/, "\\\\%")
post.update_column(:raw, raw)
end
but unfortunately, it doesn’t replace anything in Post.raw. Anything obvious I’m missing?
Yes, escapes in strings inside single quote marks are literal strings, all escapes intact. Escapes inside double quote marks get escaped before going to the next piece of code. i.e.
‘\\\\’ goes as ‘\\\\’ while “\\\\” goes as ‘\\’
Similar with ‘&’ → ‘&’ vs “&” → ‘&’