html

package
v0.115.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2024 License: MIT Imports: 14 Imported by: 10

Documentation

Overview

Package html contains HTML styling options.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bytes

func Bytes(resolver func(id int64) (tg.InputUserClass, error), b []byte) styling.StyledTextOption

Bytes reads HTML from given byte slice and returns styling option to build styled text block.

func Format

func Format(resolver func(id int64) (tg.InputUserClass, error), format string, args ...interface{}) styling.StyledTextOption

Format formats string using fmt, parses HTML from formatted string and returns styling option to build styled text block.

func HTML added in v0.54.0

func HTML(r io.Reader, b *entity.Builder, opts Options) error

HTML parses given input from reader and adds parsed entities to given builder. Notice that this parser ignores unsupported tags.

Parameter userResolver is used to resolve user by ID during formatting. May be nil. If userResolver is nil, formatter will create tg.InputUser using only ID. Notice that it's okay for bots, but not for users.

See https://core.telegram.org/bots/api#html-style.

func Reader

func Reader(resolver func(id int64) (tg.InputUserClass, error), r io.Reader) styling.StyledTextOption

Reader reads HTML from given reader and returns styling option to build styled text block.

func String

func String(resolver func(id int64) (tg.InputUserClass, error), s string) styling.StyledTextOption

String reads HTML from given string and returns styling option to build styled text block.

Example
package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"

	"github.com/gotd/td/telegram"
	"github.com/gotd/td/telegram/message"
	"github.com/gotd/td/telegram/message/html"
	"github.com/gotd/td/tg"
)

func sendHTML(ctx context.Context) error {
	client, err := telegram.ClientFromEnvironment(telegram.Options{})
	if err != nil {
		return err
	}

	// This example creates a styled message from BotAPI examples
	// and sends to your Saved Messages folder.
	// See https://core.telegram.org/bots/api#html-style.
	return client.Run(ctx, func(ctx context.Context) error {
		_, err := message.NewSender(tg.NewClient(client)).
			Self().StyledText(ctx, html.String(nil, `<b>bold</b>, <strong>bold</strong>
<i>italic</i>, <em>italic</em>
<u>underline</u>, <ins>underline</ins>
<s>strikethrough</s>, <strike>strikethrough</strike>, <del>strikethrough</del>
<span class="tg-spoiler">spoiler</span>, <tg-spoiler>spoiler</tg-spoiler>
<b>bold <i>italic bold <s>italic bold strikethrough <span class="tg-spoiler">italic bold strikethrough spoiler</span></s> <u>underline italic bold</u></i> bold</b>
<a href="http://www.example.com/">inline URL</a>
<a href="tg://user?id=123456789">inline mention of a user</a>
<tg-emoji emoji-id="5368324170671202286">👍</tg-emoji>
<code>inline fixed-width code</code>
<pre>pre-formatted fixed-width code block</pre>
<pre><code class="language-python">pre-formatted fixed-width code block written in the Python programming language</code></pre>
<blockquote>Block quotation started\nBlock quotation continued\nThe last line of the block quotation</blockquote>
<blockquote expandable>Expandable block quotation started\nExpandable block quotation continued\nExpandable block quotation continued\nHidden by default part of the block quotation started\nExpandable block quotation continued\nThe last line of the block quotation</blockquote>`))
		return err
	})
}

func main() {
	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
	defer cancel()

	if err := sendHTML(ctx); err != nil {
		_, _ = fmt.Fprintf(os.Stderr, "%+v\n", err)
		os.Exit(2)
	}
}
Output:

Types

type Options added in v0.54.0

type Options struct {
	// UserResolver is used to resolve user by ID during formatting. May be nil.
	//
	// If userResolver is nil, formatter will create tg.InputUser using only ID.
	// Notice that it's okay for bots, but not for users.
	UserResolver entity.UserResolver
	// DisableTelegramEscape disable Telegram BotAPI escaping and uses default
	// golang.org/x/net/html escape.
	DisableTelegramEscape bool
}

Options is options of HTML.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL