I need to wrap all instances of %{ ... %} with <span code='notranslate'>...</span> UNLESS the %{ ... } appears within an HTML tag. For example, this:
"Or %{register_text} for a new account by <a href='%{path}'>clicking here</a>."
needs to become this
"Or <span code='notranslate'>%{register_text}</span> for a new account by <a href='%{path}'>clicking here</a>."
my current regex doesn’t take into account the HTML tag situation:
x.gsub(/[?<!]%\{([a-zA-Z0-9_\-]*)\}[?>!]/i) {|s| "<span class='notranslate'>#{s}</span>"}
so I am wondering how to do this in Ruby with regex.
Any takers?
I am not sure about the input space, so this is the best that I can come up with. I also clean up the regex a bit along the way.
For a well-formed HTML, it will only match tokens that are outside tag. If the HTML is malformed, I don’t think I’m up to the task to write the regex.
I also assume that there is no embedded Javascript in the page, since
>and<in Javascript is not escaped.