mdgriffith/elm-markup/1.0.0

NOTE: the latest version is 3.0.1

Mark.Custom

type alias Inline model style msg =
Inline model style msg
String
-> (String -> style -> model -> List (Element msg))
-> Inline model style msg

Create a custom inline styler.

Custom.inline "intro"
    (\string styling ->
        let
            txt =
                String.trim string
        in
        if txt == "" then
            []


        else
            [ Element.el [ Element.alignLeft, Font.size 48 ]
                (Element.text (String.toUpper (String.slice 0 1 txt)))
            , Element.el [ Font.size 24 ]
                (Element.text (String.toUpper (String.dropLeft 1 txt)))
            ]
    )

When applied via parseWith, can then be used in markup like the following

{drop| Lorem Ipsum is simply dummy text } of the printing and...

It will turn the first letter into a dropped capital and lead in with small caps

styling is the styling record that is passed in the options of parseWith. This means you can make an inline element that can be paragraph via the options.

Block functions with arguments

type alias Block model style msg =
Block model style msg
block : String -> (style -> model -> Element msg) -> Block model style msg

A simple block that will insert Elm html.

import Element
import Element.Font as Font
import Mark.Custom as Custom


myParser =
    Mark.parseWith
        { styling = Mark.defaultStyling
        , inlines = []
        , blocks =
            [ Custom.block "red"
                (\styling model ->
                    Element.el
                        [ Font.color (Element.rgb 1 0 0) ]
                        (Element.text "Some statically defined, red text!")
                )
            ]
        }

Which can then be used in your markup like so:

| red

The element you defined will show up there.

String
-> (arg -> style -> model -> Element msg)
-> Param arg
-> Block model style msg

Same as block, but you can parse one parameter as well.

For example, here's how the builtin block, image, using block2 and two Custom.string parameters.

Custom.block2 "image"
    (\src description styling ->
        Element.image
            []
            { src = String.trim src
            , description = String.trim description
            }
    )
    Custom.string
    Custom.string

Which can then be used in your markup:

| image "http://placekitten/200/500"
    "Here's a great picture of my cat, pookie.""

or as

| image
    "http://placekitten/200/500"
    "Here's a great picture of my cat, pookie.""
String
-> (arg -> arg2 -> style -> model -> Element msg)
-> Param arg
-> Param arg2
-> Block model style msg
String
-> (arg -> arg2 -> arg3 -> style -> model -> Element msg)
-> Param arg
-> Param arg2
-> Param arg3
-> Block model style msg

Parameters

type Param arg

A parameter to use with block1 or block2.

bool : Param Bool
int : Param Int
float : Param Float
string : Param String

Parse a double quoted string.

oneOf : List ( String, value ) -> Param value

Define a list of options. Useful for working with custom types.

Styled

String
-> ( List (Element msg)
-> { a
| link : List (Attribute msg)
, token : List (Attribute msg)
, root : List (Attribute msg)
, block : List (Attribute msg)
}
-> model
-> Element msg
)
-> Block
model
{ a
| link : List (Attribute msg)
, token : List (Attribute msg)
, root : List (Attribute msg)
, block : List (Attribute msg)
}
msg

A block that expects a single paragraph of styled text as input. The header block that is built in uses this.

| header
    My super sweet, /styled/ header.

Note The actual paragraph text is required to be on the next line and indented four spaces.

String
-> ( List (Element msg)
-> { a
| link : List (Attribute msg)
, token : List (Attribute msg)
, root : List (Attribute msg)
, block : List (Attribute msg)
}
-> model
-> Element msg
)
-> Block
model
{ a
| link : List (Attribute msg)
, token : List (Attribute msg)
, root : List (Attribute msg)
, block : List (Attribute msg)
}
msg

Like Custom.paragraph, but parses many styled paragraphs.

Note Parsing ends when there are three consecutive newlines.

String
-> (String -> style -> model -> Element msg)
-> Block model style msg

Parse a 4-space-indented, unstyled block of text.

It ends after three consecutive newline characters.

Advanced

String
-> ( Parser (style -> model -> List (Element msg))
-> Parser (style -> model -> Element msg)
)
-> Block model style msg

Run a parser created with elm/parser where you can parse whatever you'd like. This is actually how the list block is implemented.

I highly recommend using the other Custom parsers if possible as this is the only one that can "break" an elm-markup file.

You're in charge of defining when this parser will stop parsing.

Current conventions:

  • stop parsing after three consecutive newlines
  • blocks should be indented 4 spaces

These conventions may be enforced in the future.

Here's a terrible example.

Custom.parser "bananaTree"
    (\inlines ->
        -- `inlines` is a parser for styled text which you may or maynot need,
        Parser.succeed identity
            |. Parser.token  " "
            |= (Parser.map
                    -- the result of our parser should be a function that
                    -- takes a `styling` and a `model` and returns `Element msg`
                    (\_ styling model ->
                            Element.el [] (Element.text "bananas"
                    )
                    (Parser.token "bananas")
                )
            |. Parser.token "\n"
    )

It parses bananas and then says bananas. If you wanted to do exactly this in real life, you would probably use some form of Custom.block.