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 | module Dot (plugin) where
import Network.Gitit.Interface import System.Process import System.Exit
import Data.ByteString.Lazy.UTF8 (fromString)
import Data.Digest.Pure.SHA import System.FilePath import Control.Monad.Trans (liftIO)
plugin :: Plugin plugin = mkPageTransformM transformBlock
transformBlock :: Block -> PluginM Block transformBlock (CodeBlock (_, classes, namevals) contents) | "dot" `elem` classes = do cfg <- askConfig let (name, outfile) = case lookup "name" namevals of Just fn -> ([Str fn], fn ++ ".png") Nothing -> ([], uniqueName contents ++ ".png") liftIO $ do (ec, out, err) <- readProcessWithExitCode "dot" ["-Tpng"] contents if ec == ExitSuccess then writeFile (staticDir cfg </> "img" </> outfile) out else error $ "dot returned an error status: " ++ err return $ Para [Image name ("/_static/img" </> outfile, "")] transformBlock x = return x
uniqueName :: String -> String uniqueName = showDigest . sha1 . fromString
|