utility for percent-escaping images

This commit is contained in:
Damien Elmes 2011-02-02 18:36:58 +09:00
parent b426ad4271
commit 80bb59024c

View file

@ -8,11 +8,12 @@ Media support
""" """
__docformat__ = 'restructuredtext' __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.db import *
from anki.utils import checksum, genID from anki.utils import checksum, genID
from anki.lang import _ from anki.lang import _
# other code depends on this order, so don't reorder
regexps = ("(?i)(\[sound:([^]]+)\])", regexps = ("(?i)(\[sound:([^]]+)\])",
"(?i)(<img[^>]+src=[\"']?([^\"'>]+)[\"']?[^>]*>)") "(?i)(<img[^>]+src=[\"']?([^\"'>]+)[\"']?[^>]*>)")
@ -133,6 +134,13 @@ def stripMedia(txt):
txt = re.sub(reg, "", txt) txt = re.sub(reg, "", txt)
return 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 # Rebuilding DB
########################################################################## ##########################################################################