git.iko.soy/typeable/xml-conduit-writer
on master help sign in
element accepts any ToXML-convertible value
f7807edwiz
src/Text/XML/Writer.hs+18−14
@@ -5,11 +5,11 @@
5-- > {-# LANGUAGE OverloadedStrings #-}
5-- > {-# LANGUAGE OverloadedStrings #-}
6-- >
6-- >
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!"
14--
14--
15
15
@@ -24,7 +24,7 @@ module Text.XML.Writer
24 , node
24 , node
25 , instruction
25 , instruction
26 , comment
26 , comment
27− , element, elementA
27+ , element, elementMaybe, elementA
28 , content
28 , content
29 , empty
29 , empty
30 , many
30 , many
@@ -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
66
66
67+-- | Do nothing.
68+empty :: XML
69+empty = return ()
70+
67−-- | Insert one node
71+-- | Insert one node.
68node :: Node -> XML
72node :: Node -> XML
69node = tell . DL.singleton
73node = tell . DL.singleton
70
74
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)
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)
74
82
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)
78
86
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
90
98
91−-- | Do nothing.
92−empty :: XML
93−empty = return ()
94−
95-- | Mass-convert to nodes.
99-- | Mass-convert to nodes.
96--
100--
97-- > let array = element "container" $ many "wrapper" [1..3]
101-- > let array = element "container" $ many "wrapper" [1..3]
test/Main.hs+2−2
@@ -24,7 +24,7 @@ main :: IO ()
24main = do
24main = do
25 pprint $ document "root" $ do
25 pprint $ document "root" $ do
26 element "{ns:uri}pseudo:prefix" $ do
26 element "{ns:uri}pseudo:prefix" $ do
27− element "unprefixed" "empty NS"
27+ element "unprefixed" $ comment "empty NS"
28 element "pseudo:prefixed" $ comment "wrong!"
28 element "pseudo:prefixed" $ comment "wrong!"
29
29
30 element ("sns" !: "{silly:ns:uri}spam") $ do
30 element ("sns" !: "{silly:ns:uri}spam") $ do
@@ -41,7 +41,7 @@ main = do
41 pprint $ soap () $ do
41 pprint $ soap () $ do
42 element ("v" !: "{vendor:uri}request") $ do
42 element ("v" !: "{vendor:uri}request") $ do
43 element "complex" $ do
43 element "complex" $ do
44− element "key" "value"
44+ element "key" $ T.pack "value"
45 elementA "tag" [("key", "value")] empty
45 elementA "tag" [("key", "value")] empty
46 element "text" $ content "some text"
46 element "text" $ content "some text"
47 element "bool" $ toXML True
47 element "bool" $ toXML True
xml-conduit-writer.cabal+1−1
@@ -1,5 +1,5 @@
1name: xml-conduit-writer
1name: xml-conduit-writer
2−version: 0.1.0.1
2+version: 0.1.1.0
3synopsis: Warm and fuzzy creation of XML documents.
3synopsis: Warm and fuzzy creation of XML documents.
4description:
4description:
5 “It can scarcely be denied that the supreme goal of
5 “It can scarcely be denied that the supreme goal of