mirror of
https://github.com/ankitects/anki.git
synced 2025-09-25 01:06:35 -04:00
when img/audio link pasted, convert to local
This commit is contained in:
parent
2798d4bf75
commit
21f5132c0b
1 changed files with 17 additions and 11 deletions
|
@ -1007,26 +1007,30 @@ class EditorWebView(AnkiWebView):
|
||||||
|
|
||||||
def _processUrls(self, mime):
|
def _processUrls(self, mime):
|
||||||
url = mime.urls()[0].toString()
|
url = mime.urls()[0].toString()
|
||||||
link = None
|
link = self._localizedMediaLink(url)
|
||||||
for suffix in pics+audio:
|
|
||||||
if url.lower().endswith(suffix):
|
|
||||||
link = self._retrieveURL(url)
|
|
||||||
break
|
|
||||||
if not link:
|
|
||||||
# not a supported media type; include link verbatim
|
|
||||||
link = url
|
|
||||||
mime = QMimeData()
|
mime = QMimeData()
|
||||||
mime.setHtml(link)
|
mime.setHtml(link)
|
||||||
return mime
|
return mime
|
||||||
|
|
||||||
|
def _localizedMediaLink(self, url):
|
||||||
|
l = url.lower()
|
||||||
|
for suffix in pics+audio:
|
||||||
|
if l.endswith(suffix):
|
||||||
|
return self._retrieveURL(url)
|
||||||
|
# not a supported type; return link verbatim
|
||||||
|
return url
|
||||||
|
|
||||||
def _processText(self, mime):
|
def _processText(self, mime):
|
||||||
txt = unicode(mime.text())
|
txt = unicode(mime.text())
|
||||||
l = txt.lower()
|
l = txt.lower()
|
||||||
html = None
|
html = None
|
||||||
# firefox on linux just gives us a url for an image
|
# if the user is pasting an image or sound link, convert it to local
|
||||||
if "\n" in l and (l.startswith("http://") or l.startswith("file://")):
|
if l.startswith("http://") or l.startswith("file://"):
|
||||||
txt = txt.split("\r\n")[0]
|
txt = txt.split("\r\n")[0]
|
||||||
html = self._retrieveURL(txt)
|
html = self._localizedMediaLink(txt)
|
||||||
|
if html == txt:
|
||||||
|
# wasn't of a supported media type; don't change
|
||||||
|
html = None
|
||||||
new = QMimeData()
|
new = QMimeData()
|
||||||
if html:
|
if html:
|
||||||
new.setHtml(html)
|
new.setHtml(html)
|
||||||
|
@ -1067,6 +1071,7 @@ class EditorWebView(AnkiWebView):
|
||||||
if ext not in pics and ext not in audio:
|
if ext not in pics and ext not in audio:
|
||||||
return
|
return
|
||||||
# fetch it into a temporary folder
|
# fetch it into a temporary folder
|
||||||
|
self.editor.mw.progress.start(immediate=True)
|
||||||
try:
|
try:
|
||||||
req = urllib2.Request(url, None, {
|
req = urllib2.Request(url, None, {
|
||||||
'User-Agent': 'Mozilla/5.0 (compatible; Anki)'})
|
'User-Agent': 'Mozilla/5.0 (compatible; Anki)'})
|
||||||
|
@ -1078,6 +1083,7 @@ class EditorWebView(AnkiWebView):
|
||||||
file = open(path, "wb")
|
file = open(path, "wb")
|
||||||
file.write(filecontents)
|
file.write(filecontents)
|
||||||
file.close()
|
file.close()
|
||||||
|
self.editor.mw.progress.finish()
|
||||||
return self.editor._addMedia(path)
|
return self.editor._addMedia(path)
|
||||||
|
|
||||||
def _flagAnkiText(self):
|
def _flagAnkiText(self):
|
||||||
|
|
Loading…
Reference in a new issue