diff --git a/anki/media.py b/anki/media.py index af690f2b7..09676da9f 100644 --- a/anki/media.py +++ b/anki/media.py @@ -108,17 +108,43 @@ If the same name exists, compare checksums.""" def filesInStr(self, mid, string, includeRemote=False): l = [] - # convert latex first model = self.col.models.get(mid) - string = mungeQA(string, None, None, model, None, self.col) - # extract filenames - for reg in self.regexps: - for (full, fname) in re.findall(reg, string): - isLocal = not re.match("(https?|ftp)://", fname.lower()) - if isLocal or includeRemote: - l.append(fname) + strings = [] + if model['type'] == MODEL_CLOZE and "{{c" in string: + # if the field has clozes in it, we'll need to expand the + # possibilities so we can render latex + strings = self._expandClozes(string) + else: + strings = [string] + for string in strings: + # handle latex + string = mungeQA(string, None, None, model, None, self.col) + # extract filenames + for reg in self.regexps: + for (full, fname) in re.findall(reg, string): + isLocal = not re.match("(https?|ftp)://", fname.lower()) + if isLocal or includeRemote: + l.append(fname) return l + def _expandClozes(self, string): + ords = set(re.findall("{{c(\d+)::.+?}}", string)) + strings = [] + from anki.template.template import clozeReg + def qrepl(m): + if m.group(3): + return "[%s]" % m.group(3) + else: + return "[...]" + def arepl(m): + return m.group(1) + for ord in ords: + s = re.sub(clozeReg%ord, qrepl, string) + s = re.sub(clozeReg%".+?", "\\1", s) + strings.append(s) + strings.append(re.sub(clozeReg%".+?", arepl, string)) + return strings + def transformNames(self, txt, func): for reg in self.regexps: txt = re.sub(reg, func, txt)