format

package
v0.18.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2024 License: MPL-2.0 Imports: 11 Imported by: 44

Documentation

Overview

Package format contains utilities for working with Matrix HTML, specifically methods to parse Markdown into HTML and to parse Matrix HTML into text or markdown.

https://spec.matrix.org/v1.2/client-server-api/#mroommessage-msgtypes

Index

Constants

This section is empty.

Variables

View Source
var BlockTags = []string{"p", "h1", "h2", "h3", "h4", "h5", "h6", "ol", "ul", "pre", "blockquote", "div", "hr", "table"}

Functions

func DefaultPillConverter added in v0.9.14

func DefaultPillConverter(displayname, mxid, eventID string, _ Context) string

func Digits

func Digits(num int) int

Digits counts the number of digits (and the sign, if negative) in an integer.

func HTMLToContent added in v0.12.0

func HTMLToContent(html string) event.MessageEventContent

func HTMLToMarkdown added in v0.12.0

func HTMLToMarkdown(html string) string

HTMLToMarkdown converts Matrix HTML into markdown with the default settings.

Currently, the only difference to HTMLToText is how links are formatted.

func HTMLToText

func HTMLToText(html string) string

HTMLToText converts Matrix HTML into text with the default settings.

func LongestSequence added in v0.14.0

func LongestSequence(in string, of rune) int

func RenderMarkdown

func RenderMarkdown(text string, allowMarkdown, allowHTML bool) event.MessageEventContent

func RenderMarkdownCustom added in v0.12.0

func RenderMarkdownCustom(text string, renderer goldmark.Markdown) event.MessageEventContent

func UnwrapSingleParagraph added in v0.12.0

func UnwrapSingleParagraph(html string) string

UnwrapSingleParagraph removes paragraph tags surrounding a string if the string only contains a single paragraph.

Types

type CodeBlockConverter

type CodeBlockConverter func(code, language string, ctx Context) string

type ColorConverter added in v0.12.0

type ColorConverter func(text, fg, bg string, ctx Context) string

type Context added in v0.7.0

type Context struct {
	ReturnData map[string]any
	TagStack   TagStack

	PreserveWhitespace bool
}

func NewContext added in v0.14.0

func NewContext() Context

func (Context) WithTag added in v0.14.0

func (ctx Context) WithTag(tag string) Context

func (Context) WithWhitespace added in v0.14.0

func (ctx Context) WithWhitespace() Context

type HTMLParser

type HTMLParser struct {
	PillConverter           PillConverter
	TabsToSpaces            int
	Newline                 string
	HorizontalLine          string
	BoldConverter           TextConverter
	ItalicConverter         TextConverter
	StrikethroughConverter  TextConverter
	UnderlineConverter      TextConverter
	LinkConverter           LinkConverter
	SpoilerConverter        SpoilerConverter
	ColorConverter          ColorConverter
	MonospaceBlockConverter CodeBlockConverter
	MonospaceConverter      TextConverter
	TextConverter           TextConverter
}

HTMLParser is a somewhat customizable Matrix HTML parser.

func (*HTMLParser) Parse

func (parser *HTMLParser) Parse(htmlData string, ctx Context) string

Parse converts Matrix HTML into text using the settings in this parser.

type LinkConverter added in v0.12.0

type LinkConverter func(text, href string, ctx Context) string

type PillConverter added in v0.9.14

type PillConverter func(displayname, mxid, eventID string, ctx Context) string

type SpoilerConverter added in v0.12.0

type SpoilerConverter func(text, reason string, ctx Context) string

type TagStack added in v0.14.0

type TagStack []string

func (TagStack) Has added in v0.14.0

func (ts TagStack) Has(tag string) bool

func (TagStack) Index added in v0.14.0

func (ts TagStack) Index(tag string) int

type TaggedString

type TaggedString struct {
	// contains filtered or unexported fields
}

TaggedString is a string that also contains a HTML tag.

type TextConverter

type TextConverter func(string, Context) string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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