diff options
Diffstat (limited to 'src/Math.hs')
| -rw-r--r-- | src/Math.hs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/Math.hs b/src/Math.hs new file mode 100644 index 0000000..e927fdd --- /dev/null +++ b/src/Math.hs | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | module Math | ||
| 2 | ( compileMath | ||
| 3 | ) where | ||
| 4 | |||
| 5 | import System.IO.Temp (withSystemTempDirectory) | ||
| 6 | import System.Process (callProcess) | ||
| 7 | import System.Directory (copyFile, getCurrentDirectory, setCurrentDirectory) | ||
| 8 | import System.FilePath (takeFileName, FilePath(..), (</>)) | ||
| 9 | |||
| 10 | import Control.Monad | ||
| 11 | import Control.Exception (bracket) | ||
| 12 | |||
| 13 | import Control.DeepSeq (($!!)) | ||
| 14 | |||
| 15 | compileMath :: String -> IO String | ||
| 16 | compileMath = withSystemTempDirectory "math" . compileMath' | ||
| 17 | |||
| 18 | compileMath' :: String -> FilePath -> IO String | ||
| 19 | compileMath' input tmpDir = do | ||
| 20 | mapM_ (copyToTmp . ("tex" </>)) [ "preamble.tex" | ||
| 21 | , "preview.dtx" | ||
| 22 | , "preview.ins" | ||
| 23 | ] | ||
| 24 | withCurrentDirectory tmpDir $ do | ||
| 25 | callProcess "latex" [ "-interaction=batchmode" | ||
| 26 | , "preview.ins" | ||
| 27 | ] | ||
| 28 | writeFile (tmpDir </> "image.tex") input | ||
| 29 | callProcess "latex" [ "-interaction=batchmode" | ||
| 30 | , "image.tex" | ||
| 31 | ] | ||
| 32 | callProcess "dvisvgm" [ "--exact" | ||
| 33 | , "--no-fonts" | ||
| 34 | , tmpDir </> "image.dvi" | ||
| 35 | ] | ||
| 36 | (\x -> return $!! x) =<< (readFile $ tmpDir </> "image.svg") | ||
| 37 | where | ||
| 38 | copyToTmp fp = copyFile fp (tmpDir </> takeFileName fp) | ||
| 39 | |||
| 40 | withCurrentDirectory :: FilePath -- ^ Directory to execute in | ||
| 41 | -> IO a -- ^ Action to be executed | ||
| 42 | -> IO a | ||
| 43 | withCurrentDirectory dir action = | ||
| 44 | bracket getCurrentDirectory setCurrentDirectory $ \ _ -> do | ||
| 45 | setCurrentDirectory dir | ||
| 46 | action | ||
