document

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: AGPL-3.0 Imports: 33 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 document decodes documents and loads, saves, and checks local files.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrConflict    = errors.New("document: file changed on disk")
	ErrUnsupported = errors.New("document: unsupported encoding")
	ErrNotText     = errors.New("document: not a text file")
	ErrReadOnly    = errors.New("document: read-only file")
	ErrLossy       = errors.New("document: reopen with an encoding that decodes the original bytes")
)

Functions

func ConvertIndent

func ConvertIndent(text string, from, to Indent) string

ConvertIndent converts each line's leading ASCII spaces and tabs, preserving the rest of the line and its ending. Tabs advance to the next source tab stop. When converting an all-space prefix to spaces, complete source indent levels are resized and any remaining spaces are kept. A nonpositive source size uses the target size. A nonpositive target size leaves the text unchanged.

func PrepareText

func PrepareText(text string, o SaveOptions) string

PrepareText normalizes CRLF to LF and applies the requested text cleanup. Save uses the returned text before converting line endings and encoding it.

Types

type Doc

type Doc struct {
	Path     string
	Kind     Kind
	Text     string // LF-normalized for text and SVG documents.
	Data     []byte // Original bytes for images, nil for other kinds.
	Width    int    // Image width in pixels, zero if unknown.
	Height   int    // Image height in pixels, zero if unknown.
	Animated bool   // The image has multiple frames or declares animation.
	Encoding Encoding
	EOL      EOL
	Indent   Indent
	ReadOnly bool
	Lossy    bool // Text contains substitutions for undecodable bytes.
	Stamp    Stamp
}

Doc holds a loaded document. A Doc may only be mutated from one goroutine.

func Decode

func Decode(name string, data []byte, o LoadOptions) (*Doc, error)

Decode identifies and decodes an in-memory document using the extension in name. Path and Stamp are zero values. Images retain an independent copy of data. Recognized audio and video have kind Binary.

func Load

func Load(path string, o LoadOptions) (*Doc, error)

Load identifies a regular file and decodes text when present. Recognized audio and video are read-only and carry no content bytes.

type EOL

type EOL string

EOL identifies the line ending used on disk.

const (
	LF   EOL = "lf"
	CRLF EOL = "crlf"
)

type Encoding

type Encoding string

Encoding identifies the character encoding of a text document.

const (
	UTF8     Encoding = "utf-8"
	UTF8BOM  Encoding = "utf-8-bom"
	UTF16LE  Encoding = "utf-16le"
	UTF16BE  Encoding = "utf-16be"
	GBK      Encoding = "gbk"
	GB18030  Encoding = "gb18030"
	Big5     Encoding = "big5"
	ShiftJIS Encoding = "shift_jis"
	Latin1   Encoding = "latin1"
)

func Encodings

func Encodings() []Encoding

Encodings returns a fresh list of the encodings supported for loading and saving.

type File

type File struct {
	Path      string
	Workspace *workspace.Workspace
}

File encapsulates a path and its original access scope. A nil Workspace denotes a document the user explicitly opened outside workspace navigation.

func (File) Check

func (f File) Check(stamp Stamp) (State, error)

Check compares scoped files without following a newly redirected link.

func (File) Load

func (f File) Load(o LoadOptions) (*Doc, error)

Load identifies and decodes the file within its original access scope.

func (File) Save

func (f File) Save(text string, enc Encoding, eol EOL, expect Stamp, o SaveOptions) (_ Stamp, err error)

Save writes text atomically within the file's original access scope.

func (File) Stat

func (f File) Stat() (_ Stamp, err error)

Stat computes the current fingerprint using only the scoped file handle.

type Indent

type Indent struct {
	Tabs bool
	Size int
}

Indent describes the dominant indentation. Size zero means unknown.

type Kind

type Kind uint8

Kind identifies the document's content.

const (
	Text Kind = iota
	Binary
	Image
	TooLarge
	Video
	Audio
)

type LoadOptions

type LoadOptions struct {
	Encoding     Encoding // Empty to detect the encoding automatically.
	Fallback     Encoding // Used if there is no BOM and the bytes are invalid UTF-8.
	MaxSize      int64    // Zero uses the 256 MiB default. Recognized disk media bypass this limit.
	ReadOnlyOver int64    // Zero uses the 20 MiB default.
}

LoadOptions controls text decoding and size limits for documents.

type SaveOptions

type SaveOptions struct {
	Context           context.Context // Checked immediately before publishing a scoped write.
	TrimTrailingSpace bool
	FinalNewline      bool
	Force             bool
	Recreate          bool     // Publish only if the previous target is still absent.
	SourceEncoding    Encoding // Encoding used to read the existing buffer, if known.
}

SaveOptions controls text cleanup and the conditions for publishing a write.

type Stamp

type Stamp struct {
	Size            int64
	ModTime         time.Time
	Sum             [32]byte
	WorkspaceTarget string // Resolved relative workspace target for a scoped file.
	// contains filtered or unexported fields
}

Stamp describes the file on disk. Sum is the SHA-256 of its original bytes, or zero if Load returned TooLarge, Video, or Audio.

func Save

func Save(path, text string, enc Encoding, eol EOL, expect Stamp, o SaveOptions) (Stamp, error)

Save writes encoded text atomically. A zero expected stamp requests a new file. Existing-file commits serialize by the resolved parent directory across cooperating processes. Other writers can still change the path or contents between the final check and replacement.

func Stat

func Stat(path string) (Stamp, error)

Stat computes the current stamp of a regular file.

type State

type State uint8

State describes how a file on disk compares with its previous stamp.

const (
	Unchanged State = iota
	Modified
	Deleted
)

func Check

func Check(path string, s Stamp) (State, error)

Check compares the current file with a previous stamp.

Jump to

Keyboard shortcuts

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