diff --git a/__init__.py b/__init__.py
index 7d66658..cd2189b 100644
--- a/__init__.py
+++ b/__init__.py
@@ -23,6 +23,7 @@
from aqt.utils import showInfo, tooltip
from aqt.qt import *
+from anki.buildinfo import version
from anki.hooks import addHook
from . import reading
@@ -44,7 +45,6 @@ def generateFurigana(editor, s):
html = s.selected
html = re.sub('\[[^\]]*\]', '', html)
html = mecab.reading(html)
-
if html == s.selected:
tooltip(_("Nothing to generate!"))
else:
@@ -71,14 +71,21 @@ class Selection:
def __init__(self, window, callback):
self.window = window
- window.web.page().runJavaScript(self.js_get_html, lambda x: self.setHtml(x, callback))
+ self.setHtml(None, callback)
+
+ def isDeprecated(self):
+ return int(version.replace('.', '')) < 2141
def setHtml(self, elements, callback, allowEmpty=False):
- self.selected = self.window.web.selectedText()
- if self.window.web.selectedText() == '':
- self.window.web.eval("setFormat('selectAll');")
- self.window.web.page().runJavaScript(self.js_get_html, lambda x: self.setHtml(x, callback, True))
+ self.selected = elements
+ if self.selected == None:
+ if self.isDeprecated():
+ self.window.web.eval("setFormat('selectAll');")
+ self.window.web.page().runJavaScript(self.js_get_html, lambda x: self.setHtml(x, callback, True))
+ else:
+ self.window.web.page().runJavaScript("getCurrentField().fieldHTML", lambda x: self.setHtml(x, callback, True))
return
+
self.selected = self.convertMalformedSpaces(self.selected)
callback(self)
@@ -87,6 +94,9 @@ def convertMalformedSpaces(self, text):
def modify(self, html):
html = self.convertMalformedSpaces(html)
- self.window.web.eval("setFormat('insertHTML', %s);" % json.dumps(html))
+ if self.isDeprecated():
+ self.window.web.eval("setFormat('insertHTML', %s);" % json.dumps(html))
+ else:
+ self.window.web.page().runJavaScript("getCurrentField().fieldHTML = %s;" % json.dumps(html))
addHook("setupEditorButtons", addButtons)
diff --git a/reading.py b/reading.py
index b378f0c..a73dda0 100644
--- a/reading.py
+++ b/reading.py
@@ -13,7 +13,7 @@
import os
import re
import subprocess
-from anki.utils import stripHTML, isWin, isMac
+from anki.utils import isWin, isMac
kakasiArgs = ["-isjis", "-osjis", "-u", "-JH", "-KH"]
mecabArgs = ['--node-format=%m[%f[7]] ', '--eos-format=\n',
@@ -21,13 +21,23 @@
mecabDir = os.path.join(os.path.dirname(__file__), "support")
+HTML_REPLACER = '▦'
+NEWLINE_REPLACER = '▧'
+
+def htmlReplace(text):
+ pattern = r"(?:<[^<]+?>)"
+ matches = re.findall(pattern, text)
+ text = re.sub(r"<[^<]+?>", HTML_REPLACER, text)
+ return matches, text
+
def escapeText(text):
text = text.replace("\n", " ")
text = text.replace(u'\uff5e', "~")
- text = re.sub("
", "---newline---", text)
- text = stripHTML(text)
- text = text.replace("---newline---", "
")
- return text
+ text = re.sub("
", NEWLINE_REPLACER, text)
+ #showInfo(text)
+ matches, text = htmlReplace(text)
+ text = text.replace(NEWLINE_REPLACER, "
")
+ return matches, text
if sys.platform == "win32":
si = subprocess.STARTUPINFO()
@@ -72,7 +82,7 @@ def ensureOpen(self):
def reading(self, expr):
self.ensureOpen()
- expr = escapeText(expr)
+ matches, expr = escapeText(expr)
self.mecab.stdin.write(expr.encode("utf-8", "ignore") + b'\n')
self.mecab.stdin.flush()
expr = self.mecab.stdout.readline().rstrip(b'\r\n').decode('utf-8', "ignore")
@@ -126,7 +136,9 @@ def reading(self, expr):
reading[:placeL], kanji[placeL:-placeR],
reading[placeL:-placeR], reading[-placeR:]))
fin = ''.join(out)
- return fin.strip().replace("< br>", "
")
+ for match in matches:
+ fin = fin.replace(HTML_REPLACER, match, 1)
+ return re.sub(r'& ?nbsp ?;', ' ', re.sub(r"< ?br ?>", "
", fin.strip()))
# Kakasi
@@ -152,7 +164,7 @@ def ensureOpen(self):
def reading(self, expr):
self.ensureOpen()
- expr = escapeText(expr)
+ _, expr = escapeText(expr)
self.kakasi.stdin.write(expr.encode("sjis", "ignore") + b'\n')
self.kakasi.stdin.flush()
res = self.kakasi.stdout.readline().rstrip(b'\r\n').decode("sjis")