summaryrefslogtreecommitdiff
path: root/src/Pages.hs
diff options
context:
space:
mode:
authorTom Smeding <tom@tomsmeding.com>2026-11-08 14:37:43 +0100
committerTom Smeding <tom@tomsmeding.com>2026-11-08 16:09:23 +0100
commitffbde7ba6bc275e7cc8c6b2dcca15ad775bedb9b (patch)
treec346e6b7380109b86bcc917bfa3ca9cfa3861c90 /src/Pages.hs
parent96fc53b35c1101e23290450c1f15360214ebcd36 (diff)
Use own mustache parser; drop mustache&aeson deps
Diffstat (limited to 'src/Pages.hs')
-rw-r--r--src/Pages.hs44
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)