From 8f36841281a291c854d530dbc7daf9f40ff9b57c Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Fri, 17 May 2013 12:23:56 +0900 Subject: [PATCH] ensure we match on identical closing quote (#769) --- anki/media.py | 12 +++++++----- anki/upgrade.py | 7 ++++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/anki/media.py b/anki/media.py index 4e8e5a38d..26fdb056d 100644 --- a/anki/media.py +++ b/anki/media.py @@ -13,8 +13,9 @@ from anki.latex import mungeQA class MediaManager(object): # other code depends on this order, so don't reorder - regexps = ("(?i)(\[sound:([^]]+)\])", - "(?i)(]+src=[\"']?([^\"'>]+)[\"']?[^>]*>)") + regexps = ("(?i)(\[sound:(?P[^]]+)\])", + "(?i)(]+src=(?P[\"']?)"+ + "(?P[^>]+?)(?P=str)[^>]*>)") def __init__(self, col, server): self.col = col @@ -124,7 +125,8 @@ If the same name exists, compare checksums.""" string = mungeQA(string, None, None, model, None, self.col) # extract filenames for reg in self.regexps: - for (full, fname) in re.findall(reg, string): + for match in re.finditer(reg, string): + fname = match.group("fname") isLocal = not re.match("(https?|ftp)://", fname.lower()) if isLocal or includeRemote: l.append(fname) @@ -167,8 +169,8 @@ If the same name exists, compare checksums.""" if isWin: return string def repl(match): - tag = match.group(1) - fname = match.group(2) + tag = match.group(0) + fname = match.group("fname") if re.match("(https?|ftp)://", fname): return tag return tag.replace( diff --git a/anki/upgrade.py b/anki/upgrade.py index b400d92c1..4a13fc268 100644 --- a/anki/upgrade.py +++ b/anki/upgrade.py @@ -535,7 +535,8 @@ order by ordinal""", mid)): def _rewriteMediaRefs(self): col = self.col def rewriteRef(key): - all, fname = match + all = match.group(0) + fname = match.group("fname") if all in state['mflds']: # we've converted this field before new = state['mflds'][all] @@ -590,9 +591,9 @@ order by ordinal""", mid)): state = dict(mflds={}, fields=0) for t in m['tmpls']: for r in regexps: - for match in re.findall(r, t['qfmt']): + for match in re.finditer(r, t['qfmt']): rewriteRef('qfmt') - for match in re.findall(r, t['afmt']): + for match in re.finditer(r, t['afmt']): rewriteRef('afmt') if state['fields']: col.models.save(m)