Beispiel #1
0
def highlight_terms(webview: AnkiWebView, terms: List[str]):
    # FIXME: anki21 does not seem to support highlighting more than one
    # term at once. Likely a Qt bug / regression.
    # TODO: Perhaps choose to highlight the longest term on anki21.
    # TODO: Find a way to exclude UI text in editor pane from highlighting
    for term in terms:
        webview.findText(term)
class CardPreviewer(QDialog):
    """Custom card previewer dialog"""

    title = "Card {}: '{}...'"

    def __init__(self, cid, highlight):
        super(CardPreviewer, self).__init__(parent=mw.app.activeWindow())
        self.mw = mw
        self.cid = cid
        self.highlight = highlight
        self.form = previewer.Ui_Dialog()
        self.form.setupUi(self)
        self.setupEvents()
        self.setupUi()
        ret = self.setCard(self.cid)
        self.setHighlight(self.highlight)
        restoreGeom(self, "irpreviewer")
        if ret is not False:
            self.show()
        else:
            self.close()

    #  UI

    def setupUi(self):
        self.web = AnkiWebView()
        self.web.setLinkHandler(linkHandler)
        self.form.verticalLayout.insertWidget(0, self.web)


    def setupEvents(self):
        self.form.btnBrowse.clicked.connect(self.onBrowse)
        self.form.btnBacklinks.clicked.connect(self.onBacklinks)


    def setCard(self, cid):
        """
        Set title and webview HTML
        """
        try:
            card = self.mw.col.getCard(cid)
        except TypeError:
            tooltip("Could not find linked card with cid:'{}'.".format(cid))
            return False

        # Set previewer title based on note contents
        note = card.note()
        fields = note.fields
        model = note.model()
        fnames = mw.col.models.fieldNames(model)
        idx = 0
        if "Note ID" in note:
            nid_idx = fnames.index("Note ID")
            if nid_idx == idx:
                idx = min(idx+1, len(fields))
        field1 = stripHTML(fields[idx])
        title = self.title.format(cid, field1[:50])
        self.setWindowTitle(title)

        # Set card HTML
        html = card.a()
        html = runFilter("previewerMungeQA", html)

        ti = lambda x: x
        base = getBase(self.mw.col)
        css = self.mw.reviewer._styles()
        if preview_jsbooster:
            # JS Booster available
            baseUrlText = getBaseUrlText(self.mw.col) + "__previewer__.html"
            stdHtmlWithBaseUrl(self.web,
                ti(mungeQA(self.mw.col, html)), baseUrlText, css,
                bodyClass="card card%d" % (card.ord+1), 
                head=base, js=browserSel)
        else:
            # fall back to default
            self.web.stdHtml(
                ti(mungeQA(self.mw.col, html)), css, 
                bodyClass="card card%d" % (card.ord+1), 
                head=base, js=browserSel)

        # Handle audio
        clearAudioQueue()
        if self.mw.reviewer.autoplay(card):
            playFromText(html)


    def setHighlight(self, highlight):
        self.web.findText(highlight, QWebPage.HighlightAllOccurrences)
        self.web.findText(highlight)


    def onBrowse(self):
        search = "cid:{}".format(self.cid)
        openBrowseLink(search, self.highlight)


    def onBacklinks(self):
        openBrowseLink(self.cid, self.highlight)


    def closeEvent(self, event):
        if self.mw.pm.profile is not None:
            saveGeom(self, "irpreviewer")
        event.accept()
Beispiel #3
0
def clear_highlights(webview: AnkiWebView):
    webview.findText("")