Created
April 27, 2018 14:11
-
-
Save jmn/bc1c195ae6e22847b5c1aa96d628ca69 to your computer and use it in GitHub Desktop.
absolute urls in haskell with hxt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Text.XML.HXT.Core | |
import Data.Maybe | |
mkAbsRefs | |
:: ArrowXml a | |
=> String -> a XmlTree XmlTree | |
mkAbsRefs base = | |
processTopDown | |
(editRef "a" "href" -- (2) | |
>>> | |
editRef "img" "src" -- (3) | |
>>> | |
editRef "link" "href" -- (4) | |
>>> | |
editRef "script" "src" -- (5) | |
) | |
where | |
editRef en an -- (1) | |
= | |
processAttrl (changeAttrValue (absHRef base) `when` hasName an) `when` | |
(isElem >>> hasName en) | |
where | |
absHRef :: String -> String -> String | |
absHRef base url = fromMaybe url . expandURIString url $ base | |
sanitize base = concat . runLA (hread >>> mkAbsRefs base >>> writeDocumentToString cfg) | |
where | |
cfg = [withOutputEncoding utf8, withOutputHTML, withRemoveWS yes] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment