module Data.Niagra.Block
(
Block(..),
Declaration(..),
isEmpty,
buildBlock,
)
where
import Data.Niagra.Selector
import Data.Monoid
import Data.Text.Lazy (Text)
import Data.Text.Lazy.Builder
data Declaration = Declaration Text Text deriving (Show)
data Block = DeclarationBlock Selector [Declaration]
| BuilderBlock Selector Builder
instance Show Block where
show (DeclarationBlock s decls) = "(DeclarationBlock (" ++ show s ++ ") " ++ show decls ++ ")"
isEmpty :: Block -> Bool
isEmpty (DeclarationBlock _ d2) = null d2
isEmpty (BuilderBlock _ b2) = b2 == mempty
buildBlock :: Block
-> Builder
buildBlock (BuilderBlock sel b) = mconcat [buildSelector sel, "{", b, "}"]
buildBlock (DeclarationBlock sel d) = buildBlock $ BuilderBlock sel $ buildDecls mempty d
where
pair p v = fromLazyText p <> ":" <> fromLazyText v
buildDecls accum [] = accum
buildDecls accum [Declaration p v] = accum <> pair p v
buildDecls accum (x:xs) = buildDecls (buildDecls accum [x] <> ";") xs