packages feed

taggy 0.1.4 → 0.2.0

raw patch · 7 files changed

+40/−133 lines, 7 filesdep ~base

Dependency ranges changed: base

Files

src/Text/Taggy.hs view
@@ -32,8 +32,7 @@ -- * If you want to parse the document as a DOM tree and --   traverse it to find the information you need, --   use 'Text.Taggy.DOM.parseDOM'. This is especially useful---   when combined with the helpful combinators from---   "Text.Taggy.Combinators".+--   when used in conjunction with <http://hackage.haskell.org/package/taggy-lens taggy-lens>. -- * If you build some HTML manually --   or just transform some existing DOM tree --   and want to turn it into a 'Data.Text.Lazy.Text'@@ -43,12 +42,10 @@     module Text.Taggy.Types   , module Text.Taggy.Parser   , module Text.Taggy.DOM-  , module Text.Taggy.Combinators   , module Text.Taggy.Renderer   ) where   import Text.Taggy.Types import Text.Taggy.Parser import Text.Taggy.DOM-import Text.Taggy.Combinators import Text.Taggy.Renderer
− src/Text/Taggy/Combinators.hs
@@ -1,88 +0,0 @@-{-# LANGUAGE LambdaCase #-}--- |--- Module       : Text.Taggy.DOM--- Copyright    : (c) 2014 Alp Mestanogullari, Vikram Verma--- License      : BSD3--- Maintainer   : alpmestan@gmail.com--- Stability    : experimental------ Many useful combinators for querying 'Element's--- of a DOM tree.-module Text.Taggy.Combinators (hasName, hasAttr, getAttr, innerText, (//), (/&), (/*), trees, subtrees) where--import Prelude hiding (lookup)-import Data.Monoid (mconcat)-import Control.Monad (ap, (<=<))-import Data.Text (Text)-import Text.Taggy.DOM (Element(..), Node(..), AttrName, AttrValue)-import Data.HashMap.Strict (lookup, keys)---- | Does the given 'Element' have---   the given name?-hasName :: Element -> Text -> Bool-hasName = (==) . eltName---- | Does the given element have---   an attribute with the given name (or /key/)-hasAttr :: Element -> AttrName -> Bool-hasAttr = flip elem . keys . eltAttrs---- | Get the value for the given attribute name---   in the given 'Element'. Returns 'Nothing' if---   the provided 'Element' doesn't have an attribute---   with that name.-getAttr :: Element -> AttrName -> Maybe AttrValue-getAttr = flip lookup . eltAttrs---- | Get all the bits of raw text present---   everywhere below the given 'Element'---   in the DOM tree.-innerText :: Element -> Text-innerText = mconcat . map getContent . eltChildren-  where getContent = \case { NodeElement e -> innerText e; NodeContent x -> x }---- | Filter an element and its children to those---   satisfying a given predicate.-(//) :: Element -> (Element -> Bool) -> [Element]-(//) = flip filter . trees---- | Given a sequence of predicates, filter an element--- and its children, selecting only those subtrees who --- match the provided predicate for each point.------ >>> let element = (\(NodeElement e) -> e) . head . domify . taggyWith False $ "<html>foo<bar class=\"el\">baz</bar><qux class=\"el\"><quux></quux></qux></html>"--- >>> element /& [const False]--- []--- >>> element /& [flip hasAttr "class", flip hasName "quux"]--- [Element "quux" "" ""]--(/&) :: Element -> [(Element -> Bool)] -> [Element]-(/&) element [] = [element]-(/&) element (x:xs) = (/& xs) <=< filter x . catElements $ eltChildren element---- | Filter from all subtrees (including the one --- with the target as its root), those matching the --- given sequence of predicates.--(/*) :: Element -> [(Element -> Bool)] -> [Element]-(/*) element selector = concat . filter (not.null) . map (/& selector) $ trees element---- | Extracts all subtrees of its target, including the target.--trees :: Element -> [Element]-trees = ap (:) subtrees---- | Extracts all subtrees of its target, excluding the target.--subtrees :: Element -> [Element]-subtrees = ap (:) subtrees <=< catElements . eltChildren--isElement :: Node -> Bool-isElement = \case { NodeElement _ -> True; _ -> False }--unsafeFromElement :: Node -> Element-unsafeFromElement (NodeElement e) = e-unsafeFromElement _ = error "unsafeFromElement isn't well-defined, use with caution. ;-)"--catElements :: [Node] -> [Element]-catElements = map unsafeFromElement . filter isElement
src/Text/Taggy/DOM.hs view
@@ -12,8 +12,8 @@ -- way you like. -- -- This is especially useful when used in--- conjunction with <http://hackage.haskell.org/package/taggy-lens taggy-lens>, or--- with the "Text.Taggy.Combinators" module (which is used by /taggy-lens/).+-- conjunction with <http://hackage.haskell.org/package/taggy-lens taggy-lens>+ module Text.Taggy.DOM where  import Data.HashMap.Strict (HashMap)
src/Text/Taggy/Parser.hs view
@@ -5,7 +5,7 @@ -- License      : BSD3 -- Maintainer   : alpmestan@gmail.com -- Stability    : experimental--- +-- -- Parse an HTML or XML document as a list of 'Tag's -- with 'taggyWith' or 'run'. module Text.Taggy.Parser@@ -19,7 +19,7 @@   , tagscript   , tagtext   , htmlWith-  ) where +  ) where  import Control.Applicative import Data.Attoparsec.Combinator as Atto@@ -45,8 +45,8 @@                | otherwise                       = Just 0  matchUntil :: T.Text -> Parser T.Text-matchUntil endStr = -  T.dropEnd (T.length endStr) +matchUntil endStr =+  T.dropEnd (T.length endStr)     `fmap` scan 0 (scannerFor endStr)  delimitedBy :: T.Text -> T.Text -> Parser (T.Text, T.Text, T.Text)@@ -83,12 +83,12 @@           <|> return ()  ident :: Parser T.Text-ident = -  takeWhile1 (\c -> isAlphaNum c || c `elem` "-_:.")+ident =+  takeWhile1 (\c -> isAlphaNum c || c `elem` ("-_:." :: String))  attribute_ident :: Parser T.Text-attribute_ident = -  takeWhile1 (`notElem` ">=")+attribute_ident =+  takeWhile1 (`notElem` (">=" :: String))  tagopen :: Bool -> Parser Tag tagopen cventities = do@@ -116,8 +116,8 @@  attributes :: Bool -> Parser ([Attribute], Bool) attributes cventities = postProcess `fmap` go emptyL-  where -    go l =  (do autoclose <- tagends +  where+    go l =  (do autoclose <- tagends                 return (l, autoclose)             )         <|> ( do attr <- attribute cventities@@ -142,7 +142,7 @@ attribute cventities = do   skipSpace   key <- quoted <|> attribute_ident-  value <- option "" $ fmap (if cventities then convertEntities else id) $ do +  value <- option "" $ fmap (if cventities then convertEntities else id) $ do     possibly ' '     "="     possibly ' '
src/Text/Taggy/Renderer.hs view
@@ -10,7 +10,7 @@ -- using the excellent blaze markup rendering library. module Text.Taggy.Renderer where -import Data.Foldable (Foldable(foldMap))+import Data.Foldable (foldMap) import Data.HashMap.Strict (HashMap, foldlWithKey') import Data.Monoid ((<>)) import Data.Text (Text, unpack)
src/Text/Taggy/Types.hs view
@@ -8,31 +8,31 @@ --  -- Core types of /taggy/. module Text.Taggy.Types-	( -- * 'Tag' type-	  Tag(..)-	, tname-	, isTagOpen-	, isTagClose-	, isTagText-	, isTagComment-	, isTagScript-	, isTagStyle-	, tagsNamed+  ( -- * 'Tag' type+    Tag(..)+  , tname+  , isTagOpen+  , isTagClose+  , isTagText+  , isTagComment+  , isTagScript+  , isTagStyle+  , tagsNamed      , -- * 'Attribute's       Attribute(..)-	, attrs-	, attrKey-	, attrValue+  , attrs+  , attrKey+  , attrValue -	, -- * A small difference list implementation-	  L-	, emptyL-	, appL-	, insertL-	, singletonL-	, toListL-	) where+  , -- * A small difference list implementation+    L+  , emptyL+  , appL+  , insertL+  , singletonL+  , toListL+  ) where  import Data.Text (Text, toCaseFold) @@ -119,11 +119,11 @@ -- | Get all the (opening) tags with the given name tagsNamed :: Text -> [Tag] -> [Tag] tagsNamed nam = filter (named nam)-  +   where named n (TagOpen t _ _) = toCaseFold n == toCaseFold t         named _ _               = False -newtype L a = L { list :: [a] -> [a] }+newtype L a = L ([a] -> [a])  emptyL :: L a emptyL = L $ const []
taggy.cabal view
@@ -1,5 +1,5 @@ name:                taggy-version:             0.1.4+version:             0.2.0 synopsis:            Efficient and simple HTML/XML parsing library description:            /taggy/ is a simple package for parsing HTML (and should work with XML)@@ -29,8 +29,7 @@   If you want to parse the document as a DOM tree and   traverse it to find the information you need,   use 'Text.Taggy.DOM.parseDOM'. This is especially useful-  when combined with the helpful combinators from-  "Text.Taggy.Combinators".+  when used in conjunction with <http://hackage.haskell.org/package/taggy-lens taggy-lens>.   .   If you build some HTML manually   or just transform some existing DOM tree@@ -50,7 +49,6 @@  library   exposed-modules:     Text.Taggy,-                       Text.Taggy.Combinators,                        Text.Taggy.DOM,                        Text.Taggy.Entities,                        Text.Taggy.Parser,