summaryrefslogtreecommitdiff
path: root/build/tex-filter.hs
diff options
context:
space:
mode:
Diffstat (limited to 'build/tex-filter.hs')
-rwxr-xr-xbuild/tex-filter.hs61
1 files changed, 0 insertions, 61 deletions
diff --git a/build/tex-filter.hs b/build/tex-filter.hs
deleted file mode 100755
index 1e2face..0000000
--- a/build/tex-filter.hs
+++ /dev/null
@@ -1,61 +0,0 @@
1#!/usr/bin/env runhaskell
2{-# LANGUAGE OverloadedStrings #-}
3
4import Text.Pandoc.JSON
5
6import Control.Monad
7import Control.Applicative
8import qualified Data.ByteString.Char8 as BS
9import Crypto.Hash
10import qualified System.IO as SIO
11import System.IO.Strict hiding (writeFile, readFile)
12import System.Directory
13import System.FilePath
14import Text.Printf
15
16import qualified Data.Text
17
18import Prelude
19
20type TeX = String
21type RawHTML = String
22
23texToPath :: FilePath -> TeX -> FilePath
24texToPath base s = base </> "tex" </> (show ( hash (BS.pack s) :: Digest SHA1))
25
26texToSVGPath :: FilePath -> TeX -> FilePath
27texToSVGPath b s = texToPath b s </> "image.svg"
28
29texToAlignPath :: FilePath -> TeX -> FilePath
30texToAlignPath b s = texToPath b s </> "vertical-align"
31
32texToExprPath :: FilePath -> TeX -> FilePath
33texToExprPath b s = texToPath b s </> "image.expr"
34
35compileToSVG :: FilePath -> MathType -> TeX -> IO RawHTML
36compileToSVG basepath t s = let tex = Data.Text.unpack $ Data.Text.strip $ Data.Text.pack s
37 path = texToPath basepath tex
38 svg_path = texToSVGPath basepath tex
39 svg_web_path = texToSVGPath "" tex
40 align_path = texToAlignPath basepath tex
41 expr_path = texToExprPath basepath tex
42
43 display DisplayMath = "\\displaystyle\n"
44 display InlineMath = ""
45 in do createDirectoryIfMissing True path
46 any (/= True) <$> mapM doesFileExist [svg_path, align_path, expr_path] >>= (flip when)
47 (do writeFile expr_path $ (display t) ++ tex
48 writeFile align_path "0")
49 alignment <- head.lines <$> readFile align_path
50 SIO.hPutStrLn SIO.stdout path
51 return $ printf "<object data=\"/%s\" type=\"image/svg+xml\" style=\"vertical-align:-%s\">%s</object>" svg_web_path alignment tex
52
53texify :: [String] -> Inline -> IO Inline
54texify [basepath] (Math t s) = do svg <- compileToSVG basepath t s
55 return $ Span ("", [classOf t], []) [RawInline (Format "html") svg]
56 where classOf DisplayMath = "display-math"
57 classOf InlineMath = "inline-math"
58texify [_] x = return x
59
60main :: IO ()
61main = toJSONFilter texify