git.iko.soy/typeable/xml-conduit-writer
on master help sign in
element accepts any ToXML-convertible value
commitf7807ed09a2e608e3e457ea64a0c2e3c08c45d2e authorwiz <aenor.realm@gmail.com> dateFri 12 Apr 2013, 13:16 (14 years ago) parenteb7a506
3 files changed+21−17
src/Text/XML/Writer.hs+18−14 test/Main.hs+2−2 xml-conduit-writer.cabal+1−1
src/Text/XML/Writer.hs+18−14
@@ -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]
test/Main.hs+2−2
@@ -24,7 +24,7 @@ main :: IO ()
2424main = do
2525 pprint $ document "root" $ do
2626 element "{ns:uri}pseudo:prefix" $ do
27− element "unprefixed" "empty NS"
27+ element "unprefixed" $ comment "empty NS"
2828 element "pseudo:prefixed" $ comment "wrong!"
2929
3030 element ("sns" !: "{silly:ns:uri}spam") $ do
@@ -41,7 +41,7 @@ main = do
4141 pprint $ soap () $ do
4242 element ("v" !: "{vendor:uri}request") $ do
4343 element "complex" $ do
44− element "key" "value"
44+ element "key" $ T.pack "value"
4545 elementA "tag" [("key", "value")] empty
4646 element "text" $ content "some text"
4747 element "bool" $ toXML True
xml-conduit-writer.cabal+1−1
@@ -1,5 +1,5 @@
11name: xml-conduit-writer
2−version: 0.1.0.1
2+version: 0.1.1.0
33synopsis: Warm and fuzzy creation of XML documents.
44description:
55 “It can scarcely be denied that the supreme goal of