Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-# LANGUAGE OverloadedStrings #-}
- module Splices where
- import Snap.Snaplet.Heist.Interpreted
- import Heist
- import Heist.Interpreted
- import qualified Text.XmlHtml as X
- import Application
- import Data.List (find)
- -- <if><condition>
- -- <then>content if condition is true</then>
- -- <else>content if condition is false</else>
- -- </if>
- -- available conditions: equal, not (or not equal ...)
- ifSplice :: SnapletISplice App
- ifSplice = do
- nodes <- runChildrenWith [("equal", equalSplice), ("not", notSplice)]
- if null nodes
- then return []
- else case (head nodes) of
- X.TextNode t -> if t == "1" then returnThenNode nodes else returnElseNode nodes
- _ -> return [X.TextNode "Element w <if> musi być równy 1 lub 0"]
- where
- returnThenNode [] = return [X.TextNode "Brakuje elementu <then></then>"]
- returnThenNode l = return $ case find isThenNode l of
- Just (X.Element _ _ c) -> c
- _ -> [X.TextNode "Brakuje elementu <then></then>"]
- returnElseNode [] = return [X.TextNode "Brakuje elementu <else></else>"]
- returnElseNode l = return $ case find isElseNode l of
- Just (X.Element _ _ c) -> c
- _ -> [X.TextNode "Brakuje elementu <else></else>"]
- isThenNode (X.Element "then" _ _) = True
- isThenNode _ = False
- isElseNode (X.Element "else" _ _) = True
- isElseNode _ = False
- -- <equal a="" b=""/>
- equalSplice :: SnapletISplice App
- equalSplice = do
- (X.Element _ attrs _) <- getParamNode
- let a = find (\(n,_) -> n == "a") attrs
- b = find (\(n,_) -> n == "b") attrs
- case a of
- Nothing -> return [X.TextNode "Element <equal> musi posiadać atrybut a"]
- Just (_,aa) -> case b of
- Nothing -> return [X.TextNode "Element <equal> musi posiadać atrybut b"]
- Just (_,bb) -> if aa == bb
- then return [X.TextNode "1"]
- else return [X.TextNode "0"]
- -- <not>0/1</not>
- notSplice :: SnapletISplice App
- notSplice = do
- c <- runChildren
- if length c /= 1
- then return [X.TextNode "Element <not> może przyjmować tylko 1 element wartości 0 lub 1"]
- else case head c of
- X.TextNode t -> if t == "1"
- then return [X.TextNode "0"]
- else return [X.TextNode "1"]
- _ -> return [X.TextNode "Element <not> może przyjmować tylko wartości 0 lub 1"]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement