diff options
author | Łukasz Hanuszczak <lukasz.hanuszczak@gmail.com> | 2015-08-14 00:34:10 +0200 |
---|---|---|
committer | Łukasz Hanuszczak <lukasz.hanuszczak@gmail.com> | 2015-08-22 23:40:27 +0200 |
commit | 5934c411a8ebe0ba1a317f7c95babfbd63106254 (patch) | |
tree | fbc0150d93030e365a67fd79adcb239826b15727 /haddock-test/src/Test/Haddock/Xhtml.hs | |
parent | 869ee23cc7ec1bd2fa9299323b74d71fe6023ef2 (diff) |
Refactor and simplify XHTML helper module of test package.
Diffstat (limited to 'haddock-test/src/Test/Haddock/Xhtml.hs')
-rw-r--r-- | haddock-test/src/Test/Haddock/Xhtml.hs | 40 |
1 files changed, 24 insertions, 16 deletions
diff --git a/haddock-test/src/Test/Haddock/Xhtml.hs b/haddock-test/src/Test/Haddock/Xhtml.hs index 35f5910a..b6941496 100644 --- a/haddock-test/src/Test/Haddock/Xhtml.hs +++ b/haddock-test/src/Test/Haddock/Xhtml.hs @@ -2,47 +2,55 @@ {-# LANGUAGE StandaloneDeriving #-} -module Test.Haddock.Xhtml where +module Test.Haddock.Xhtml + ( Xhtml(..) + , parseXhtml, dumpXhtml + , stripLinks, stripFooter + ) where -import Control.Monad - import Data.Generics.Aliases import Data.Generics.Schemes import Text.XML.Light -deriving instance Eq Content +newtype Xhtml = Xhtml + { xhtmlElement :: Element + } deriving Eq + + +-- TODO: Find a way to avoid warning about orphan instances. deriving instance Eq Element +deriving instance Eq Content deriving instance Eq CData -readXml :: FilePath -> IO (Maybe Element) -readXml = liftM parseXMLDoc . readFile +parseXhtml :: String -> Maybe Xhtml +parseXhtml = fmap Xhtml . parseXMLDoc -strip :: Element -> Element -strip = stripFooter . stripLinks +dumpXhtml :: Xhtml -> String +dumpXhtml = ppElement . xhtmlElement -stripLinks :: Element -> Element +stripLinks :: Xhtml -> Xhtml stripLinks = - everywhere (mkT unlink) + Xhtml . everywhere (mkT unlink) . xhtmlElement where unlink attr@(Attr { attrKey = key }) | qName key == "href" = attr { attrVal = "#" } | otherwise = attr -stripFooter :: Element -> Element +stripFooter :: Xhtml -> Xhtml stripFooter = - everywhere (mkT defoot) + Xhtml . everywhere (mkT defoot) . xhtmlElement where - defoot elem - | isFooter elem = elem { elContent = [] } - | otherwise = elem - isFooter elem = any isFooterAttr $ elAttribs elem + defoot el + | isFooter el = el { elContent = [] } + | otherwise = el + isFooter el = any isFooterAttr $ elAttribs el isFooterAttr (Attr { .. }) = and [ qName attrKey == "id" , attrVal == "footer" |