Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions typogrify/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def process_ignores(text, ignore_tags=None):
can also be filtered on id and class using CSS notation.
For example, div#test (div with id='test'), div.test
(div with class='test'), #test (any tag with id='test')
or .test (any tag with class='test').
or .test (any tag with class='test').

Returns in the following format:

[
Expand Down Expand Up @@ -54,9 +54,9 @@ def _filter_tag(match):
{attribute} \s*=\s*
(['"]) {attribute_value} \{0}
))""".format(_filter_tag.group, **locals())

return result

_filter_tag.group = 1
position = 0
sections = []
Expand Down Expand Up @@ -281,7 +281,7 @@ def smartypants(text):


def widont(text):
"""Replaces the space between the last two words in a string with `` ``
r"""Replaces the space between the last two words in a string with `` ``
Works in these block tags ``(h1-h6, p, li, dd, dt)`` and also accounts for
potential closing inline elements ``a, em, strong, span, b, i``

Expand Down Expand Up @@ -319,11 +319,15 @@ def widont(text):

>>> widont('<div><p>But divs with paragraphs do!</p></div>')
'<div><p>But divs with paragraphs&nbsp;do!</p></div>'

Newlines shouldn't count as "the space to replace".
>>> widont('blah<br>\nblah')
'blah<br>\nblah'
"""

widont_finder = re.compile(r"""((?:</?(?:a|em|span|strong|i|b)[^>]*>)|[^<>\s]) # must be proceeded by an approved inline opening or closing tag or a nontag/nonspace
\s+ # the space to replace
([^<>\s]+ # must be flollowed by non-tag non-space characters
[^\S\n] # the space to replace
([^<>\s]+ # must be followed by non-tag non-space characters
\s* # optional white space!
(</(a|em|span|strong|i|b)>\s*)* # optional closing inline tags with optional white space after each
((</(p|h[1-6]|li|dt|dd)>)|$)) # end with a closing p, h1-6, li or the end of the string
Expand Down