mad/byg/src/SiteGenerator.hs

65 lines
2.5 KiB
Haskell
Raw Normal View History

2024-09-23 22:11:54 +02:00
module SiteGenerator (generateSite) where
import Types
import DependencyGenerator
2024-09-26 23:02:29 +02:00
import Data.Text (Text)
2024-09-26 00:33:52 +02:00
thumbnailImageFilename :: Token FilePath -> DepGenM' FilePath
thumbnailImageFilename filename = do
2024-09-26 23:40:26 +02:00
(base, ext) <- untupleDepGenM $ fileComponents filename
appendStrings base
$ appendStrings (inject "-thumbnail.") ext
2024-09-26 00:33:52 +02:00
2024-09-26 23:02:29 +02:00
makeImageHTML :: Token (FilePath, FilePath) -> DepGenM' Text
2024-09-26 00:33:52 +02:00
makeImageHTML t = do
(thumbnail, actual) <- untupleDepGenM t
2024-09-26 23:40:26 +02:00
appendTexts
(inject "<p><a href=\"")
(appendTexts
(toText actual)
(appendTexts
(inject "\"><img src=\"")
(appendTexts
(toText thumbnail)
(inject "\"></a></p>"))))
2024-09-25 23:51:33 +02:00
handleRecipeDir :: Token FilePath -> Token Template -> Token FilePath -> Token FilePath -> DepGenM ()
handleRecipeDir outputDir template indexName dir = do
2024-09-26 23:40:26 +02:00
recipeDirOut <- joinPaths outputDir dir
2024-09-26 00:09:48 +02:00
makeDir recipeDirOut
2024-09-23 22:11:54 +02:00
dirContents <- listDirectory dir
2024-09-28 13:57:53 +02:00
areImageFilenames <- mapDepGenM hasImageExtension
2024-09-26 23:40:26 +02:00
$ unzipSndDepGenM $ mapDepGenM fileComponents dirContents
2024-09-25 23:12:32 +02:00
imageFilenames <- filterDepGenM areImageFilenames dirContents
2024-09-26 23:40:26 +02:00
imagePaths <- mapDepGenM (joinPaths dir) imageFilenames
imagePathsOut <- mapDepGenM (joinPaths recipeDirOut) imageFilenames
mapDepGenM_ copyFile' $ zipDepGenM imagePaths imagePathsOut
2024-09-26 00:33:52 +02:00
thumbnailImageFilenames <- mapDepGenM thumbnailImageFilename imageFilenames
2024-09-26 23:40:26 +02:00
thumbnailImagePaths <- mapDepGenM (joinPaths recipeDirOut) thumbnailImageFilenames
mapDepGenM_
2024-09-26 23:40:26 +02:00
(\files -> convertImage files $ inject $ ResizeToWidth 800)
(zipDepGenM imagePaths thumbnailImagePaths)
htmlBodyBase <- runPandoc $ joinPaths dir $ inject "ret.md"
htmlBodyImages <- mapDepGenM makeImageHTML $ zipDepGenM thumbnailImageFilenames imageFilenames
2024-09-26 23:02:29 +02:00
imagesHtml <- concatTexts htmlBodyImages
2024-09-26 23:40:26 +02:00
htmlBody <- appendTexts htmlBodyBase imagesHtml
html <- applyTemplate template htmlBody
saveFile html $ joinPaths recipeDirOut indexName
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-09-25 23:51:33 +02:00
recipesDir <- inject "retter"
2024-09-26 23:40:26 +02:00
outputRecipesDir <- joinPaths outputDir recipesDir
2024-09-25 23:51:33 +02:00
makeDir outputRecipesDir
2024-09-26 23:40:26 +02:00
template <- readTemplate $ inject "template.html"
2024-09-25 23:51:33 +02:00
indexName <- inject "index.html"
dirContents <- listDirectory recipesDir
mapDepGenM_ (handleRecipeDir outputRecipesDir template indexName) dirContents
2024-09-26 23:40:26 +02:00
html <- applyTemplate template $ runPandoc $ inject "om.md"
aboutDir <- joinPaths outputDir $ inject "om"
2024-09-25 23:51:33 +02:00
makeDir aboutDir
2024-09-26 23:40:26 +02:00
saveFile html $ joinPaths aboutDir indexName