git.iko.soy/typeable/dom-parser
on master help sign in
Upd: use Name instead of Text for names
7aa37c9Alexey Uimanov
src/Text/XML/DOM/Parser/Class.hs+5−1
@@ -165,7 +165,11 @@ voidFromDom :: (Monad m) => DomParserT Identity m Void
165voidFromDom = empty
165voidFromDom = empty
166
166
167class FromAttribute a where
167class FromAttribute a where
168− fromAttribute :: Text -> Either Text a
168+ fromAttribute
169+ :: Text
170+ -- ^ Attribute contents to parse
171+ -> Either Text a
172+ -- ^ Either error message or result
169
173
170instance FromAttribute () where
174instance FromAttribute () where
171 fromAttribute _ = Right ()
175 fromAttribute _ = Right ()
src/Text/XML/DOM/Parser/Combinators.hs+15−17
@@ -86,38 +86,38 @@ inFilteredTrav deeper = traverseElems trav
86
86
87inElemTrav
87inElemTrav
88 :: (Monad m, Foldable g, DomTraversable f)
88 :: (Monad m, Foldable g, DomTraversable f)
89− => Text
89+ => Name -- ^ Name of tag to traverse in
90 -> DomParserT Identity m a
90 -> DomParserT Identity m a
91 -> DomParserT g m (f a)
91 -> DomParserT g m (f a)
92inElemTrav n = inFilteredTrav deeper
92inElemTrav n = inFilteredTrav deeper
93 where
93 where
94− deeper = (DomPath [n],) . toListOf (folded . nodes . folded . _Element . ell n)
94+ deeper = (DomPath [n],) . toListOf (folded . nodes . folded . _Element . el n)
95
95
96-- | Runs parser inside first children element with given name
96-- | Runs parser inside first children element with given name
97inElem
97inElem
98 :: (Monad m, Foldable g)
98 :: (Monad m, Foldable g)
99− => Text
99+ => Name
100 -> DomParserT Identity m a
100 -> DomParserT Identity m a
101 -> DomParserT g m a
101 -> DomParserT g m a
102inElem n = fmap runIdentity . inElemTrav n
102inElem n = fmap runIdentity . inElemTrav n
103
103
104inElemAll
104inElemAll
105 :: (Monad m, Foldable g)
105 :: (Monad m, Foldable g)
106− => Text
106+ => Name
107 -> DomParserT Identity m a
107 -> DomParserT Identity m a
108 -> DomParserT g m [a]
108 -> DomParserT g m [a]
109inElemAll = inElemTrav
109inElemAll = inElemTrav
110
110
111inElemMay
111inElemMay
112 :: (Monad m, Foldable g)
112 :: (Monad m, Foldable g)
113− => Text
113+ => Name
114 -> DomParserT Identity m a
114 -> DomParserT Identity m a
115 -> DomParserT g m (Maybe a)
115 -> DomParserT g m (Maybe a)
116inElemMay = inElemTrav
116inElemMay = inElemTrav
117
117
118inElemNe
118inElemNe
119 :: (Monad m, Foldable g)
119 :: (Monad m, Foldable g)
120− => Text
120+ => Name
121 -> DomParserT Identity m a
121 -> DomParserT Identity m a
122 -> DomParserT g m (NonEmpty a)
122 -> DomParserT g m (NonEmpty a)
123inElemNe = inElemTrav
123inElemNe = inElemTrav
@@ -159,11 +159,11 @@ divePath path = magnify $ to modElems
159 . over pdPath (<> path)
159 . over pdPath (<> path)
160 diver :: Fold Element Element
160 diver :: Fold Element Element
161 diver = foldr (.) id $ map toDive $ unDomPath path
161 diver = foldr (.) id $ map toDive $ unDomPath path
162− toDive n = nodes . folded . _Element . ell n
162+ toDive n = nodes . folded . _Element . el n
163
163
164diveElem
164diveElem
165 :: (Monad m, Foldable g)
165 :: (Monad m, Foldable g)
166− => Text
166+ => Name
167 -> DomParserT [] m a
167 -> DomParserT [] m a
168 -> DomParserT g m a
168 -> DomParserT g m a
169diveElem p = divePath $ DomPath [p]
169diveElem p = divePath $ DomPath [p]
@@ -211,15 +211,15 @@ ignoreBlank = ignoreElem test
211-- | Returns name of current element.
211-- | Returns name of current element.
212--
212--
213-- @since 1.0.0
213-- @since 1.0.0
214−getCurrentName :: (Monad m) => DomParserT Identity m Text
214+getCurrentName :: (Monad m) => DomParserT Identity m Name
215−getCurrentName = view $ pdElements . to runIdentity . localName
215+getCurrentName = view $ pdElements . to runIdentity . name
216
216
217-- | If name of current tag differs from first argument throws 'PENotFound' with
217-- | If name of current tag differs from first argument throws 'PENotFound' with
218-- tag name replaced in last path's segment. Useful for checking root
218-- tag name replaced in last path's segment. Useful for checking root
219-- document's element name.
219-- document's element name.
220checkCurrentName
220checkCurrentName
221 :: (Monad m)
221 :: (Monad m)
222− => Text
222+ => Name
223 -> DomParserT Identity m ()
223 -> DomParserT Identity m ()
224checkCurrentName n = do
224checkCurrentName n = do
225 cn <- getCurrentName
225 cn <- getCurrentName
@@ -294,18 +294,16 @@ getCurrentAttributes = view $ pdElements . to runIdentity . attrs
294-- | Returns element with given name or 'Nothing'
294-- | Returns element with given name or 'Nothing'
295--
295--
296-- @since 1.0.0
296-- @since 1.0.0
297−getCurrentAttribute :: (Monad m) => Text -> DomParserT Identity m (Maybe Text)
297+getCurrentAttribute :: (Monad m) => Name -> DomParserT Identity m (Maybe Text)
298−getCurrentAttribute attrName'
298+getCurrentAttribute attrName
299 = preview $ pdElements . to runIdentity . attr attrName
299 = preview $ pdElements . to runIdentity . attr attrName
300− where
301− attrName = Name attrName' Nothing Nothing
302
300
303-- | Parses attribute with given name, throws error if attribute is not found.
301-- | Parses attribute with given name, throws error if attribute is not found.
304--
302--
305-- @since 1.0.0
303-- @since 1.0.0
306parseAttribute
304parseAttribute
307 :: (Monad m)
305 :: (Monad m)
308− => Text
306+ => Name
309 -- ^ Attribute name
307 -- ^ Attribute name
310 -> (Text -> Either Text a)
308 -> (Text -> Either Text a)
311 -- ^ Attribute content parser
309 -- ^ Attribute content parser
@@ -320,7 +318,7 @@ parseAttribute attrName parser =
320-- @since 1.0.0
318-- @since 1.0.0
321parseAttributeMaybe
319parseAttributeMaybe
322 :: (Monad m)
320 :: (Monad m)
323− => Text
321+ => Name
324 -- ^ Attribute name
322 -- ^ Attribute name
325 -> (Text -> Either Text a)
323 -> (Text -> Either Text a)
326 -- ^ Attribute content parser
324 -- ^ Attribute content parser
src/Text/XML/DOM/Parser/Types.hs+5−5
@@ -33,8 +33,8 @@ import Text.XML
33import Text.XML.Lens
33import Text.XML.Lens
34
34
35newtype DomPath = DomPath
35newtype DomPath = DomPath
36− { unDomPath :: [Text]
36+ { unDomPath :: [Name]
37− } deriving (Eq, Ord, Show, Read, Monoid)
37+ } deriving (Eq, Ord, Show, Monoid)
38
38
39-- | DOM parser error description.
39-- | DOM parser error description.
40data ParserError
40data ParserError
@@ -51,7 +51,7 @@ data ParserError
51
51
52 -- | Could not parse attribute
52 -- | Could not parse attribute
53 | PEAttributeWrongFormat
53 | PEAttributeWrongFormat
54− { _peAttributeName :: Text
54+ { _peAttributeName :: Name
55 , _peDetails :: Text
55 , _peDetails :: Text
56 , _pePath :: DomPath
56 , _pePath :: DomPath
57 }
57 }
@@ -63,7 +63,7 @@ data ParserError
63
63
64 -- | Expected attribute but not found
64 -- | Expected attribute but not found
65 | PEAttributeNotFound
65 | PEAttributeNotFound
66− { _peAttributeName :: Text
66+ { _peAttributeName :: Name
67 , _pePath :: DomPath
67 , _pePath :: DomPath
68 }
68 }
69
69
@@ -122,7 +122,7 @@ runDomParserT
122runDomParserT doc par =
122runDomParserT doc par =
123 let pd = ParserData
123 let pd = ParserData
124 { _pdElements = doc ^. root . to pure
124 { _pdElements = doc ^. root . to pure
125− , _pdPath = DomPath [doc ^. root . localName]
125+ , _pdPath = DomPath [doc ^. root . name]
126 }
126 }
127 in runExceptT $ runReaderT par pd
127 in runExceptT $ runReaderT par pd
128
128
test/Main.hs+1−1
@@ -33,7 +33,7 @@ testStructureAttributes = do
33 int <- parseAttribute "int" readContent
33 int <- parseAttribute "int" readContent
34 bool <- parseAttribute "bool" readBool
34 bool <- parseAttribute "bool" readBool
35 return $ TestStructure
35 return $ TestStructure
36− { tsName = name
36+ { tsName = nameLocalName name
37 , tsInts = [int]
37 , tsInts = [int]
38 , tsBools = pure bool }
38 , tsBools = pure bool }
39
39