Documentation
¶
Overview ¶
Package rtf converts between a practical subset of the Rich Text Format (RTF) and the neutral github.com/go-richdoc/richdoc document model.
Parse tokenises RTF (groups, control words, control symbols, \'hh hex bytes and \uN Unicode escapes) and folds the group-scoped character state (bold, italic, strikethrough, monospace) into a richdoc.Document. Write emits a minimal, well-formed RTF document from a richdoc.Document. The two directions are designed as a faithful round-trip for the supported subset: Parse(Write(Parse(src))) is semantically equal to Parse(src).
Footnote groups map to richdoc.Footnote, bookmarks ({\*\bkmkstart}/{\*\bkmkend}) to richdoc.Anchor, and REF/PAGEREF fields to a RefLabel richdoc.CrossRef; HYPERLINK fields stay a richdoc.Link.
RTF has no native concept for several richdoc nodes (code blocks, block and inline math, tables, images with embedded data). Those are emitted in a best-effort visual form by Write and are documented as one-way mappings. Conversely, constructs the model has no node for (embedded pictures and fields other than hyperlinks and references) are preserved verbatim through richdoc.RawInline with Format "rtf", so nothing in the source is silently lost.
The package is pure Go and builds with CGO disabled, including for GOOS=js/GOARCH=wasm.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrEmpty is returned for empty input. ErrEmpty = errors.New("rtf: empty input") // ErrNotRTF is returned when the stream does not begin with {\rtf. ErrNotRTF = errors.New("rtf: not an RTF document (missing {\\rtf header)") // ErrUnbalanced is returned for a group brace mismatch. ErrUnbalanced = errors.New("rtf: unbalanced group braces") // ErrTruncated is returned for a control sequence cut off by end of input. ErrTruncated = errors.New("rtf: truncated control sequence") // ErrBadHex is returned for a malformed \'hh escape. ErrBadHex = errors.New("rtf: invalid hex escape") )
Sentinel errors returned by Parse. They are wrapped-free so callers can compare with errors.Is.
Functions ¶
func Parse ¶
Parse converts a practical subset of RTF into a richdoc.Document.
func Write ¶
Write emits a well-formed RTF document from a richdoc.Document. It always succeeds; the error result is part of the symmetric API with Parse. A nil document produces an empty but valid RTF file.
The header declares two fonts (proportional \f0, monospace \f1) and a small stylesheet mapping \s1..\s6 to heading levels, which Parse reads back to reconstruct headings. Font 1 also carries inline code, code blocks and math.
Types ¶
This section is empty.