From 18c280dea25bf82ccf8859026f9261a406184f9a Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Wed, 29 Feb 2012 23:43:05 -0800 Subject: [PATCH] [issue #76] Ensure "smarty-pants" extra doesn't destroy image links and links with title text. --- CHANGES.txt | 3 +++ lib/markdown2.py | 10 +++++++++- test/tm-cases/smarty_pants_image_links.html | 6 ++++++ test/tm-cases/smarty_pants_image_links.text | 6 ++++++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CHANGES.txt b/CHANGES.txt index f5a42e9a..387ebb3f 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -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.: diff --git a/lib/markdown2.py b/lib/markdown2.py index b8a76a40..45022659 100755 --- a/lib/markdown2.py +++ b/lib/markdown2.py @@ -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 = '' % (url, title_str) result = '%s%s' % (result_head, link_text) + if "smarty-pants" in self.extras: + result = result.replace('"', self._escape_table['"']) # allowed from curr_pos on, from # anchor_allowed_pos on. curr_pos = start_idx + len(result_head) @@ -1114,6 +1118,8 @@ def _do_links(self, text): % (url.replace('"', '"'), link_text.replace('"', '"'), 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: @@ -1121,6 +1127,8 @@ def _do_links(self, text): % (url, title_str, link_text) result_head = '' % (url, title_str) result = '%s%s' % (result_head, link_text) + if "smarty-pants" in self.extras: + result = result.replace('"', self._escape_table['"']) # allowed from curr_pos on, from # anchor_allowed_pos on. curr_pos = start_idx + len(result_head) @@ -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'''(?''', re.I) + _naked_gt_re = re.compile(r'''(?''', re.I) def _encode_amps_and_angles(self, text): # Smart processing for ampersands and angle brackets that need diff --git a/test/tm-cases/smarty_pants_image_links.html b/test/tm-cases/smarty_pants_image_links.html index 1b8bca67..d2bdd0f9 100644 --- a/test/tm-cases/smarty_pants_image_links.html +++ b/test/tm-cases/smarty_pants_image_links.html @@ -1 +1,7 @@

alt text

+ +

How about alt text in other paragraph content?

+ +

How about sans alt text?

+ +

How about a regular link here?

diff --git a/test/tm-cases/smarty_pants_image_links.text b/test/tm-cases/smarty_pants_image_links.text index 8d86b645..04cb6c6f 100644 --- a/test/tm-cases/smarty_pants_image_links.text +++ b/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?