1
0
Fork 0
tooty/src/View/Formatter.elm

94 lines
2.5 KiB
Elm
Raw Normal View History

module View.Formatter exposing (formatContent)
import Html exposing (..)
import Html.Attributes exposing (..)
import HtmlParser
2017-04-27 14:34:27 +00:00
import String.Extra exposing (replace)
2017-04-30 10:15:33 +00:00
import Mastodon.Model exposing (..)
import Types exposing (..)
import View.Events exposing (..)
-- Custom Events
-- Views
2017-04-30 10:15:33 +00:00
formatContent : String -> List Mention -> List (Html Msg)
formatContent content mentions =
content
2017-04-27 14:34:27 +00:00
|> replace " ?" " ?"
|> replace " !" " !"
|> replace " :" " :"
|> HtmlParser.parse
|> toVirtualDom mentions
{-| Converts nodes to virtual dom nodes.
-}
2017-04-30 10:15:33 +00:00
toVirtualDom : List Mention -> List HtmlParser.Node -> List (Html Msg)
toVirtualDom mentions nodes =
List.map (toVirtualDomEach mentions) nodes
2017-04-30 10:15:33 +00:00
createLinkNode : List ( String, String ) -> List HtmlParser.Node -> List Mention -> Html Msg
createLinkNode attrs children mentions =
let
maybeMention =
getMentionForLink attrs mentions
in
case maybeMention of
Just mention ->
Html.node "a"
((List.map toAttribute attrs)
2017-04-27 20:01:51 +00:00
++ [ onClickWithPreventAndStop (LoadAccount mention.id) ]
)
(toVirtualDom mentions children)
Nothing ->
Html.node "a"
((List.map toAttribute attrs)
++ [ onClickWithStop NoOp, target "_blank" ]
)
(toVirtualDom mentions children)
getHrefLink : List ( String, String ) -> Maybe String
getHrefLink attrs =
attrs
|> List.filter (\( name, value ) -> (name == "href"))
|> List.map (\( name, value ) -> value)
|> List.head
2017-04-30 10:15:33 +00:00
getMentionForLink : List ( String, String ) -> List Mention -> Maybe Mention
getMentionForLink attrs mentions =
case getHrefLink attrs of
Just href ->
mentions
|> List.filter (\m -> m.url == href)
|> List.head
Nothing ->
Nothing
2017-04-30 10:15:33 +00:00
toVirtualDomEach : List Mention -> HtmlParser.Node -> Html Msg
toVirtualDomEach mentions node =
case node of
HtmlParser.Element "a" attrs children ->
createLinkNode attrs children mentions
HtmlParser.Element name attrs children ->
Html.node name (List.map toAttribute attrs) (toVirtualDom mentions children)
HtmlParser.Text s ->
text s
HtmlParser.Comment _ ->
text ""
toAttribute : ( String, String ) -> Attribute msg
toAttribute ( name, value ) =
attribute name value