1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE ViewPatterns #-}
5 ( -- * Constructing forests
7 , ParseTokenForestError(..), PStack(..)
9 -- * Deconstructing forests
15import Data.Text (Text)
19import Text.HTML.Parser
21-- | construct a 'Forest' from a 'Token' list.
23-- This code correctly handles void elements. Void elements are required to have a start tag and must not have an end tag. See 'nonClosing'.
25-- This code does __not__ correctly handle optional tags. It assumes all optional start and end tags are present.
27-- <https:\/\/www.w3.org\/TR\/html52\/syntax.html#optional-tags>
28tokensToForest :: [Token] -> Either ParseTokenForestError (Forest Token)
29tokensToForest = f (PStack [] [])
31 f (PStack ss []) [] = Right (reverse ss)
32 f pstack [] = Left $ ParseTokenForestErrorBracketMismatch pstack Nothing
33 f pstack (t : ts) = case t of
34 TagOpen n _ -> if n `elem` nonClosing
35 then f (pushFlatSibling t pstack) ts
36 else f (pushParent t pstack) ts
37 TagSelfClose {} -> f (pushFlatSibling t pstack) ts
38 TagClose n -> (`f` ts) =<< popParent n pstack
39 ContentChar _ -> f (pushFlatSibling t pstack) ts
40 ContentText _ -> f (pushFlatSibling t pstack) ts
41 Comment _ -> f (pushFlatSibling t pstack) ts
42 Doctype _ -> f (pushFlatSibling t pstack) ts
44-- | void elements which must not have an end tag
46-- This list does not include the obsolete @\<command\>@ and @\<keygen\>@ elements.
48-- @ nonClosing = ["br", "hr", "img", "meta", "area", "base", "col", "embed", "input", "link", "param", "source", "track", "wbr"] @
50-- <https:\/\/www.w3.org\/TR\/html52\/syntax.html#void-elements>
52nonClosing = ["br", "hr", "img", "meta", "area", "base", "col", "embed", "input", "link", "param", "source", "track", "wbr"]
54data ParseTokenForestError =
55 ParseTokenForestErrorBracketMismatch PStack (Maybe Token)
59 { _pstackToplevelSiblings :: Forest Token
60 , _pstackParents :: [(Token, Forest Token)]
64pushParent :: Token -> PStack -> PStack
65pushParent t (PStack ss ps) = PStack [] ((t, ss) : ps)
67popParent :: TagName -> PStack -> Either ParseTokenForestError PStack
68popParent n (PStack ss ((p@(TagOpen n' _), ss') : ps))
69 | n == n' = Right $ PStack (Node p (reverse ss) : ss') ps
71 = Left $ ParseTokenForestErrorBracketMismatch pstack (Just $ TagClose n)
73pushFlatSibling :: Token -> PStack -> PStack
74pushFlatSibling t (PStack ss ps) = PStack (Node t [] : ss) ps
76-- | convert a 'Forest' of 'Token' into a list of 'Token'.
78-- This code correctly handles void elements. Void elements are required to have a start tag and must not have an end tag. See 'nonClosing'.
79tokensFromForest :: Forest Token -> [Token]
80tokensFromForest = mconcat . fmap tokensFromTree
82-- | convert a 'Tree' of 'Token' into a list of 'Token'.
84-- This code correctly handles void elements. Void elements are required to have a start tag and must not have an end tag. See 'nonClosing'.
85tokensFromTree :: Tree Token -> [Token]
86tokensFromTree (Node o@(TagOpen n _) ts) | n `notElem` nonClosing
87 = [o] <> tokensFromForest ts <> [TagClose n]
88tokensFromTree (Node t [])
91 = 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