This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.

Content.Text

Constructors for text content

Constructors

empty : Content.Text msg

Construct empty text content as a placeholder

Content.Text.empty
  |> Content.Text.wrapper "p"
  |> Dom.Element.toNode

--> <p></p>
plain : String -> Content.Text msg

Construct plain text content

"Hello World!"
  |> Content.Text.plain
  |> Content.Text.wrapper "p"
  |> Dom.Element.toNode

--> <p>Hello World!</p>
styled : String -> String -> Content.Text msg

Construct styled text content using the HTML tag specified in the first argument

"Hello World!"
  |> Content.Text.styled "strong"
  |> Content.Text.wrapper "p"
  |> Dom.Element.toNode

--> <p><strong>Hello World!<strong></p>
link : (String, Bool) -> String -> Content.Text msg

Construct linked text content; the first argument gives the URI and the second argument gives the text displayed

"Click here to download"
  |> Content.Text.link ("http://example.com/files/myfile.pdf", True)
  |> Content.Text.wrapper "p"
  |> Dom.Element.toNode

--> <p>
--    <a href="http://example.com/files/myfile.pdf" target="_blank">Click here to download</a>
--  </p>
fromMarkdown : (String -> Dom.Node msg) -> String -> Content.Text msg

Construct markdown text content using the markdown rendering function specified in the first argument

"*Hello* World!"
  |> Content.Text.fromMarkdown (Markdown.toHtml [])
  |> Content.Text.wrapper "section"
  |> Dom.Element.toNode

--> <section>
--    <div>
--      <p><em>Hello</em> World!</p>
--    </div>
--  </section>

Putting text together

append : Content.Text msg -> Content.Text msg -> Content.Text msg

Add the text in the first argument to the end of the text in the second argument (intended to be used as a pipeline function)

"Hello "
  |> Content.Text.plain
  |> Content.Text.append
    ( "World!"
      |> Content.Text.styled "strong"
    )
  |> Content.Text.wrapper "p"
  |> Dom.Element.toNode

--> <p>Hello <strong>World!</strong></p>
prepend : Content.Text msg -> Content.Text msg -> Content.Text msg

Add the text in the first argument at the beginning of the text in the second argument (intended to be used as a pipeline function)

"World!"
  |> Content.Text.styled "strong"
  |> Content.Text.prepend
    ( "Hello "
      |> Content.Text.plain
    )
  |> Content.Text.wrapper "p"
  |> Dom.Element.toNode

--> <p>Hello <strong>World!</strong></p>
concat : List (Content.Text msg) -> Content.Text msg

Combine multiple segments of text into one

[ "Hello "
  |> Content.Text.plain
, "World!"
  |> Content.Text.styled "strong"
]
  |> Content.Text.concat
  |> Content.Text.wrapper "p"
  |> Dom.Element.toNode

--> <p>Hello <strong>World!</strong></p>
join : String -> List (Content.Text msg) -> Content.Text msg

Combine multiple segments of text into one, with the string given in the first argument inserted as a separator

[ "Hello"
  |> Content.Text.plain
, "World!"
  |> Content.Text.styled "strong"
]
  |> Content.Text.join ", "
  |> Content.Text.wrapper "p"
  |> Dom.Element.toNode

--> <p>Hello, <strong>World!</strong></p>

Rendering

wrapper : String -> Content.Text msg -> Dom.Element msg

Wrap text content in a Dom.Element, with the HTML tag given in the first argument (see above for examples)

module Content.Text exposing
  ( empty
  , plain, styled, link, fromMarkdown
  , append, prepend
  , concat, join
  , wrapper
  )

{-| Constructors for text content

# Constructors
@docs empty, plain, styled, link, fromMarkdown

# Putting text together
@docs append, prepend, concat, join

# Rendering
@docs wrapper

-}

import Dom
import Dom.Node
import Dom.Text
import Dom.Property
import Dom.Element
import Content


{-| Construct empty text content as a placeholder

    Content.Text.empty
      |> Content.Text.wrapper "p"
      |> Dom.Element.toNode

    --> <p></p>
-}
empty : Content.Text msg
empty =
  []
    |> Content.Text


{-| Construct plain text content

    "Hello World!"
      |> Content.Text.plain
      |> Content.Text.wrapper "p"
      |> Dom.Element.toNode

    --> <p>Hello World!</p>

-}
plain : String -> Content.Text msg
plain someText =
  [ someText
    |> Dom.Text.node
  ]
    |> Content.Text


{-| Construct styled text content using the HTML tag specified in the first
argument

    "Hello World!"
      |> Content.Text.styled "strong"
      |> Content.Text.wrapper "p"
      |> Dom.Element.toNode

    --> <p><strong>Hello World!<strong></p>

-}
styled : String -> String -> Content.Text msg
styled htmlTag someText =
  [ someText
    |> Dom.Node.textWrapper htmlTag []
  ]
    |> Content.Text


{-| Construct linked text content; the first argument gives the URI and the
second argument gives the text displayed

    "Click here to download"
      |> Content.Text.link ("http://example.com/files/myfile.pdf", True)
      |> Content.Text.wrapper "p"
      |> Dom.Element.toNode

    --> <p>
    --    <a href="http://example.com/files/myfile.pdf" target="_blank">Click here to download</a>
    --  </p>

-}
link : (String, Bool) -> String -> Content.Text msg
link (path, newWindow) someText =
  let
    attrs =
      case newWindow of
        True ->
          [ "href"
            |> Dom.Property.string path
          , "target"
            |> Dom.Property.string "_blank"
          ]

        False ->
          [ "href"
            |> Dom.Property.string path
          ]

  in
    [ someText
      |> Dom.Node.textWrapper "a" attrs
    ]
      |> Content.Text


{-| Construct markdown text content using the markdown rendering function
specified in the first argument

    "*Hello* World!"
      |> Content.Text.fromMarkdown (Markdown.toHtml [])
      |> Content.Text.wrapper "section"
      |> Dom.Element.toNode

    --> <section>
    --    <div>
    --      <p><em>Hello</em> World!</p>
    --    </div>
    --  </section>

-}
fromMarkdown : (String -> Dom.Node msg) -> String -> Content.Text msg
fromMarkdown markdownRenderer someText =
  [ someText
    |> markdownRenderer
  ]
    |> Content.Text


{-| Add the text in the first argument to the end of the text in the second
argument (intended to be used as a pipeline function)

    "Hello "
      |> Content.Text.plain
      |> Content.Text.append
        ( "World!"
          |> Content.Text.styled "strong"
        )
      |> Content.Text.wrapper "p"
      |> Dom.Element.toNode

    --> <p>Hello <strong>World!</strong></p>

-}
append : Content.Text msg -> Content.Text msg -> Content.Text msg
append newText currentText =
  [ currentText
  , newText
  ]
    |> concat


{-| Add the text in the first argument at the beginning of the text in the
second argument (intended to be used as a pipeline function)

    "World!"
      |> Content.Text.styled "strong"
      |> Content.Text.prepend
        ( "Hello "
          |> Content.Text.plain
        )
      |> Content.Text.wrapper "p"
      |> Dom.Element.toNode

    --> <p>Hello <strong>World!</strong></p>

-}
prepend : Content.Text msg -> Content.Text msg -> Content.Text msg
prepend newText currentText =
  [ newText
  , currentText
  ]
    |> concat


{-| Combine multiple segments of text into one

    [ "Hello "
      |> Content.Text.plain
    , "World!"
      |> Content.Text.styled "strong"
    ]
      |> Content.Text.concat
      |> Content.Text.wrapper "p"
      |> Dom.Element.toNode

    --> <p>Hello <strong>World!</strong></p>

-}
concat : List (Content.Text msg) -> Content.Text msg
concat contentList =
  let
    getNodeList element =
      case element of
        Content.Text nodeList ->
          nodeList

  in
    contentList
      |> List.map getNodeList
      |> List.concat
      |> Content.Text


{-| Combine multiple segments of text into one, with the string given in the
first argument inserted as a separator

    [ "Hello"
      |> Content.Text.plain
    , "World!"
      |> Content.Text.styled "strong"
    ]
      |> Content.Text.join ", "
      |> Content.Text.wrapper "p"
      |> Dom.Element.toNode

    --> <p>Hello, <strong>World!</strong></p>

-}
join : String -> List (Content.Text msg) -> Content.Text msg
join separator contentList =
  let
    getNodeList element =
      case element of
        Content.Text nodeList ->
          nodeList

  in
    contentList
      |> List.map getNodeList
      |> List.concat
      |> List.intersperse
        ( separator
          |> Dom.Text.node
        )
      |> Content.Text


{-| Wrap text content in a `Dom.Element`, with the HTML tag given in the first
argument (see above for examples)
-}
wrapper : String -> Content.Text msg -> Dom.Element msg
wrapper htmlTag textContent =
  { tag = htmlTag
  , attributes = []
  , classes = []
  , children =
    ( case textContent of
        Content.Text nodeList ->
          nodeList
    )
  , text = ""
  , namespace = ""
  , keys = []
  }