Polyjuice Client v0.4.4 Polyjuice.Client.MsgBuilder View Source

Build a message out of composable parts.

Link to this section Summary

Functions

Escape special HTML characters.

Create an image.

Create a mathematical expression using LaTeX format.

Create a link.

Colours text as a rainbow.

Generate a Matrix message contents from message data.

Link to this section Functions

Link to this function

html_escape(str, esc_q)

View Source
html_escape(str :: String.t(), escape_quotes :: boolean()) :: iodata()

Escape special HTML characters.

Returns an iodata.

Examples:

iex> Polyjuice.Client.MsgBuilder.html_escape("a<>&\"", false)
...> |> IO.iodata_to_binary()
"a&lt;&gt;&amp;\""

iex> Polyjuice.Client.MsgBuilder.html_escape("a<>&b\"", true)
...> |> IO.iodata_to_binary()
"a&lt;&gt;&amp;b&quot;"
Link to this function

image(src, attrs \\ [])

View Source
image(src :: String.t(), attrs :: list()) ::
  Polyjuice.Client.MsgBuilder.MsgData.t()

Create an image.

Examples:

iex> Polyjuice.Client.MsgBuilder.to_message(
...>   Polyjuice.Client.MsgBuilder.image("mxc://example.com/foo")
...> )
%{
  "msgtype" => "m.text",
  "body" => "[mxc://example.com/foo]",
  "format" => "org.matrix.custom.html",
  "formatted_body" => "<img src=\"mxc://example.com/foo\" />"
}

iex> Polyjuice.Client.MsgBuilder.to_message(
...>   Polyjuice.Client.MsgBuilder.image("mxc://example.com/foo", alt: "some image")
...> )
%{
  "msgtype" => "m.text",
  "body" => "some image",
  "format" => "org.matrix.custom.html",
  "formatted_body" => "<img src=\"mxc://example.com/foo\" alt=\"some image\" />"
}

iex> Polyjuice.Client.MsgBuilder.to_message(
...>   Polyjuice.Client.MsgBuilder.image(
...>     "mxc://example.com/foo", alt: "some image", width: 100
...>   )
...> )
%{
  "msgtype" => "m.text",
  "body" => "some image",
  "format" => "org.matrix.custom.html",
  "formatted_body" => "<img src=\"mxc://example.com/foo\" alt=\"some image\" width=\"100\" />"
}
Link to this function

latex(expression, display \\ false)

View Source
latex(expression :: String.t(), display :: boolean()) ::
  Polyjuice.Client.MsgBuilder.MsgData.t()

Create a mathematical expression using LaTeX format.

Note: This uses the format defined in MSC2191, which is not part of the Matrix spec (yet).

Examples:

iex> Polyjuice.Client.MsgBuilder.to_message(
...>   Polyjuice.Client.MsgBuilder.latex("x^2")
...> )
%{
  "msgtype" => "m.text",
  "body" => "$x^2$",
  "format" => "org.matrix.custom.html",
  "formatted_body" => "<span data-mx-maths=\"x^2\"><code>x^2</code></span>"
}

iex> Polyjuice.Client.MsgBuilder.to_message(
...>   Polyjuice.Client.MsgBuilder.latex("x^2", true)
...> )
%{
  "msgtype" => "m.text",
  "body" => "$$\nx^2\n$$",
  "format" => "org.matrix.custom.html",
  "formatted_body" => "<div data-mx-maths=\"x^2\"><code>x^2</code></div>"
}

Create a link.

Examples:

iex> Polyjuice.Client.MsgBuilder.to_message(
...>   Polyjuice.Client.MsgBuilder.link(
...>     Polyjuice.Client.MsgBuilder.image(
...>       "mxc://example.com/foo", alt: "some image", width: 100
...>     ),
...>     "https://matrix.org/"
...>   )
...> )
%{
  "msgtype" => "m.text",
  "body" => "[some image](https://matrix.org/)",
  "format" => "org.matrix.custom.html",
  "formatted_body" => "<a href=\"https://matrix.org/\"><img src=\"mxc://example.com/foo\" alt=\"some image\" width=\"100\" /></a>"
}
Link to this function

mention(user_id, display_name \\ nil)

View Source
mention(user_id :: String.t(), display_name :: String.t() | nil) ::
  Polyjuice.Client.MsgBuilder.MsgData.t()

Mention a user.

Examples:

iex> Polyjuice.Client.MsgBuilder.to_message(
...>   Polyjuice.Client.MsgBuilder.mention("@alice:example.com", "Alice")
...> )
%{
  "msgtype" => "m.text",
  "body" => "Alice",
  "format" => "org.matrix.custom.html",
  "formatted_body" => "<a href=\"https://matrix.to/#/@alice:example.com\">Alice</a>"
}

iex> Polyjuice.Client.MsgBuilder.to_message(
...>   Polyjuice.Client.MsgBuilder.mention("@alice:example.com")
...> )
%{
  "msgtype" => "m.text",
  "body" => "@alice:example.com",
  "format" => "org.matrix.custom.html",
  "formatted_body" => "<a href=\"https://matrix.to/#/@alice:example.com\">@alice:example.com</a>"
}

Colours text as a rainbow.

Example:

iex> Polyjuice.Client.MsgBuilder.to_message(
...>   Polyjuice.Client.MsgBuilder.rainbow("Hello, world!")
...> )
%{
  "body" => "๐ŸŒˆHello, world!๐ŸŒˆ",
  "format" => "org.matrix.custom.html",
  "formatted_body" => ~s(<font color="#FF00BE">H</font><font color="#FF0055">e</font><font color="#FF7200">l</font><font color="#F6AB00">l</font><font color="#92CB00">o</font><font color="#00DC00">,</font><font color="#00E47C"> </font><font color="#00E7EE">w</font><font color="#00E6FF">o</font><font color="#00DCFF">r</font><font color="#00C1FF">l</font><font color="#BE8EFF">d</font><font color="#FF21FF">!</font>),
  "msgtype" => "m.text"
}
Link to this function

to_message(msgdata, msgtype \\ "m.text")

View Source
to_message(
  msgdata :: Polyjuice.Client.MsgBuilder.MsgData.t(),
  msgtype :: String.t()
) :: Polyjuice.Util.event_content()

Generate a Matrix message contents from message data.

Examples:

iex> Polyjuice.Client.MsgBuilder.to_message("foo")
%{"msgtype" => "m.text", "body" => "foo"}

iex> Polyjuice.Client.MsgBuilder.to_message(["foo", "bar"])
%{"msgtype" => "m.text", "body" => "foobar"}