From 8a7fc93730e66a3d0aa2d880a919c5e10247a8f0 Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Thu, 7 Sep 2023 15:39:05 +1200 Subject: [PATCH] Add infrastructure for rendering Ginger templates. --- bureaucromancy.cabal | 7 ++++--- src/Text/HTML/Form.hs | 4 +++- src/Text/HTML/Form/WebApp.hs | 21 +++++++++++++++++++-- tpl/.keep | 0 4 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 tpl/.keep diff --git a/bureaucromancy.cabal b/bureaucromancy.cabal index aa5e29a..c7ab7ee 100644 --- a/bureaucromancy.cabal +++ b/bureaucromancy.cabal @@ -62,7 +62,8 @@ library import: warnings -- Modules exported by the library. - exposed-modules: Text.HTML.Form, Text.HTML.Form.WebApp + exposed-modules: Text.HTML.Form, + Text.HTML.Form.WebApp, Text.HTML.Form.WebApp.Ginger -- Modules included in this library but not exported. -- other-modules: @@ -71,8 +72,8 @@ library -- other-extensions: -- Other library packages from which modules are imported. - build-depends: base ^>=4.16.4.0, ginger, - bytestring, text, xml-conduit, network-uri, regex-tdfa + build-depends: base ^>=4.16.4.0, ginger, file-embed-lzma, file-embed, mtl, + bytestring, text, xml-conduit, network-uri, regex-tdfa, containers -- Directories containing source files. hs-source-dirs: src diff --git a/src/Text/HTML/Form.hs b/src/Text/HTML/Form.hs index eeff4fd..4db6dc1 100644 --- a/src/Text/HTML/Form.hs +++ b/src/Text/HTML/Form.hs @@ -1,5 +1,7 @@ {-# LANGUAGE OverloadedStrings, TypeSynonymInstances, FlexibleInstances #-} -module Text.HTML.Form (Form(..), Input(..), parseElement, parseDocument) where +module Text.HTML.Form (Form(..), Input(..), OptionGroup(..), Option(..), + FileSelector(..), defaultFileData, ImageData(..), defaultImageData, + TextArea(..), defaultTextArea, parseElement, parseDocument) where import Data.Text (Text) import qualified Data.Text as Txt diff --git a/src/Text/HTML/Form/WebApp.hs b/src/Text/HTML/Form/WebApp.hs index 08ac5a7..9a8f186 100644 --- a/src/Text/HTML/Form/WebApp.hs +++ b/src/Text/HTML/Form/WebApp.hs @@ -3,7 +3,24 @@ module Text.HTML.Form.WebApp (renderPage, Form(..)) where import Data.ByteString as BS import Data.Text as Txt -import Text.HTML.Form (Form(..)) +import Text.Read (readMaybe) + +import Text.HTML.Form (Form(..), Input(..)) +import Text.HTML.Form.WebApp.Ginger (template) renderPage :: Form -> [Text] -> [(ByteString, Maybe ByteString)] -> IO (Maybe Text) -renderPage _ _ _ = return $ Just "" +renderPage form (n:ix:path) query + | Just ix' <- readMaybe $ Txt.unpack ix, ix' < Prelude.length inputs' = + renderInput form (inputs' !! ix') path query + | input:_ <- inputs' = renderInput form input (ix:path) query + where inputs' = Prelude.filter (\x -> inputName x == n) $ inputs form +renderPage form [n] query + | input:_ <- Prelude.filter (\x -> inputName x == n) $ inputs form = + renderInput form input [] query +renderPage _ _ _ = return Nothing + +renderInput :: Form -> Input -> [Text] -> [(ByteString, Maybe ByteString)] -> + IO (Maybe Text) +renderInput form input@Input {inputType = "checkbox"} [] q = + template "checkbox" form input q +renderInput _ _ _ _ = return Nothing diff --git a/tpl/.keep b/tpl/.keep new file mode 100644 index 0000000..e69de29 -- 2.30.2