mad/byg/src/SiteGenerator.hs

92 lines
3.3 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-05 15:58:55 +02:00
import Control.Monad (forM_)
handleRecipeDir :: Token FilePath -> Token Template -> Token FilePath -> Token FilePath -> DepGenM ()
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
thumbnailName <- concatStrings [ 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-05 22:51:13 +02:00
concatTexts [ inject "<p class=\"image\"><a href=\""
, toText name
, inject "\"><img src=\""
, toText 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
html <- applyTemplate htmlTemplate
$ writeHtml
$ readMarkdown
2024-10-05 22:51:13 +02:00
$ applyTemplate mdTemplate
$ concatTexts [ inject "<p class=\"imagetext\">Opskrift fortsætter efter billedet.</p>\n"
, concatTexts htmlBodyImages
, inject "\n"
, pure ingredienserHeadline
]
saveTextFile html (joinPaths recipeDirOut indexName)
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"
makeDir $ joinPaths outputDir recipesDir
2024-10-05 22:51:13 +02:00
recipeSubDirs <- filterDepGenM isDirectory
$ mapDepGenM (joinPaths recipesDir)
$ listDirectory recipesDir
mapDepGenM_ (handleRecipeDir outputDir htmlTemplate indexName) recipeSubDirs
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