-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpandoc-tex2svg.hs
More file actions
51 lines (44 loc) · 1.71 KB
/
pandoc-tex2svg.hs
File metadata and controls
51 lines (44 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
{-# LANGUAGE ScopedTypeVariables #-}
module Main where
import Text.Pandoc.JSON
import Control.Monad (when)
import System.Exit
import System.Process
import Data.IORef
import qualified Data.Map as M
import System.Directory (findExecutable)
import System.IO (stderr, hPutStrLn)
newtype Cache = Cache (M.Map (MathType, String) String)
lookupCache :: (MathType, String) -> Cache -> Maybe String
lookupCache s (Cache c) = M.lookup s c
addToCache :: (MathType, String) -> String -> Cache -> Cache
addToCache s v (Cache c) = Cache $ M.insert s v c
main :: IO ()
main = do
cache <- newIORef (Cache M.empty)
toJSONFilter (tex2svg cache)
tex2svg :: IORef Cache -> Inline -> IO Inline
tex2svg cr (Math mt math) = do
mbfp <- findExecutable "tex2svg"
when (mbfp == Nothing) $ do
hPutStrLn stderr $ "The tex2svg program was not found in the path.\n" ++
"Install MathJax-node (https://github.com/mathjax/MathJax-node)\n" ++
"and ensure that tex2svg is in your path."
exitWith $ ExitFailure 1
cache <- readIORef cr
svg <- case lookupCache (mt, math) cache of
Just s -> return s
Nothing -> do
svg' <- readProcess "tex2svg"
(["--inline" | mt == InlineMath] ++ [math]) ""
modifyIORef cr (addToCache (mt, math) svg')
return svg'
if null svg -- indicates an error -- tex2svg doesn't return error status
then do
hPutStrLn stderr $ "Could not convert: " ++ math
return $ Math mt math
else return $ RawInline (Format "html") $
"<span class=\"math " ++
(if mt == InlineMath then "inline" else "display") ++ "\">" ++
svg ++ "</span>"
tex2svg _ il = return il