Skip to content

Instantly share code, notes, and snippets.

@jmn
Created April 27, 2018 14:11
Show Gist options
  • Save jmn/bc1c195ae6e22847b5c1aa96d628ca69 to your computer and use it in GitHub Desktop.
Save jmn/bc1c195ae6e22847b5c1aa96d628ca69 to your computer and use it in GitHub Desktop.
absolute urls in haskell with hxt
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