fix reporting of latex errors; catch some bad commands

This commit is contained in:
Damien Elmes 2011-10-29 15:12:57 +09:00
parent 6911ae839e
commit ed7367d67f
5 changed files with 26 additions and 9 deletions

View file

@ -74,6 +74,10 @@ def _buildImg(deck, latex, fname, model):
latex = (model["latexPre"] + "\n" + latex = (model["latexPre"] + "\n" +
latex + "\n" + latex + "\n" +
model["latexPost"]) model["latexPost"])
# it's only really secure if run in a jail, but these are the most common
for bad in ("write18", "\\readline", "\\input", "\\include", "\\catcode",
"\\openout", "\\write", "\\loop", "\\def", "\\shipout"):
assert bad not in latex
# write into a temp file # write into a temp file
log = open(namedtmp("latex_log.txt"), "w") log = open(namedtmp("latex_log.txt"), "w")
texfile = file(namedtmp("tmp.tex"), "w") texfile = file(namedtmp("tmp.tex"), "w")
@ -101,7 +105,7 @@ def _buildImg(deck, latex, fname, model):
def _errMsg(type): def _errMsg(type):
msg = (_("Error executing %s.") % type) + "<br>" msg = (_("Error executing %s.") % type) + "<br>"
try: try:
log = open(namedtmp("latex_log.txt")).read() log = open(namedtmp("latex_log.txt", rm=False)).read()
if not log: if not log:
raise Exception() raise Exception()
msg += "<small><pre>" + cgi.escape(log) + "</pre></small>" msg += "<small><pre>" + cgi.escape(log) + "</pre></small>"

View file

@ -9,6 +9,7 @@ from anki.utils import checksum, intTime, namedtmp, isWin
from anki.lang import _ from anki.lang import _
from anki.db import DB from anki.db import DB
from anki.consts import * from anki.consts import *
from anki.latex import mungeQA
class MediaManager(object): class MediaManager(object):
@ -83,8 +84,13 @@ If the same name exists, compare checksums."""
# String manipulation # String manipulation
########################################################################## ##########################################################################
def mediaFiles(self, string, includeRemote=False): def files(self, mid, string, includeRemote=False):
l = [] l = []
# convert latex first
model = self.deck.models.get(mid)
string = mungeQA(string, None, None, model, None, self.deck)
print string
# extract filenames
for reg in self.regexps: for reg in self.regexps:
for (full, fname) in re.findall(reg, string): for (full, fname) in re.findall(reg, string):
isLocal = not re.match("(https?|ftp)://", fname.lower()) isLocal = not re.match("(https?|ftp)://", fname.lower())

View file

@ -22,7 +22,7 @@ defaultModel = {
'latexPre': """\ 'latexPre': """\
\\documentclass[12pt]{article} \\documentclass[12pt]{article}
\\special{papersize=3in,5in} \\special{papersize=3in,5in}
\\usepackage[utf8x]{inputenc} % \\usepackage[utf8x]{inputenc} % uncomment this for foreign characters
\\usepackage{amssymb,amsmath} \\usepackage{amssymb,amsmath}
\\pagestyle{empty} \\pagestyle{empty}
\\setlength{\\parindent}{0in} \\setlength{\\parindent}{0in}

View file

@ -461,6 +461,11 @@ order by ordinal""", mid)):
new = fld new = fld
# rewrite reference in template # rewrite reference in template
t[key] = t[key].replace(all, "{{{%s}}}" % new) t[key] = t[key].replace(all, "{{{%s}}}" % new)
regexps = deck.media.regexps + (
r"(\[latex\](.+?)\[/latex\])",
r"(\[\$\](.+?)\[/\$\])",
r"(\[\$\$\](.+?)\[/\$\$\])")
# process each model
for m in deck.models.all(): for m in deck.models.all():
state = dict(mflds={}, fields=0) state = dict(mflds={}, fields=0)
for t in m['tmpls']: for t in m['tmpls']:
@ -469,7 +474,8 @@ order by ordinal""", mid)):
rewriteRef('qfmt') rewriteRef('qfmt')
for match in re.findall(r, t['afmt']): for match in re.findall(r, t['afmt']):
rewriteRef('afmt') rewriteRef('afmt')
if state['fields']:
deck.models.save(m)
# Upgrading deck # Upgrading deck
###################################################################### ######################################################################

View file

@ -242,13 +242,14 @@ def tmpfile(prefix="", suffix=""):
os.close(fd) os.close(fd)
return name return name
def namedtmp(name): def namedtmp(name, rm=True):
"Return tmpdir+name. Deletes any existing file." "Return tmpdir+name. Deletes any existing file."
path = os.path.join(tmpdir(), name) path = os.path.join(tmpdir(), name)
try: if rm:
os.unlink(path) try:
except (OSError, IOError): os.unlink(path)
pass except (OSError, IOError):
pass
return path return path
# Cmd invocation # Cmd invocation