Skip to content

Commit

Permalink
Preserve HTML formatting when generating readings
Browse files Browse the repository at this point in the history
  • Loading branch information
obynio committed May 19, 2021
1 parent f2fb21c commit eac2b34
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
24 changes: 17 additions & 7 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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)

Expand All @@ -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)
28 changes: 20 additions & 8 deletions reading.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,31 @@
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',
'--unk-format=%m[] ']

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("<br( /)?>", "---newline---", text)
text = stripHTML(text)
text = text.replace("---newline---", "<br>")
return text
text = re.sub("<br( /)?>", NEWLINE_REPLACER, text)
#showInfo(text)
matches, text = htmlReplace(text)
text = text.replace(NEWLINE_REPLACER, "<br>")
return matches, text

if sys.platform == "win32":
si = subprocess.STARTUPINFO()
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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>", "<br>")
for match in matches:
fin = fin.replace(HTML_REPLACER, match, 1)
return re.sub(r'& ?nbsp ?;', ' ', re.sub(r"< ?br ?>", "<br>", fin.strip()))

# Kakasi

Expand All @@ -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")
Expand Down

0 comments on commit eac2b34

Please sign in to comment.