1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE ViewPatterns #-}
5 ( -- * Constructing forests
7 , ParseTokenForestError(..), PStack(..)
9 -- * Deconstructing forests
15import Data.Text (Text)
17import Text.HTML.Parser
20tokensToForest :: [Token] -> Either ParseTokenForestError (Forest Token)
21tokensToForest = f (PStack [] [])
23 f (PStack ss []) [] = Right (reverse ss)
24 f pstack [] = Left $ ParseTokenForestErrorBracketMismatch pstack Nothing
25 f pstack (t : ts) = case t of
26 TagOpen n _ -> if n `elem` nonClosing
27 then f (pushFlatSibling t pstack) ts
28 else f (pushParent t pstack) ts
29 TagClose n -> (`f` ts) =<< popParent n pstack
30 ContentChar _ -> f (pushFlatSibling t pstack) ts
31 ContentText _ -> f (pushFlatSibling t pstack) ts
32 Comment _ -> f (pushFlatSibling t pstack) ts
33 Doctype _ -> f (pushFlatSibling t pstack) ts
36nonClosing = ["br", "hr", "img"]
38data ParseTokenForestError =
39 ParseTokenForestErrorBracketMismatch PStack (Maybe Token)
43 { _pstackToplevelSiblings :: Forest Token
44 , _pstackParents :: [(Token, Forest Token)]
48pushParent :: Token -> PStack -> PStack
49pushParent t (PStack ss ps) = PStack [] ((t, ss) : ps)
51popParent :: TagName -> PStack -> Either ParseTokenForestError PStack
52popParent n (PStack ss ((p@(TagOpen n' _), ss') : ps))
53 | n == n' = Right $ PStack (Node p (reverse ss) : ss') ps
55 = Left $ ParseTokenForestErrorBracketMismatch pstack (Just $ TagClose n)
57pushFlatSibling :: Token -> PStack -> PStack
58pushFlatSibling t (PStack ss ps) = PStack (Node t [] : ss) ps
61tokensFromForest :: Forest Token -> [Token]
62tokensFromForest = mconcat . fmap tokensFromTree
64tokensFromTree :: Tree Token -> [Token]
65tokensFromTree (Node o@(TagOpen n _) ts) | n `notElem` nonClosing
66 = [o] <> tokensFromForest ts <> [TagClose n]
67tokensFromTree (Node t [])
70 = error "renderTokenTree: leaf node with children."
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