mad/byg/src/SiteGenerator.hs

102 lines
3.9 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-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-05 15:58:55 +02:00
import Control.Monad (forM_)
handleRecipeDir :: Token FilePath -> Token Template -> Token FilePath -> Token FilePath -> DepGenM (Token (Text, FilePath))
2024-10-05 22:51:13 +02:00
handleRecipeDir outputDir htmlTemplate indexName dir = do
recipeDirOut <- joinPaths outputDir dir
2024-09-26 00:09:48 +02:00
makeDir recipeDirOut
2024-10-05 22:51:13 +02:00
imageFilenames <- filterDepGenM (hasExtension (inject ["jpg"]))
$ listDirectory dir
htmlBodyImages <- forDepGenM imageFilenames $ \name -> do
path <- joinPaths dir name
path `copyTo` outputDir
(base, ext) <- untupleDepGenM $ fileComponents name
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=\""
, onToken T.pack name
, 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
2024-10-11 22:41:31 +02:00
pandoc <- readMarkdown
$ applyTemplate mdTemplate
2024-10-14 23:33:05 +02:00
$ onToken T.concat [ inject "<p class=\"imagetext\">Opskrift fortsætter efter billedet.</p>\n"
, onToken T.concat htmlBodyImages
, inject "\n"
, pure ingredienserHeadline
]
2024-10-11 22:41:31 +02:00
title <- extractTitle pandoc
2024-10-05 22:51:13 +02:00
html <- applyTemplate htmlTemplate
2024-10-11 22:41:31 +02:00
$ writeHtml pandoc
2024-10-05 22:51:13 +02:00
saveTextFile html (joinPaths recipeDirOut indexName)
pure $ TupleToken title dir
2024-10-05 22:51:13 +02:00
handleFontDir :: Token FilePath -> Token FilePath -> DepGenM ()
handleFontDir outputDir dir = do
makeDir $ joinPaths outputDir dir
paths <- filterDepGenM (hasExtension (inject ["woff2", "css"]))
$ mapDepGenM (joinPaths dir)
$ listDirectory dir
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
2024-10-05 22:51:13 +02:00
recipeSubDirs <- filterDepGenM isDirectory
$ mapDepGenM (joinPaths recipesDir)
$ listDirectory recipesDir
infos <- mapDepGenM (handleRecipeDir outputDir htmlTemplate indexName) recipeSubDirs
allRecipesHtml <- applyTemplate htmlTemplate
$ writeHtml
$ readMarkdown
$ onToken (T.append "# Alle retter\n\n" . T.intercalate "\n" . map (\(t, u) -> T.concat ["- ", "[", t, "](/", T.pack u, ")"])) infos
saveTextFile allRecipesHtml (joinPaths outputRecipesDir indexName)
2024-10-05 22:51:13 +02:00
-- Handle about page
outputAboutDir <- joinPaths outputDir (inject "om")
makeDir outputAboutDir
aboutHtml <- applyTemplate htmlTemplate
$ writeHtml
$ readMarkdown
$ readTextFile
$ inject "om.md"
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 <- filterDepGenM isDirectory
$ mapDepGenM (joinPaths fontsDir)
$ listDirectory fontsDir
mapDepGenM_ (handleFontDir outputDir) fontSubDirs