From 1cb714e35337a6b17d7fc37f086914f43f7f2da3 Mon Sep 17 00:00:00 2001 From: Ɓukasz Hanuszczak Date: Fri, 14 Aug 2015 20:41:41 +0200 Subject: Implement utility functions for conditional link stripping. --- haddock-test/src/Test/Haddock/Xhtml.hs | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'haddock-test/src') diff --git a/haddock-test/src/Test/Haddock/Xhtml.hs b/haddock-test/src/Test/Haddock/Xhtml.hs index b6941496..d8c26249 100644 --- a/haddock-test/src/Test/Haddock/Xhtml.hs +++ b/haddock-test/src/Test/Haddock/Xhtml.hs @@ -5,7 +5,7 @@ module Test.Haddock.Xhtml ( Xhtml(..) , parseXhtml, dumpXhtml - , stripLinks, stripFooter + , stripLinks, stripLinksWhen, stripAnchorsWhen, stripFooter ) where @@ -35,14 +35,31 @@ dumpXhtml = ppElement . xhtmlElement stripLinks :: Xhtml -> Xhtml -stripLinks = - Xhtml . everywhere (mkT unlink) . xhtmlElement +stripLinks = stripLinksWhen (const True) + + +stripLinksWhen :: (String -> Bool) -> Xhtml -> Xhtml +stripLinksWhen p = + processAnchors unlink + where + unlink attr@(Attr { attrKey = key, attrVal = val }) + | qName key == "href" && p val = attr { attrVal = "#" } + | otherwise = attr + + +stripAnchorsWhen :: (String -> Bool) -> Xhtml -> Xhtml +stripAnchorsWhen p = + processAnchors unname where - unlink attr@(Attr { attrKey = key }) - | qName key == "href" = attr { attrVal = "#" } + unname attr@(Attr { attrKey = key, attrVal = val }) + | qName key == "name" && p val = attr { attrVal = "" } | otherwise = attr +processAnchors :: (Attr -> Attr) -> Xhtml -> Xhtml +processAnchors f = Xhtml . everywhere (mkT f) . xhtmlElement + + stripFooter :: Xhtml -> Xhtml stripFooter = Xhtml . everywhere (mkT defoot) . xhtmlElement -- cgit v1.2.3