Skip to content

Instantly share code, notes, and snippets.

@matsubara0507
Created January 16, 2018 14:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matsubara0507/b3e0368d5fc363b02c1c06ed20d7129b to your computer and use it in GitHub Desktop.
Save matsubara0507/b3e0368d5fc363b02c1c06ed20d7129b to your computer and use it in GitHub Desktop.
MD -> LaTeX を監視してもらうんやー
-- 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
@matsubara0507
Copy link
Author

@matsubara0507
Copy link
Author

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