From c3d2bdad2e0f631a5a92efa7b8b5f3bee612063d Mon Sep 17 00:00:00 2001 From: etienne Date: Wed, 21 Jun 2023 16:14:12 +0200 Subject: [PATCH] Tinkered with templates compiling, no need for runGHC. Issue created https://git.moqueur.chat/etienne/etienne-moqueur/issues/20 to address the feature. --- app/Main.hs | 26 ++++++++++++------- ...-in-haskell-using-hakyll-blaze-and-clay.md | 15 +++++++++++ src/Core/Compilers.hs | 10 +++++++ src/Utils/Routes.hs | 9 +++++++ 4 files changed, 51 insertions(+), 9 deletions(-) create mode 100644 src/Utils/Routes.hs diff --git a/app/Main.hs b/app/Main.hs index 8ca2409..6f8c26a 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -11,9 +11,11 @@ import Text.Blaze.Html import Components import Core.Compilers import Core.Render ( ) +import Core.Routers import Core.Rules import Routes import Utils.FileTree +import Utils.Routes import Kit.Templates.Index ( indexTemplate ) import Kit.Templates.Post ( postTemplate ) @@ -41,21 +43,27 @@ main = hakyll $ do >>= compileTemplateItem >>= makeItem - match' (Post "*.md") $ do + match (patrn $ Post "*.md") $ do route $ setExtension "html" - compile - $ pandocCompiler - >>= saveSnapshot "content" - >>= loadAndApplyTemplate "src/Kit/Templates/Post.hs" defaultContext - . fmap demoteHeaders + compile $ do + pandocCompiler + >>= saveSnapshot "content" + >>= componentTemplate postTemplate defaultContext + . fmap demoteHeaders + + tags <- buildTags (patrn $ Post "*.md") (fromCapture . patrn $ Tag "*") create ["index.html"] $ do route idRoute compile $ do pages <- recentFirst =<< loadAllSnapshots "posts/*.md" "content" - let ctx = listField "pages" - (defaultContext <> teaserField "teaser" "content") - (return pages) + let ctx = listField + "pages" + ( teaserField "teaser" "content" + <> tagsField "tags" tags + <> defaultContext + ) + (return pages) makeItem "" >>= loadAndApplyTemplate "src/Kit/Templates/Index.hs" ctx create [cssHash] $ do diff --git a/posts/2023-06-20-atomic-design-in-haskell-using-hakyll-blaze-and-clay.md b/posts/2023-06-20-atomic-design-in-haskell-using-hakyll-blaze-and-clay.md index 38c1af6..824fb3c 100644 --- a/posts/2023-06-20-atomic-design-in-haskell-using-hakyll-blaze-and-clay.md +++ b/posts/2023-06-20-atomic-design-in-haskell-using-hakyll-blaze-and-clay.md @@ -3,6 +3,7 @@ title: Atomic design in Haskell using Hakyll, Blaze and Clay tags: web, haskell, hakyll, atomic-design description: An overview of the framework I use to make this site. date: 2023-06-20 +updated: 2023-06-21 --- This post presents an overview of the mechanics I use to develop a pure Haskell framework that is atomic design compliant. I intend to write no `html` file, no `css` file and no `js` file, only `hs`. It uses [Hakyll](https://hackage.haskell.org/package/hakyll) to compile the site, [Blaze](https://hackage.haskell.org/package/blaze-html) to generate HTML and [Clay](https://hackage.haskell.org/package/clay) to generate CSS. @@ -119,6 +120,20 @@ main = hakyll $ do compile $ compileHs ``` +**EDIT**: (2023-06-21) Actually, no need to make those main functions. Something like + +```haskell +import Text.Blaze.Html.Renderer.Pretty +import Hakyll + +main :: IO () +main = hakyll $ do + create ["myUl.html"] $ do + route idRoute + compile $ makeItem (renderHtml someHtml) -- someHtml :: Html +``` +will work just fine. + # Creating components suited for atomic design ## Overview of the problem diff --git a/src/Core/Compilers.hs b/src/Core/Compilers.hs index 6e3d8dd..e081f5c 100644 --- a/src/Core/Compilers.hs +++ b/src/Core/Compilers.hs @@ -1,8 +1,12 @@ module Core.Compilers ( runGHC + , componentTemplate ) where +import Components +import Core.Render ( ) import Hakyll +import Text.Blaze.Html -- Redundant... Those default extensions should be kept in sync with package.yaml, but `stack runghc` won't take those in account. defaultExtensions :: [String] @@ -12,3 +16,9 @@ defaultExtensions = runGHC :: Compiler (Item String) runGHC = getResourceString >>= withItemBody (unixFilter "stack" $ "runghc" : defaultExtensions) + +componentTemplate + :: Component Html -> Context a -> Item a -> Compiler (Item String) +componentTemplate comp ctx itm = do + tpl <- compileTemplateItem =<< makeItem (show . getHtml $ comp) + applyTemplate tpl ctx itm diff --git a/src/Utils/Routes.hs b/src/Utils/Routes.hs new file mode 100644 index 0000000..43116f3 --- /dev/null +++ b/src/Utils/Routes.hs @@ -0,0 +1,9 @@ +module Utils.Routes + ( patrn + ) where + +import Hakyll +import Routes + +patrn :: Route -> Pattern +patrn = fromGlob . tail . path