revert to old style file-only latex handling

This commit is contained in:
Damien Elmes 2009-07-08 12:20:56 +09:00
parent 0011489220
commit 6f27ae52cc
2 changed files with 21 additions and 31 deletions

View file

@ -8,9 +8,8 @@ Latex support
""" """
__docformat__ = 'restructuredtext' __docformat__ = 'restructuredtext'
import re, tempfile, os, sys, subprocess, stat, time import re, tempfile, os, sys, subprocess, stat, time, shutil
from anki.utils import genID, checksum from anki.utils import genID, checksum
from anki.media import copyToMedia
from htmlentitydefs import entitydefs from htmlentitydefs import entitydefs
latexPreamble = ("\\documentclass[12pt]{article}\n" latexPreamble = ("\\documentclass[12pt]{article}\n"
@ -77,8 +76,7 @@ def call(argv, wait=True, **kwargs):
def latexImgFile(deck, latexCode): def latexImgFile(deck, latexCode):
key = checksum(latexCode) key = checksum(latexCode)
return deck.s.scalar("select filename from media where originalPath = :k", return "latex-%s.png" % key
k=key)
def latexImgPath(deck, file): def latexImgPath(deck, file):
"Return the path to the cache file in system encoding format." "Return the path to the cache file in system encoding format."
@ -137,8 +135,9 @@ def buildImg(deck, latex):
stdout=log, stderr=log, startupinfo=si): stdout=log, stderr=log, startupinfo=si):
return (False, errmsg) return (False, errmsg)
# add to media # add to media
path = copyToMedia(deck, "tmp.png", latex=checksum(latex)) target = latexImgPath(deck, latexImgFile(deck, latex))
return (True, path) shutil.copy2("tmp.png", target)
return (True, target)
finally: finally:
os.chdir(oldcwd) os.chdir(oldcwd)

View file

@ -51,15 +51,11 @@ def mediaFilename(path):
ext = os.path.splitext(path)[1].lower() ext = os.path.splitext(path)[1].lower()
return "%s%s" % (new, ext) return "%s%s" % (new, ext)
def copyToMedia(deck, path, latex=None): def copyToMedia(deck, path):
"""Copy PATH to MEDIADIR, and return new filename. """Copy PATH to MEDIADIR, and return new filename.
Update media table. If file already exists, don't copy.""" Update media table. If file already exists, don't copy."""
if latex: origPath = path
origPath = latex description = os.path.splitext(os.path.basename(path))[0]
description = "latex"
else:
origPath = path
description = os.path.splitext(os.path.basename(path))[0]
newBase = mediaFilename(path) newBase = mediaFilename(path)
new = os.path.join(deck.mediaDir(create=True), newBase) new = os.path.join(deck.mediaDir(create=True), newBase)
# copy if not existing # copy if not existing
@ -73,17 +69,14 @@ Update media table. If file already exists, don't copy."""
if not deck.s.scalar( if not deck.s.scalar(
"select 1 from media where filename = :f", "select 1 from media where filename = :f",
f=newBase): f=newBase):
if description != "latex": # if the user has modified a hashed file, try to remember the old
# if the user has modified a hashed file, try to remember the old # filename
# filename old = deck.s.scalar(
old = deck.s.scalar( "select originalPath from media where filename = :s",
"select originalPath from media where filename = :s", s=os.path.basename(origPath))
s=os.path.basename(origPath)) if old:
if old: origPath = old
origPath = old description = os.path.splitext(os.path.basename(origPath))[0]
description = os.path.splitext(os.path.basename(origPath))[0]
print "orig", old
print "desc", description
try: try:
path = unicode(path, sys.getfilesystemencoding()) path = unicode(path, sys.getfilesystemencoding())
except TypeError: except TypeError:
@ -150,6 +143,8 @@ def rebuildMediaDir(deck, deleteRefs=False, dirty=True):
for c, oldBase in enumerate(files): for c, oldBase in enumerate(files):
if mod and not c % mod: if mod and not c % mod:
deck.updateProgress() deck.updateProgress()
if oldBase.startswith("latex-"):
continue
oldPath = os.path.join(deck.mediaDir(), oldBase) oldPath = os.path.join(deck.mediaDir(), oldBase)
if oldBase.startswith("."): if oldBase.startswith("."):
continue continue
@ -203,14 +198,12 @@ def rebuildMediaDir(deck, deleteRefs=False, dirty=True):
# build cache of db records # build cache of db records
deck.updateProgress(_("Delete unused files...")) deck.updateProgress(_("Delete unused files..."))
mediaIds = dict(deck.s.all("select filename, id from media")) mediaIds = dict(deck.s.all("select filename, id from media"))
# assume latex files exist
for f in deck.s.column0(
"select filename from media where description = 'latex'"):
usedFiles[f] = 1
# look through the media dir for any unused files, and delete # look through the media dir for any unused files, and delete
for f in os.listdir(unicode(deck.mediaDir())): for f in os.listdir(unicode(deck.mediaDir())):
if f.startswith("."): if f.startswith("."):
continue continue
if f.startswith("latex-"):
continue
path = os.path.join(deck.mediaDir(), f) path = os.path.join(deck.mediaDir(), f)
if os.path.isdir(path): if os.path.isdir(path):
shutil.rmtree(path) shutil.rmtree(path)
@ -282,9 +275,7 @@ def exportOriginalFiles(deck):
except (IOError, OSError): except (IOError, OSError):
pass pass
cnt = 0 cnt = 0
for row in deck.s.all(""" for row in deck.s.all("select filename, originalPath from media"):
select filename, originalPath from media
where description != 'latex'"""):
(fname, path) = row (fname, path) = row
base = os.path.basename(path) base = os.path.basename(path)
if base == fname: if base == fname: