Module tbot::markup

source ·
Expand description

Utilities for working with markup.

The purpose of this module is to make it easy and painless to work with different markups correctly. This module contains abstract formatters like bold, italic, link, etc. You can pass strings, other formatters, tuples, slices/arrays and vectors of strings and formatters to them:

use tbot::markup::{italic, bold};
let message = bold((
    "*This is <b>old, ",
    italic("and this is bold and italic!"),
));

However, you can’t use their return values directly — indeed, how do they know if they need to format their inputs as HTML or MarkdownV2? That’s where markup formatters html and markdown_v2 come into play. They take the return values from the abstract utilities and return values that can finally be turned into Text instances:

use tbot::{markup::markdown_v2, types::parameters::Text};
assert_eq!(
    Text::from(markdown_v2(message)),
    Text::with_markdown_v2(
        // the extra `\r`s are needed for correct parsing in edge cases
        "*\\*This is <b\\>old, \r_and this is bold and italic\\!\r_*",
    ),
);

As you can see, you can fearlessly pass any strings to formatters and they’ll be automatically properly escaped. Magic!

Note that methods that support sending markup take impl Into<Text>, so you don’t need to turn formatters into Text manually:

use tbot::{Bot, markup::html, types::chat};

let bot = Bot::from_env("BOT_TOKEN");
bot
    .send_message(chat::Id(42), html("<escaped text, sent as html!>"))
    .call()
    .await
    .unwrap();
}

Re-exports

pub use html::html;
pub use markdown_v2::markdown_v2;

Modules

HTML markup utilities.
MarkdownV2 markup utilities.

Structs

Formats text in bold. Can be created with bold.
Formats a block of code. Can be created with code_block.
Formats an inline piece of code. Can be created with inline_code.
Formats text in italic. Can be created with italic.
Formats a link. Can be created with link or mention.
Represents a raw string for formatting. Can be created with raw.
Formats text with strikethrough. Can be created with strikethrough.
Formats text underlined. Can be created with underline.

Traits

A value that can be formatted in all markups.

Functions

Formats text in bold.
Formats a block of code.
Formats an inline piece of code.
Formats text in italic.
Creates a link.
Creates a mention by ID.
Creates a raw string for formatting.
Formats text with strikethrough.
Formats text underlined.