mad/byg/app/SiteGenerator.hs

164 lines
5.8 KiB
Haskell
Raw Normal View History

2024-09-23 22:11:54 +02:00
module SiteGenerator (generateSite) where
import Byg.Types (Token(..), Date(..), formatDate, formatDateShort)
import Byg.DependencyGenerator
import Byg.Functions
2024-09-23 22:11:54 +02:00
2024-10-16 22:09:51 +02:00
import Data.List (sort, elemIndex)
import Data.Function ((&))
2024-10-11 22:41:31 +02:00
import Data.Text (Text)
2024-10-14 23:33:05 +02:00
import qualified Data.Text as T
2024-10-15 23:32:15 +02:00
import Control.Monad (forM_, (<=<))
2024-10-16 22:09:51 +02:00
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)
2024-10-20 15:35:24 +02:00
head' :: [a] -> a
head' [] = error "error"
head' (x : _) = x
2024-10-15 23:32:15 +02:00
handleRecipeDir :: Token FilePath -> Token FilePath
-> Token Template -> Token FilePath
2024-10-16 22:09:51 +02:00
-> Token [FilePath] -> Token FilePath
-> DepGenM (Token ((Text, Date), FilePath))
handleRecipeDir recipesDir outputDir htmlTemplate indexName recipeSubDirs name = do
2024-10-15 23:32:15 +02:00
dir <- joinPaths recipesDir name
recipeDirOut <- joinPaths outputDir dir
2024-09-26 00:09:48 +02:00
makeDir recipeDirOut
imageFilenames <-
listDirectory dir
& filterDepGenM (hasExtension (inject ["jpg"]))
2024-10-15 23:32:15 +02:00
htmlBodyImages <- forDepGenM imageFilenames $ \filename -> do
path <- joinPaths dir filename
2024-10-05 22:51:13 +02:00
path `copyTo` outputDir
2024-10-15 23:32:15 +02:00
(base, ext) <- untupleDepGenM $ fileComponents filename
2024-10-14 23:33:05 +02:00
thumbnailName <- onToken concat [ pure base, inject "-thumbnail.", pure ext ]
2024-10-05 23:37:54 +02:00
imageResized <- convertImage (openImage path) (inject (ResizeToWidth 800))
saveImage imageResized (joinPaths recipeDirOut thumbnailName)
2024-10-14 23:33:05 +02:00
onToken T.concat [ inject "<p class=\"image\"><a href=\""
2024-10-15 23:32:15 +02:00
, onToken T.pack filename
2024-10-14 23:33:05 +02:00
, inject "\"><img src=\""
, onToken T.pack thumbnailName
, inject "\"></a></p>\n"
]
2024-10-05 19:15:20 +02:00
ingredienserHeadline <- inject "## Ingredienser"
2024-10-05 22:51:13 +02:00
mdTemplate <- makeTemplate
(readTextFile (joinPaths dir (inject "ret.md")))
ingredienserHeadline
pandoc <-
[ inject "<p class=\"imagetext\">Opskrift fortsætter efter billedet.</p>\n"
, onToken T.concat htmlBodyImages
, inject "\n"
, pure ingredienserHeadline
]
& onToken T.concat
& applyTemplate mdTemplate
& readMarkdown
2024-10-11 22:41:31 +02:00
title <- extractTitle pandoc
2024-10-15 23:32:15 +02:00
date <- extractDate name
2024-10-16 22:09:51 +02:00
pandoc' <- injectAfterTitle (
(onToken T.concat [ inject "<p class=\"date\">Offentliggjort "
, onToken formatDate date
, inject "</p>"
]))
2024-10-15 23:32:15 +02:00
pandoc
2024-10-16 22:09:51 +02:00
dirIndex <- onTupleToken elemIndex' name recipeSubDirs
prev <- onTupleToken (
\ds i -> if i < length ds - 1
then T.concat [ "<a class=\"prev\" href=\"/retter/"
, T.pack (ds !! (i + 1))
, "\">Forrige</a>"
]
else "")
recipeSubDirs dirIndex
next <- onTupleToken (
\ds i -> if i > 0
then T.concat [ "<a class=\"next\" href=\"/retter/"
, T.pack (ds !! (i - 1))
, "\">Næste</a>"
]
else "")
recipeSubDirs dirIndex
prevnext <- onToken T.concat [ inject "<p class=\"prevnext\">", pure prev, pure next, inject "</p>" ]
html <-
2024-10-15 23:32:15 +02:00
pandoc'
& writeHtml
2024-10-16 22:09:51 +02:00
& onTupleToken T.append prevnext
& onTupleToken (flip T.append) prevnext
& applyTemplate htmlTemplate
2024-10-05 22:51:13 +02:00
saveTextFile html (joinPaths recipeDirOut indexName)
2024-10-15 23:32:15 +02:00
pure $ TupleToken (TupleToken title date) dir
2024-10-05 22:51:13 +02:00
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"]))
2024-10-05 22:51:13 +02:00
mapDepGenM_ (`copyTo` outputDir) paths
2024-09-23 22:11:54 +02:00
2024-09-24 23:09:35 +02:00
generateSite :: DepGenM ()
2024-09-23 22:11:54 +02:00
generateSite = do
2024-09-24 22:38:52 +02:00
outputDir <- inject "site"
makeDir outputDir
2024-10-05 22:51:13 +02:00
htmlTemplate <- makeTemplate (readTextFile (inject "template.html")) (inject "CONTENT")
indexName <- inject "index.html"
-- Handle recipes
2024-09-25 23:51:33 +02:00
recipesDir <- inject "retter"
outputRecipesDir <- joinPaths outputDir recipesDir
makeDir $ outputRecipesDir
recipeSubDirs <-
listDirectory recipesDir
2024-10-16 22:09:51 +02:00
& onToken (reverse . sort)
2024-10-15 23:32:15 +02:00
& filterDepGenM (isDirectory <=< joinPaths recipesDir)
2024-10-16 22:09:51 +02:00
infos <- mapDepGenM (handleRecipeDir recipesDir outputDir htmlTemplate indexName recipeSubDirs) recipeSubDirs
allRecipesHtml <-
infos
& onToken (T.append "# Alle retter\n\n"
. T.intercalate "\n"
2024-10-16 22:09:51 +02:00
. map (\((t, d), u) -> T.concat ["- ", "[", t, " *(", formatDateShort d, ")*](/", T.pack u, ")"]))
& markdownToHtml
& applyTemplate htmlTemplate
saveTextFile allRecipesHtml (joinPaths outputRecipesDir indexName)
2024-10-20 15:35:24 +02:00
newest <- onToken (snd . fst . head') infos
atomText <- generateAtom newest infos
saveTextFile atomText (joinPaths outputDir (inject "atom.xml"))
redirectTemplate <- makeTemplate (readTextFile (inject "redirect.html")) (inject "URL")
newestSlug <- onToken (T.pack . snd . head') infos
saveTextFile (applyTemplate redirectTemplate newestSlug) (joinPaths outputDir indexName)
2024-10-05 22:51:13 +02:00
-- Handle about page
outputAboutDir <- joinPaths outputDir (inject "om")
makeDir outputAboutDir
aboutHtml <-
inject "om.md"
& readTextFile
& markdownToHtml
& applyTemplate htmlTemplate
2024-10-05 22:51:13 +02:00
saveTextFile aboutHtml (joinPaths outputAboutDir indexName)
-- Handle style
inject "style.css" `copyTo` outputDir
-- Handle images
imgDir <- inject "img"
makeDir $ joinPaths outputDir imgDir
2024-11-10 12:54:47 +01:00
forM_ ["mad-icon.png", "mad-logo.png", "julekalender.png", "platte.jpg"] $ \name ->
2024-10-05 22:51:13 +02:00
joinPaths imgDir (inject name) `copyTo` outputDir
-- Handle fonts
fontsDir <- inject "fonts"
makeDir $ joinPaths outputDir fontsDir
fontSubDirs <-
fontsDir
& listDirectory
& mapDepGenM (joinPaths fontsDir)
& filterDepGenM isDirectory
2024-10-05 22:51:13 +02:00
mapDepGenM_ (handleFontDir outputDir) fontSubDirs