Add latex cache.

This commit is contained in:
David 2019-02-15 16:40:51 +01:00
commit 3d7bd1289f
2 changed files with 17 additions and 6 deletions

View file

@ -22,7 +22,8 @@ library
Reanimate.Combinators
Reanimate.LaTeX
build-depends: base >=4.12 && <4.13,
lucid-svg, time, text, unix, lucid, filepath, process, directory
lucid-svg, time, text, unix, lucid, filepath, process, directory,
containers
executable reanimate-viewer
main-is: SvgViewer.hs

View file

@ -1,6 +1,9 @@
{-# LANGUAGE ScopedTypeVariables, OverloadedStrings #-}
module Reanimate.LaTeX where
module Reanimate.LaTeX (latex) where
import Data.IORef
import qualified Data.Map as Map
import Data.Map (Map)
import System.Process
import System.Exit
import System.IO
@ -10,13 +13,20 @@ import System.Directory
import Control.Exception
import Lucid (toHtmlRaw, toHtml)
import Lucid.Svg (Svg, text_, font_size_, fill_)
--toHtmlRaw svgFile
--latex -interaction=batchmode -halt-on-error
--dvisvgm {file} -n -v 0 -o out
{-# NOINLINE cache #-}
cache :: IORef (Map String (Svg ()))
cache = unsafePerformIO (newIORef Map.empty)
latex :: String -> Svg ()
latex = unsafePerformIO . latexToSVG
latex tex = unsafePerformIO $ do
store <- readIORef cache
case Map.lookup tex store of
Just svg -> return svg
Nothing -> do
svg <- latexToSVG tex
atomicModifyIORef cache (\store -> (Map.insert tex svg store, svg))
latexToSVG :: String -> IO (Svg ())
latexToSVG tex = handle (\(e::SomeException) -> return (failedSvg tex)) $ do