Merge pull request #703 from ANH25/master

fix help-wanted#13
This commit is contained in:
Damien Elmes 2020-07-29 14:14:57 +10:00 committed by GitHub
commit 93ada5d374
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 25 deletions

View file

@ -44,6 +44,7 @@ neitrinoweb <github.com/neitrinoweb/>
Andreas Reis <github.com/rathsky>
Matt Krump <github.com/mkrump>
Alexander Presnyakov <flagist0@gmail.com>
abdo <github.com/ANH25>
********************
The text of the 3 clause BSD license follows:

View file

@ -1093,34 +1093,37 @@ class EditorWebView(AnkiWebView):
return None
txt = mime.text()
processed = []
lines = txt.split("\n")
# inlined data in base64?
if txt.startswith("data:image/"):
return self.editor.inlinedImageToLink(txt)
for line in lines:
for token in re.split(r"(\S+)", line):
# inlined data in base64?
if token.startswith("data:image/"):
processed.append(self.editor.inlinedImageToLink(token))
elif self.editor.isURL(token):
# if the user is pasting an image or sound link, convert it to local
link = self.editor.urlToLink(token)
if link:
processed.append(link)
else:
# not media; add it as a normal link
link = '<a href="{}">{}</a>'.format(token, html.escape(token))
processed.append(link)
else:
token = html.escape(token).replace("\t", " " * 4)
# if there's more than one consecutive space,
# use non-breaking spaces for the second one on
def repl(match):
return match.group(1).replace(" ", "&nbsp;") + " "
# if the user is pasting an image or sound link, convert it to local
if self.editor.isURL(txt):
url = txt.split("\r\n")[0]
link = self.editor.urlToLink(url)
if link:
return link
token = re.sub(" ( +)", repl, token)
processed.append(token)
# not media; add it as a normal link if pasting with shift
link = '<a href="{}">{}</a>'.format(url, html.escape(txt))
return link
# normal text; convert it to HTML
txt = html.escape(txt)
txt = txt.replace("\n", "<br>").replace("\t", " " * 4)
# if there's more than one consecutive space,
# use non-breaking spaces for the second one on
def repl(match):
return match.group(1).replace(" ", "&nbsp;") + " "
txt = re.sub(" ( +)", repl, txt)
return txt
processed.append("<br>")
# remove last <br>
processed.pop()
return "".join(processed)
def _processHtml(self, mime: QMimeData) -> Tuple[Optional[str], bool]:
if not mime.hasHtml():