packages feed

tagsoup-parsec (empty) → 0.0.1

raw patch · 4 files changed

+203/−0 lines, 4 filesdep +basedep +parsecdep +tagsoupsetup-changed

Dependencies added: base, parsec, tagsoup

Files

+ LICENSE view
@@ -0,0 +1,7 @@+Copyright (c) 2009 Johnny Morrice++Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,6 @@+module Main where++import Distribution.Simple++main :: IO ()+main = defaultMain
+ Text/HTML/TagSoup/Parsec.hs view
@@ -0,0 +1,172 @@+module Text.HTML.TagSoup.Parsec +   ( module Text.HTML.TagSoup+   , TagParser+   , WholeTag +   , tParse+   , openTag +   , maybeOpenTag+   , notOpenTag+   , allOpenTags+   , wholeTag+   , maybeWholeTag+   , allWholeTags+   , closeTag+   , maybeCloseTag+   , notCloseTag+   , allCloseTags+   , maybeP+   , allP+   )+   where++import Text.ParserCombinators.Parsec+import Text.ParserCombinators.Parsec.Pos+import Text.HTML.TagSoup++import Data.Maybe+import Data.Char++-- | A type represent the TagOpen, any inner tags , and the TagClose.+type WholeTag =+   ( Tag , [ Tag ] , Tag )++-- | The Tag parser, using Tag as the token.+type TagParser =+   GenParser Tag ( )++-- | Used to invoke parsing of Tags.+tParse :: TagParser a -> [ Tag ] -> a+tParse p ts =+   case parse p "tagsoup" ts of+      Left err ->+         error $ show err+      Right crap ->+         crap++-- Tag eater is the basic tag matcher, it increments the line number for each tag parsed.+tagEater matcher =+   tokenPrim show +             ( \ oldSp _ _ -> do +                  setSourceLine oldSp ( 1 + sourceLine oldSp )+             )+             matcher++-- make a string lowercase+lowercase =+   map toLower+++-- | openTag matches a TagOpen with the given name.  It is not case sensitive.+openTag :: String -> TagParser Tag+openTag soughtName =+   openTagMatch soughtName ( Just ) $ \ _ -> Nothing++-- | notOpenTag will match any tag which is not a TagOpen with the given name.  It is not case sensitive.+notOpenTag :: String -> TagParser Tag+notOpenTag avoidName =+   openTagMatch avoidName ( \ _ -> Nothing ) Just++-- openTagMatch is the higher order function which will receive a TagOpen, and call match if it matches the soughtName, and noMatch if it doesn't+openTagMatch soughtName match noMatch =+   tagEater $ \ tag ->+                 case tag of+                    t@( TagOpen tname atrs ) ->+                       if lowercase tname == lowercase soughtName+                          then+                             match t+                          else+                             noMatch t+                    t ->+                       noMatch t++-- closeTagMatch is the higher order function which will receive a TagClose, and call match if it matches the soughtName and noMatch if it doesn't.+closeTagMatch soughtName match noMatch =+   tagEater $ \ tag ->+                 case tag of+                    t@( TagClose tname ) ->+                       if lowercase tname == lowercase soughtName+                          then+                             match t+                          else+                             noMatch t+                    t ->+                       noMatch t++-- | wholeTag matches a TagOpen with the given name,+-- then all intervening tags,+-- until it reaches a TagClose with the given name.+-- It is not case sensitive.+wholeTag :: String -> TagParser WholeTag+wholeTag soughtName = do+   open <- openTag soughtName+   ts <- many $ notCloseTag soughtName+   close <- closeTag soughtName+   return ( open , ts , close )++-- | closeTag matches a TagClose with the given name.  It is not case sensitive.+closeTag :: String -> TagParser Tag+closeTag soughtName =+   closeTagMatch soughtName ( Just ) $ \ _ -> Nothing++-- | notCloseTag will match any tag which is not a TagClose with the given name.  It is not case sensitive.+notCloseTag :: String -> TagParser Tag+notCloseTag avoidName =+   closeTagMatch avoidName ( \ _ -> Nothing ) Just++-- | maybeOpenTag will return `Just` the tag if it gets a TagOpen with he given name,+-- It will return `Nothing` otherwise.+-- It is not case sensitive.+maybeOpenTag :: String -> TagParser ( Maybe Tag )+maybeOpenTag soughtName =+   maybeP $ openTag soughtName++-- | maybeCloseTag will return `Just` the tag if it gets a TagClose with he given name,+-- It will return `Nothing` otherwise.+-- It is not case sensitive.+maybeCloseTag :: String -> TagParser ( Maybe Tag )+maybeCloseTag soughtName =+   maybeP $ closeTag soughtName++-- | maybeWholeTag will return `Just` the tag if it gets a WholeTag with he given name,+-- It will return `Nothing` otherwise.+-- It is not case sensitive.+maybeWholeTag :: String -> TagParser ( Maybe WholeTag )+maybeWholeTag soughtName =+   maybeP $ wholeTag soughtName++-- | allOpenTags will return all TagOpen with the given name.+-- It is not case sensitive.+allOpenTags :: String -> TagParser [ Tag ]+allOpenTags t =+   allP $ maybeOpenTag t ++-- | allCloseTags will return all TagClose with the given name.+-- It is not case sensitive.+allCloseTags :: String -> TagParser [ Tag ]+allCloseTags t =+   allP $ maybeCloseTag t++-- | allWholeTags will return all WholeTag with the given name.+-- It is not case sensitive.+allWholeTags :: String -> TagParser [ WholeTag ]+allWholeTags t =+   allP $ maybeWholeTag t+      +-- | allP takes a parser which returns  a `Maybe` value, and returns a list of matching tokens.+allP :: GenParser tok st ( Maybe a ) -> GenParser tok st [ a ]+allP p = do+   ts <- many p+   let ls = +          catMaybes ts+   return ls++-- | maybeP takes a parser, and becomes its `Maybe` equivalent -- returning `Just` if it matches, and `Nothing` if it doesn't.+maybeP :: Show tok => GenParser tok st a -> GenParser tok st ( Maybe a )+maybeP p =+   try ( do t <- p+            return $ Just t+       ) <|> ( do anyToken+                  return Nothing+             )++
+ tagsoup-parsec.cabal view
@@ -0,0 +1,18 @@+Name:           tagsoup-parsec +Version:        0.0.1+License:        BSD3 +License-file:   LICENSE+Author:         Johnny Morrice+Maintainer:     spoon@killersmurf.com+Homepage:       http://www.killersmurf.com/projects/tagsoup-parsec+Category:       Language+Synopsis:       Tokenizes Tag, so [ Tag ] can be used as parser input.+Description:    Tokenizes Tag, so [ Tag ] can be used as parser input.  Provides+                basic combinators.+Build-type:     Simple+Extra-source-files: LICENSE+Cabal-version:  >=1.2.0++Library+   Build-Depends:  base, tagsoup, parsec==2.1.0.1+   Exposed-modules: Text.HTML.TagSoup.Parsec