mirror of
https://github.com/reanimate/reanimate.git
synced 2026-09-14 09:32:22 +00:00
Move shared tools to a separate module.
This commit is contained in:
parent
f29294a9e4
commit
1a520f0c56
4 changed files with 61 additions and 81 deletions
|
|
@ -2,19 +2,16 @@
|
|||
{-# LANGUAGE ScopedTypeVariables #-}
|
||||
module Reanimate.LaTeX (latex) where
|
||||
|
||||
import Control.Exception
|
||||
import Control.Exception (SomeException, handle)
|
||||
import qualified Data.ByteString as B
|
||||
import Data.IORef
|
||||
import Data.Map (Map)
|
||||
import qualified Data.Map as Map
|
||||
import Lucid (ToHtml(..))
|
||||
import Lucid (ToHtml (..))
|
||||
import Lucid.Svg (Svg, fill_, font_size_, text_)
|
||||
import System.Directory
|
||||
import System.Exit
|
||||
import System.FilePath
|
||||
import System.IO
|
||||
import System.IO.Unsafe
|
||||
import System.Process
|
||||
import Reanimate.Misc
|
||||
import System.FilePath (replaceExtension, takeFileName, (</>))
|
||||
import System.IO.Unsafe (unsafePerformIO)
|
||||
|
||||
import Graphics.Svg (loadSvgFile, parseSvgFile,
|
||||
xmlOfDocument)
|
||||
|
|
@ -59,40 +56,6 @@ failedSvg tex =
|
|||
text_ [ font_size_ "20"
|
||||
, fill_ "white"] (toHtml $ "bad latex: "++tex)
|
||||
|
||||
runCmd exec args = do
|
||||
(ret, stdout, stderr) <- readProcessWithExitCode exec args ""
|
||||
evaluate (length stdout + length stderr)
|
||||
case ret of
|
||||
ExitSuccess -> return ()
|
||||
ExitFailure err -> do
|
||||
putStrLn $
|
||||
"Failed to run: " ++ showCommandForUser exec args ++ "\n" ++
|
||||
"Error code: " ++ show err ++ "\n" ++
|
||||
"stderr: " ++ show stderr
|
||||
throwIO (ExitFailure err)
|
||||
|
||||
withTempDir action = do
|
||||
dir <- getTemporaryDirectory
|
||||
(path, handle) <- openTempFile dir "reanimate-XXXXXX"
|
||||
hClose handle
|
||||
removeFile path
|
||||
createDirectory (dir </> path)
|
||||
action (dir </> path) `finally` removeDirectoryRecursive (dir </> path)
|
||||
|
||||
withTempFile ext action = do
|
||||
dir <- getTemporaryDirectory
|
||||
(path, handle) <- openTempFile dir ("reanimate-XXXXXX" <.> ext)
|
||||
hClose handle
|
||||
action path `finally` removeFile path
|
||||
|
||||
requireExecutable :: String -> IO FilePath
|
||||
requireExecutable exec = do
|
||||
mbPath <- findExecutable exec
|
||||
case mbPath of
|
||||
Nothing -> error $ "Couldn't find executable: " ++ exec
|
||||
Just path -> return path
|
||||
|
||||
|
||||
tex_prologue =
|
||||
"\\documentclass[preview]{standalone}\n\
|
||||
\\\usepackage[english]{babel}\n\
|
||||
|
|
|
|||
52
src/Reanimate/Misc.hs
Normal file
52
src/Reanimate/Misc.hs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
module Reanimate.Misc
|
||||
( requireExecutable
|
||||
, runCmd
|
||||
, withTempDir
|
||||
, withTempFile
|
||||
) where
|
||||
|
||||
import Control.Exception (evaluate, finally, throwIO)
|
||||
import System.Directory (createDirectory, findExecutable,
|
||||
getTemporaryDirectory,
|
||||
removeDirectoryRecursive, removeFile)
|
||||
import System.Exit (ExitCode (..))
|
||||
import System.FilePath ((<.>), (</>))
|
||||
import System.IO (hClose, openTempFile)
|
||||
import System.Process (readProcessWithExitCode, showCommandForUser)
|
||||
|
||||
|
||||
requireExecutable :: String -> IO FilePath
|
||||
requireExecutable exec = do
|
||||
mbPath <- findExecutable exec
|
||||
case mbPath of
|
||||
Nothing -> error $ "Couldn't find executable: " ++ exec
|
||||
Just path -> return path
|
||||
|
||||
runCmd :: FilePath -> [String] -> IO ()
|
||||
runCmd exec args = do
|
||||
(ret, stdout, stderr) <- readProcessWithExitCode exec args ""
|
||||
evaluate (length stdout + length stderr)
|
||||
case ret of
|
||||
ExitSuccess -> return ()
|
||||
ExitFailure err -> do
|
||||
putStrLn $
|
||||
"Failed to run: " ++ showCommandForUser exec args ++ "\n" ++
|
||||
"Error code: " ++ show err ++ "\n" ++
|
||||
"stderr: " ++ show stderr
|
||||
throwIO (ExitFailure err)
|
||||
|
||||
withTempDir :: (FilePath -> IO a) -> IO a
|
||||
withTempDir action = do
|
||||
dir <- getTemporaryDirectory
|
||||
(path, handle) <- openTempFile dir "reanimate-XXXXXX"
|
||||
hClose handle
|
||||
removeFile path
|
||||
createDirectory (dir </> path)
|
||||
action (dir </> path) `finally` removeDirectoryRecursive (dir </> path)
|
||||
|
||||
withTempFile :: String -> (FilePath -> IO a) -> IO a
|
||||
withTempFile ext action = do
|
||||
dir <- getTemporaryDirectory
|
||||
(path, handle) <- openTempFile dir ("reanimate-XXXXXX" <.> ext)
|
||||
hClose handle
|
||||
action path `finally` removeFile path
|
||||
|
|
@ -2,16 +2,12 @@ module Reanimate.Render
|
|||
( render
|
||||
) where
|
||||
|
||||
import Control.Exception (evaluate, finally, throwIO)
|
||||
import Control.Monad (forM_)
|
||||
import Lucid.Svg (renderToFile)
|
||||
import Reanimate.Arrow (Ani, animationDuration, frameAt)
|
||||
import Reanimate.Examples
|
||||
import System.Directory
|
||||
import System.Exit
|
||||
import System.FilePath
|
||||
import System.IO
|
||||
import System.Process
|
||||
import Reanimate.Misc
|
||||
import System.FilePath (takeExtension, (</>))
|
||||
import Text.Printf (printf)
|
||||
|
||||
|
||||
|
|
@ -74,36 +70,3 @@ generateFrames ani rate action = withTempDir $ \tmp -> do
|
|||
frameCount = round (animationDuration ani * fromIntegral rate) :: Int
|
||||
nameTemplate :: String
|
||||
nameTemplate = "render-%05d.svg"
|
||||
|
||||
requireExecutable :: String -> IO FilePath
|
||||
requireExecutable exec = do
|
||||
mbPath <- findExecutable exec
|
||||
case mbPath of
|
||||
Nothing -> error $ "Couldn't find executable: " ++ exec
|
||||
Just path -> return path
|
||||
|
||||
runCmd exec args = do
|
||||
(ret, stdout, stderr) <- readProcessWithExitCode exec args ""
|
||||
evaluate (length stdout + length stderr)
|
||||
case ret of
|
||||
ExitSuccess -> return ()
|
||||
ExitFailure err -> do
|
||||
putStrLn $
|
||||
"Failed to run: " ++ showCommandForUser exec args ++ "\n" ++
|
||||
"Error code: " ++ show err ++ "\n" ++
|
||||
"stderr: " ++ show stderr
|
||||
throwIO (ExitFailure err)
|
||||
|
||||
withTempDir action = do
|
||||
dir <- getTemporaryDirectory
|
||||
(path, handle) <- openTempFile dir "reanimate-XXXXXX"
|
||||
hClose handle
|
||||
removeFile path
|
||||
createDirectory (dir </> path)
|
||||
action (dir </> path) `finally` removeDirectoryRecursive (dir </> path)
|
||||
|
||||
withTempFile ext action = do
|
||||
dir <- getTemporaryDirectory
|
||||
(path, handle) <- openTempFile dir ("reanimate-XXXXXX" <.> ext)
|
||||
hClose handle
|
||||
action path `finally` removeFile path
|
||||
|
|
|
|||
Loading…
Reference in a new issue