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 TagSelfClose {} -> f (pushFlatSibling t pstack) ts
30 TagClose n -> (`f` ts) =<< popParent n pstack
31 ContentChar _ -> f (pushFlatSibling t pstack) ts
32 ContentText _ -> f (pushFlatSibling t pstack) ts
33 Comment _ -> f (pushFlatSibling t pstack) ts
34 Doctype _ -> f (pushFlatSibling t pstack) ts
37nonClosing = ["br", "hr", "img"]
39data ParseTokenForestError =
40 ParseTokenForestErrorBracketMismatch PStack (Maybe Token)
44 { _pstackToplevelSiblings :: Forest Token
45 , _pstackParents :: [(Token, Forest Token)]
49pushParent :: Token -> PStack -> PStack
50pushParent t (PStack ss ps) = PStack [] ((t, ss) : ps)
52popParent :: TagName -> PStack -> Either ParseTokenForestError PStack
53popParent n (PStack ss ((p@(TagOpen n' _), ss') : ps))
54 | n == n' = Right $ PStack (Node p (reverse ss) : ss') ps
56 = Left $ ParseTokenForestErrorBracketMismatch pstack (Just $ TagClose n)
58pushFlatSibling :: Token -> PStack -> PStack
59pushFlatSibling t (PStack ss ps) = PStack (Node t [] : ss) ps
62tokensFromForest :: Forest Token -> [Token]
63tokensFromForest = mconcat . fmap tokensFromTree
65tokensFromTree :: Tree Token -> [Token]
66tokensFromTree (Node o@(TagOpen n _) ts) | n `notElem` nonClosing
67 = [o] <> tokensFromForest ts <> [TagClose n]
68tokensFromTree (Node t [])
71 = 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