git.iko.soy/typeable/pandoc
on bf42fa1 help sign in
typeable/pandoc/Setup.hs
65 lines 2.9 kB history raw
1import Distribution.Simple
2import Control.Exception ( bracket_ )
3import Control.Monad ( unless )
4import System.Process ( runCommand, runProcess, waitForProcess )
5import System.FilePath ( (</>), (<.>) )
6import System.Directory
7import System.IO ( stderr, openTempFile )
8import System.Exit
9import System.Time
10import System.IO.Error ( isDoesNotExistError )
11import Data.Maybe ( fromJust, isNothing, catMaybes )
12import Data.List ( isInfixOf )
14main = do
15 defaultMainWithHooks $ simpleUserHooks { runTests = runTestSuite
16 , postBuild = makeManPages }
17 exitWith ExitSuccess
19-- | Run test suite.
20runTestSuite _ _ _ _ = do
21 tempPath <- catch getTemporaryDirectory (\_ -> return ".")
22 (outputPath, hOut) <- openTempFile tempPath "out"
23 runProcess "pandoc" ["--version"] Nothing Nothing Nothing (Just hOut) Nothing >>= waitForProcess
24 output <- readFile outputPath
25 let highlightingSupport = "with syntax highlighting" `isInfixOf` output
26 let testArgs = if highlightingSupport then ["lhs"] else []
27 let testCmd = "runhaskell -i.. RunTests.hs " ++ unwords testArgs
28 inDirectory "tests" $ runCommand testCmd >>= waitForProcess >>= exitWith
30-- | Build man pages from markdown sources in man/man1/.
31makeManPages _ _ _ _ = do
32 mapM_ makeManPage ["pandoc.1", "hsmarkdown.1", "html2markdown.1", "markdown2pdf.1"]
34-- | Build a man page from markdown source in man/man1.
35makeManPage manpage = do
36 let manDir = "man" </> "man1"
37 let pandoc = "dist" </> "build" </> "pandoc" </> "pandoc"
38 let page = manDir </> manpage
39 let source = manDir </> manpage <.> "md"
40 modifiedDeps <- modifiedDependencies page [source]
41 unless (null modifiedDeps) $ do
42 ec <- runProcess pandoc ["-s", "-S", "-r", "markdown", "-w", "man", "-o", page, source]
43 Nothing Nothing Nothing Nothing (Just stderr) >>= waitForProcess
44 case ec of
45 ExitSuccess -> putStrLn $ "Created " ++ manDir </> manpage
46 _ -> do putStrLn $ "Error creating " ++ manDir </> manpage
47 exitWith ec
49-- | Returns a list of 'dependencies' that have been modified after 'file'.
50modifiedDependencies :: FilePath -> [FilePath] -> IO [FilePath]
51modifiedDependencies file dependencies = do
52 fileModTime <- catch (getModificationTime file) $
53 \e -> if isDoesNotExistError e
54 then return (TOD 0 0) -- the minimum ClockTime
55 else ioError e
56 depModTimes <- mapM getModificationTime dependencies
57 let modified = zipWith (\dep time -> if time > fileModTime then Just dep else Nothing) dependencies depModTimes
58 return $ catMaybes modified
60-- | Perform an IO action in a directory.
61inDirectory :: FilePath -> IO a -> IO a
62inDirectory dir action = do
63 oldDir <- getCurrentDirectory
64 bracket_ (setCurrentDirectory dir) (setCurrentDirectory oldDir) action