pandoc-plantuml 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/runhaskell
  2. {-|
  3. Script adapted from pandoc-plantuml-filter by Kurt Bonne
  4. Original source at https://github.com/kbonne/pandoc-plantuml-filter.git
  5. This script is meant to be run by pandoc executed by the pandoc_reader pelican plugin.
  6. I've changed output paths to be compatibile with pelican output structure.
  7. If using the pandoc_reader pelican plugin with this script, the plantuml plugin is not necessary.
  8. Installation:
  9. -------------
  10. This script requires Haskell, but if you are using pandoc, it's already installed :-)
  11. Copy this file in your pelican project, in the same directory of pelicanconf.py, and make sure it is executable.
  12. In the pelicanconf.py configure the need plugins:
  13. PLUGINS = ['pandoc_reader']
  14. PANDOC_ARGS = ['--filter=pandoc-plantuml']
  15. If this script will be putted in a different location, adapt the PANDOC_ARGS value.
  16. Usage:
  17. ------
  18. In Markdown posts use the following syntax to include PlantUML diagrams:
  19. ```plantuml
  20. @startuml
  21. Alice -> Bob: Authentication Request
  22. Bob --> Alice: Authentication Response
  23. Alice -> Bob: Another authentication Request
  24. Alice <-- Bob: another authentication Response
  25. @enduml
  26. ```
  27. Rendered images will bu put in the output/images folder.
  28. -}
  29. import Text.Pandoc.JSON
  30. import Data.ByteString.Lazy (hGetContents, hPut)
  31. import Data.ByteString.Lazy.UTF8 (fromString)
  32. import Data.Digest.Pure.SHA (sha1, showDigest)
  33. import System.IO (hClose, hPutStr, IOMode(..), openBinaryFile, hPutStrLn, stderr)
  34. import System.Process
  35. import System.Directory
  36. processBlocks :: Block -> IO Block
  37. processBlocks b =
  38. case b of
  39. CodeBlock (_ , ["plantuml"], _) content -> plantUMLToImg content
  40. _ -> return b
  41. plantUMLToImg :: String -> IO Block
  42. plantUMLToImg content = do
  43. path <- renderImage content
  44. --hPutStrLn stderr "dopo renderImage"
  45. return $ Para [Image [] (path, "")]
  46. renderImage :: String -> IO String
  47. renderImage content = do
  48. createDirectoryIfMissing (True) "output/images"
  49. let path = uniqueName content ++ ".png"
  50. (Just hIn, Just hOut, _, _) <-
  51. createProcess (proc "plantuml" ["-pipe", "-tepg"]){ std_in = CreatePipe,
  52. std_out = CreatePipe }
  53. hPutStr hIn content
  54. hClose hIn
  55. let outPath = "output/images/" ++ path
  56. hFile <- openBinaryFile outPath WriteMode
  57. img <- hGetContents hOut
  58. hPut hFile img
  59. hClose hFile
  60. hClose hOut
  61. let imgPath = "images/" ++ path
  62. return imgPath
  63. uniqueName :: String -> String
  64. uniqueName = showDigest . sha1 . fromString
  65. main :: IO ()
  66. main = toJSONFilter processBlocks