her.esy.fun/Shakefile.hs

135 lines
4.7 KiB
Haskell
Raw Normal View History

2020-05-25 20:30:22 +00:00
{-# LANGUAGE OverloadedStrings #-}
2020-06-22 07:39:44 +00:00
{-# LANGUAGE NoImplicitPrelude #-}
2020-06-22 09:44:11 +00:00
import Protolude
2020-05-25 20:30:22 +00:00
2020-06-22 09:44:11 +00:00
import Development.Shake
import Development.Shake.Command
import Development.Shake.FilePath
import Development.Shake.Util
2020-06-14 11:19:13 +00:00
2020-06-22 09:44:11 +00:00
import Control.Monad.Fail
import Data.Default ( Default(def) )
import qualified Data.Set as Set
import qualified Data.Text as T
import Text.Pandoc.Class ( PandocPure
, PandocMonad
)
import qualified Text.Pandoc.Class as Pandoc
import Text.Pandoc.Definition ( Pandoc(..)
, Block(..)
, Inline
, nullMeta
, docTitle
, docDate
, docAuthors
)
import Text.Pandoc.Extensions ( getDefaultExtensions )
import Text.Pandoc.Options ( ReaderOptions(..)
, TrackChanges(RejectChanges)
)
import qualified Text.Pandoc.Readers as Readers
import qualified Text.Pandoc.Writers as Writers
2020-05-25 20:28:06 +00:00
main :: IO ()
2020-06-22 10:08:07 +00:00
main = shakeArgs shOpts buildRules
where
shOpts =
shakeOptions
{ shakeVerbosity = Chatty
, shakeLintInside = ["\\"]
}
-- Configuration
-- Should probably go in a Reader Monad
siteDir :: FilePath
siteDir = "_site"
optimDir :: FilePath
optimDir = "_optim"
-- BlogPost data structure (a bit of duplication because the metas are in Pandoc)
2020-05-25 20:28:06 +00:00
2020-06-22 07:39:44 +00:00
data BlogPost =
BlogPost { postTitle :: T.Text
, postDate :: T.Text
, postAuthors :: [T.Text]
, postUrl :: FilePath
2020-06-22 09:44:11 +00:00
, postBody :: Pandoc
2020-06-22 07:39:44 +00:00
}
inlineToText :: PandocMonad m => [Inline] -> m T.Text
inlineToText inline =
2020-06-22 09:44:11 +00:00
Writers.writeAsciiDoc def (Pandoc nullMeta [Plain inline])
getBlogpostFromMetas
:: (MonadIO m, MonadFail m) => [Char] -> Pandoc -> m BlogPost
getBlogpostFromMetas path pandoc@(Pandoc meta _) = do
eitherBlogpost <- liftIO $ Pandoc.runIO $ do
title <- inlineToText $ docTitle meta
date <- inlineToText $ docDate meta
authors <- mapM inlineToText $ docAuthors meta
-- let url = dropExtension path
return $ BlogPost title date authors path pandoc
case eitherBlogpost of
Left _ -> fail "BAD"
Right bp -> return bp
sortByPostDate :: [BlogPost] -> [BlogPost]
sortByPostDate =
2020-06-22 10:08:07 +00:00
sortBy (\b a -> compare (Down (postDate a)) (Down (postDate b)))
2020-06-22 07:39:44 +00:00
2020-06-22 10:27:45 +00:00
build = (</>) siteDir
2020-06-14 11:19:13 +00:00
buildRules :: Rules ()
2020-05-25 20:28:06 +00:00
buildRules = do
2020-06-22 10:08:07 +00:00
cleanRule
2020-06-22 10:27:45 +00:00
allRule
2020-06-22 10:08:07 +00:00
getPost <- mkGetPost
getPosts <- mkGetPosts getPost
let cssDeps = map (siteDir </>) <$> getDirectoryFiles "" ["src/css/*.css"]
2020-06-22 10:27:45 +00:00
build "articles.html" %> \out -> do
2020-06-22 09:44:11 +00:00
css <- cssDeps
posts <- getPosts ()
2020-06-22 10:08:07 +00:00
need $ css <> map postUrl (sortByPostDate posts)
let titles = T.unpack $ T.intercalate "\n" $ map postTitle posts
writeFile' out titles
2020-06-22 10:27:45 +00:00
build "index.html" %> \out -> do
css <- cssDeps
need $ css <> ["src/index.org"]
bp <- getPost "src/index.org"
eitherHtml <- liftIO $ Pandoc.runIO $ Writers.writeHtml5String def (postBody bp)
case eitherHtml of
Left _ -> fail "BAD"
Right htmlFile -> writeFile' out (T.unpack htmlFile)
2020-06-22 10:08:07 +00:00
-- build "//*.html" %> \out -> do
-- css <- cssDeps
-- let orgfile = dropDirectory1 out
-- post <- getPost orgfile
2020-06-22 09:44:11 +00:00
build "src/css/*.css" %> \out -> copyFile' (dropDirectory1 out) out
2020-06-22 07:39:44 +00:00
2020-06-22 10:27:45 +00:00
allRule :: Rules ()
allRule =
phony "all" $
need (map build [ "index.html"
, "articles.html"])
2020-06-22 10:08:07 +00:00
cleanRule :: Rules ()
cleanRule =
phony "clean" $ do
putInfo "Cleaning files in _site and _optim"
forM_ [siteDir,optimDir] $ flip removeFilesAfter ["//*"]
mkGetPost = newCache $ \path -> do
fileContent <- readFile' path
eitherResult <- liftIO $ Pandoc.runIO $ Readers.readOrg def (T.pack fileContent)
case eitherResult of
Left _ -> fail "BAD"
Right pandoc -> getBlogpostFromMetas path pandoc
2020-06-14 11:19:13 +00:00
2020-06-22 10:08:07 +00:00
mkGetPosts getPost =
newCache $ \() -> mapM getPost =<< getDirectoryFiles "" ["src/posts//*.org"]