2024-09-23 22:11:54 +02:00
|
|
|
module SiteGenerator (generateSite) where
|
|
|
|
|
2024-10-05 17:35:47 +02:00
|
|
|
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>"))))
|
2024-09-25 23:32:49 +02:00
|
|
|
|
2024-10-05 17:35:47 +02:00
|
|
|
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
|
2024-10-05 17:35:47 +02:00
|
|
|
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
|
2024-09-25 22:09:26 +02:00
|
|
|
mapDepGenM_
|
2024-09-26 23:40:26 +02:00
|
|
|
(\files -> convertImage files $ inject $ ResizeToWidth 800)
|
|
|
|
(zipDepGenM imagePaths thumbnailImagePaths)
|
2024-10-05 18:13:49 +02:00
|
|
|
htmlBodyBase <- runPandoc $ readTextFile $ joinPaths dir $ inject "ret.md"
|
2024-09-26 23:40:26 +02:00
|
|
|
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 18:07:26 +02:00
|
|
|
template <- makeTemplate (readTextFile (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-10-05 18:13:49 +02:00
|
|
|
html <- applyTemplate template $ runPandoc $ readTextFile $ inject "om.md"
|
2024-09-26 23:40:26 +02:00
|
|
|
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)
|
2024-10-05 17:35:47 +02:00
|
|
|
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'
|