Last active
August 29, 2015 14:24
-
-
Save JustusAdam/a5eac223291777f51f29 to your computer and use it in GitHub Desktop.
Haskell watcher for compiling markdown to reveljs scripts and tex automatically.
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
{-# LANGUAGE OverloadedStrings #-} | |
{-# LANGUAGE UnicodeSyntax #-} | |
import Control.Concurrent (threadDelay) | |
import Control.Monad (forever) | |
import Filesystem | |
import Filesystem.Path.CurrentOS | |
import Prelude hiding (FilePath) | |
import Prelude.Unicode | |
import System.Environment | |
import System.FSNotify | |
import System.Process | |
import Text.Printf | |
sourceDir = "md/slides" | |
targetHtmlDir = "rbuild" | |
targetTexDir = "latex/slides" | |
fileFilter = flip hasExtension "md" | |
revealCommand = printf "pandoc -t revealjs %s -o %s --no-highlight --template md/template.html -s" | |
toRevealName file = targetHtmlDir </> addExtension (basename file) "html" | |
texCommand = printf "pandoc -t beamer %s -o %s" | |
toTexName file = targetTexDir </> addExtension (basename file) "tex" | |
eventHandler ∷ Event → IO () | |
eventHandler e = | |
case e of | |
(Added file _) → build file | |
(Modified file _) → build file | |
_ → return () | |
build ∷ FilePath → IO () | |
build file = do | |
putStrLn $ printf "compiling $ html '%s' → '%s'" fileName revealName | |
callCommand $ revealCommand fileName revealName | |
putStrLn $ printf "compiling $ tex '%s' → '%s'" fileName texName | |
callCommand $ texCommand fileName texName | |
where | |
fileName = encodeString file | |
revealName = encodeString $ toRevealName file | |
texName = encodeString $ toTexName file | |
main ∷ IO () | |
main = do | |
args ← getArgs | |
case args of | |
[] → watch | |
["watch"] → watch | |
["compile"] → compile | |
[_] → error "unknown command line argument" | |
_ → error "too many command line arguments (expected no more than one)" | |
where | |
compile = do | |
allFiles <- listDirectory sourceDir | |
mapM_ build $ filter fileFilter allFiles | |
watch = do | |
compile | |
withManager $ \mgr → do | |
watchDir mgr sourceDir (fileFilter ∘ eventPath) eventHandler | |
forever $ threadDelay maxBound |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment