diff --git a/src/octoprint/plugins/announcements/__init__.py b/src/octoprint/plugins/announcements/__init__.py index 16b1db10..42ddcee0 100644 --- a/src/octoprint/plugins/announcements/__init__.py +++ b/src/octoprint/plugins/announcements/__init__.py @@ -374,9 +374,24 @@ class AnnouncementPlugin(octoprint.plugin.AssetPlugin, _image_tag_re = re.compile(r'') def _strip_images(text): + """ + >>> _strip_images(u"I'm a link and this is an image: foo") + u"I'm a link and this is an image: " + >>> _strip_images(u"One and two and three and four \\"four\\"") + u'One and two and three and four ' + >>> _strip_images(u"No images here") + u'No images here' + """ return _image_tag_re.sub('', text) def _replace_images(text, callback): + """ + >>> callback = lambda img: "foobar" + >>> _replace_images(u"I'm a link and this is an image: foo", callback) + u"I'm a link and this is an image: foobar" + >>> _replace_images(u"One and two and three and four \\"four\\"", callback) + u'One foobar and two foobar and three foobar and four foobar' + """ result = text for match in _image_tag_re.finditer(text): tag = match.group(0) @@ -384,16 +399,29 @@ def _replace_images(text, callback): result = result.replace(tag, replaced) return result -_image_src_re = re.compile(r'src=\"(.*?)\"') +_image_src_re = re.compile(r'src=(?P[\'"]*)(?P.*?)(?P=quote)(?=\s+|>)') def _lazy_images(text, placeholder=None): + """ + >>> _lazy_images(u"I'm a link and this is an image: foo") + u'I\\'m a link and this is an image: \\'foo\\'' + >>> _lazy_images(u"I'm a link and this is an image: foo", placeholder="ph.png") + u'I\\'m a link and this is an image: \\'foo\\'' + >>> _lazy_images(u"One and two and three and four \\"four\\"", placeholder="ph.png") + u'One and two and three and four four' + >>> _lazy_images(u"No images here") + u'No images here' + """ if placeholder is None: - placeholder = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" # 1px transparent gif + # 1px transparent gif + placeholder = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" def callback(img_tag): match = _image_src_re.search(img_tag) if match is not None: - src = match.group(1) - img_tag = img_tag.replace(match.group(0), 'src="{}" data-src="{}"'.format(placeholder, src)) + src = match.group("src") + quote = match.group("quote") + quoted_src = quote + src + quote + img_tag = img_tag.replace(match.group(0), 'src="{}" data-src={}'.format(placeholder, quoted_src)) return img_tag return _replace_images(text, callback)