123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- package plaintext
- import (
- "servitor/ansi"
- "servitor/style"
- "regexp"
- "strings"
- )
- type Markup struct {
- text string
- cached string
- cachedWidth int
- }
- func NewMarkup(text string) (*Markup, []string, error) {
- rendered, links := renderWithLinks(text, 80)
- return &Markup{
- text: text,
- cached: rendered,
- cachedWidth: 80,
- }, links, nil
- }
- func (m *Markup) Render(width int) string {
- if m.cachedWidth == width {
- return m.cached
- }
- rendered, _ := renderWithLinks(m.text, width)
- m.cached = rendered
- m.cachedWidth = width
- return rendered
- }
- func renderWithLinks(text string, width int) (string, []string) {
-
- links := []string{}
- url := regexp.MustCompile(`[A-Za-z][A-Za-z0-9+\-.]*://[A-Za-z0-9.?#/@:%_~!$&'()*+,;=\[\]\-]+`)
- rendered := url.ReplaceAllStringFunc(text, func(link string) string {
- links = append(links, link)
- return style.Link(link, len(links))
- })
- wrapped := ansi.Wrap(rendered, width)
- return strings.Trim(wrapped, "\n"), links
- }
|