1--------------------------------------------------------------------------------
2-- | Provides utilities to manipulate HTML pages
17 -- * Stripping/escaping
23--------------------------------------------------------------------------------
24import Data.Char (digitToInt, intToDigit,
26import Data.List (isPrefixOf)
27import qualified Data.Set as S
28import System.FilePath (joinPath, splitPath,
30import Text.Blaze.Html (toHtml)
31import Text.Blaze.Html.Renderer.String (renderHtml)
32import qualified Text.HTML.TagSoup as TS
35--------------------------------------------------------------------------------
36-- | Map over all tags in the document
37withTags :: (TS.Tag String -> TS.Tag String) -> String -> String
38withTags f = renderTags' . map f . TS.parseTags
41--------------------------------------------------------------------------------
42-- | Map every @h1@ to an @h2@, @h2@ to @h3@, etc.
43demoteHeaders :: String -> String
44demoteHeaders = withTags $ \tag -> case tag of
45 TS.TagOpen t a -> TS.TagOpen (demote t) a
46 TS.TagClose t -> TS.TagClose (demote t)
50 | isDigit n = ['h', intToDigit (min 6 $ digitToInt n + 1)]
55--------------------------------------------------------------------------------
56isUrlAttribute :: String -> Bool
57isUrlAttribute = (`elem` ["src", "href"])
60--------------------------------------------------------------------------------
61getUrls :: [TS.Tag String] -> [String]
62getUrls tags = [v | TS.TagOpen _ as <- tags, (k, v) <- as, isUrlAttribute k]
65--------------------------------------------------------------------------------
66-- | Apply a function to each URL on a webpage
67withUrls :: (String -> String) -> String -> String
68withUrls f = withTags tag
70 tag (TS.TagOpen s a) = TS.TagOpen s $ map attr a
72 attr (k, v) = (k, if isUrlAttribute k then f v else v)
75--------------------------------------------------------------------------------
76-- | Customized TagSoup renderer. The default TagSoup renderer escape CSS
77-- within style tags, and doesn't properly minimize.
78renderTags' :: [TS.Tag String] -> String
79renderTags' = TS.renderTagsOptions TS.renderOptions
80 { TS.optRawTag = (`elem` ["script", "style"]) . map toLower
81 , TS.optMinimize = (`S.member` minimize) . map toLower
84 -- A list of elements which must be minimized
86 [ "area", "br", "col", "embed", "hr", "img", "input", "meta", "link"
91--------------------------------------------------------------------------------
92-- | Convert a filepath to an URL starting from the site root
96-- > toUrl "foo/bar.html"
101toUrl :: FilePath -> String
102toUrl ('/' : xs) = '/' : xs
106--------------------------------------------------------------------------------
107-- | Get the relative url to the site root, for a given (absolute) url
108toSiteRoot :: String -> String
109toSiteRoot = emptyException . joinPath . map parent
110 . filter relevant . splitPath . takeDirectory
113 emptyException [] = "."
120--------------------------------------------------------------------------------
121-- | Check if an URL links to an external HTTP(S) source
122isExternal :: String -> Bool
123isExternal url = any (flip isPrefixOf url) ["http://", "https://"]
126--------------------------------------------------------------------------------
127-- | Strip all HTML tags from a string
131-- > stripTags "<p>foo</p>"
137-- This also works for incomplete tags
141-- > stripTags "<p>foo</p"
146stripTags :: String -> String
148stripTags ('<' : xs) = stripTags $ drop 1 $ dropWhile (/= '>') xs
149stripTags (x : xs) = x : stripTags xs
152--------------------------------------------------------------------------------
153-- | HTML-escape a string
157-- > escapeHtml "Me & Dean"
162escapeHtml :: String -> String
163escapeHtml = renderHtml . toHtml
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list