-
Notifications
You must be signed in to change notification settings - Fork 868
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
PrettifyTreeprocessor stripping whitespace in <pre><code>
#1263
Comments
When you made your previous report my first thought was that it is strange the we are altering whitespace in a code block. But then I realized that the intent was only to remove extra blank lines at the end of a code block. Perhaps the earlier code which parses the code block should be removing those blank lines instead of addressing it here. Regardless, the issues you are encountering are specifically because the text of the element is empty and/or only contains whitespace, which is not something which can be done with the built-in features. I accepted the previous change only because that avoided an error being raised when the default value for the text attribute was encountered. In this case, the only way this specific edge case can be encountered is by a third-party extension building elements in this unexpected way. That being the case, the best way forward would be for your extension to also provide a replacement As an aside, I find it odd that you are wrapping each individual line with |
On further reflection, it occurs to me that perhaps a general fix could be to add something like |
Thanks for the quick reply!
I needed to preserve the leading whitespace (the spaces before
I think this may have been the original intent, so your suggested fix seems better. As long as the code doesn't have any highlighting, it seems natural to assume that it strips the end of the text, since it would perfectly fix the following: <pre><code>def foo():
return 2 </code></pre> The prettifier could rstrip the tail of the last element if there are children. If only Solution 1 if len(pre) and pre[0].tag == 'code':
code = pre[0]
if len(code)
if code[-1].tail is not None:
code[-1].tail = util.AtomicString(code[-1].tail.rstrip() + '\n')
else:
if code.text is not None:
code.text = util.AtomicString(code.text.rstrip() + '\n') Solution 2 if len(pre) and pre[0].tag == 'code':
code = pre[0]
if len(code) and code[-1].tail is not None:
code[-1].tail = util.AtomicString(code[-1].tail.rstrip() + '\n')
elif not len(code) and code.text is not None:
code.text = util.AtomicString(code.text.rstrip() + '\n') I've just made a custom Prettifier with the section removed, but it's probably more useful to include it by default. |
Ah, right. Although you could address that through CSS alone. Regardless, we do want to generally support syntax highlighters which output ET so we should probably fix the general issue.
This has been a never-ending frustration of mine. If I was to start over, I would have never used ET based on this issue alone. but I digress... I am unsure about specifically trimming the tail. I expect that we would only ever see tails in code blocks if ET objects are custom built in an extension. That being the case, my thinking was to not alter them at all in my previous response. I would accept either in a PR. Although, I think we should probably include some tests for various edge cases to test every posable variation of the logic in the if statements. We would probably need to have each test case build up an ET object, pass that to an instance of the |
Again this small code section markdown.treeprocessor.py:L432. I hope I'm not too annoying hitting these edge cases 😅
My element looks like this; it's a table where each line is wrapped in
<pre><code>
.which looks like this
The Prettifier turns this into something like this
which looks like this
Looking at Babelmark only pandoc and Python-Markdown appears to be stripping whitespace in
<pre><code>
, though fenced code isn't supported by default.Possible fixes:
pre[0].text.isspace()
isTrue
(would fix my case).The text was updated successfully, but these errors were encountered: