@@ -5,11 +5,11 @@
55-- > {-# LANGUAGE OverloadedStrings #-}
66-- >
77-- > let doc = document "root" $ do
8−-- > element "hello" "world"
8+-- > element "hello" $ content "world"
99-- > element "hierarchy" $ do
10−-- > element "simple" $ toXML True
11−-- > element "as" "it should be"
12−-- > toXML $ Just "like this"
10+-- > element "simple" True
11+-- > element "as" ("it should be" :: Text)
12+-- > toXML $ Just . T.pack $ "like this"
1313-- > comment "that's it!"
1414--
1515
@@ -24,7 +24,7 @@ module Text.XML.Writer
2424 , node
2525 , instruction
2626 , comment
27− , element, elementA
27+ , element, elementMaybe, elementA
2828 , content
2929 , empty
3030 , many
@@ -64,17 +64,25 @@ pprint = TL.putStrLn . renderText def { rsPretty = True }
6464render :: XML -> [Node]
6565render = DL.toList . execWriter
6666
67−-- | Insert one node
67+-- | Do nothing.
68+empty :: XML
69+empty = return ()
70+
71+-- | Insert one node.
6872node :: Node -> XML
6973node = tell . DL.singleton
7074
7175-- | Insert an "Element" node constructed with name and children.
72−element :: Name -> XML -> XML
73−element name children = node . NodeElement $! Element name def (render children)
76+element :: (ToXML a) => Name -> a -> XML
77+element name children = node . NodeElement $! Element name def (render $ toXML children)
78+
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)
7482
7583-- | Insert an "Element" node constructed with name, attributes and children.
76−elementA :: Name -> [(Name, Text)] -> XML -> XML
77−elementA name attrs children = node . NodeElement $! Element name (M.fromList attrs) (render children)
84+elementA :: (ToXML a) => Name -> [(Name, Text)] -> a -> XML
85+elementA name attrs children = node . NodeElement $! Element name (M.fromList attrs) (render $ toXML children)
7886
7987-- | Insert an "Instruction" node.
8088instruction :: Text -> Text -> XML
@@ -88,10 +96,6 @@ comment = node . NodeComment
8896content :: Text -> XML
8997content = node . NodeContent
9098
91−-- | Do nothing.
92−empty :: XML
93−empty = return ()
94−
9599-- | Mass-convert to nodes.
96100--
97101-- > let array = element "container" $ many "wrapper" [1..3]