~alcinnz/bureaucromancy

8a7fc93730e66a3d0aa2d880a919c5e10247a8f0 — Adrian Cochrane 1 year, 4 months ago de2d2b6
Add infrastructure for rendering Ginger templates.
4 files changed, 26 insertions(+), 6 deletions(-)

M bureaucromancy.cabal
M src/Text/HTML/Form.hs
M src/Text/HTML/Form/WebApp.hs
A tpl/.keep
M bureaucromancy.cabal => bureaucromancy.cabal +4 -3
@@ 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

M src/Text/HTML/Form.hs => src/Text/HTML/Form.hs +3 -1
@@ 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

M src/Text/HTML/Form/WebApp.hs => src/Text/HTML/Form/WebApp.hs +19 -2
@@ 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

A tpl/.keep => tpl/.keep +0 -0