2017-06-02 08:11:50 -04:00
|
|
|
module View.Formatter exposing (formatContent, textContent)
|
2017-04-22 04:16:14 -04:00
|
|
|
|
2017-05-27 03:32:15 -04:00
|
|
|
import Dict
|
2017-05-13 05:21:29 -04:00
|
|
|
import Elmoji
|
2017-04-22 04:16:14 -04:00
|
|
|
import Html exposing (..)
|
|
|
|
import Html.Attributes exposing (..)
|
|
|
|
import HtmlParser
|
2017-06-02 08:11:50 -04:00
|
|
|
import HtmlParser.Util as ParseUtil
|
2017-05-27 03:32:15 -04:00
|
|
|
import Http
|
2017-04-30 06:15:33 -04:00
|
|
|
import Mastodon.Model exposing (..)
|
2017-05-27 03:32:15 -04:00
|
|
|
import String.Extra exposing (replace, rightOf)
|
2017-04-29 12:05:06 -04:00
|
|
|
import Types exposing (..)
|
2017-05-02 02:27:01 -04:00
|
|
|
import View.Events exposing (..)
|
2017-04-22 04:16:14 -04:00
|
|
|
|
|
|
|
|
2017-04-30 06:15:33 -04:00
|
|
|
formatContent : String -> List Mention -> List (Html Msg)
|
2017-04-22 04:16:14 -04:00
|
|
|
formatContent content mentions =
|
|
|
|
content
|
2017-05-03 02:25:47 -04:00
|
|
|
|> replace " ?" " ?"
|
|
|
|
|> replace " !" " !"
|
|
|
|
|> replace " :" " :"
|
2017-04-22 04:16:14 -04:00
|
|
|
|> HtmlParser.parse
|
|
|
|
|> toVirtualDom mentions
|
|
|
|
|
|
|
|
|
2017-06-02 08:11:50 -04:00
|
|
|
textContent : String -> String
|
|
|
|
textContent html =
|
|
|
|
html |> HtmlParser.parse |> ParseUtil.textContent
|
|
|
|
|
|
|
|
|
2017-04-22 04:16:14 -04:00
|
|
|
{-| Converts nodes to virtual dom nodes.
|
|
|
|
-}
|
2017-04-30 06:15:33 -04:00
|
|
|
toVirtualDom : List Mention -> List HtmlParser.Node -> List (Html Msg)
|
2017-04-22 04:16:14 -04:00
|
|
|
toVirtualDom mentions nodes =
|
|
|
|
List.map (toVirtualDomEach mentions) nodes
|
|
|
|
|
|
|
|
|
2017-05-29 10:28:01 -04:00
|
|
|
replaceHref : String -> List ( String, String ) -> List (Attribute Msg)
|
|
|
|
replaceHref newHref attrs =
|
|
|
|
attrs
|
|
|
|
|> List.map toAttribute
|
|
|
|
|> List.append [ onClickWithPreventAndStop <| Navigate newHref ]
|
|
|
|
|
|
|
|
|
2017-04-30 06:15:33 -04:00
|
|
|
createLinkNode : List ( String, String ) -> List HtmlParser.Node -> List Mention -> Html Msg
|
2017-04-22 04:16:14 -04:00
|
|
|
createLinkNode attrs children mentions =
|
2017-05-13 05:21:29 -04:00
|
|
|
case (getMentionForLink attrs mentions) of
|
|
|
|
Just mention ->
|
|
|
|
Html.node "a"
|
2017-11-29 08:06:08 -05:00
|
|
|
(replaceHref ("#account/" ++ mention.id) attrs)
|
2017-05-13 05:21:29 -04:00
|
|
|
(toVirtualDom mentions children)
|
|
|
|
|
|
|
|
Nothing ->
|
2017-05-27 03:32:15 -04:00
|
|
|
case getHashtagForLink attrs of
|
|
|
|
Just hashtag ->
|
|
|
|
Html.node "a"
|
2017-05-29 10:28:01 -04:00
|
|
|
(replaceHref ("#hashtag/" ++ hashtag) attrs)
|
2017-05-27 03:32:15 -04:00
|
|
|
(toVirtualDom mentions children)
|
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
Html.node "a"
|
|
|
|
((List.map toAttribute attrs)
|
|
|
|
++ [ onClickWithStop NoOp, target "_blank" ]
|
|
|
|
)
|
|
|
|
(toVirtualDom mentions children)
|
2017-04-22 04:16:14 -04:00
|
|
|
|
|
|
|
|
|
|
|
getHrefLink : List ( String, String ) -> Maybe String
|
|
|
|
getHrefLink attrs =
|
|
|
|
attrs
|
2017-05-13 05:21:29 -04:00
|
|
|
|> List.filter (\( name, _ ) -> name == "href")
|
|
|
|
|> List.map (\( _, value ) -> value)
|
2017-04-22 04:16:14 -04:00
|
|
|
|> List.head
|
|
|
|
|
|
|
|
|
2017-05-27 03:32:15 -04:00
|
|
|
getHashtagForLink : List ( String, String ) -> Maybe String
|
|
|
|
getHashtagForLink attrs =
|
|
|
|
let
|
|
|
|
hashtag =
|
|
|
|
attrs
|
|
|
|
|> Dict.fromList
|
|
|
|
|> Dict.get "href"
|
|
|
|
|> Maybe.withDefault ""
|
|
|
|
|> rightOf "/tags/"
|
|
|
|
|> Http.decodeUri
|
|
|
|
|> Maybe.withDefault ""
|
|
|
|
in
|
|
|
|
if hashtag /= "" then
|
|
|
|
Just hashtag
|
|
|
|
else
|
|
|
|
Nothing
|
|
|
|
|
|
|
|
|
2017-04-30 06:15:33 -04:00
|
|
|
getMentionForLink : List ( String, String ) -> List Mention -> Maybe Mention
|
2017-04-22 04:16:14 -04:00
|
|
|
getMentionForLink attrs mentions =
|
|
|
|
case getHrefLink attrs of
|
|
|
|
Just href ->
|
|
|
|
mentions
|
|
|
|
|> List.filter (\m -> m.url == href)
|
|
|
|
|> List.head
|
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
Nothing
|
|
|
|
|
|
|
|
|
2017-04-30 06:15:33 -04:00
|
|
|
toVirtualDomEach : List Mention -> HtmlParser.Node -> Html Msg
|
2017-04-22 04:16:14 -04:00
|
|
|
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 ->
|
2017-05-13 05:21:29 -04:00
|
|
|
Elmoji.text_ s
|
2017-04-22 04:16:14 -04:00
|
|
|
|
|
|
|
HtmlParser.Comment _ ->
|
|
|
|
text ""
|
|
|
|
|
|
|
|
|
|
|
|
toAttribute : ( String, String ) -> Attribute msg
|
|
|
|
toAttribute ( name, value ) =
|
|
|
|
attribute name value
|