mad/byg/src/SiteGenerator.hs

125 lines
4.2 KiB
Haskell
Raw Normal View History

2024-09-23 22:11:54 +02:00
module SiteGenerator (generateSite) where
import Types (Token(..))
2024-09-23 22:11:54 +02:00
import DependencyGenerator
import Functions
2024-09-23 22:11:54 +02:00
2024-10-15 23:32:15 +02:00
import Data.Ord (comparing)
import Data.List (sortBy)
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-15 23:32:15 +02:00
handleRecipeDir :: Token FilePath -> Token FilePath
-> Token Template -> Token FilePath
-> Token FilePath -> DepGenM (Token ((Text, Date), FilePath))
handleRecipeDir recipesDir outputDir htmlTemplate indexName name = do
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
pandoc' <- injectAfterTitle (onTupleToken T.append
(inject "Offentliggjort ")
(onToken formatDate date))
pandoc
html <-
2024-10-15 23:32:15 +02:00
pandoc'
& writeHtml
& 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-15 23:32:15 +02:00
& filterDepGenM (isDirectory <=< joinPaths recipesDir)
infos <- mapDepGenM (handleRecipeDir recipesDir outputDir htmlTemplate indexName) recipeSubDirs
allRecipesHtml <-
infos
& onToken (T.append "# Alle retter\n\n"
. T.intercalate "\n"
2024-10-15 23:32:15 +02:00
. map (\((t, d), u) -> T.concat ["- ", "[", t, " *(", formatDateShort d, ")*](/", T.pack u, ")"])
. sortBy (flip (comparing (\((_, d), _) -> d))))
& markdownToHtml
& applyTemplate htmlTemplate
saveTextFile allRecipesHtml (joinPaths outputRecipesDir 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
forM_ ["mad-icon.png", "mad-logo.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
2024-10-05 22:51:13 +02:00
mapDepGenM_ (handleFontDir outputDir) fontSubDirs