mirror of
https://github.com/typeable/xml-conduit-writer.git
synced 2024-08-17 12:00:29 +03:00
element accepts any ToXML-convertible value
This commit is contained in:
parent
eb7a506419
commit
f7807ed09a
@ -5,11 +5,11 @@
|
|||||||
-- > {-# LANGUAGE OverloadedStrings #-}
|
-- > {-# LANGUAGE OverloadedStrings #-}
|
||||||
-- >
|
-- >
|
||||||
-- > let doc = document "root" $ do
|
-- > let doc = document "root" $ do
|
||||||
-- > element "hello" "world"
|
-- > element "hello" $ content "world"
|
||||||
-- > element "hierarchy" $ do
|
-- > element "hierarchy" $ do
|
||||||
-- > element "simple" $ toXML True
|
-- > element "simple" True
|
||||||
-- > element "as" "it should be"
|
-- > element "as" ("it should be" :: Text)
|
||||||
-- > toXML $ Just "like this"
|
-- > toXML $ Just . T.pack $ "like this"
|
||||||
-- > comment "that's it!"
|
-- > comment "that's it!"
|
||||||
--
|
--
|
||||||
|
|
||||||
@ -24,7 +24,7 @@ module Text.XML.Writer
|
|||||||
, node
|
, node
|
||||||
, instruction
|
, instruction
|
||||||
, comment
|
, comment
|
||||||
, element, elementA
|
, element, elementMaybe, elementA
|
||||||
, content
|
, content
|
||||||
, empty
|
, empty
|
||||||
, many
|
, many
|
||||||
@ -64,17 +64,25 @@ pprint = TL.putStrLn . renderText def { rsPretty = True }
|
|||||||
render :: XML -> [Node]
|
render :: XML -> [Node]
|
||||||
render = DL.toList . execWriter
|
render = DL.toList . execWriter
|
||||||
|
|
||||||
-- | Insert one node
|
-- | Do nothing.
|
||||||
|
empty :: XML
|
||||||
|
empty = return ()
|
||||||
|
|
||||||
|
-- | Insert one node.
|
||||||
node :: Node -> XML
|
node :: Node -> XML
|
||||||
node = tell . DL.singleton
|
node = tell . DL.singleton
|
||||||
|
|
||||||
-- | Insert an "Element" node constructed with name and children.
|
-- | Insert an "Element" node constructed with name and children.
|
||||||
element :: Name -> XML -> XML
|
element :: (ToXML a) => Name -> a -> XML
|
||||||
element name children = node . NodeElement $! Element name def (render children)
|
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.
|
-- | Insert an "Element" node constructed with name, attributes and children.
|
||||||
elementA :: Name -> [(Name, Text)] -> XML -> XML
|
elementA :: (ToXML a) => Name -> [(Name, Text)] -> a -> XML
|
||||||
elementA name attrs children = node . NodeElement $! Element name (M.fromList attrs) (render children)
|
elementA name attrs children = node . NodeElement $! Element name (M.fromList attrs) (render $ toXML children)
|
||||||
|
|
||||||
-- | Insert an "Instruction" node.
|
-- | Insert an "Instruction" node.
|
||||||
instruction :: Text -> Text -> XML
|
instruction :: Text -> Text -> XML
|
||||||
@ -88,10 +96,6 @@ comment = node . NodeComment
|
|||||||
content :: Text -> XML
|
content :: Text -> XML
|
||||||
content = node . NodeContent
|
content = node . NodeContent
|
||||||
|
|
||||||
-- | Do nothing.
|
|
||||||
empty :: XML
|
|
||||||
empty = return ()
|
|
||||||
|
|
||||||
-- | Mass-convert to nodes.
|
-- | Mass-convert to nodes.
|
||||||
--
|
--
|
||||||
-- > let array = element "container" $ many "wrapper" [1..3]
|
-- > let array = element "container" $ many "wrapper" [1..3]
|
||||||
|
@ -24,7 +24,7 @@ main :: IO ()
|
|||||||
main = do
|
main = do
|
||||||
pprint $ document "root" $ do
|
pprint $ document "root" $ do
|
||||||
element "{ns:uri}pseudo:prefix" $ do
|
element "{ns:uri}pseudo:prefix" $ do
|
||||||
element "unprefixed" "empty NS"
|
element "unprefixed" $ comment "empty NS"
|
||||||
element "pseudo:prefixed" $ comment "wrong!"
|
element "pseudo:prefixed" $ comment "wrong!"
|
||||||
|
|
||||||
element ("sns" !: "{silly:ns:uri}spam") $ do
|
element ("sns" !: "{silly:ns:uri}spam") $ do
|
||||||
@ -41,7 +41,7 @@ main = do
|
|||||||
pprint $ soap () $ do
|
pprint $ soap () $ do
|
||||||
element ("v" !: "{vendor:uri}request") $ do
|
element ("v" !: "{vendor:uri}request") $ do
|
||||||
element "complex" $ do
|
element "complex" $ do
|
||||||
element "key" "value"
|
element "key" $ T.pack "value"
|
||||||
elementA "tag" [("key", "value")] empty
|
elementA "tag" [("key", "value")] empty
|
||||||
element "text" $ content "some text"
|
element "text" $ content "some text"
|
||||||
element "bool" $ toXML True
|
element "bool" $ toXML True
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
name: xml-conduit-writer
|
name: xml-conduit-writer
|
||||||
version: 0.1.0.1
|
version: 0.1.1.0
|
||||||
synopsis: Warm and fuzzy creation of XML documents.
|
synopsis: Warm and fuzzy creation of XML documents.
|
||||||
description:
|
description:
|
||||||
“It can scarcely be denied that the supreme goal of
|
“It can scarcely be denied that the supreme goal of
|
||||||
|
Loading…
Reference in New Issue
Block a user