Created
January 16, 2018 14:21
-
-
Save matsubara0507/b3e0368d5fc363b02c1c06ed20d7129b to your computer and use it in GitHub Desktop.
MD -> LaTeX を監視してもらうんやー
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
-- stack --resolver lts-10.3 runghc --package text --package shelly --package fsnotify | |
{-# LANGUAGE OverloadedStrings #-} | |
module Main where | |
import Prelude hiding (FilePath) | |
import Data.Text (pack, unpack) | |
import qualified Data.Text.IO as T | |
import Filesystem.Path (basename, extension) | |
import Shelly | |
import System.Environment (getArgs) | |
import System.Exit | |
import System.FSNotify | |
import System.IO.Error | |
main :: IO () | |
main = do | |
[inDir, outDir] <- fmap pack <$> getArgs | |
withManager $ \mgr -> do | |
watchDir mgr (unpack inDir) (const True) (runPandoc' . fromText $ outDir) | |
waitBreak | |
runPandoc' :: FilePath -> Event -> IO () | |
runPandoc' outDir (Added f _) = runPandoc outDir $ fromText (pack f) | |
runPandoc' outDir (Modified f _) = runPandoc outDir $ fromText (pack f) | |
runPandoc' _ (Removed f _) = pure () | |
runPandoc :: FilePath -> FilePath -> IO () | |
runPandoc outDir input = do | |
when (extension input == Just "md") $ do | |
shelly $ run_ "pandoc" [input', "-o", output] | |
T.putStrLn $ mconcat ["build ", input', " to ", output] | |
where | |
input' = toTextArg input | |
output = toTextArg $ outDir </> basename input <.> "tex" | |
waitBreak :: IO () | |
waitBreak = do | |
_ <- catchIOError getLine (\e -> if isEOFError e then exitSuccess else exitFailure) | |
waitBreak |
treeExtExists がうまく動作しなかったり、サンプルコードの forever $ threadDelay 1000000
では動かなかったり、いまいち更新が通知されないときがあった。
たぶん Win だからかなぁ。ないし、stack runghc
してるからか。
一応、上のコードは動いた。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
参考にした
http://d.hatena.ne.jp/satosystems/20131215/1387112017