her.esy.fun/Shakefile.hs

172 lines
6.3 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
2020-06-22 21:01:47 +00:00
import Data.Aeson
import Text.Megaparsec
import Data.Default ( Default(def) )
import qualified Data.Set as Set
import qualified Data.Text as T
import Text.Mustache
import Text.Pandoc.Class ( PandocPure, PandocMonad)
import qualified Text.Pandoc.Class as Pandoc
import Text.Pandoc.Definition ( Pandoc(..)
, Block(..)
, Inline
, nullMeta
, docTitle
2020-06-23 06:44:02 +00:00
, lookupMeta
2020-06-22 21:01:47 +00:00
, docDate
, docAuthors
)
import Text.Pandoc.Extensions ( getDefaultExtensions )
import Text.Pandoc.Options ( ReaderOptions(..)
2020-06-23 07:15:03 +00:00
, WriterOptions(..)
, TrackChanges(RejectChanges)
)
2020-06-22 21:01:47 +00:00
import qualified Text.Pandoc.Readers as Readers
import qualified Text.Pandoc.Templates as PandocTemplates
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-23 07:15:03 +00:00
, postToc :: Bool
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
2020-06-23 07:15:03 +00:00
:: (MonadIO m, MonadFail m) => [Char] -> Bool -> Pandoc -> m BlogPost
getBlogpostFromMetas path toc pandoc@(Pandoc meta _) = do
2020-06-22 09:44:11 +00:00
eitherBlogpost <- liftIO $ Pandoc.runIO $ do
title <- inlineToText $ docTitle meta
date <- inlineToText $ docDate meta
authors <- mapM inlineToText $ docAuthors meta
2020-06-23 07:15:03 +00:00
return $ BlogPost title date authors path toc pandoc
2020-06-22 09:44:11 +00:00
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
2020-06-22 20:13:12 +00:00
build :: FilePath -> FilePath
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
2020-06-22 21:01:47 +00:00
getTemplate <- mkGetTemplate
2020-06-22 20:13:12 +00:00
let cssDeps = map (siteDir </>) <$> getDirectoryFiles "src" ["css/*.css"]
2020-06-22 21:01:47 +00:00
-- templateDeps = getDirectoryFiles "templates" ["*.mustache"]
2020-06-22 20:13:12 +00:00
build "//*.html" %> \out -> do
css <- cssDeps
2020-06-22 21:01:47 +00:00
-- templates <- templateDeps
template <- getTemplate ("templates" </> "main.mustache")
2020-06-22 20:13:12 +00:00
let srcFile = "src" </> (dropDirectory1 (replaceExtension out "org"))
2020-06-22 21:01:47 +00:00
liftIO $ putText $ "need: " <> (toS srcFile) <> " <- " <> (toS out)
2020-06-22 20:13:12 +00:00
need $ css <> [srcFile]
bp <- getPost srcFile
2020-06-23 07:15:03 +00:00
eitherHtml <- liftIO $ Pandoc.runIO $ Writers.writeHtml5String (def { writerTableOfContents = (postToc bp) }) (postBody bp)
2020-06-22 20:13:12 +00:00
case eitherHtml of
Left _ -> fail "BAD"
2020-06-22 21:01:47 +00:00
Right innerHtml ->
let htmlContent = renderMustache template $ object [ "title" .= postTitle bp
, "authors" .= postAuthors bp
, "date" .= postDate bp
, "body" .= innerHtml
]
in writeFile' out (toS htmlContent)
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)
2020-06-22 21:01:47 +00:00
let titles = toS $ T.intercalate "\n" $ map postTitle posts
2020-06-22 10:08:07 +00:00
writeFile' out titles
2020-06-22 20:13:12 +00:00
build "css/*.css" %> \out -> do
let src = "src" </> (dropDirectory1 out)
dst = out
2020-06-22 21:01:47 +00:00
liftIO $ putText $ toS $ "src:" <> src <> " => dst: " <> dst
2020-06-22 20:13:12 +00:00
copyFile' src dst
2020-06-22 10:27:45 +00:00
allRule :: Rules ()
allRule =
2020-06-22 20:13:12 +00:00
phony "all" $ do
allOrgFiles <- getDirectoryFiles "src" ["//*.org"]
let allHtmlFiles = map (flip replaceExtension "html") allOrgFiles
need (map build (allHtmlFiles <> ["index.html", "articles.html"]))
2020-06-22 10:27:45 +00:00
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 ["//*"]
2020-06-22 21:01:47 +00:00
mkGetTemplate :: Rules (FilePath -> Action Template)
mkGetTemplate = newCache $ \path -> do
fileContent <- readFile' path
let res = compileMustacheText "page" (toS fileContent)
case res of
Left _ -> fail "BAD"
Right template -> return template
2020-06-23 07:15:03 +00:00
tocRequested :: Text -> Bool
tocRequested fc =
let toc = fc & T.lines
& map T.toLower
& filter (T.isPrefixOf (T.pack "#+options: "))
& head
& fmap (filter (T.isPrefixOf (T.pack "toc:")) . T.words)
in toc == Just ["toc:t"]
2020-06-23 06:44:02 +00:00
2020-06-22 20:13:12 +00:00
mkGetPost :: Rules (FilePath -> Action BlogPost)
2020-06-22 10:08:07 +00:00
mkGetPost = newCache $ \path -> do
fileContent <- readFile' path
2020-06-23 07:15:03 +00:00
let toc = tocRequested (toS fileContent)
2020-06-22 21:01:47 +00:00
eitherResult <- liftIO $ Pandoc.runIO $ Readers.readOrg def (toS fileContent)
2020-06-22 10:08:07 +00:00
case eitherResult of
Left _ -> fail "BAD"
2020-06-23 07:15:03 +00:00
Right pandoc -> getBlogpostFromMetas path toc pandoc
2020-06-14 11:19:13 +00:00
2020-06-22 20:13:12 +00:00
mkGetPosts :: (FilePath -> Action b) -> Rules (() -> Action [b])
2020-06-22 10:08:07 +00:00
mkGetPosts getPost =
newCache $ \() -> mapM getPost =<< getDirectoryFiles "" ["src/posts//*.org"]