From 918694a0964656840d3bfb771d74544b61199631 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Wed, 22 May 2013 09:45:58 +0900 Subject: [PATCH] more media regexp fixes - split quoted and unquoted image case into separate regexps, since we can't include a group reference inside a character set - disallow spaces in the non-quoted case - this should fix matching on images with other attributes again --- anki/media.py | 16 +++++++++++----- tests/test_media.py | 6 +++++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/anki/media.py b/anki/media.py index d7e51cbe4..5ee7bc260 100644 --- a/anki/media.py +++ b/anki/media.py @@ -12,10 +12,14 @@ from anki.latex import mungeQA class MediaManager(object): - # other code depends on this order, so don't reorder - regexps = ("(?i)(\[sound:(?P[^]]+)\])", - "(?i)(]+src=(?P[\"']?)"+ - "(?P[^>]+)(?P=str)[^>]*>)") + soundRegexps = ["(?i)(\[sound:(?P[^]]+)\])"] + imgRegexps = [ + # src element quoted case + "(?i)(]+src=(?P[\"'])(?P[^>]+?)(?P=str)[^>]*>)", + # unquoted case + "(?i)(]+src=(?!['\"])(?P[^ >]+)[^>]*?>)", + ] + regexps = soundRegexps + imgRegexps def __init__(self, col, server): self.col = col @@ -175,7 +179,9 @@ If the same name exists, compare checksums.""" return tag return tag.replace( fname, urllib.quote(fname.encode("utf-8"))) - return re.sub(self.regexps[1], repl, string) + for reg in self.imgRegexps: + string = re.sub(reg, repl, string) + return string # Rebuilding DB ########################################################################## diff --git a/tests/test_media.py b/tests/test_media.py index 308ddd297..49c349be9 100644 --- a/tests/test_media.py +++ b/tests/test_media.py @@ -23,7 +23,11 @@ def test_strings(): mid = d.models.models.keys()[0] assert mf(mid, "aoeu") == [] assert mf(mid, "aoeuao") == ["foo.jpg"] - assert mf(mid, "aoeuao") == ["foo bar.jpg"] + assert mf(mid, "aoeuao") == ["foo.jpg"] + assert mf(mid, "aoeuao") == [ + "foo.jpg", "bar.jpg"] + assert mf(mid, "aoeuao") == ["foo.jpg"] + assert mf(mid, "") == ["one", "two"] assert mf(mid, "aoeuao") == ["foo.jpg"] assert mf(mid, "aoeuao") == [ "foo.jpg", "fo"]