git.iko.soy/typeable/dom-parser
on master help sign in
Upd: improve docs, split module `Common`
commit467a73396c720883142ada7f29190542447c346e authorAleksey Uimanov <s9gf4ult@gmail.com> dateSat 7 Jan 2017, 00:20 (10 years ago) parent350b2eb
8 files changed+58−39
dom-parser.cabal+2−1 src/Text/XML/DOM/Parser.hs+5−3 src/Text/XML/DOM/Parser/Buildable.hs+26 src/Text/XML/DOM/Parser/Combinators.hs+3−3 src/Text/XML/DOM/Parser/Common.hs → src/Text/XML/DOM/Parser/Content.hs+9−29 src/Text/XML/DOM/Parser/FromAttribute.hs+4−1 src/Text/XML/DOM/Parser/FromDom.hs+1−1 src/Text/XML/DOM/Parser/Types.hs+8−1
dom-parser.cabal+2−1
@@ -57,8 +57,9 @@ library
5757 , xml-conduit
5858 , xml-lens
5959 exposed-modules: Text.XML.DOM.Parser
60+ , Text.XML.DOM.Parser.Buildable
6061 , Text.XML.DOM.Parser.Combinators
61− , Text.XML.DOM.Parser.Common
62+ , Text.XML.DOM.Parser.Content
6263 , Text.XML.DOM.Parser.FromAttribute
6364 , Text.XML.DOM.Parser.FromDom
6465 , Text.XML.DOM.Parser.Types
src/Text/XML/DOM/Parser.hs+5−3
@@ -1,13 +1,15 @@
11module Text.XML.DOM.Parser
2− ( module Text.XML.DOM.Parser.Combinators
3− , module Text.XML.DOM.Parser.Common
2+ ( module Text.XML.DOM.Parser.Buildable
3+ , module Text.XML.DOM.Parser.Combinators
4+ , module Text.XML.DOM.Parser.Content
45 , module Text.XML.DOM.Parser.FromAttribute
56 , module Text.XML.DOM.Parser.FromDom
67 , module Text.XML.DOM.Parser.Types
78 ) where
89
10+import Text.XML.DOM.Parser.Buildable
911import Text.XML.DOM.Parser.Combinators
10−import Text.XML.DOM.Parser.Common
12+import Text.XML.DOM.Parser.Content
1113import Text.XML.DOM.Parser.FromAttribute
1214import Text.XML.DOM.Parser.FromDom
1315import Text.XML.DOM.Parser.Types
src/Text/XML/DOM/Parser/Buildable.hsnew file+26
@@ -0,0 +1,26 @@
1+module Text.XML.DOM.Parser.Buildable
2+ ( Buildable(..)
3+ ) where
4+
5+import Data.Functor.Identity
6+import Data.List.NonEmpty as NE
7+import Data.Maybe
8+
9+-- | Class of traversable functors which may be constructed from list
10+class Traversable f => Buildable f where
11+ -- | If method return Nothing this means we can not build
12+ -- traversable from given list. In this case 'inFilteredTrav' should
13+ -- fail traversing.
14+ build :: [a] -> Maybe (f a)
15+
16+instance Buildable Identity where
17+ build = fmap Identity . listToMaybe
18+
19+instance Buildable [] where
20+ build = Just
21+
22+instance Buildable Maybe where
23+ build = Just . listToMaybe
24+
25+instance Buildable NonEmpty where
26+ build = NE.nonEmpty
src/Text/XML/DOM/Parser/Combinators.hs+3−3
@@ -2,7 +2,7 @@ module Text.XML.DOM.Parser.Combinators
22 ( -- * Generic combinators to traverse descendants
33 traverseElems
44 , inFilteredTrav
5− -- * Using 'DomTraversable'
5+ -- * Using 'Buildable'
66 , inElemTrav
77 , inElem
88 , inElemAll
@@ -26,7 +26,7 @@ import Data.Monoid
2626import Data.Text as T
2727import Data.Traversable
2828import Text.XML
29−import Text.XML.DOM.Parser.Common
29+import Text.XML.DOM.Parser.Buildable
3030import Text.XML.DOM.Parser.Types
3131import Text.XML.Lens
3232
@@ -57,7 +57,7 @@ inFilteredTrav
5757 :: (Monad m, Foldable g, Buildable f)
5858 => ([Element] -> (DomPath, [Element]))
5959 -- ^ Function returning some filtered elements with path suffixes which will
60− -- be appended to parser's state
60+ -- be appended to parser's state. The argument is a list of current elements.
6161 -> DomParserT Identity m a
6262 -> DomParserT g m (f a)
6363inFilteredTrav deeper = traverseElems trav
src/Text/XML/DOM/Parser/Common.hs → src/Text/XML/DOM/Parser/Content.hsrenamed+9−29
@@ -1,8 +1,6 @@
1−module Text.XML.DOM.Parser.Common
2− ( -- * Buildable
3− Buildable(..)
4− -- * Content readers
5− , readBool
1+module Text.XML.DOM.Parser.Content
2+ ( -- * Content readers
3+ readBool
64 , readChar
75 -- ** Reader constructors
86 , maybeReadContent
@@ -24,9 +22,7 @@ import Control.Lens
2422import Control.Monad
2523import Control.Monad.Except
2624import Data.List as L
27−import Data.List.NonEmpty as NE
2825import Data.Map.Strict as M
29−import Data.Maybe
3026import Data.Monoid
3127import Data.Text as T
3228import Data.Typeable
@@ -34,25 +30,6 @@ import Text.Read
3430import Text.XML.DOM.Parser.Types
3531import Text.XML.Lens
3632
37−-- | Class of traversable functors which may be constructed from list. Or may
38−-- not.
39−class Traversable f => Buildable f where
40− -- | If method return Nothing this means we can not build
41− -- traversable from given list. In this case 'inFilteredTrav' should
42− -- fail traversing.
43− build :: [a] -> Maybe (f a)
44−
45−instance Buildable Identity where
46− build = fmap Identity . listToMaybe
47−
48−instance Buildable [] where
49− build = Just
50−
51−instance Buildable Maybe where
52− build = Just . listToMaybe
53−
54−instance Buildable NonEmpty where
55− build = NE.nonEmpty
5633
5734-- | @since 1.0.0
5835readBool :: Text -> Either Text Bool
@@ -76,7 +53,8 @@ readChar t = case T.unpack $ T.strip t of
7653 _ -> Left "Should have exactly one non-blank character"
7754
7855-- | If reader returns 'Nothing' then resulting function returns 'Left
79−-- "error message"'
56+-- "error message"'. 'Typeable' is used for generating usefull error
57+-- message.
8058--
8159-- @since 1.0.0
8260maybeReadContent
@@ -94,7 +72,8 @@ maybeReadContent f t = maybe (Left msg) Right $ f t
9472
9573
9674-- | Tries to read given text to value using 'Read'. Useful to use
97−-- with 'parseContent' and 'parseAttribute'
75+-- with 'parseContent' and 'parseAttribute'. Content is stripped
76+-- before reading.
9877readContent
9978 :: (Read a, Typeable a)
10079 => Text
@@ -186,7 +165,8 @@ parseAttribute attrName parser =
186165 Nothing -> throwParserError $ PEAttributeNotFound attrName
187166 Just a -> return a
188167
189−-- | Parses attribute with given name.
168+-- | Parses attribute with given name. Returns Nothing if attribute is
169+-- not found.
190170--
191171-- @since 1.0.0
192172parseAttributeMaybe
src/Text/XML/DOM/Parser/FromAttribute.hs+4−1
@@ -21,8 +21,11 @@ import Data.Scientific
2121import Data.Text as T
2222import Data.Typeable
2323import Data.Void
24−import Text.XML.DOM.Parser.Common
24+import Text.XML.DOM.Parser.Content
2525
26+-- | Class of types which can be get from attribute value. Method
27+-- 'fromAttribute' is a convenient default parameter for
28+-- 'parseAttribute'
2629class FromAttribute a where
2730 fromAttribute
2831 :: Text
src/Text/XML/DOM/Parser/FromDom.hs+1−1
@@ -27,7 +27,7 @@ import Data.Text as T hiding (empty)
2727import Data.Typeable
2828import Data.Void
2929import Text.XML
30−import Text.XML.DOM.Parser.Common
30+import Text.XML.DOM.Parser.Content
3131import Text.XML.DOM.Parser.Types
3232import TypeFun.Data.List hiding (Union)
3333
src/Text/XML/DOM/Parser/Types.hs+8−1
@@ -57,18 +57,24 @@ instance IsString NameMatcher where
5757instance Show NameMatcher where
5858 show = T.unpack . _nmShow
5959
60+-- | Makes matcher which matches only local part of name igoring
61+-- namespace and prefix. Local name matching is case sensitive.
6062matchLocalName :: Text -> NameMatcher
6163matchLocalName tname = NameMatcher
6264 { _nmMatch = \n -> nameLocalName n == tname
6365 , _nmShow = tname
6466 }
6567
68+-- | Makes matcher which matches only local part of name igoring
69+-- namespace and prefix. Local name matching is case insensitive. This
70+-- is the most common case.
6671matchCILocalName :: Text -> NameMatcher
6772matchCILocalName tname = NameMatcher
6873 { _nmMatch = \n -> CI.mk (nameLocalName n) == CI.mk tname
6974 , _nmShow = tname
7075 }
7176
77+-- | Makes matcher which match name by 'Eq' with given
7278matchName :: Name -> NameMatcher
7379matchName n = NameMatcher
7480 { _nmMatch = (== n)
@@ -87,12 +93,13 @@ data ParserError
8793 -- | Tag not found which should be.
8894 = PENotFound
8995 { _pePath :: DomPath
96+ -- ^ Path of element error occured in
9097 }
9198
9299 -- | Tag contents has wrong format, (could not read text to value)
93100 | PEWrongFormat
94101 { _peDetails :: Text
95− , _pePath :: DomPath -- ^ path of element
102+ , _pePath :: DomPath
96103 }
97104
98105 -- | Could not parse attribute