text

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: Apache-2.0 Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package text reads text documents as UTF-8, from whatever character set they're in, and writes plain text as Markdown, so it's shown as it is

Index

Constants

View Source
const (
	// MaxBytes is the most of a text document which is read, as the
	// document is decoded, or parsed, in memory. A larger document is read
	// up to this size.
	MaxBytes = 16 << 20
)

Variables

This section is empty.

Functions

func Decode

func Decode(data []byte, cut bool) (string, error)

Decode returns the text as UTF-8, from the character set of its byte order mark, or UTF-8 when it's valid UTF-8, or else Windows-1252. When the text was cut off, a character cut off at its end is removed first, so it isn't read as invalid UTF-8.

func Escape

func Escape(text string) string

Escape returns the text as Markdown within a line, such as a cell of a table, with its characters which are Markdown escaped, and its line breaks and tabs as spaces

func HasText added in v1.0.6

func HasText(r io.Reader) bool

HasText returns true when the start of the text has a character which isn't white space, and no NUL, as text which is binary has, which is read without moving r, when it can be read at an offset, as a handler's input can

func NewReader added in v1.0.6

func NewReader(r io.Reader) io.Reader

NewReader returns a reader of the text as UTF-8, as it's read, so a text of any size is read a part at a time, rather than in memory: from the character set of its byte order mark, or UTF-8 when its start is valid UTF-8, or else Windows-1252, as Decode does. Sequences of UTF-8 which aren't valid, after its start, are replaced. When its start can't be read, the reader returns the error.

func PlainText

func PlainText(value string) string

PlainText returns text which may be HTML, such as a description, as plain text, without its tags, with its entities decoded, and its spaces collapsed

func Read

func Read(r io.Reader) (string, error)

Read returns at most MaxBytes of the text as UTF-8, from the character set of its byte order mark, or UTF-8 when it's valid UTF-8, or else Windows-1252

func WriteCodeBlock added in v1.0.6

func WriteCodeBlock(w io.StringWriter, language string, r io.Reader) error

WriteCodeBlock writes the text read from r as a fenced Markdown code block in the language, as CodeBlock returns it, as it's read, so it stops reading when a write returns an error. Its fence is longer than any run of backticks in the text, which it's read for first, when r can seek, or else it's read in memory, up to MaxBytes.

func WriteLF added in v1.0.6

func WriteLF(w io.StringWriter, r io.Reader) error

WriteLF writes the text read from r to w, without its carriage returns before line feeds, as it's read, so it stops reading when a write returns an error

func WriteMarkdown

func WriteMarkdown(w io.StringWriter, text string) error

WriteMarkdown writes the text as Markdown, a line at a time, so it's shown as it is: its characters which are Markdown are escaped, its line breaks are kept, a blank line separates its paragraphs, and its indentation is kept without the line being code. It stops when a write returns an error.

func WriteMarkdownFrom added in v1.0.6

func WriteMarkdownFrom(w io.StringWriter, r io.Reader) error

WriteMarkdownFrom writes the text read from r as Markdown, as WriteMarkdown does, a character at a time, as it's read, so it stops reading when a write returns an error, such as when the writer has the part of the Markdown it keeps. Only white space, and digits at the start of a line, are held in memory, rather than a line, so the memory used doesn't grow with the length of a line.

Types

This section is empty.

Jump to

Keyboard shortcuts

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