mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 06:22:22 -04:00
Merge pull request #727 from hgiesel/mungehtml
Add editor_will_munge_html hook
This commit is contained in:
commit
fbdd72817e
3 changed files with 42 additions and 3 deletions
|
@ -420,9 +420,7 @@ class Editor:
|
||||||
print("uncaught cmd", cmd)
|
print("uncaught cmd", cmd)
|
||||||
|
|
||||||
def mungeHTML(self, txt):
|
def mungeHTML(self, txt):
|
||||||
if txt in ("<br>", "<div><br></div>"):
|
return gui_hooks.editor_will_munge_html(txt, self)
|
||||||
return ""
|
|
||||||
return txt
|
|
||||||
|
|
||||||
# Setting/unsetting the current note
|
# Setting/unsetting the current note
|
||||||
######################################################################
|
######################################################################
|
||||||
|
@ -1201,4 +1199,9 @@ def fontMungeHack(font):
|
||||||
return re.sub(" L$", " Light", font)
|
return re.sub(" L$", " Light", font)
|
||||||
|
|
||||||
|
|
||||||
|
def munge_html(txt, editor):
|
||||||
|
return "" if txt in ("<br>", "<div><br></div>") else txt
|
||||||
|
|
||||||
|
|
||||||
gui_hooks.editor_will_use_font_for_field.append(fontMungeHack)
|
gui_hooks.editor_will_use_font_for_field.append(fontMungeHack)
|
||||||
|
gui_hooks.editor_will_munge_html.append(munge_html)
|
||||||
|
|
|
@ -1564,6 +1564,36 @@ class _EditorWillLoadNoteFilter:
|
||||||
editor_will_load_note = _EditorWillLoadNoteFilter()
|
editor_will_load_note = _EditorWillLoadNoteFilter()
|
||||||
|
|
||||||
|
|
||||||
|
class _EditorWillMungeHtmlFilter:
|
||||||
|
"""Allows manipulating the text that will be saved by the editor"""
|
||||||
|
|
||||||
|
_hooks: List[Callable[[str, "aqt.editor.Editor"], str]] = []
|
||||||
|
|
||||||
|
def append(self, cb: Callable[[str, "aqt.editor.Editor"], str]) -> None:
|
||||||
|
"""(txt: str, editor: aqt.editor.Editor)"""
|
||||||
|
self._hooks.append(cb)
|
||||||
|
|
||||||
|
def remove(self, cb: Callable[[str, "aqt.editor.Editor"], str]) -> None:
|
||||||
|
if cb in self._hooks:
|
||||||
|
self._hooks.remove(cb)
|
||||||
|
|
||||||
|
def count(self) -> int:
|
||||||
|
return len(self._hooks)
|
||||||
|
|
||||||
|
def __call__(self, txt: str, editor: aqt.editor.Editor) -> str:
|
||||||
|
for filter in self._hooks:
|
||||||
|
try:
|
||||||
|
txt = filter(txt, editor)
|
||||||
|
except:
|
||||||
|
# if the hook fails, remove it
|
||||||
|
self._hooks.remove(filter)
|
||||||
|
raise
|
||||||
|
return txt
|
||||||
|
|
||||||
|
|
||||||
|
editor_will_munge_html = _EditorWillMungeHtmlFilter()
|
||||||
|
|
||||||
|
|
||||||
class _EditorWillShowContextMenuHook:
|
class _EditorWillShowContextMenuHook:
|
||||||
_hooks: List[Callable[["aqt.editor.EditorWebView", QMenu], None]] = []
|
_hooks: List[Callable[["aqt.editor.EditorWebView", QMenu], None]] = []
|
||||||
|
|
||||||
|
|
|
@ -568,6 +568,12 @@ hooks = [
|
||||||
args=["note: anki.notes.Note"],
|
args=["note: anki.notes.Note"],
|
||||||
legacy_hook="tagsUpdated",
|
legacy_hook="tagsUpdated",
|
||||||
),
|
),
|
||||||
|
Hook(
|
||||||
|
name="editor_will_munge_html",
|
||||||
|
args=["txt: str", "editor: aqt.editor.Editor"],
|
||||||
|
return_type="str",
|
||||||
|
doc="""Allows manipulating the text that will be saved by the editor""",
|
||||||
|
),
|
||||||
Hook(
|
Hook(
|
||||||
name="editor_will_use_font_for_field",
|
name="editor_will_use_font_for_field",
|
||||||
args=["font: str"],
|
args=["font: str"],
|
||||||
|
|
Loading…
Reference in a new issue