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
|
2024-10-05 20:21:06 +02:00
|
|
|
suffix <- inject "-thumbnail."
|
|
|
|
concatStrings [ base, suffix, 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-10-05 20:28:25 +02:00
|
|
|
concatTexts [ inject "<p class=\"image\"><a href=\""
|
|
|
|
, toText actual
|
|
|
|
, inject "\"><img src=\""
|
|
|
|
, 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-10-05 20:35:34 +02:00
|
|
|
handleRecipeDir :: Token FilePath -> Token Template -> Token FilePath -> Token FilePath -> DepGenM ()
|
|
|
|
handleRecipeDir outputDir template indexName dir = do
|
|
|
|
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-10-05 21:57:04 +02:00
|
|
|
imageFilenames <- filterDepGenM (hasExtension $ inject ["jpg"]) 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 19:15:20 +02:00
|
|
|
md <- readTextFile $ joinPaths dir $ inject "ret.md"
|
|
|
|
ingredienserHeadline <- inject "## Ingredienser"
|
|
|
|
mdTemplate <- makeTemplate md ingredienserHeadline
|
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-10-05 20:28:25 +02:00
|
|
|
md' <- applyTemplate mdTemplate
|
|
|
|
$ concatTexts [ inject "<p class=\"imagetext\">Opskrift fortsætter efter billedet.</p>\n"
|
|
|
|
, pure imagesHtml
|
|
|
|
, inject "\n\n"
|
|
|
|
, pure ingredienserHeadline
|
|
|
|
]
|
2024-10-05 19:15:20 +02:00
|
|
|
htmlBody <- runPandoc md'
|
2024-09-26 23:40:26 +02:00
|
|
|
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-10-05 20:35:34 +02:00
|
|
|
makeDir $ joinPaths outputDir recipesDir
|
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
|
2024-10-05 21:57:04 +02:00
|
|
|
dirPaths' <- filterDepGenM isDirectory dirPaths
|
2024-10-05 20:35:34 +02:00
|
|
|
mapDepGenM_ (handleRecipeDir outputDir template indexName) dirPaths'
|
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
|
2024-10-05 21:57:04 +02:00
|
|
|
fontsPaths' <- filterDepGenM isDirectory fontsPaths
|
2024-10-05 17:35:47 +02:00
|
|
|
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
|
2024-10-05 21:57:04 +02:00
|
|
|
paths' <- filterDepGenM (hasExtension $ inject ["woff2", "css"]) paths
|
2024-10-05 17:35:47 +02:00
|
|
|
mapDepGenM_ (\p -> copyFile p (joinPaths outputDir p)) paths'
|