def rubify(editor: Editor): selected_text: str = editor.web.selectedText() if not selected_text: showInfo("""You must select some text first""", parent=editor.widget, title='Simple Mandarin Ruby') return try: editor.mw.app.setOverrideCursor(Qt.WaitCursor) global dict_has_loaded if not dict_has_loaded: from .data.custom_phrases_dict import phrases_dict pypinyin.load_phrases_dict(phrases_dict) dict_has_loaded = True pinyins = pypinyin.lazy_pinyin(selected_text, style=pypinyin.Style.TONE, errors=lambda chars: list(chars)) if len(pinyins) != len(selected_text): raise IndexError("""Generated pinyin list didn't match string length""") pairs = zip(selected_text, pinyins) output = pairs_to_html(pairs) # Uses document.execCommand('insertHTML', ...) in JS under the hood: editor.doPaste(output, internal=False, extended=True) finally: editor.mw.app.restoreOverrideCursor()
def insert_image_html(editor: Editor, image_filename: str): editor.doPaste(html=f'<img src="{image_filename}">', internal=True)