-
Notifications
You must be signed in to change notification settings - Fork 0
/
pandoc-sh.hs
executable file
·44 lines (37 loc) · 1.07 KB
/
pandoc-sh.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
{-# LANGUAGE NoMonomorphismRestriction #-}
import Text.Pandoc.JSON
import Text.Pandoc.Generic
import System.Process
import System.Posix.Temp
import System.Posix.Files
import System.IO
import Data.Maybe
import Control.Monad
includeShellBlock :: Block -> IO Block
includeShellBlock (CodeBlock (id, cls, attrs) code)
| "sh" `elem` cls
= do
(path, hndl) <- mkstemp "/tmp/pandoc-z-sh"
hPutStr hndl code
hClose hndl
out <- readProcess cmd [path] ""
setFileMode path ownerExecuteMode
return $ Plain [Str out]
where
cmd = fromMaybe "bash" $ lookup "bashcmd" attrs
includeShellBlock x = return x
includeShellInline :: Block -> IO Block
includeShellInline (Para [Code (id, cls, attrs) code])
| "sh" `elem` cls
= do
(path, hndl) <- mkstemp "/tmp/pandoc-z-sh"
hPutStr hndl code
hClose hndl
out <- readProcess cmd [path] ""
setFileMode path ownerExecuteMode
return $ Plain [Str out]
where
cmd = fromMaybe "bash" $ lookup "bashcmd" attrs
includeShellInline x = return $ x
main :: IO ()
main = toJSONFilter (bottomUpM includeShellInline <=< includeShellBlock)