Skip to content

Instantly share code, notes, and snippets.

@Terrance
Created November 18, 2015 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Terrance/53b5887538998ea42f26 to your computer and use it in GitHub Desktop.
Save Terrance/53b5887538998ea42f26 to your computer and use it in GitHub Desktop.
An Anki plugin to preview cards in the browser. Old and probably broken; uploaded here for completeness.
# Card Viewer 1.3: a small plugin to show cards without reviewing.
# This plugin will export cards to HTML, and open them in your browser.
# Select a card, then go to Edit > View Card, select View from the toolbar, or press Ctrl+Return.
from aqt.browser import BrowserToolbar
from aqt import mw
from aqt.qt import *
from aqt.utils import shortcut, showInfo
from anki.find import fieldNames
from anki.hooks import addHook, wrap
import os, platform, re
if platform.system() != "Windows":
import webbrowser
def writeCard(id, title, content):
# files saved by default to current profile's media folder, change this to your liking
# should hopefully work cross platform (not sure where Mac version stores Anki folder)
docs = ""
if platform.system() == "Windows":
docs = "Documents\\"
# Anki folder inside Documents
# XP and below's documents stored in My Documents
winVer = int(platform.win32_ver()[1].split(".")[0])
if winVer < 6:
docs = "My " + docs
path = os.path.join(os.path.expanduser("~"), docs + "Anki", mw.pm.name, "collection.media", str(id) + ".html")
# write file
file = open(path, "w")
file.write(content.encode("utf8"))
file.close()
# open with default handler
if platform.system() == "Windows":
os.startfile(path)
else:
webbrowser.open(path)
# write card to file, open in browser
def doView(self):
sf = self.selectedNotes()
# no cards selected
if not sf:
showInfo("You need to select a card first.")
return
# for each selected card
for id in sf:
# fetch card
note = mw.col.getNote(id)
card = note.cards()[0]
# read card
title = note.fields[0]
content = card.a()
# generate HTML around content
content = content.split("</style>")
content[0] = re.sub("\.cloze.*?\{.*?\}", "", content[0], flags=re.DOTALL)
content = """<html>
<head>
<title>""" + title + """</title>
""" + content[0] + """</style>
</head>
<body>
""" + content[1] + """
</body>
</html>"""
writeCard(id, title, content)
return
# add menu option
def setupMenu(editor):
editor.form.menuEdit.addSeparator()
a = QAction("View Card", editor)
a.setShortcut("Ctrl+Return")
editor.form.menuEdit.addAction(a)
editor.connect(a, SIGNAL("triggered()"), lambda e=editor: doView(e))
# show option in top toolbar, copied from aqt/browser.py:BrowserToolbar()
# not an ideal way of doing this, can be disabled by commenting out lines 98 and 99
def setupButtonCopy(self):
mark = self.browser.isMarked()
pause = self.browser.isSuspended()
def borderImg(link, icon, on, title, tooltip=None):
if on:
fmt = '''\
<a class=hitem title="%s" href="%s">\
<img valign=bottom style='border: 1px solid #aaa;' src="qrc:/icons/%s.png"> %s</a>'''
else:
fmt = '''\
<a class=hitem title="%s" href="%s"><img style="padding: 1px;" valign=bottom src="qrc:/icons/%s.png"> %s</a>'''
return fmt % (tooltip or title, link, icon, title)
right = "<div>"
right += borderImg("add", "add16", False, _("Add"))
right += borderImg("info", "info", False, _("Info"),
shortcut(_("Card Info (Ctrl+Shift+I)")))
right += borderImg("view", "none", False, _("View"),
shortcut(_("View Card (Ctrl+Return)")))
right += borderImg("mark", "star16", mark, _("Mark"),
shortcut(_("Mark Note (Ctrl+K)")))
right += borderImg("pause", "pause16", pause, _("Suspend"))
right += borderImg("setDeck", "deck16", False, _("Change Deck"),
shortcut(_("Move To Deck (Ctrl+D)")))
right += borderImg("addtag", "addtag16", False, _("Add Tags"),
shortcut(_("Bulk Add Tags (Ctrl+Shift+T)")))
right += borderImg("deletetag", "deletetag16", False,
_("Remove Tags"), shortcut(_(
"Bulk Remove Tags (Ctrl+Alt+T)")))
right += borderImg("delete", "delete16", False, _("Delete"))
right += "</div>"
self.web.page().currentFrame().setScrollBarPolicy(
Qt.Horizontal, Qt.ScrollBarAlwaysOff)
self.web.stdHtml(self._body % (
"", right, ""), self._css + """
#header { font-weight: normal; }
a { margin-right: 1em; }
.hitem { overflow: hidden; white-space: nowrap;}
""")
# wrapper to add button function
def setupLink(self, l):
if l == "view":
doView(self.browser)
# hooks
addHook("browser.setupMenus", setupMenu)
BrowserToolbar.draw = setupButtonCopy
BrowserToolbar._linkHandler = wrap(BrowserToolbar._linkHandler, setupLink)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment