@@ -5,11 +5,11 @@
5-- > {-# LANGUAGE OverloadedStrings #-}
5-- > {-# LANGUAGE OverloadedStrings #-}
7-- > let doc = document "root" $ do
7-- > let doc = document "root" $ do
8−-- > element "hello" "world"
8+-- > element "hello" $ content "world"
9-- > element "hierarchy" $ do
9-- > element "hierarchy" $ do
10−-- > element "simple" $ toXML True
10+-- > element "simple" True
11−-- > element "as" "it should be"
11+-- > element "as" ("it should be" :: Text)
12−-- > toXML $ Just "like this"
12+-- > toXML $ Just . T.pack $ "like this"
13-- > comment "that's it!"
13-- > comment "that's it!"
@@ -24,7 +24,7 @@ module Text.XML.Writer
25 , instruction
25 , instruction
27− , element, elementA
27+ , element, elementMaybe, elementA
@@ -64,17 +64,25 @@ pprint = TL.putStrLn . renderText def { rsPretty = True }
64render :: XML -> [Node]
64render :: XML -> [Node]
65render = DL.toList . execWriter
65render = DL.toList . execWriter
67−-- | Insert one node
71+-- | Insert one node.
68node :: Node -> XML
72node :: Node -> XML
69node = tell . DL.singleton
73node = tell . DL.singleton
71-- | Insert an "Element" node constructed with name and children.
75-- | Insert an "Element" node constructed with name and children.
72−element :: Name -> XML -> XML
76+element :: (ToXML a) => Name -> a -> XML
73−element name children = node . NodeElement $! Element name def (render children)
77+element name children = node . NodeElement $! Element name def (render $ toXML children)
79+-- | Insert an "Element" node converted from Maybe value or do nothing.
80+elementMaybe :: (ToXML a) => Name -> Maybe a -> XML
81+elementMaybe name = maybe empty (element name)
75-- | Insert an "Element" node constructed with name, attributes and children.
83-- | Insert an "Element" node constructed with name, attributes and children.
76−elementA :: Name -> [(Name, Text)] -> XML -> XML
84+elementA :: (ToXML a) => Name -> [(Name, Text)] -> a -> XML
77−elementA name attrs children = node . NodeElement $! Element name (M.fromList attrs) (render children)
85+elementA name attrs children = node . NodeElement $! Element name (M.fromList attrs) (render $ toXML children)
79-- | Insert an "Instruction" node.
87-- | Insert an "Instruction" node.
80instruction :: Text -> Text -> XML
88instruction :: Text -> Text -> XML
@@ -88,10 +96,6 @@ comment = node . NodeComment
88content :: Text -> XML
96content :: Text -> XML
89content = node . NodeContent
97content = node . NodeContent
95-- | Mass-convert to nodes.
99-- | Mass-convert to nodes.
97-- > let array = element "container" $ many "wrapper" [1..3]
101-- > let array = element "container" $ many "wrapper" [1..3]