From eaf935c3ff5ac247b2f66b5cae2aa06b65b4c6f5 Mon Sep 17 00:00:00 2001 From: ANH Date: Fri, 24 Jul 2020 05:51:36 +0300 Subject: [PATCH 1/5] fix ankitects/help-wanted#13 --- qt/aqt/editor.py | 53 ++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/qt/aqt/editor.py b/qt/aqt/editor.py index e07060616..a265ca604 100644 --- a/qt/aqt/editor.py +++ b/qt/aqt/editor.py @@ -1091,34 +1091,35 @@ 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('(\S+)', line): + # inlined data in base64? + if token.startswith("data:image/"): + processed += 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 += link + else: + # not media; add it as a normal link + link = '{}'.format(token, html.escape(token)) + processed += 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(" ", " ") + " " + token = re.sub(" ( +)", repl, token) + processed += token - # 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 - - # not media; add it as a normal link if pasting with shift - link = '{}'.format(url, html.escape(txt)) - return link - - # normal text; convert it to HTML - txt = html.escape(txt) - txt = txt.replace("\n", "
").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(" ", " ") + " " - - txt = re.sub(" ( +)", repl, txt) - - return txt + processed += "
" + # return without last
+ return processed[:-4] def _processHtml(self, mime: QMimeData) -> Tuple[Optional[str], bool]: if not mime.hasHtml(): From ea99c7afac18c5e1e40ca74714dc2d10177a8ea0 Mon Sep 17 00:00:00 2001 From: ANH Date: Fri, 24 Jul 2020 05:53:40 +0300 Subject: [PATCH 2/5] add ANH25 to CONTRIBUTORS --- CONTRIBUTORS | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 2d3d27f4f..8a2f8b4a2 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -44,6 +44,7 @@ neitrinoweb Andreas Reis Alexander Presnyakov Matt Krump +abdo ******************** The text of the 3 clause BSD license follows: From e122534ba45fc33d16100d62749b59563c216521 Mon Sep 17 00:00:00 2001 From: ANH Date: Fri, 24 Jul 2020 08:12:46 +0300 Subject: [PATCH 3/5] avoid string concatenation --- qt/aqt/editor.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/qt/aqt/editor.py b/qt/aqt/editor.py index a265ca604..7ac79bfe5 100644 --- a/qt/aqt/editor.py +++ b/qt/aqt/editor.py @@ -1091,23 +1091,23 @@ class EditorWebView(AnkiWebView): return None txt = mime.text() - processed = '' + processed = [] lines = txt.split("\n") for line in lines: - for token in re.split('(\S+)', line): + for token in re.split("(\S+)", line): # inlined data in base64? if token.startswith("data:image/"): - processed += self.editor.inlinedImageToLink(token) + 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 += link + processed.append(link) else: # not media; add it as a normal link link = '{}'.format(token, html.escape(token)) - processed += link + processed.append(link) else: token = html.escape(token).replace("\t", " " * 4) # if there's more than one consecutive space, @@ -1115,11 +1115,12 @@ class EditorWebView(AnkiWebView): def repl(match): return match.group(1).replace(" ", " ") + " " token = re.sub(" ( +)", repl, token) - processed += token + processed.append(token) - processed += "
" - # return without last
- return processed[:-4] + processed.append("
") + # remove last
+ processed.pop() + return "".join(processed) def _processHtml(self, mime: QMimeData) -> Tuple[Optional[str], bool]: if not mime.hasHtml(): From da3761341ed89a27e678a580167e86399f70b35d Mon Sep 17 00:00:00 2001 From: ANH Date: Fri, 24 Jul 2020 09:00:34 +0300 Subject: [PATCH 4/5] formatting --- qt/aqt/editor.py | 1 + 1 file changed, 1 insertion(+) diff --git a/qt/aqt/editor.py b/qt/aqt/editor.py index 7ac79bfe5..d4c65d553 100644 --- a/qt/aqt/editor.py +++ b/qt/aqt/editor.py @@ -1114,6 +1114,7 @@ class EditorWebView(AnkiWebView): # use non-breaking spaces for the second one on def repl(match): return match.group(1).replace(" ", " ") + " " + token = re.sub(" ( +)", repl, token) processed.append(token) From fd0c3cf57ec5b4f1767346cd5e05c0614de3f6fd Mon Sep 17 00:00:00 2001 From: ANH Date: Fri, 24 Jul 2020 09:18:05 +0300 Subject: [PATCH 5/5] add missing string r flag --- qt/aqt/editor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qt/aqt/editor.py b/qt/aqt/editor.py index d4c65d553..545a65888 100644 --- a/qt/aqt/editor.py +++ b/qt/aqt/editor.py @@ -1095,7 +1095,7 @@ class EditorWebView(AnkiWebView): lines = txt.split("\n") for line in lines: - for token in re.split("(\S+)", line): + for token in re.split(r"(\S+)", line): # inlined data in base64? if token.startswith("data:image/"): processed.append(self.editor.inlinedImageToLink(token))