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.hooks import addHook
|
||||||
from anki.lang import _
|
from anki.lang import _
|
||||||
|
|
||||||
# extension we output
|
pngCommands = [
|
||||||
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 = [
|
|
||||||
["latex", "-interaction=nonstopmode", "tmp.tex"],
|
["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
|
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."
|
"Return an img link for LATEX, creating if necesssary."
|
||||||
txt = _latexFromHtml(col, latex)
|
txt = _latexFromHtml(col, latex)
|
||||||
|
|
||||||
|
if model.get("latexsvg", False):
|
||||||
|
ext = "svg"
|
||||||
|
else:
|
||||||
|
ext = "png"
|
||||||
|
|
||||||
# is there an existing file?
|
# is there an existing file?
|
||||||
fnamePrefix = "latex-%s." % checksum(txt.encode("utf8"))
|
fname = "latex-%s.%s" % (checksum(txt.encode("utf8")), ext)
|
||||||
found = False
|
|
||||||
for ext in supportedExts:
|
|
||||||
fname = fnamePrefix + ext
|
|
||||||
if os.path.exists(fname):
|
|
||||||
found = True
|
|
||||||
break
|
|
||||||
if not found:
|
|
||||||
fname = fnamePrefix + outputFileExt
|
|
||||||
link = '<img class=latex src="%s">' % fname
|
link = '<img class=latex src="%s">' % fname
|
||||||
if found:
|
if os.path.exists(fname):
|
||||||
return link
|
return link
|
||||||
|
|
||||||
# building disabled?
|
# 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 \
|
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 \
|
it by placing the command in a different package, and importing that \
|
||||||
package in the LaTeX header instead.""") % bad
|
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
|
# write into a temp file
|
||||||
log = open(namedtmp("latex_log.txt"), "w")
|
log = open(namedtmp("latex_log.txt"), "w")
|
||||||
texpath = namedtmp("tmp.tex")
|
texpath = namedtmp("tmp.tex")
|
||||||
|
@ -111,7 +115,7 @@ package in the LaTeX header instead.""") % bad
|
||||||
texfile.close()
|
texfile.close()
|
||||||
mdir = col.media.dir()
|
mdir = col.media.dir()
|
||||||
oldcwd = os.getcwd()
|
oldcwd = os.getcwd()
|
||||||
png = namedtmp("tmp.%s" % outputFileExt)
|
png = namedtmp("tmp.%s" % ext)
|
||||||
try:
|
try:
|
||||||
# generate png
|
# generate png
|
||||||
os.chdir(tmpdir())
|
os.chdir(tmpdir())
|
||||||
|
@ -133,7 +137,7 @@ def _errMsg(type, texpath):
|
||||||
raise Exception()
|
raise Exception()
|
||||||
msg += "<small><pre>" + cgi.escape(log) + "</pre></small>"
|
msg += "<small><pre>" + cgi.escape(log) + "</pre></small>"
|
||||||
except:
|
except:
|
||||||
msg += _("Have you installed latex and dvipng?")
|
msg += _("Have you installed latex and dvipng/dvisvgm?")
|
||||||
pass
|
pass
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
|
|
|
@ -107,6 +107,7 @@ class Models(QDialog):
|
||||||
d = QDialog(self)
|
d = QDialog(self)
|
||||||
frm = aqt.forms.modelopts.Ui_Dialog()
|
frm = aqt.forms.modelopts.Ui_Dialog()
|
||||||
frm.setupUi(d)
|
frm.setupUi(d)
|
||||||
|
frm.latexsvg.setChecked(self.model.get("latexsvg", False))
|
||||||
frm.latexHeader.setText(self.model['latexPre'])
|
frm.latexHeader.setText(self.model['latexPre'])
|
||||||
frm.latexFooter.setText(self.model['latexPost'])
|
frm.latexFooter.setText(self.model['latexPost'])
|
||||||
frm.newStyleWhitespace.setChecked(self.model.get("prewrap", False))
|
frm.newStyleWhitespace.setChecked(self.model.get("prewrap", False))
|
||||||
|
@ -115,6 +116,7 @@ class Models(QDialog):
|
||||||
restoreGeom(d, "modelopts")
|
restoreGeom(d, "modelopts")
|
||||||
d.exec_()
|
d.exec_()
|
||||||
saveGeom(d, "modelopts")
|
saveGeom(d, "modelopts")
|
||||||
|
self.model['latexsvg'] = frm.latexsvg.isChecked()
|
||||||
self.model['latexPre'] = str(frm.latexHeader.toPlainText())
|
self.model['latexPre'] = str(frm.latexHeader.toPlainText())
|
||||||
self.model['latexPost'] = str(frm.latexFooter.toPlainText())
|
self.model['latexPost'] = str(frm.latexFooter.toPlainText())
|
||||||
self.model['prewrap'] = frm.newStyleWhitespace.isChecked()
|
self.model['prewrap'] = frm.newStyleWhitespace.isChecked()
|
||||||
|
|
|
@ -9,8 +9,8 @@
|
||||||
<rect>
|
<rect>
|
||||||
<x>0</x>
|
<x>0</x>
|
||||||
<y>0</y>
|
<y>0</y>
|
||||||
<width>276</width>
|
<width>374</width>
|
||||||
<height>323</height>
|
<height>344</height>
|
||||||
</rect>
|
</rect>
|
||||||
</property>
|
</property>
|
||||||
<property name="windowTitle">
|
<property name="windowTitle">
|
||||||
|
@ -27,6 +27,13 @@
|
||||||
<string>LaTeX</string>
|
<string>LaTeX</string>
|
||||||
</attribute>
|
</attribute>
|
||||||
<layout class="QVBoxLayout" name="verticalLayout">
|
<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>
|
<item>
|
||||||
<widget class="QLabel" name="label_6">
|
<widget class="QLabel" name="label_6">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
|
|
Loading…
Reference in a new issue