packages feed

xml-extractors 0.2.1.0 → 0.3.0.0

raw patch · 5 files changed

+59/−35 lines, 5 files

Files

+ changelog.md view
@@ -0,0 +1,4 @@+0.3.0.0++  * Added `anyContent`+  * Added `choice`
src/Text/XML/Light/Extractors.hs view
@@ -1,6 +1,6 @@-{-# LANGUAGE NoMonomorphismRestriction, GeneralizedNewtypeDeriving #-}+{-# LANGUAGE GeneralizedNewtypeDeriving #-} --- | A library for making extraction of information from parsed XML easier.+-- | Functions to extract data from parsed XML. -- -- = Example --@@ -80,6 +80,8 @@   , element   , text   , textAs+  , choice+  , anyContent   , eoc   , only   ) @@ -87,7 +89,6 @@  import Control.Applicative - import           Text.XML.Light.Types as XML import qualified Text.XML.Light.Proc  as XML @@ -147,6 +148,8 @@   -- | @only p@ fails if there is more contents than extracted by @p@.+--+-- > only p = p <* eoc only :: ContentsExtractor a -> ContentsExtractor a only p = p <* eoc @@ -169,3 +172,13 @@ -- | Extracts text applied to a conversion function. textAs :: (String -> Either Err a) -> ContentsExtractor a textAs = ContentsExtractor . Internal.textAs+++-- | Extracts first matching.+choice :: [ContentsExtractor a] -> ContentsExtractor a+choice = foldr (<|>) empty+++-- | Extracts one 'Content' item.+anyContent :: ContentsExtractor Content+anyContent = ContentsExtractor Internal.anyContent
src/Text/XML/Light/Extractors/Internal.hs view
@@ -19,6 +19,7 @@   , element   , text   , textAs+  , anyContent   , eoc   )  where@@ -28,7 +29,7 @@ import Control.Monad.Trans.State  import           Text.XML.Light.Types as XML-import qualified Text.XML.Light.Proc  as XML+import qualified Text.XML.Light       as XML  import           Text.XML.Light.Extractors.Internal.Result hiding (throwError, throwFatal) import qualified Text.XML.Light.Extractors.Internal.Result as R@@ -38,23 +39,23 @@ elemName :: Element -> String elemName = qName . elName -qname :: String -> QName-qname name = QName name Nothing Nothing- --------------------------------------------------------------------------------  -- | Location for some content.+--+-- For now it is a reversed list of content indices and element+-- names. This may change to something less \"stringly typed\". type Path = [String]  -addIdx :: Int -> Path -> Path-addIdx i p = show i : p+pushIdx :: Int -> Path -> Path+pushIdx i p = show i : p -addElem :: XML.Element -> Path -> Path-addElem e p = elemName e : p+pushElem :: XML.Element -> Path -> Path+pushElem e p = elemName e : p -addAttrib :: String -> Path -> Path-addAttrib a p = ('@':a) : p+pushAttrib :: String -> Path -> Path+pushAttrib a p = ('@':a) : p  -------------------------------------------------------------------------------- @@ -112,8 +113,8 @@          -> ElementExtractor a attribAs name f = do   (path,x) <- ask-  let path' = addAttrib name path-  case XML.lookupAttr (qname name) (elAttribs x) of+  let path' = pushAttrib name path+  case XML.lookupAttr (XML.unqual name) (elAttribs x) of     Nothing -> throwError $ ExtractionErr (ErrAttr name x) path     Just s  ->       case f s of@@ -143,7 +144,6 @@     Left e   -> throwError (ExtractionErr e path)     Right a  -> return a   - --------------------------------------------------------------------------------  type Ctx = (Path, Int, [XML.Content])@@ -161,7 +161,7 @@   case xs of     []     -> throwError $ ExtractionErr (ErrNull expect) path     (x:xs) -> do-      case f x (addIdx i path) of+      case f x (pushIdx i path) of         Fatal e -> throwFatal e         Fail  e -> throwError e         Ok    a -> do@@ -169,19 +169,11 @@           return a  -eoc :: ContentsExtractor ()-eoc = do-  (path,_,xs) <- get-  case xs of-    []    -> return ()-    (x:_) -> throwError (ExtractionErr (ErrEnd x) path)-- element :: String -> ElementExtractor a -> ContentsExtractor a element name p = first expect go   where     go (Elem x) path-      | elemName x == name = escalate $ runElementExtractor p x (addElem x path)+      | elemName x == name = escalate $ runElementExtractor p x (pushElem x path)     go c        path       = Fail (ExtractionErr (ErrExpect expect c) path)      expect = "element " ++ show name@@ -197,4 +189,17 @@     go c path = Fail $ ExtractionErr (ErrExpect "text" c) path  +text :: ContentsExtractor String text = textAs return+++anyContent :: ContentsExtractor Content+anyContent = first "something" (const . return)+++eoc :: ContentsExtractor ()+eoc = do+  (path,_,xs) <- get+  case xs of+    []    -> return ()+    (x:_) -> throwError (ExtractionErr (ErrEnd x) path)
src/Text/XML/Light/Extractors/ShowErr.hs view
@@ -1,4 +1,4 @@--- | This module provide default functions to translate errors into strings.+-- | This module provides functions to translate errors into strings. module Text.XML.Light.Extractors.ShowErr   ( showExtractionErr   , showErr@@ -14,7 +14,8 @@   showExtractionErr :: ExtractionErr -> String-showExtractionErr (ExtractionErr e path) = showErr e ++ "in path: " ++ showPath path+showExtractionErr (ExtractionErr e path) =+  unlines [showErr e ++ "in path: " ++ showPath path]  showPath :: Path -> String showPath = intercalate "/" . reverse
xml-extractors.cabal view
@@ -1,12 +1,12 @@ name:                xml-extractors-version:             0.2.1.0-synopsis:            Simple wrapper over xml to extract data from parsed xml+version:             0.3.0.0+synopsis:            Wrapper over xml to extract data from parsed xml description:   This is a library to simplify extraction of data from parsed xml.   .   See the 'Text.XML.Light.Extractors' module for an example.   .-  = Motivation+  /Motivation/   .   The `xml` package provides functions to parse and get information from   xml data. It will parse an xml string into a generic tree@@ -17,9 +17,9 @@   If there is an error during extraction (expected information is   absent or wrong), it will return an error value with position information.   .-  = Some limitations+  /Some limitations/   .-    * Only handles unqualified names.+    * Only handles unqualified names. (This is by design to simplify usage.)   .     * No column number and sometimes no line number reference in error values.  @@ -30,9 +30,9 @@ maintainer:          holmisen@gmail.com -- copyright:            category:            XML-tested-with:         GHC == 7.8.3+tested-with:         GHC == 7.8.3, GHC == 7.8.4 build-type:          Simple-extra-source-files:  README.md+extra-source-files:  README.md, changelog.md cabal-version:       >=1.10  library@@ -50,3 +50,4 @@                        safe >=0.3 && <0.4   hs-source-dirs:      src   default-language:    Haskell2010+--  ghc-options:         -Wall