text

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package text reads plain text into documents and writes documents to text files.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FileWriter added in v0.18.0

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

FileWriter persists documents as plain text. It honors Append, optionally injects document-marker headers, and calls *os.File.Sync before returning so callers can rely on durability when the call completes.

Example:

w, err := text.NewFileWriter(text.FileWriterConfig{
    Path:            "out.txt",
    DocumentMarkers: true,
})
err = w.Write(ctx, docs)
Example
package main

import (
	"context"
	"fmt"
	"os"
	"path/filepath"

	"github.com/Tangerg/scope/core/document"
	"github.com/Tangerg/scope/etl/text"
)

func main() {
	directory, err := os.MkdirTemp("", "scope-text-example-")
	if err != nil {
		panic(err)
	}
	defer func() {
		if cleanupErr := os.RemoveAll(directory); cleanupErr != nil {
			panic(cleanupErr)
		}
	}()
	path := filepath.Join(directory, "documents.txt")
	writer, err := text.NewFileWriter(text.FileWriterConfig{Path: path})
	if err != nil {
		panic(err)
	}
	doc, err := document.NewDocument("Retrieved evidence.", nil)
	if err != nil {
		panic(err)
	}
	if writeErr := writer.Write(context.Background(), []*document.Document{doc}); writeErr != nil {
		panic(writeErr)
	}
	contents, err := os.ReadFile(path)
	if err != nil {
		panic(err)
	}
	fmt.Print(string(contents))
}
Output:
Retrieved evidence.

func NewFileWriter added in v0.18.0

func NewFileWriter(config FileWriterConfig) (*FileWriter, error)

NewFileWriter validates its filesystem boundary before accepting documents.

func (*FileWriter) Write added in v0.18.0

func (f *FileWriter) Write(ctx context.Context, docs []*document.Document) (err error)

Write validates and renders every document before opening the destination, so document or formatting failures leave an existing file untouched. Once the destination is opened, the prepared payload is committed even if ctx is subsequently canceled. Close errors are joined with earlier I/O failures.

type FileWriterConfig added in v0.18.0

type FileWriterConfig struct {
	// Path is required. Existing files are replaced unless Append is true.
	Path string
	// DocumentMarkers adds an index header before each document.
	DocumentMarkers bool
	// Append preserves existing file contents.
	Append bool
	// Formatter renders each document. Nil writes document text only.
	Formatter etl.Formatter
}

FileWriterConfig fixes the output authority and filename policy at construction time.

type Reader

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

Reader reads the entire contents of an io.Reader and packages it into one *document.Document. Use it for files, in-memory buffers, or network streams that fit comfortably in memory.

func NewReader

func NewReader(source io.Reader, config ReaderConfig) (*Reader, error)

NewReader fixes decoding and source-budget policy before extraction.

func (*Reader) Read

func (r *Reader) Read(ctx context.Context) ([]*document.Document, error)

Read consumes the source and returns one document containing its text. Blank input returns no documents.

type ReaderConfig

type ReaderConfig struct {
	SourceBudget etl.SourceBudget
}

ReaderConfig controls the whole-source memory budget. The zero budget uses etl.DefaultMaxSourceBytes.

Jump to

Keyboard shortcuts

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