diff options
Diffstat (limited to 'build/tex-filter.hs')
-rwxr-xr-x | build/tex-filter.hs | 61 |
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 | |||
4 | import Text.Pandoc.JSON | ||
5 | |||
6 | import Control.Monad | ||
7 | import Control.Applicative | ||
8 | import qualified Data.ByteString.Char8 as BS | ||
9 | import Crypto.Hash | ||
10 | import qualified System.IO as SIO | ||
11 | import System.IO.Strict hiding (writeFile, readFile) | ||
12 | import System.Directory | ||
13 | import System.FilePath | ||
14 | import Text.Printf | ||
15 | |||
16 | import qualified Data.Text | ||
17 | |||
18 | import Prelude | ||
19 | |||
20 | type TeX = String | ||
21 | type RawHTML = String | ||
22 | |||
23 | texToPath :: FilePath -> TeX -> FilePath | ||
24 | texToPath base s = base </> "tex" </> (show ( hash (BS.pack s) :: Digest SHA1)) | ||
25 | |||
26 | texToSVGPath :: FilePath -> TeX -> FilePath | ||
27 | texToSVGPath b s = texToPath b s </> "image.svg" | ||
28 | |||
29 | texToAlignPath :: FilePath -> TeX -> FilePath | ||
30 | texToAlignPath b s = texToPath b s </> "vertical-align" | ||
31 | |||
32 | texToExprPath :: FilePath -> TeX -> FilePath | ||
33 | texToExprPath b s = texToPath b s </> "image.expr" | ||
34 | |||
35 | compileToSVG :: FilePath -> MathType -> TeX -> IO RawHTML | ||
36 | compileToSVG 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 | |||
53 | texify :: [String] -> Inline -> IO Inline | ||
54 | texify [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" | ||
58 | texify [_] x = return x | ||
59 | |||
60 | main :: IO () | ||
61 | main = toJSONFilter texify | ||