-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
html.go
46 lines (40 loc) · 1.44 KB
/
html.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Package html contains HTML styling options.
package html
import (
"bytes"
"fmt"
"io"
"strings"
"github.com/gotd/td/telegram/message/entity"
"github.com/gotd/td/telegram/message/styling"
"github.com/gotd/td/tg"
)
// Bytes reads HTML from given byte slice and returns styling option
// to build styled text block.
func Bytes(resolver func(id int) (tg.InputUserClass, error), b []byte) styling.StyledTextOption {
return Reader(resolver, bytes.NewReader(b))
}
// String reads HTML from given string and returns styling option
// to build styled text block.
func String(resolver func(id int) (tg.InputUserClass, error), s string) styling.StyledTextOption {
return Reader(resolver, strings.NewReader(s))
}
// Format formats string using fmt, parses HTML from formatted string and returns styling option
// to build styled text block.
func Format(resolver func(id int) (tg.InputUserClass, error), format string, args ...interface{}) styling.StyledTextOption {
return styling.Custom(func(eb *entity.Builder) error {
var buf bytes.Buffer
_, err := fmt.Fprintf(&buf, format, args...)
if err != nil {
return err
}
return entity.HTML(&buf, eb, resolver)
})
}
// Reader reads HTML from given reader and returns styling option
// to build styled text block.
func Reader(resolver func(id int) (tg.InputUserClass, error), r io.Reader) styling.StyledTextOption {
return styling.Custom(func(eb *entity.Builder) error {
return entity.HTML(r, eb, resolver)
})
}