874c3e0dea
+ In Text.Pandoc.Definition, added processIn, processInM, and queryIn, and deprecated processPandoc and queryPandoc for these more general functions, which are useful in writing plugins. + Added module Text.Pandoc.Plugins. + Added a --plugins option to Main, and code to run the parsed pandoc document through all the plugins. + Provided five sample plugin files in the plugins/ directory. + Documented --plugin in the pandoc man page and README. git-svn-id: https://pandoc.googlecode.com/svn/trunk@1519 788f1e2b-df1e-0410-8736-df70ead52e1b
30 lines
1 KiB
Haskell
30 lines
1 KiB
Haskell
module DotPlugin (transform) where
|
|
import Text.Pandoc
|
|
import Text.Pandoc.Shared
|
|
import System.Process (readProcess)
|
|
import Data.Char (ord)
|
|
-- from the utf8-string package on HackageDB:
|
|
import Data.ByteString.Lazy.UTF8 (fromString)
|
|
-- from the SHA package on HackageDB:
|
|
import Data.Digest.Pure.SHA
|
|
|
|
-- This plugin allows you to include a graphviz "dot" diagram
|
|
-- in a document like this:
|
|
--
|
|
-- ~~~ {.dot name="diagram1"}
|
|
-- digraph G {Hello->World}
|
|
-- ~~~
|
|
|
|
transform :: Block -> IO Block
|
|
transform (CodeBlock (id, classes, namevals) contents) | "dot" `elem` classes = do
|
|
let (name, outfile) = case lookup "name" namevals of
|
|
Just fn -> ([Str fn], fn ++ ".png")
|
|
Nothing -> ([], uniqueName contents ++ ".png")
|
|
result <- readProcess "dot" ["-Tpng"] contents
|
|
writeFile outfile result
|
|
return $ Para [Image name (outfile, "")]
|
|
transform x = return x
|
|
|
|
-- | Generate a unique filename given the file's contents.
|
|
uniqueName :: String -> String
|
|
uniqueName = showDigest . sha1 . fromString
|