elm/libraries/Text.elm

223 lines
5.6 KiB
Elm
Raw Normal View History

2013-04-28 12:21:46 +00:00
module Text where
2014-03-08 00:28:36 +00:00
{-| A library for styling and displaying text. Whlie the `String` library
focuses on representing and manipulating strings of character strings, the
`Text` library focuses on how those strings should look on screen. It lets
you make text bold or italic, set the typeface, set the text size, etc.
2013-09-10 03:57:06 +00:00
2013-09-10 06:07:49 +00:00
# Creating Text
2013-09-10 03:57:06 +00:00
@docs toText
2013-09-10 06:07:49 +00:00
# Creating Elements
2014-03-08 00:28:36 +00:00
Each of the following functions places `Text` into a box. The function you use
determines the alignment of the text.
@docs leftAligned, rightAligned, centered, justified
2013-09-10 03:57:06 +00:00
2014-03-02 04:23:01 +00:00
# Links and Style
2014-03-08 00:28:36 +00:00
@docs link, Style, style, defaultStyle, Line
2013-09-10 06:07:49 +00:00
# Convenience Functions
There are two convenience functions for creating an `Element` which can be
useful when debugging or prototyping:
@docs plainText, asText
There are also a bunch of functions to set parts of a `Style` individually:
@docs typeface, monospace, height, color, bold, italic, line
2013-09-10 03:57:06 +00:00
-}
import Basics (..)
import String
2014-03-02 04:23:01 +00:00
import Color (Color, black)
2013-07-27 11:08:46 +00:00
import Graphics.Element (Element, Three, Pos, ElementPrim, Properties)
2014-03-02 04:23:01 +00:00
import Maybe (Maybe, Nothing)
2013-07-27 11:08:46 +00:00
import JavaScript (JSString)
import Native.Show
import Native.Text
2013-07-27 11:08:46 +00:00
data Text = Text
2014-03-08 00:28:36 +00:00
{-| Styles for lines on text. This allows you to add an underline, an overline,
or a strike out text:
line Under (toText "underline")
line Over (toText "overline")
line Through (toText "strike out")
-}
data Line = Under | Over | Through
2014-03-08 00:28:36 +00:00
{-| Representation of all the ways you can style `Text`. If the `typeface` list
is empty or the `height` is `Nothing`, the users will fall back on their
browser's default settings. The following `Style` is black, 16 pixel tall,
underlined, and Times New Roman (assuming that typeface is available on the
user's computer):
{ typeface = [ "Times New Roman", "serif" ]
, height = Just 16
, color = black
, bold = False
, italic = False
, line = Just Under
}
-}
type Style =
{ typeface : [String]
, height : Maybe Float
, color : Color
, bold : Bool
, italic : Bool
, line : Maybe Line
}
2014-03-02 04:23:01 +00:00
{-| Plain black text. It uses the browsers default typeface and text height.
No decorations are used:
{ typeface = []
, height = Nothing
, color = black
, bold = False
, italic = False
, line = Nothing
}
-}
defaultStyle : Style
defaultStyle =
{ typeface = []
, height = Nothing
, color = black
, bold = False
, italic = False
, line = Nothing
}
2014-03-08 00:28:36 +00:00
{-| Convert a string into text which can be styled and displayed. To show the
string `"Hello World!"` on screen in italics, you could say:
main = leftAligned (italic (toText "Hello World!"))
-}
toText : String -> Text
toText = Native.Text.toText
2014-03-08 00:28:36 +00:00
{-| Set the style of some text. For example, if you design a `Style` called
`footerStyle` that is specifically for the bottom of your page, you could apply
it to text like this:
style footerStyle (toText "the old prince / 2007")
-}
style : Style -> Text -> Text
style = Native.Text.style
{-| Provide a list of prefered typefaces for some text.
2013-09-10 03:57:06 +00:00
["helvetica","arial","sans-serif"]
2013-09-10 03:57:06 +00:00
2014-03-08 00:28:36 +00:00
Not every browser has access to the same typefaces, so rendering will use the
first typeface in the list that is found on the user's computer. If there are
no matches, it will use their default typeface. This works the same as the CSS
font-family property.
2013-09-10 03:57:06 +00:00
-}
typeface : [String] -> Text -> Text
typeface = Native.Text.typeface
2014-03-08 00:28:36 +00:00
{-| Switch to a monospace typeface. Good for code snippets.
monospace (toText "foldl (+) 0 [1,2,3]")
-}
monospace : Text -> Text
monospace = Native.Text.monospace
2014-03-08 00:28:36 +00:00
{-| Create a link by providing a URL and the text of the link:
link "http://elm-lang.org" (toText "Elm Website")
-}
2013-04-29 01:52:13 +00:00
link : String -> Text -> Text
link = Native.Text.link
2014-03-08 00:28:36 +00:00
{-| Set the height of some text:
height 40 (toText "Title")
-}
height : Float -> Text -> Text
height = Native.Text.height
2014-03-08 00:28:36 +00:00
{-| Set the color of some text:
color red (toText "Red")
-}
color : Color -> Text -> Text
color = Native.Text.color
2014-03-08 00:28:36 +00:00
{-| Make text bold:
toText "sometimes you want " ++ bold (toText "emphasis")
-}
bold : Text -> Text
bold = Native.Text.bold
2014-03-08 00:28:36 +00:00
{-| Make text italic:
toText "make it " ++ italic (toText "important")
-}
italic : Text -> Text
italic = Native.Text.italic
2014-03-08 00:28:36 +00:00
{-| Put lines on text:
line Under (toText "underlined")
line Over (toText "overlined")
line Through (toText "strike out")
-}
line : Line -> Text -> Text
line = Native.Text.line
2014-03-08 00:28:36 +00:00
{-| `Text` is aligned along the left side of the text block. This is sometimes
known as *ragged right*.
-}
leftAligned : Text -> Element
leftAligned = Native.Text.leftAligned
2014-03-08 00:28:36 +00:00
{-| `Text` is aligned along the right side of the text block. This is sometimes
known as *ragged left*.
-}
rightAligned : Text -> Element
rightAligned = Native.Text.rightAligned
2014-03-08 00:28:36 +00:00
{-| `Text` is centered in the text block. There is equal spacing on either side
of a line of text.
-}
centered : Text -> Element
centered = Native.Text.centered
2014-03-08 00:28:36 +00:00
{-| `Text` is aligned along the left and right sides of the text block. Word
spacing is adjusted to make this possible.
-}
justified : Text -> Element
justified = Native.Text.justified
2014-03-08 00:28:36 +00:00
{-| Display a string with no styling:
plainText string = leftAligned (toText string)
-}
plainText : String -> Element
plainText str =
leftAligned (toText str)
2013-09-10 03:57:06 +00:00
{-| for internal use only -}
markdown : Element
markdown = Native.Text.markdown
2013-10-30 15:19:02 +00:00
{-| Convert anything to its textual representation and make it displayable in
2013-10-30 15:37:28 +00:00
the browser:
2013-09-10 03:57:06 +00:00
2014-03-08 00:28:36 +00:00
asText value = text (monospace (show value))
2013-09-10 03:57:06 +00:00
Excellent for debugging.
-}
asText : a -> Element
asText value =
leftAligned (monospace (toText (Native.Show.show value)))