-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prompt Regex Escaping Confusion #86
Comments
hmmmm - a couple quick thoughts:
|
I can make a PR at the weekend, maybe we even get the escaping thingy resolved by then. 😄 What definitely is a JS thing is that two different regexs match the same string. Ha I just found tha jinja2 has the format filter builtin. so changing:
to: return formatCopyText(target.innerText, {{ "{!r}".format(copybutton_prompt_text) }}, {{ copybutton_prompt_is_regexp | lower }}, {{ copybutton_only_copy_prompt_lines | lower }}, {{ copybutton_remove_prompts | lower }}) Also does the trick, looks nicer and should be more reliable than my repr solution. |
Ah cool didn't notice that, yep that looks like it should improve the issue thanks @s-weigand, all them backslashes are a right pain Hopefully, this will fix my issue here: #84 (comment) |
nice catch @s-weigand ! |
This fixes the way that the `copybutton_prompt_text` value is injected into `copybutton.js_t` (as discussed in #86). The raw string formatting means that backslashes are now propagated correctly and removes the need for "double escaping". Users will need to update their `copybutton_prompt_text` string accordingly. Co-authored-by: Chris Sewell <chrisj_sewell@hotmail.com>
First of all thanks for the new Prompt RegEx feature ❤️
I did just try it out and after some fiddeling around with the escaping, it works like a charm.
Expected behaviour
First of all I think in some edge cases it might make a difference to mention that the RegEx you type in your
config.py
is a JS RegEx and not a python one.So that said IMHO it should work similar to this:
🌈 🦄
What I did
In my case the prompt I wanted to get rid of was of the form
r"In \[\d*\]: |\.\.\.: "
which is the
jupyter console
style and pretty much the same as the iPython style from the docs"\\[\\d*\\]: |\\.\\.\\.: "
So it should be straight forward
"In \\[\\d*\\]: |\\.\\.\\.: "
as in the docs and I would be done?
But it didn't work, so I did some digging and ended up with:
"In \\[\\d*\\]: |\\.\\.\\.: |\\$ "
What internally happens
In my
config.py
, I setcopybutton_prompt_text
which is the same as:
Sphinx now replaces the context variable
copybutton_prompt_text
incopybutton.js_t
and generatescopybutton.js
.But when we look at that string which should have just been passed as is, it is missing backslashes.
'In \[\d*\]: |\.\.\.: |\$ '
Well this looks like a totaly valid JS RegEx, so this is fine?
And it would be if written like this (JS just wants keep people on their toes, so why not a different character for RegEx strings?):
/In \[\d*\]: |\.\.\.: |\$ /
Now the string gets passed to the RegExp constuctor, which expects strings to be escaped like python, if you don't use raw strings.
But it doesn't match.
Why is this going wrong and why didn't the tests catch this?
TLDR
IMHO this all comes down ro JS RegExp 'helping' you in some cases when it can interprete what you might have ment, which gives you false security (false positiv match in the tests).
Details
Disclaimer I'm in no way a JS expert with insight into the internals, I just found this due to WTF moments + trail and error.
So far so good, all testcases work fine, as we would expect since they pass.
The only explaination I got for this behaviour, is that JS wants to "help" the user by guessing it's intention, like the famous adding thingy:
but at some points it is like "I don't get it", and the pattern that worked before fails.
Possible fix
Use
repr
inadd_to_context
If this line was changed to:
I know it looks ugly, but would add extra backslashes for escaping (maybe someone else has a more elegant solution).
I'm not sure how much
escapeRegExp
would need to be adjusted to those changes.I didn't have luck with backslash replacing in JS:
So in conclusion:
Python ❤️
JS 😠
P.S.: Wish we had the
sphinx-copybutton
on github, for the JS blobs I posted 😢The text was updated successfully, but these errors were encountered: