Start extracting titles from recipes

This commit is contained in:
Niels G. W. Serup 2024-10-11 22:41:31 +02:00
parent afec7f814f
commit eae34707e9
No known key found for this signature in database
GPG Key ID: 38EEEBCE67324F19
2 changed files with 27 additions and 10 deletions

View File

@ -1,6 +1,7 @@
module Functions.Pandoc module Functions.Pandoc
( readMarkdown ( readMarkdown
, writeHtml , writeHtml
, extractTitle
) where ) where
import Types (IsFunction(..), Token) import Types (IsFunction(..), Token)
@ -8,6 +9,8 @@ import DependencyGenerator (DepGenM, TokenableTo(..), runFunction)
import Data.Text (Text) import Data.Text (Text)
import Text.Pandoc.Definition (Pandoc) import Text.Pandoc.Definition (Pandoc)
import qualified Text.Pandoc.Definition as PD
import qualified Text.Pandoc.Shared as PS
import qualified Text.Pandoc as P import qualified Text.Pandoc as P
runPandoc :: P.PandocPure a -> a runPandoc :: P.PandocPure a -> a
@ -33,3 +36,13 @@ instance IsFunction WriteHtml Pandoc Text where
writeHtml :: TokenableTo Pandoc a => a -> DepGenM (Token Text) writeHtml :: TokenableTo Pandoc a => a -> DepGenM (Token Text)
writeHtml a = runFunction WriteHtml =<< toToken a writeHtml a = runFunction WriteHtml =<< toToken a
data ExtractTitle = ExtractTitle deriving Show
instance IsFunction ExtractTitle Pandoc Text where
evalFunction ExtractTitle (PD.Pandoc _ blocks) = case blocks of
(PD.Header 1 _ inlines : _) -> PS.stringify inlines
_ -> error "unexpected"
extractTitle :: TokenableTo Pandoc a => a -> DepGenM (Token Text)
extractTitle a = runFunction ExtractTitle =<< toToken a

View File

@ -4,9 +4,10 @@ import Types (Token)
import DependencyGenerator import DependencyGenerator
import Functions import Functions
import Data.Text (Text)
import Control.Monad (forM_) import Control.Monad (forM_)
handleRecipeDir :: Token FilePath -> Token Template -> Token FilePath -> Token FilePath -> DepGenM () handleRecipeDir :: Token FilePath -> Token Template -> Token FilePath -> Token FilePath -> DepGenM (Token Text)
handleRecipeDir outputDir htmlTemplate indexName dir = do handleRecipeDir outputDir htmlTemplate indexName dir = do
recipeDirOut <- joinPaths outputDir dir recipeDirOut <- joinPaths outputDir dir
makeDir recipeDirOut makeDir recipeDirOut
@ -29,16 +30,18 @@ handleRecipeDir outputDir htmlTemplate indexName dir = do
mdTemplate <- makeTemplate mdTemplate <- makeTemplate
(readTextFile (joinPaths dir (inject "ret.md"))) (readTextFile (joinPaths dir (inject "ret.md")))
ingredienserHeadline ingredienserHeadline
html <- applyTemplate htmlTemplate pandoc <- readMarkdown
$ writeHtml
$ readMarkdown
$ applyTemplate mdTemplate $ applyTemplate mdTemplate
$ concatTexts [ inject "<p class=\"imagetext\">Opskrift fortsætter efter billedet.</p>\n" $ concatTexts [ inject "<p class=\"imagetext\">Opskrift fortsætter efter billedet.</p>\n"
, concatTexts htmlBodyImages , concatTexts htmlBodyImages
, inject "\n" , inject "\n"
, pure ingredienserHeadline , pure ingredienserHeadline
] ]
title <- extractTitle pandoc
html <- applyTemplate htmlTemplate
$ writeHtml pandoc
saveTextFile html (joinPaths recipeDirOut indexName) saveTextFile html (joinPaths recipeDirOut indexName)
pure title
handleFontDir :: Token FilePath -> Token FilePath -> DepGenM () handleFontDir :: Token FilePath -> Token FilePath -> DepGenM ()
handleFontDir outputDir dir = do handleFontDir outputDir dir = do
@ -61,7 +64,8 @@ generateSite = do
recipeSubDirs <- filterDepGenM isDirectory recipeSubDirs <- filterDepGenM isDirectory
$ mapDepGenM (joinPaths recipesDir) $ mapDepGenM (joinPaths recipesDir)
$ listDirectory recipesDir $ listDirectory recipesDir
mapDepGenM_ (handleRecipeDir outputDir htmlTemplate indexName) recipeSubDirs _titles <- mapDepGenM (handleRecipeDir outputDir htmlTemplate indexName) recipeSubDirs
-- TODO: Use titles
-- Handle about page -- Handle about page
outputAboutDir <- joinPaths outputDir (inject "om") outputAboutDir <- joinPaths outputDir (inject "om")