element accepts any ToXML-convertible value

This commit is contained in:
wiz 2013-04-12 13:16:50 +04:00
parent eb7a506419
commit f7807ed09a
3 changed files with 21 additions and 17 deletions

View File

@ -5,11 +5,11 @@
-- > {-# LANGUAGE OverloadedStrings #-}
-- >
-- > let doc = document "root" $ do
-- > element "hello" "world"
-- > element "hello" $ content "world"
-- > element "hierarchy" $ do
-- > element "simple" $ toXML True
-- > element "as" "it should be"
-- > toXML $ Just "like this"
-- > element "simple" True
-- > element "as" ("it should be" :: Text)
-- > toXML $ Just . T.pack $ "like this"
-- > comment "that's it!"
--
@ -24,7 +24,7 @@ module Text.XML.Writer
, node
, instruction
, comment
, element, elementA
, element, elementMaybe, elementA
, content
, empty
, many
@ -64,17 +64,25 @@ pprint = TL.putStrLn . renderText def { rsPretty = True }
render :: XML -> [Node]
render = DL.toList . execWriter
-- | Insert one node
-- | Do nothing.
empty :: XML
empty = return ()
-- | Insert one node.
node :: Node -> XML
node = tell . DL.singleton
-- | Insert an "Element" node constructed with name and children.
element :: Name -> XML -> XML
element name children = node . NodeElement $! Element name def (render children)
element :: (ToXML a) => Name -> a -> XML
element name children = node . NodeElement $! Element name def (render $ toXML children)
-- | Insert an "Element" node converted from Maybe value or do nothing.
elementMaybe :: (ToXML a) => Name -> Maybe a -> XML
elementMaybe name = maybe empty (element name)
-- | Insert an "Element" node constructed with name, attributes and children.
elementA :: Name -> [(Name, Text)] -> XML -> XML
elementA name attrs children = node . NodeElement $! Element name (M.fromList attrs) (render children)
elementA :: (ToXML a) => Name -> [(Name, Text)] -> a -> XML
elementA name attrs children = node . NodeElement $! Element name (M.fromList attrs) (render $ toXML children)
-- | Insert an "Instruction" node.
instruction :: Text -> Text -> XML
@ -88,10 +96,6 @@ comment = node . NodeComment
content :: Text -> XML
content = node . NodeContent
-- | Do nothing.
empty :: XML
empty = return ()
-- | Mass-convert to nodes.
--
-- > let array = element "container" $ many "wrapper" [1..3]

View File

@ -24,7 +24,7 @@ main :: IO ()
main = do
pprint $ document "root" $ do
element "{ns:uri}pseudo:prefix" $ do
element "unprefixed" "empty NS"
element "unprefixed" $ comment "empty NS"
element "pseudo:prefixed" $ comment "wrong!"
element ("sns" !: "{silly:ns:uri}spam") $ do
@ -41,7 +41,7 @@ main = do
pprint $ soap () $ do
element ("v" !: "{vendor:uri}request") $ do
element "complex" $ do
element "key" "value"
element "key" $ T.pack "value"
elementA "tag" [("key", "value")] empty
element "text" $ content "some text"
element "bool" $ toXML True

View File

@ -1,5 +1,5 @@
name: xml-conduit-writer
version: 0.1.0.1
version: 0.1.1.0
synopsis: Warm and fuzzy creation of XML documents.
description:
“It can scarcely be denied that the supreme goal of