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 “&” → ‘&’