Initial
This commit is contained in:
commit
61675c9d29
50 files changed
+2814
No files matched your search
@@ -0,0 +1,145 @@
|
||||
module SiteGenerator (generateSite) where
|
||||
|
||||
import Byg.Types (Token(..), Date(..), formatDate, formatDateShort)
|
||||
import Byg.DependencyGenerator
|
||||
import Byg.Functions
|
||||
|
||||
import Data.List (sort, elemIndex)
|
||||
import Data.Function ((&))
|
||||
import Data.Text (Text)
|
||||
import qualified Data.Text as T
|
||||
import Control.Monad (forM_)
|
||||
|
||||
elemIndex' :: (Show a, Eq a) => a -> [a] -> Int
|
||||
elemIndex' x xs = case elemIndex x xs of
|
||||
Just i -> i
|
||||
Nothing -> error ("unexpected unknown directory index for " ++ show x ++ " in " ++ show xs)
|
||||
|
||||
head' :: [a] -> a
|
||||
head' [] = error "error"
|
||||
head' (x : _) = x
|
||||
|
||||
removeDotMd :: FilePath -> FilePath
|
||||
removeDotMd s = take (length s - 3) s
|
||||
|
||||
handleRecipeDir :: Token FilePath -> Token FilePath
|
||||
-> Token Template -> Token FilePath
|
||||
-> Token [FilePath] -> Token FilePath
|
||||
-> DepGenM (Token ((Text, Date), FilePath))
|
||||
handleRecipeDir recipesDir outputDir htmlTemplate indexName recipeSubDirs name = do
|
||||
filename <- joinPaths recipesDir name
|
||||
dir <- onToken removeDotMd name
|
||||
recipeDirOut <- joinPaths outputDir dir
|
||||
makeDir recipeDirOut
|
||||
pandoc <- readMarkdown (readTextFile filename)
|
||||
title <- extractTitle pandoc
|
||||
date <- extractDate name
|
||||
pandoc' <- injectAfterTitle (
|
||||
(onToken T.concat [ inject "<p class=\"date\">Published: "
|
||||
, onToken formatDate date
|
||||
, inject "</p>"
|
||||
]))
|
||||
pandoc
|
||||
dirIndex <- onTupleToken elemIndex' name recipeSubDirs
|
||||
prev <- onTupleToken (
|
||||
\ds i -> if i < length ds - 1
|
||||
then T.concat [ "<a class=\"prev\" href=\"/chapters/"
|
||||
, T.pack (removeDotMd (ds !! (i + 1)))
|
||||
, "/\">Previous</a>"
|
||||
]
|
||||
else "")
|
||||
recipeSubDirs dirIndex
|
||||
next <- onTupleToken (
|
||||
\ds i -> if i > 0
|
||||
then T.concat [ "<a class=\"next\" href=\"/chapters/"
|
||||
, T.pack (removeDotMd (ds !! (i - 1)))
|
||||
, "/\">Next</a>"
|
||||
]
|
||||
else "")
|
||||
recipeSubDirs dirIndex
|
||||
prevnext <- onToken T.concat [ inject "<p class=\"prevnext\">", pure prev, pure next, inject "</p>" ]
|
||||
html <-
|
||||
pandoc'
|
||||
& writeHtml
|
||||
& onTupleToken T.append prevnext
|
||||
& onTupleToken (flip T.append) prevnext
|
||||
& applyTemplate htmlTemplate
|
||||
saveTextFile html (joinPaths recipeDirOut indexName)
|
||||
pure $ TupleToken (TupleToken title date) dir
|
||||
|
||||
handleFontDir :: Token FilePath -> Token FilePath -> DepGenM ()
|
||||
handleFontDir outputDir dir = do
|
||||
makeDir $ joinPaths outputDir dir
|
||||
paths <-
|
||||
listDirectory dir
|
||||
& mapDepGenM (joinPaths dir)
|
||||
& filterDepGenM (hasExtension (inject ["woff2", "css"]))
|
||||
mapDepGenM_ (`copyTo` outputDir) paths
|
||||
|
||||
generateSite :: DepGenM ()
|
||||
generateSite = do
|
||||
outputDir <- inject "site"
|
||||
makeDir outputDir
|
||||
htmlTemplate <- makeTemplate (readTextFile (inject "template.html")) (inject "CONTENT")
|
||||
indexName <- inject "index.html"
|
||||
|
||||
-- Handle recipes
|
||||
recipesDir <- inject "chapters"
|
||||
outputRecipesDir <- joinPaths outputDir recipesDir
|
||||
makeDir $ outputRecipesDir
|
||||
recipeSubDirs <-
|
||||
listDirectory recipesDir
|
||||
& onToken (reverse . sort)
|
||||
infos <- mapDepGenM (handleRecipeDir recipesDir outputRecipesDir htmlTemplate indexName recipeSubDirs) recipeSubDirs
|
||||
allRecipesHtml <-
|
||||
infos
|
||||
& onToken (T.append "# All chapters\n\n"
|
||||
. T.intercalate "\n"
|
||||
. (\case
|
||||
[] -> ["(No chapters yet.)"]
|
||||
infos' -> map (\((t, d), u) -> T.concat ["- ", "[", t, " *(", formatDateShort d, ")*](/chapters/", T.pack u, "/)"]) infos'))
|
||||
& markdownToHtml
|
||||
& applyTemplate htmlTemplate
|
||||
saveTextFile allRecipesHtml (joinPaths outputRecipesDir indexName)
|
||||
|
||||
newest <- onToken (\case
|
||||
[] -> Nothing
|
||||
infos' -> Just ((snd . fst . head') infos')) infos
|
||||
atomText <- generateAtom newest infos
|
||||
saveTextFile atomText (joinPaths outputDir (inject "atom.xml"))
|
||||
|
||||
redirectTemplate <- makeTemplate (readTextFile (inject "redirect.html")) (inject "URL")
|
||||
newestSlug <- onToken (\case
|
||||
[] -> "about/"
|
||||
infos' -> (T.pack . (++ "/") . ("chapters/" ++) . snd . head') infos')
|
||||
infos
|
||||
saveTextFile (applyTemplate redirectTemplate newestSlug) (joinPaths outputDir indexName)
|
||||
|
||||
-- Handle about page
|
||||
outputAboutDir <- joinPaths outputDir (inject "about")
|
||||
makeDir outputAboutDir
|
||||
aboutHtml <-
|
||||
inject "about.md"
|
||||
& readTextFile
|
||||
& markdownToHtml
|
||||
& applyTemplate htmlTemplate
|
||||
saveTextFile aboutHtml (joinPaths outputAboutDir indexName)
|
||||
|
||||
-- Handle style
|
||||
inject "style.css" `copyTo` outputDir
|
||||
|
||||
-- Handle images
|
||||
imgDir <- inject "img"
|
||||
makeDir $ joinPaths outputDir imgDir
|
||||
forM_ ["neelix-icon.png", "neelix-logo.png", "julekalender.png"] $ \name ->
|
||||
joinPaths imgDir (inject name) `copyTo` outputDir
|
||||
|
||||
-- Handle fonts
|
||||
fontsDir <- inject "fonts"
|
||||
makeDir $ joinPaths outputDir fontsDir
|
||||
fontSubDirs <-
|
||||
fontsDir
|
||||
& listDirectory
|
||||
& mapDepGenM (joinPaths fontsDir)
|
||||
& filterDepGenM isDirectory
|
||||
mapDepGenM_ (handleFontDir outputDir) fontSubDirs
|
||||
Reference in new issue
Block a user