diff --git a/src/Text/XML/Writer.hs b/src/Text/XML/Writer.hs index a4ee998..4ad04ca 100644 --- a/src/Text/XML/Writer.hs +++ b/src/Text/XML/Writer.hs @@ -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] diff --git a/test/Main.hs b/test/Main.hs index 4c92210..81e3acf 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -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 diff --git a/xml-conduit-writer.cabal b/xml-conduit-writer.cabal index b15a46c..9139e78 100644 --- a/xml-conduit-writer.cabal +++ b/xml-conduit-writer.cabal @@ -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