From 20609401d231b31d817837b8dc61ddbff159138c Mon Sep 17 00:00:00 2001 From: Tobias Manske Date: Thu, 9 Sep 2021 17:58:14 +0200 Subject: [PATCH] init --- .gitignore | 166 ++++++++++++++++++++++++++++++++++++++++++++++++++++ __init__.py | 50 ++++++++++++++++ 2 files changed, 216 insertions(+) create mode 100644 .gitignore create mode 100644 __init__.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..638ec3c --- /dev/null +++ b/.gitignore @@ -0,0 +1,166 @@ + +# Created by https://www.toptal.com/developers/gitignore/api/vim,python +# Edit at https://www.toptal.com/developers/gitignore?templates=vim,python + +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +### Vim ### +# Swap +[._]*.s[a-v][a-z] +!*.svg # comment out if you don't need vector files +[._]*.sw[a-p] +[._]s[a-rt-v][a-z] +[._]ss[a-gi-z] +[._]sw[a-p] + +# Session +Session.vim +Sessionx.vim + +# Temporary +.netrwhist +*~ +# Auto-generated tag files +tags +# Persistent undo +[._]*.un~ + +# End of https://www.toptal.com/developers/gitignore/api/vim,python diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..2c1afd1 --- /dev/null +++ b/__init__.py @@ -0,0 +1,50 @@ +from aqt.qt import * +import json +from anki import hooks +from aqt import editor +from aqt.webview import AnkiWebView +from aqt.utils import showInfo +from aqt.theme import theme_manager +from aqt import addcards +from aqt import gui_hooks +from aqt import mw + +class Editor_Preview(object): + + def addcards_hook(self, ac: addcards): + layout = ac.form.fieldsArea.layout() + ac.web_view = AnkiWebView(title="preview") + layout.addWidget(ac.web_view) + ac.web_view.stdHtml( + ac.mw.reviewer.revHtml(), + css=["css/reviewer.css"], + js=[ + "js/mathjax.js", + "js/vendor/mathjax/tex-chtml.js", + "js/reviewer.js", + ], + context=ac, + ) + note = ac.editor.note + if ac.editor.card: + ord = ac.editor.card.ord + else: + ord = 0 + c = note.ephemeral_card() + a = ac.mw.prepare_card_text_for_display(c.answer()) + a = gui_hooks.card_will_show(a, c, "clayoutAnswer") + bodyclass = theme_manager.body_classes_for_card_ord(c.ord, ac.mw.pm.night_mode()) + ac.web_view.eval(f"_showAnswer({json.dumps(a)},'{bodyclass}');") + self.web_view = ac.web_view + + def onedit_hook(self, note): + c = note.ephemeral_card() + a = mw.prepare_card_text_for_display(c.answer()) + a = gui_hooks.card_will_show(a, c, "clayoutAnswer") + bodyclass = theme_manager.body_classes_for_card_ord(c.ord, mw.pm.night_mode()) + self.web_view.eval(f"_showAnswer({json.dumps(a)},'{bodyclass}');") + + +eprev = Editor_Preview() +gui_hooks.add_cards_did_init.append(eprev.addcards_hook) +gui_hooks.editor_did_fire_typing_timer.append(eprev.onedit_hook)