-
Notifications
You must be signed in to change notification settings - Fork 1
/
Shakefile.hs
executable file
·80 lines (62 loc) · 3.39 KB
/
Shakefile.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env stack runhaskell --
import System.Directory hiding (doesDirectoryExist, getDirectoryContents)
import System.FilePath
import Control.Monad.Extra
import Development.Shake
import System.IO
outputDirectory :: FilePath
outputDirectory = "docs"
scriptOutputDirectory :: FilePath
scriptOutputDirectory = outputDirectory </> "script"
slidesOutputDirectory :: FilePath
slidesOutputDirectory = outputDirectory </> "slides"
revealResourcesDirectory :: FilePath
revealResourcesDirectory = slidesOutputDirectory </> "reveal.js"
copyFromReveal :: FilePath -> Action ()
copyFromReveal out = do
need [originalFile]
liftIO $ copyFile originalFile out
where
originalFile = joinPath $ dropWhile (/= "reveal.js/") $ splitPath $ slidesOutputDirectory `makeRelative` out
needRstSources :: Action ()
needRstSources = getDirectoryFiles "script/source" ["*.rst", "exercises/*.rst"] >>= need . map ("script/source" </>)
buildSlideWithTheme :: String -> FilePath -> Action ()
buildSlideWithTheme theme out = do
need [sourceFile, templateFile]
-- getDirectoryFiles "slides/img" ["*.png"] >>= need . map ((slidesOutputDirectory </> "img") </>)
cmd "pandoc" [sourceFile, "--slide-level", "2", "-o", out, "-t", "revealjs", "-s", "--variable=theme:" ++ theme, "--template", templateFile, "--variable=revealjs-url:" ++ revealPath]
where
sourceFile = "slides" </> takeFileName out -<.> ".md"
templateFile = "slides/template.html"
revealPath = (++ "reveal.js") $ joinPath $ map (const "../") $ filter (/= ".") $ splitPath $ takeDirectory $ makeRelative slidesOutputDirectory out
main :: IO ()
main = shakeArgs shakeOptions $ do
want ["script", "slides", outputDirectory </> ".nojekyll"]
phony "script" $ need [scriptOutputDirectory </> "index.html", outputDirectory </> "script.pdf"]
phony "slides" $ do
slideMds <- getDirectoryFiles "slides" ["*.md"]
let htmls = map (-<.> "html") slideMds
need $ map (slidesOutputDirectory </>) htmls
phony "clean" $ do
removeFilesAfter outputDirectory ["*.pdf"]
removeFilesAfter slidesOutputDirectory ["*.html"]
whenM (doesDirectoryExist scriptOutputDirectory) $
liftIO $ removeDirectoryRecursive scriptOutputDirectory
mapM_ (liftIO . removeDirectoryRecursive) =<< filterM doesDirectoryExist =<< map (slidesOutputDirectory </>) . filter (/= "reveal.js") <$> getDirectoryContents slidesOutputDirectory
phony "script-html" $ need [scriptOutputDirectory </> "index.html"]
"**/.nojekyll" %> \out -> liftIO $ withFile out AppendMode (`hPutChar` ' ')
(scriptOutputDirectory </> "index.html") %> \out' -> do
let out = takeDirectory out'
needRstSources
unit $ cmd "make" ["html"] (Cwd "script")
liftIO $ removeDirectoryRecursive out
liftIO $ renameDirectory "script/build/html" out
(outputDirectory </> "script.pdf") %> \out -> do
needRstSources
unit $ cmd "make" ["latexpdf"] (Cwd "script")
liftIO $ renameFile "script/build/latex/HaskellLessons.pdf" out
(slidesOutputDirectory </> "img" </> "*") %> liftIO . (copyFile <$> ("slides/img" </>) . takeFileName <*> id)
(slidesOutputDirectory </> "*.html") %> buildSlideWithTheme "white"
(slidesOutputDirectory </> "*/*.html") %> \out ->
let theme = takeBaseName $ takeDirectory $ makeRelative slidesOutputDirectory out
in buildSlideWithTheme theme out