Parser.purs 1.4 KB

12345678910111213141516171819202122232425262728293031323334
  1. module LinkedIn.UI.Elements.Parser where
  2. import Prelude
  3. import Control.Alt ((<|>))
  4. import Data.Either (Either(..))
  5. import Data.List as L
  6. import Data.Maybe (Maybe(..))
  7. import LinkedIn.DetachedNode (DetachedNode(..))
  8. import LinkedIn.UI.Strings.Parser (uiStringP)
  9. import LinkedIn.UI.Elements.Types (UIElement(..))
  10. import Parsing (ParseError(..), initialPos, runParser)
  11. -- TODO : should certainly use another type than ParseError here
  12. toUIElement ∷ DetachedNode → Either ParseError UIElement
  13. toUIElement (DetachedElement {content}) = wrapInUiElement content
  14. toUIElement (DetachedComment str) = wrapInUiElement str
  15. toUIElement (DetachedText str) = wrapInUiElement str
  16. toUIElement (DetachedSvgElement {id, dataTestIcon, tag: "svg"}) = case id <|> dataTestIcon of
  17. Just i -> Right (UIIcon i)
  18. Nothing -> Left (ParseError "SVG element could not be identified" initialPos)
  19. toUIElement (DetachedSvgElement _) = Left (ParseError "SVG element could not be identified" initialPos)
  20. toUIElement (DetachedLiIcon i) = Right (UIIcon i)
  21. toUIElement (DetachedButton {content, role, classes}) = map toButton $ runParser content uiStringP
  22. where toButton ui = UIButton {role, label: ui, mainClass: L.head classes}
  23. toUIElement (DetachedA {content, href}) = map toLink $ runParser content uiStringP
  24. where toLink ui = UILink href ui
  25. wrapInUiElement ∷ String → Either ParseError UIElement
  26. wrapInUiElement string = map UIElement $ runParser string uiStringP