diff options
Diffstat (limited to 'src/Pages.hs')
| -rw-r--r-- | src/Pages.hs | 44 |
1 files changed, 14 insertions, 30 deletions
diff --git a/src/Pages.hs b/src/Pages.hs index 003b5ed..ef794ab 100644 --- a/src/Pages.hs +++ b/src/Pages.hs @@ -10,15 +10,15 @@ module Pages where import Control.Monad (forM) import Control.Monad.IO.Class (liftIO) -import Data.List (find) +import Data.Map.Strict qualified as Map import Data.Text (Text) +import Data.Text qualified as T (pack) import Data.Text.IO qualified as T import Language.Haskell.TH.Syntax (addDependentFile) import System.Directory (makeAbsolute, listDirectory) -import Text.Mustache.Compile qualified as M -import Text.Mustache.Types qualified as M -import Pages.TH +import Mustache.TH +import Mustache.TH.Parser data IndexData = IndexData @@ -132,37 +132,21 @@ data SpamcheckData = SpamcheckData $(do let dropSuffix suf str = let (actualSuf, rstr) = splitAt (length suf) (reverse str) in if actualSuf == reverse suf then reverse rstr else str - let inlinePartials :: [M.Template] -> M.Template -> M.Template - inlinePartials ps (M.Template topname ast _) = M.Template topname (concatMap go ast) mempty - where - go :: M.Node Text -> [M.Node Text] - go n@M.TextBlock{} = [n] - go (M.Section named nodes) = [M.Section named (concatMap go nodes)] - go (M.InvertedSection named nodes) = [M.InvertedSection named (concatMap go nodes)] - go n@M.Variable{} = [n] - -- sorry for the lost indent - go (M.Partial _indent partialname) = - case find ((== partialname) . M.name) ps of - Just (M.Template _ ast' _) -> concatMap go ast' - Nothing -> error $ "Partial not found: " ++ partialname partialFiles <- liftIO $ listDirectory "pages/partials" - partials <- forM partialFiles $ \fname -> do - path <- liftIO $ makeAbsolute ("pages/partials/" ++ fname) - tplSrc <- liftIO $ T.readFile path - addDependentFile path - let name = dropSuffix ".mustache" fname - case M.compileTemplate name tplSrc of - Right tpl -> return tpl - Left err -> fail $ "Reading " ++ path ++ ": " ++ show err - let makeDecs name dataname = do - path <- liftIO $ makeAbsolute ("pages/" ++ name ++ ".mustache") + let loadTemplate fname = do + path <- liftIO $ makeAbsolute fname tplSrc <- liftIO $ T.readFile path addDependentFile path - tpl <- case M.compileTemplate name tplSrc of + case parseTemplate tplSrc of Right tpl -> return tpl Left err -> fail $ "Reading " ++ path ++ ": " ++ show err - let tpl' = inlinePartials partials tpl - makeRender tpl' dataname + partials <- fmap Map.fromList . forM partialFiles $ \fname -> do + let name = T.pack (dropSuffix ".mustache" fname) + (name,) <$> loadTemplate ("pages/partials/" ++ fname) + let makeDecs name dataname = do + tpl <- loadTemplate ("pages/" ++ name ++ ".mustache") + let tpl' = inlinePartials True False partials tpl + makeRender name tpl' dataname concat <$> mapM (uncurry makeDecs) [("log", ''LogData) ,("plainlog", ''PlainLogData) |
