mirror of
https://github.com/ankitects/anki.git
synced 2025-09-24 16:56:36 -04:00
turn latex svg into a note type option
needed until the other clients support svgs
This commit is contained in:
parent
608a5662a9
commit
8cd20f3352
3 changed files with 36 additions and 23 deletions
|
@ -7,16 +7,14 @@ from anki.utils import checksum, call, namedtmp, tmpdir, isMac, stripHTML
|
|||
from anki.hooks import addHook
|
||||
from anki.lang import _
|
||||
|
||||
# extension we output
|
||||
outputFileExt = "svg"
|
||||
# extensions of existing images we look for
|
||||
supportedExts = ["svg", "png"]
|
||||
|
||||
# if you modify these in an add-on, you must make sure to take tmp.tex as the
|
||||
# input, and change outputFileExt to the output file extension
|
||||
latexCmds = [
|
||||
pngCommands = [
|
||||
["latex", "-interaction=nonstopmode", "tmp.tex"],
|
||||
["dvisvgm", "--no-fonts", "-Z", "2", "tmp.dvi", "-o", "tmp.%s" % outputFileExt]
|
||||
["dvipng", "-D", "200", "-T", "tight", "tmp.dvi", "-o", "tmp.png"]
|
||||
]
|
||||
|
||||
svgCommands = [
|
||||
["latex", "-interaction=nonstopmode", "tmp.tex"],
|
||||
["dvisvgm", "--no-fonts", "-Z", "2", "tmp.dvi", "-o", "tmp.svg"]
|
||||
]
|
||||
|
||||
build = True # if off, use existing media but don't create new
|
||||
|
@ -56,18 +54,15 @@ def _imgLink(col, latex, model):
|
|||
"Return an img link for LATEX, creating if necesssary."
|
||||
txt = _latexFromHtml(col, latex)
|
||||
|
||||
if model.get("latexsvg", False):
|
||||
ext = "svg"
|
||||
else:
|
||||
ext = "png"
|
||||
|
||||
# is there an existing file?
|
||||
fnamePrefix = "latex-%s." % checksum(txt.encode("utf8"))
|
||||
found = False
|
||||
for ext in supportedExts:
|
||||
fname = fnamePrefix + ext
|
||||
if os.path.exists(fname):
|
||||
found = True
|
||||
break
|
||||
if not found:
|
||||
fname = fnamePrefix + outputFileExt
|
||||
fname = "latex-%s.%s" % (checksum(txt.encode("utf8")), ext)
|
||||
link = '<img class=latex src="%s">' % fname
|
||||
if found:
|
||||
if os.path.exists(fname):
|
||||
return link
|
||||
|
||||
# building disabled?
|
||||
|
@ -103,6 +98,15 @@ def _buildImg(col, latex, fname, model):
|
|||
For security reasons, '%s' is not allowed on cards. You can still use \
|
||||
it by placing the command in a different package, and importing that \
|
||||
package in the LaTeX header instead.""") % bad
|
||||
|
||||
# commands to use?
|
||||
if model.get("latexsvg", False):
|
||||
latexCmds = svgCommands
|
||||
ext = "svg"
|
||||
else:
|
||||
latexCmds = pngCommands
|
||||
ext = "png"
|
||||
|
||||
# write into a temp file
|
||||
log = open(namedtmp("latex_log.txt"), "w")
|
||||
texpath = namedtmp("tmp.tex")
|
||||
|
@ -111,7 +115,7 @@ package in the LaTeX header instead.""") % bad
|
|||
texfile.close()
|
||||
mdir = col.media.dir()
|
||||
oldcwd = os.getcwd()
|
||||
png = namedtmp("tmp.%s" % outputFileExt)
|
||||
png = namedtmp("tmp.%s" % ext)
|
||||
try:
|
||||
# generate png
|
||||
os.chdir(tmpdir())
|
||||
|
@ -133,7 +137,7 @@ def _errMsg(type, texpath):
|
|||
raise Exception()
|
||||
msg += "<small><pre>" + cgi.escape(log) + "</pre></small>"
|
||||
except:
|
||||
msg += _("Have you installed latex and dvipng?")
|
||||
msg += _("Have you installed latex and dvipng/dvisvgm?")
|
||||
pass
|
||||
return msg
|
||||
|
||||
|
|
|
@ -107,6 +107,7 @@ class Models(QDialog):
|
|||
d = QDialog(self)
|
||||
frm = aqt.forms.modelopts.Ui_Dialog()
|
||||
frm.setupUi(d)
|
||||
frm.latexsvg.setChecked(self.model.get("latexsvg", False))
|
||||
frm.latexHeader.setText(self.model['latexPre'])
|
||||
frm.latexFooter.setText(self.model['latexPost'])
|
||||
frm.newStyleWhitespace.setChecked(self.model.get("prewrap", False))
|
||||
|
@ -115,6 +116,7 @@ class Models(QDialog):
|
|||
restoreGeom(d, "modelopts")
|
||||
d.exec_()
|
||||
saveGeom(d, "modelopts")
|
||||
self.model['latexsvg'] = frm.latexsvg.isChecked()
|
||||
self.model['latexPre'] = str(frm.latexHeader.toPlainText())
|
||||
self.model['latexPost'] = str(frm.latexFooter.toPlainText())
|
||||
self.model['prewrap'] = frm.newStyleWhitespace.isChecked()
|
||||
|
|
|
@ -9,8 +9,8 @@
|
|||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>276</width>
|
||||
<height>323</height>
|
||||
<width>374</width>
|
||||
<height>344</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
|
@ -27,6 +27,13 @@
|
|||
<string>LaTeX</string>
|
||||
</attribute>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="QCheckBox" name="latexsvg">
|
||||
<property name="text">
|
||||
<string>Create scalable images with dvisvgm</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="label_6">
|
||||
<property name="text">
|
||||
|
|
Loading…
Reference in a new issue