Skip to content
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

widont line break/newline behavior #38

Open
ryneeverett opened this issue Oct 2, 2014 · 1 comment · May be fixed by #39
Open

widont line break/newline behavior #38

ryneeverett opened this issue Oct 2, 2014 · 1 comment · May be fixed by #39

Comments

@ryneeverett
Copy link

If a string ends with a <br> and a single word, widont does nothing:

>>> widont('blah<br>blah')
'blah<br>blah'

This makes sense to me. But if a string ends with a <br>\n, widont replaces the newline with a &nbsp;:

>>> widont('blah<br>\nblah')
'blah<br>&nbsp;blah'

This doesn't seem right. While the first would render:

blah
blah

the second would render:

blah
 blah

@ryneeverett
Copy link
Author

>>> re.match(r'\s', '\n')
<_sre.SRE_Match object; span=(0, 1), match='\n'>
>>> re.match(r'\s', r'\n')
>>>

This result came as a surprise to me, but explains why widont has this behavior with newlines. But is this the desired behavior? That is, is the text passed in supposed to be escaped already?

I believe this would be the easiest way to get the correct behavior in the above example:

text = 'blah<br>\nblah'
text = text.encode('unicode-escape')  # b'blah<br>\\nblah'
text = text.decode('utf-8')  # 'blah<br>\\nblah'
text = widont(text)  # 'blah<br>\\nblah'
text = text.encode('utf-8')  # b'blah<br>\\nblah'
text = text.decode('unicode-escape')  # 'blah<br>\nblah'

It seems like it would be preferable for typogrify to deal with this, and I think it can be done without any encoding/decoding.

@ryneeverett ryneeverett linked a pull request Oct 25, 2014 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant