mad/byg/src/SiteGenerator.hs

100 lines
4.0 KiB
Haskell
Raw Normal View History

2024-09-23 22:11:54 +02:00
module SiteGenerator (generateSite) where
import Prelude hiding (String, FilePath)
2024-09-23 22:11:54 +02:00
import Types
import DependencyGenerator
2024-10-05 15:58:55 +02:00
import Control.Monad (forM_)
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>"))))
hasExtension :: (TokenableTo [String] a, TokenableTo FilePath b) => a -> b -> DepGenM' Bool
hasExtension exts filename = do
ext <- lowerString $ untupleSndDepGenM $ fileComponents filename
ext `elemOf` exts
2024-09-30 23:31:16 +02:00
handleRecipeDir :: Token FilePath -> Token FilePath -> Token Template -> Token FilePath -> Token FilePath -> DepGenM ()
handleRecipeDir inputDir outputDir template indexName name = do
dir <- joinPaths inputDir name
recipeDirOut <- joinPaths outputDir name
2024-09-26 00:09:48 +02:00
makeDir recipeDirOut
2024-09-23 22:11:54 +02:00
dirContents <- listDirectory dir
areImageFilenames <- mapDepGenM (hasExtension $ inject ["jpg"]) 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-10-05 17:59:52 +02:00
template <- readTemplate (inject "template.html") (inject "CONTENT")
2024-09-25 23:51:33 +02:00
indexName <- inject "index.html"
2024-09-30 23:31:16 +02:00
dirNames <- listDirectory recipesDir
dirPaths <- mapDepGenM (joinPaths recipesDir) dirNames
dirPathsAreSubdirs <- mapDepGenM isDirectory dirPaths
dirNames' <- filterDepGenM dirPathsAreSubdirs dirNames
mapDepGenM_ (handleRecipeDir recipesDir outputRecipesDir template indexName) dirNames'
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
2024-09-30 23:40:53 +02:00
styleName <- inject "style.css"
copyFile styleName (joinPaths outputDir styleName)
2024-10-05 15:58:55 +02:00
imgName <- inject "img"
imgPathOut <- joinPaths outputDir imgName
makeDir imgPathOut
forM_ ["mad-icon.png", "mad-logo.png"] $ \name -> do
val <- inject name
copyFile (joinPaths imgName val) (joinPaths imgPathOut val)
fontsDir <- inject "fonts"
fontsNames <- listDirectory fontsDir
fontsPaths <- mapDepGenM (joinPaths fontsDir) fontsNames
fontsPathsAreSubdirs <- mapDepGenM isDirectory fontsPaths
fontsPaths' <- filterDepGenM fontsPathsAreSubdirs fontsPaths
makeDir (joinPaths outputDir fontsDir)
mapDepGenM_ (handleFontDir outputDir) fontsPaths'
handleFontDir :: Token FilePath -> Token FilePath -> DepGenM ()
handleFontDir outputDir fontPath = do
makeDir (joinPaths outputDir fontPath)
files <- listDirectory fontPath
paths <- mapDepGenM (joinPaths fontPath) files
mask <- mapDepGenM (hasExtension $ inject ["woff2", "css"]) paths
paths' <- filterDepGenM mask paths
mapDepGenM_ (\p -> copyFile p (joinPaths outputDir p)) paths'