From 80bb59024c2e300039d4d8989c63ac9c53d8fbfd Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Wed, 2 Feb 2011 18:36:58 +0900 Subject: [PATCH] utility for percent-escaping images --- anki/media.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/anki/media.py b/anki/media.py index 4cab24780..09c478309 100644 --- a/anki/media.py +++ b/anki/media.py @@ -8,11 +8,12 @@ Media support """ __docformat__ = 'restructuredtext' -import os, shutil, re, urllib2, time, tempfile, unicodedata +import os, shutil, re, urllib2, time, tempfile, unicodedata, urllib from anki.db import * from anki.utils import checksum, genID from anki.lang import _ +# other code depends on this order, so don't reorder regexps = ("(?i)(\[sound:([^]]+)\])", "(?i)(]+src=[\"']?([^\"'>]+)[\"']?[^>]*>)") @@ -133,6 +134,13 @@ def stripMedia(txt): txt = re.sub(reg, "", txt) return txt +def escapeImages(string): + def repl(match): + return match.group(1).replace( + match.group(2), + urllib.quote(match.group(2).encode("utf-8"))) + return re.sub(regexps[1], repl, string) + # Rebuilding DB ##########################################################################