NoRedInk / elm-formatted-text-19 / FormattedText

A type representing text with formatting.

Type


type alias FormattedText markup =
Internal.FormattedText markup

Text with formatting.

Creation

fromString : String -> FormattedText markup

Turn a plain string into formatted text.

unchunk : List ( String, List markup ) -> FormattedText markup

Take a list of text chunks with formatting and turn them into formatted text.

formatAll : markup -> FormattedText markup -> FormattedText markup

Apply some formatting to the entirety of a piece of formatted text.

Extraction

text : FormattedText markup -> String

Extract the plain string from formatted text.

chunks : (String -> List markup -> chunk) -> FormattedText markup -> List chunk

Certain operations, like rendering to Html, can be hard to perform with FormattedText directly. chunks splits a FormattedText up into chunks with equal markup. It's up to you to define what you want your chunks to look like. You can get tuples of Strings and markup by passing (,), or you can render directly into Html!

trees : (String -> tree) -> (markup -> List tree -> tree) -> FormattedText markup -> List tree

Certain operations, like rendering to Html, can be hard to perform with FormattedText directly. trees creates a markup tree from a FormattedText. You can pass it a function for generating a text leaf of the tree, and a function to generate a markup node of the tree.

String-like operations for FormattedText

all : (Char -> Basics.Bool) -> FormattedText markup -> Basics.Bool

Check if all characters in a FormattedText meets a condition. This is the equivalent of String.all.

any : (Char -> Basics.Bool) -> FormattedText markup -> Basics.Bool

Check if any character in a FormattedText meets a condition. This is the equivalent of String.any.

equal : FormattedText markup -> FormattedText markup -> Basics.Bool

Are two FormattedText the exact same?

append : FormattedText markup -> FormattedText markup -> FormattedText markup

Append two FormattedTexts together. The equivalent of String.append or (++).

concat : List (FormattedText markup) -> FormattedText markup

Concatenate a list of FormattedTexts together. The equivalent of String.concat.

cons : Char -> FormattedText markup -> FormattedText markup

Prepend a character onto a FormattedText. The added character will not be formatted. The equivalent of String.cons.

contains : FormattedText markup -> FormattedText markup -> Basics.Bool

Checks if a FormattedText contains a sub-FormattedText. Both text and markup of the contained occurrence need to match the sub-FormattedText. Equivalent of String.contains.

dropLeft : Basics.Int -> FormattedText markup -> FormattedText markup

Remove the first n characters of a FormattedText. Equivalent of String.dropLeft.

dropRight : Basics.Int -> FormattedText markup -> FormattedText markup

Remove the last n characters of a FormattedText. Equivalent of String.dropRight.

empty : FormattedText markup

Get an empty FormattedText, the equivalent of an empty string.

endsWith : FormattedText markup -> FormattedText markup -> Basics.Bool

Check if a FormattedText end with a sub-FormattedText. Both text and markup of the contained occurence need to match the sub-FormattedText. Equivalent of String.endsWith.

filter : (Char -> Basics.Bool) -> FormattedText markup -> FormattedText markup

Filter out characters from a FormattedText. This is the equivalent of String.filter.

foldl : (Char -> b -> b) -> b -> FormattedText markup -> b

Fold from the left over a Formattedtext. This is the equivalent of String.foldl.

foldr : (Char -> b -> b) -> b -> FormattedText markup -> b

Fold from the right over a Formattedtext. This is the equivalent of String.foldr.

fromChar : Char -> FormattedText markup

Turn a single character into a FormattedText with no markup applied. The equivalent of String.fromChar.

fromList : List Char -> FormattedText markup

Generate a FormattedText from a list of Chars. The resulting FormattedText will not contain any markup. This is the equivalent of String.fromList.

indexes : FormattedText markup -> FormattedText markup -> List Basics.Int

Get indexes of occurences of a sub-FormattdText inside a larger one. Both text and markup of the contained occurrence need to match the sub-FormattedText. The equivalent of String.indexes.

indices : FormattedText markup -> FormattedText markup -> List Basics.Int

Alias of indexes.

isEmpty : FormattedText markup -> Basics.Bool

Check if a FormattedText is empty. Same as text >> String.isEmpty.

join : FormattedText markup -> List (FormattedText markup) -> FormattedText markup

Join a list FormattedTexts on a joining FormattedText. This is the equivalent of String.join. Markup on all the characters of the component FormattedTexts will be preserved.

left : Basics.Int -> FormattedText markup -> FormattedText markup

Take the first n characters of a FormattedText, with their markup. Equivalent of String.left.

length : FormattedText markup -> Basics.Int

Length of the string in a FormattedText.

lines : FormattedText markup -> List (FormattedText markup)

Split a FormattedText on newline characters. The equivalent of String.lines.

map : (Char -> Char) -> FormattedText markup -> FormattedText markup

Modify each character of a FormattedText by passing it through a mapping function. This will not affect markup, which is to say: the character returned by the mapping function will have the same markup applied to it that the character passed into the mapping function had applied to it. This is the equivalent of String.map.

pad : Basics.Int -> Char -> List markup -> FormattedText markup -> FormattedText markup

Pad a FormattedText on both sides with a character, until the length of the FormattedText matches or exceeds a certain length. You can provide markup to be applied to the padding. This is the equivalent of String.pad.

padLeft : Basics.Int -> Char -> List markup -> FormattedText markup -> FormattedText markup

Pad a FormattedText on the left with a character, until the length of the FormattedText matches or exceeds a certain length. You can provide markup to be applied to the padding. This is the equivalent of String.padLeft.

padRight : Basics.Int -> Char -> List markup -> FormattedText markup -> FormattedText markup

Pad a FormattedText on the right with a character, until the length of the FormattedText matches or exceeds a certain length. You can provide markup to be applied to the padding. This is the equivalent of String.padRight.

repeat : Basics.Int -> FormattedText markup -> FormattedText markup

Repeat a FormattedText a number of times. The equivalent of String.repeat.

reverse : FormattedText markup -> FormattedText markup

Reverse the string in a FormattedText along with its formatting. In other words: every single character will have the same markup after reversal as it had before.

right : Basics.Int -> FormattedText markup -> FormattedText markup

Take the last n characters of a FormattedText, with their markup. Equivalent of String.right.

slice : Basics.Int -> Basics.Int -> FormattedText markup -> FormattedText markup

Get a slice of a FormattedText as defined by a starting and ending index. Equivalent of String.slice. Each character in the slice will have the same markup it had in the original FormattedText.

split : String -> FormattedText markup -> List (FormattedText markup)

Split a FormattedText on a string. The equivalent of String.split. Markup of all characters on the FormattedText will be preserved.

startsWith : FormattedText markup -> FormattedText markup -> Basics.Bool

Check if a FormattedText starts with a sub-FormattedText. Both text and markup of the contained occurence need to match the sub-FormattedText. Equivalent of String.startsWith.

toFloat : FormattedText markup -> Maybe Basics.Float

Parse the FormattedText as a Float. Markup has no effect on the result. The equivalent of String.toInt.

toInt : FormattedText markup -> Maybe Basics.Int

Parse the FormattedText as an Int. Markup has no effect on the result. The equivalent of String.toInt.

toList : FormattedText markup -> List Char

Turn a FormattedText into a list of Chars. Markup is discarded. The equivalent of String.toList.

toLower : FormattedText markup -> FormattedText markup

Lowercase the text in a FormattedText. Markup will not be affected. This is the equivalent of String.toLowr.

toUpper : FormattedText markup -> FormattedText markup

Uppercase the text in a FormattedText. Markup will not be affected. This is the equivalent of String.toUpper.

trim : FormattedText markup -> FormattedText markup

Trim whitespace from both ends of a FormattedText. The equivalent of String.trim.

trimLeft : FormattedText markup -> FormattedText markup

Trim whitespace from the left end of a FormattedText. The equivalent of String.trimLeft.

trimRight : FormattedText markup -> FormattedText markup

Trim whitespace from the right end of a FormattedText. The equivalent of String.trimRight.

uncons : FormattedText markup -> Maybe ( Char, FormattedText markup )

Remove the first character from a FormattedText. The equivalent of String.uncons.

words : FormattedText markup -> List (FormattedText markup)

Split a FormattedText on whitespace. The equivalent of String.words.

Low-level

These functions provide a look behind the curtains at the ranges that are used to implement the FormattedText type. You might need these functions to write encoders / decoders of the FormattedText type. The FormattedText type is upholding some internal constraints, which mean that if you try to micromanage the exact ranges on it you're probably going to have a bad time. If you feel the need to do this, please create an issue on our Github repo with your use case, we might be missing a function!


type alias Range markup =
{ tag : markup
, start : Basics.Int
, end : Basics.Int 
}

A format applied to a range. Formatting applies from the start index up to but excluding the end index.

formattedText : String -> List (Range markup) -> FormattedText markup

Create a FormattedText from a string and a list of markup ranges.

Note that the FormattedText type obeys certain constraints (see documentation for the FormattedText type). This means the addRange function makes no promises with regards to what you get back when you call ranges, only that the characters in the range you indicated will have the formatting you specified attached to them.

addRange : Range markup -> FormattedText markup -> FormattedText markup

Format a range in the FormattedText with a certain type of markup.

Note that the FormattedText type obeys certain constraints (see documentation for the FormattedText type). This means the addRange function makes no promises with regards to what you get back when you call ranges, only that the characters in the range you indicated will have the formatting you specified attached to them.

ranges : FormattedText markup -> List (Range markup)

Extract all the markup ranges from the FormattedText.

This function is provided to enable encoding of the FormattedText type. If you feel you need it to achieve something else, please share your use case in an issue on the FormattedText Github repo. Your use case might be inspiration for an additional method.