Create a gist now

Instantly share code, notes, and snippets.

Monitor a directory to rebuild pandoc files automatically.
module Main where
import Filesystem (getWorkingDirectory)
import Filesystem.Path (extensions)
import System.FSNotify
import System.Exit
import System.Environment
import System.Cmd
import Control.Concurrent
import Control.Monad
import Data.String (fromString)
main = do
cwd <- getWorkingDirectory
args <- getArgs
when (null args) $ do
putStrLn "Please give at least one filename (with no extension, .pandoc is assumed)"
let files = map (\f -> (fromString $ f ++ ".pandoc", f)) args
putStrLn "Watching current directory, press RETURN to exit."
withManager $ \man -> do
_ <- forkIO $ do
watchTree man cwd (const True) $ \fp -> do
case fp of
Removed _ _ -> return ()
Added file _ -> maybeCompile file files
Modified file _ -> maybeCompile file files
_ <- getLine
maybeCompile file files = case lookup file files of
Just f -> do
let command = "pandoc " ++ f ++ ".pandoc -N --smart --webtex -c buttondown.css -o " ++ f ++ ".html"
system command
return ()
Nothing -> return ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment