diff --git a/src/Psb/Main.hs b/src/Psb/Main.hs index 2ae2e21..24b23cf 100644 --- a/src/Psb/Main.hs +++ b/src/Psb/Main.hs @@ -9,6 +9,7 @@ module Psb.Main where import Config import Control.Monad (when) +import Data.Aeson (ToJSON (toJSON)) import qualified Data.HashMap.Strict as HM import Data.List (sortOn) import qualified Data.Ord as Ord @@ -22,6 +23,7 @@ import Development.Shake.FilePath (()) import qualified Development.Shake.FilePath as FP import Templates import Text.Megaparsec (errorBundlePretty) +import Text.Mustache (ToMustache (toMustache)) import Types import Utilities.Action (getPublishedPosts, isDraft', markdownToHtml, markdownToPost, now, psbProgress) import qualified Utilities.CSS as CSS @@ -173,6 +175,9 @@ data Rss = Rss deriving (Show, Generic) deriving (ToJSON) via Vanilla Rss +instance ToMustache Rss where + toMustache = toMustache . toJSON + rss :: Rules () rss = outputDir "index.xml" %> \target -> do diff --git a/src/Templates.hs b/src/Templates.hs index 0c254cb..5493b66 100644 --- a/src/Templates.hs +++ b/src/Templates.hs @@ -8,16 +8,17 @@ import Development.Shake import qualified Development.Shake as Shake import Development.Shake.FilePath (()) import GHC.Stack (HasCallStack) +import Text.Mustache (ToMustache) import qualified Text.Mustache as Mus import qualified Text.Mustache.Compile as Mus import Types (Post (postAuthor, postContent, postDate, postDescription, postLink, postTags, postTitle), RenderedPost (RenderedPost, rPostAuthor, rPostContent, rPostDate, rPostHasTags, rPostId, rPostIsoDate, rPostLink, rPostSummary, rPostTags, rPostTitle)) import Utilities -applyTemplate :: (HasCallStack, (ToJSON a)) => String -> a -> Action Text +applyTemplate :: (HasCallStack, (ToMustache a)) => String -> a -> Action Text applyTemplate templateName context = do tmpl <- readTemplate $ "templates" templateName -- liftIO $ print $ A.toJSON context - case Mus.checkedSubstitute tmpl (A.toJSON context) of + case Mus.checkedSubstitute tmpl context of ([], text) -> return text (errs, _) -> error $ @@ -26,7 +27,7 @@ applyTemplate templateName context = do <> ": " <> unlines (map show errs) -applyTemplateAndWrite :: (ToJSON a) => String -> a -> FilePath -> Action () +applyTemplateAndWrite :: (ToMustache a) => String -> a -> FilePath -> Action () applyTemplateAndWrite templateName context outputPath = applyTemplate templateName context >>= Shake.writeFile' outputPath . T.unpack @@ -41,7 +42,7 @@ readTemplate templatePath = do case eTemplate of Right template -> do Shake.need . Mus.getPartials . Mus.ast $ template - --Shake.putInfo $ "Read " <> templatePath + -- Shake.putInfo $ "Read " <> templatePath return template Left err -> fail $ show err diff --git a/src/Types.hs b/src/Types.hs index 428a5df..69e6680 100644 --- a/src/Types.hs +++ b/src/Types.hs @@ -1,8 +1,10 @@ module Types where +import Data.Aeson (ToJSON (toJSON)) import Data.Text (Text) import Deriving.Aeson import Deriving.Aeson.Stock (PrefixedSnake) +import Text.Mustache (ToMustache (toMustache)) -- pageSection is what css class should be specified in a style html element, I would do an enum but I foresee that being a mistake data Page = Page @@ -16,6 +18,9 @@ data Page = Page deriving (Show, Generic) deriving (ToJSON) via PrefixedSnake "page" Page +instance ToMustache Page where + toMustache = toMustache . toJSON + data RenderedPost = RenderedPost { rPostTitle :: Text, rPostAuthor :: Maybe Text, @@ -31,6 +36,9 @@ data RenderedPost = RenderedPost deriving (Show, Generic) deriving (FromJSON, ToJSON) via PrefixedSnake "rPost" RenderedPost +instance ToMustache RenderedPost where + toMustache = toMustache . toJSON + data Post = Post { postTitle :: Text, postAuthor :: Maybe Text, @@ -43,3 +51,6 @@ data Post = Post } deriving (Show, Generic) deriving (FromJSON, ToJSON) via PrefixedSnake "post" Post + +instance ToMustache Post where + toMustache = toMustache . toJSON