Skip to content

Commit

Permalink
[issue trentm#76] Ensure "smarty-pants" extra doesn't destroy image l…
Browse files Browse the repository at this point in the history
…inks and links with title text.
  • Loading branch information
trentm committed Mar 1, 2012
1 parent 8d1ec6c commit 18c280d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.txt
Expand Up @@ -2,6 +2,9 @@

## python-markdown2 1.1.0 (not yet released)

- [issue #76] Ensure "smarty-pants" extra doesn't destroy image links
and links with title text.

- [issue #72] Support reading from stdin for command line tool like any
well-behaved unix tool, e.g.:

Expand Down
10 changes: 9 additions & 1 deletion lib/markdown2.py
Expand Up @@ -1068,11 +1068,15 @@ def _do_links(self, text):
% (url.replace('"', '"'),
_xml_escape_attr(link_text),
title_str, self.empty_element_suffix)
if "smarty-pants" in self.extras:
result = result.replace('"', self._escape_table['"'])
curr_pos = start_idx + len(result)
text = text[:start_idx] + result + text[match.end():]
elif start_idx >= anchor_allowed_pos:
result_head = '<a href="%s"%s>' % (url, title_str)
result = '%s%s</a>' % (result_head, link_text)
if "smarty-pants" in self.extras:
result = result.replace('"', self._escape_table['"'])
# <img> allowed from curr_pos on, <a> from
# anchor_allowed_pos on.
curr_pos = start_idx + len(result_head)
Expand Down Expand Up @@ -1114,13 +1118,17 @@ def _do_links(self, text):
% (url.replace('"', '&quot;'),
link_text.replace('"', '&quot;'),
title_str, self.empty_element_suffix)
if "smarty-pants" in self.extras:
result = result.replace('"', self._escape_table['"'])
curr_pos = start_idx + len(result)
text = text[:start_idx] + result + text[match.end():]
elif start_idx >= anchor_allowed_pos:
result = '<a href="%s"%s>%s</a>' \
% (url, title_str, link_text)
result_head = '<a href="%s"%s>' % (url, title_str)
result = '%s%s</a>' % (result_head, link_text)
if "smarty-pants" in self.extras:
result = result.replace('"', self._escape_table['"'])
# <img> allowed from curr_pos on, <a> from
# anchor_allowed_pos on.
curr_pos = start_idx + len(result_head)
Expand Down Expand Up @@ -1681,7 +1689,7 @@ def _add_footnotes(self, text):
# http://bumppo.net/projects/amputator/
_ampersand_re = re.compile(r'&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)')
_naked_lt_re = re.compile(r'<(?![a-z/?\$!])', re.I)
_naked_gt_re = re.compile(r'''(?<![a-z?!/'"-])>''', re.I)
_naked_gt_re = re.compile(r'''(?<![a-z0-9?!/'"-])>''', re.I)

def _encode_amps_and_angles(self, text):
# Smart processing for ampersands and angle brackets that need
Expand Down
6 changes: 6 additions & 0 deletions test/tm-cases/smarty_pants_image_links.html
@@ -1 +1,7 @@
<p><img src="/path/to/image.jpg" alt="alt text" /></p>

<p>How about <img src="/path/to/image.jpg" alt="alt text" /> in other paragraph content?</p>

<p>How about <img src="/path/to/image.jpg" alt="" /> sans alt text?</p>

<p>How about a <a href="http://example.com" title="with a title">regular link</a> here?</p>
6 changes: 6 additions & 0 deletions test/tm-cases/smarty_pants_image_links.text
@@ -1 +1,7 @@
![alt text](/path/to/image.jpg)

How about ![alt text](/path/to/image.jpg) in other paragraph content?

How about ![](/path/to/image.jpg) sans alt text?

How about a [regular link](http://example.com "with a title") here?

0 comments on commit 18c280d

Please sign in to comment.