Created
August 8, 2011 15:40
-
-
Save eungju/1131985 to your computer and use it in GitHub Desktop.
PIC plugin for Gitit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Pic (plugin) where | |
-- This plugin allows you to include a PIC diagram | |
-- in a page like this: | |
-- | |
-- ~~~ {.pic name="diagram1"} | |
-- box "box" | |
-- ~~~ | |
-- | |
-- The "pic2png" executable must be in the path. | |
-- The generated png file will be saved in the static img directory. | |
-- If no name is specified, a unique name will be generated from a hash | |
-- of the file contents. | |
import Network.Gitit.Interface | |
import System.Process (readProcessWithExitCode) | |
import System.Exit (ExitCode(ExitSuccess)) | |
-- from the utf8-string package on HackageDB: | |
import Data.ByteString.Lazy.UTF8 (fromString) | |
-- from the SHA package on HackageDB: | |
import Data.Digest.Pure.SHA (sha1, showDigest) | |
import System.FilePath ((</>)) | |
import Control.Monad.Trans (liftIO) | |
plugin :: Plugin | |
plugin = mkPageTransformM transformBlock | |
transformBlock :: Block -> PluginM Block | |
transformBlock (CodeBlock (_, classes, namevals) contents) | "pic" `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 "pic2png" [staticDir cfg </> "img" </> outfile] contents | |
if ec == ExitSuccess | |
then return $ Para [Image name ("/img" </> outfile, "")] | |
else error $ "pic returned an error status: " ++ err | |
transformBlock x = return x | |
-- | Generate a unique filename given the file's contents. | |
uniqueName :: String -> String | |
uniqueName = showDigest . sha1 . fromString |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
pic2graph > $1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment