git.iko.soy/typeable/pandoc
on ca712fe help sign in
typeable/pandoc/plugins/DotPlugin.hs
30 lines 1.1 kB history raw
1module DotPlugin (transform) where
2import Text.Pandoc
3import Text.Pandoc.Shared
4import System.Process (readProcess)
5import Data.Char (ord)
6-- from the utf8-string package on HackageDB:
7import Data.ByteString.Lazy.UTF8 (fromString)
8-- from the SHA package on HackageDB:
9import Data.Digest.Pure.SHA
11-- This plugin allows you to include a graphviz "dot" diagram
12-- in a document like this:
13--
14-- ~~~ {.dot name="diagram1"}
15-- digraph G {Hello->World}
16-- ~~~
18transform :: Block -> IO Block
19transform (CodeBlock (id, classes, namevals) contents) | "dot" `elem` classes = do
20 let (name, outfile) = case lookup "name" namevals of
21 Just fn -> ([Str fn], fn ++ ".png")
22 Nothing -> ([], uniqueName contents ++ ".png")
23 result <- readProcess "dot" ["-Tpng"] contents
24 writeFile outfile result
25 return $ Para [Image name (outfile, "")]
26transform x = return x
28-- | Generate a unique filename given the file's contents.
29uniqueName :: String -> String
30uniqueName = showDigest . sha1 . fromString