123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- #!/usr/bin/runhaskell
- import Text.Pandoc.JSON
- import Data.ByteString.Lazy (hGetContents, hPut)
- import Data.ByteString.Lazy.UTF8 (fromString)
- import Data.Digest.Pure.SHA (sha1, showDigest)
- import System.IO (hClose, hPutStr, IOMode(..), openBinaryFile, hPutStrLn, stderr)
- import System.Process
- import System.Directory
- processBlocks :: Block -> IO Block
- processBlocks b =
- case b of
- CodeBlock (_ , ["plantuml"], _) content -> plantUMLToImg content
- _ -> return b
- plantUMLToImg :: String -> IO Block
- plantUMLToImg content = do
- path <- renderImage content
-
-
- return $ Para [Image [] (path, "")]
- renderImage :: String -> IO String
- renderImage content = do
- createDirectoryIfMissing (True) "output/images"
- let path = uniqueName content ++ ".png"
- (Just hIn, Just hOut, _, _) <-
- createProcess (proc "plantuml" ["-pipe", "-tepg"]){ std_in = CreatePipe,
- std_out = CreatePipe }
- hPutStr hIn content
- hClose hIn
- let outPath = "output/images/" ++ path
- hFile <- openBinaryFile outPath WriteMode
- img <- hGetContents hOut
- hPut hFile img
- hClose hFile
- hClose hOut
- let imgPath = "images/" ++ path
- return imgPath
- uniqueName :: String -> String
- uniqueName = showDigest . sha1 . fromString
- main :: IO ()
- main = toJSONFilter processBlocks
|