From d0e334ca8567da06f11cac7a44696796a6dc06db Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 2 Oct 2024 22:56:04 +0200 Subject: squash some bugs --- anki-card-template/bulk-cleanup.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/anki-card-template/bulk-cleanup.py b/anki-card-template/bulk-cleanup.py index c712c51..f123cf6 100755 --- a/anki-card-template/bulk-cleanup.py +++ b/anki-card-template/bulk-cleanup.py @@ -3,6 +3,7 @@ import sys import os import re import io +import importlib from math import floor, log10 from time import sleep from pathlib import Path @@ -14,13 +15,6 @@ class TrashFileIO(object): def flush(self): pass trash_out = TrashFileIO() -# replace stderr file descriptor with /dev/null -os.dup2(os.open(os.devnull, os.O_WRONLY), 2) - -sys.stdout = trash_out -import aqt -sys.stdout = real_stdout - def recurseplainify(soup): output = "" for el in soup.children: @@ -32,7 +26,7 @@ def recurseplainify(soup): output += f"*{recurseplainify(el)}*" continue - if el.name == 'ruby': + if el.name == 'ruby' and len(el.text) > 0: output += f'[{el.text}]({el.rt.text})' continue @@ -41,10 +35,10 @@ def recurseplainify(soup): return output def html2cardtemplate(html): - soup = BeautifulSoup(html) + soup = BeautifulSoup(html, features="lxml") return recurseplainify(soup) -def main(): +def main(aqt): sys.stdout = trash_out app = aqt._run(sys.argv, False) sys.stdout = real_stdout @@ -105,5 +99,12 @@ def main(): sys.stdout = trash_out if __name__ == "__main__": - main() + # replace stderr file descriptor with /dev/null + os.dup2(os.open(os.devnull, os.O_WRONLY), 2) + + sys.stdout = trash_out + aqt = importlib.import_module("aqt") + sys.stdout = real_stdout + + main(aqt) -- cgit v1.2.3