extractor

package
v0.16.1 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2026 License: MIT Imports: 16 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 extractor provides content extraction for binary and structured file formats (HTML, PDF, Office, etc.) that cannot be read as plain text.

Index

Constants

View Source
const (
	// MaxExtractedBytes is the cap on extracted text returned by any extractor.
	// File size is irrelevant — a 20 MB PDF may contain only 50 KB of text.
	MaxExtractedBytes = 512 * 1024 // 512 KB

	// ExtractTimeout is the per-file deadline for binary extraction operations.
	ExtractTimeout = 10 // seconds — callers apply this via context.WithTimeout
)

Variables

This section is empty.

Functions

func SanitizeText

func SanitizeText(s string) string

SanitizeText removes characters that Postgres TEXT columns reject and normalises whitespace.

Postgres TEXT rejects the NUL byte (0x00) with:

ERROR: invalid byte sequence for encoding "UTF8": 0x00 (SQLSTATE 22021)

PDF extractors (and occasionally HTML parsers) may embed NUL bytes in their output. This function must be called on all extractor output before the text is written to the database.

Steps applied in order:

  1. Replace every 0x00 byte with a space (preserves word boundaries).
  2. Replace any remaining invalid UTF-8 sequences with U+FFFD (replacement character) so the result is always valid UTF-8.
  3. Collapse runs of whitespace-only lines to a single blank line to keep the output readable without inflating storage.

func TruncateUTF8

func TruncateUTF8(text string, maxBytes int) string

TruncateUTF8 trims b to at most maxBytes while preserving valid UTF-8 boundaries. It appends "\n[content truncated]" when trimming occurs.

Types

type DocxExtractor

type DocxExtractor struct{}

DocxExtractor extracts plain text from Microsoft Word (.docx) files. It unzips word/document.xml and concatenates all <w:t> text nodes.

func (*DocxExtractor) Extract

func (e *DocxExtractor) Extract(ctx context.Context, absPath string) (string, error)

Extract reads the docx file at absPath and returns its plain-text content.

func (*DocxExtractor) Supports

func (e *DocxExtractor) Supports(ext string) bool

Supports returns true for .docx files.

type Extractor

type Extractor interface {
	// Supports reports whether this extractor handles the given lowercase extension
	// (including the leading dot, e.g. ".pdf").
	Supports(ext string) bool

	// Extract reads the file at absPath and returns its plain-text content.
	// The caller is responsible for supplying a context with an appropriate
	// deadline. Extract must never panic; it returns an error on failure.
	Extract(ctx context.Context, absPath string) (string, error)
}

Extractor extracts plain text from a single file format.

type HTMLExtractor

type HTMLExtractor struct{}

HTMLExtractor extracts visible text from HTML files, stripping tags, scripts, and style blocks.

func (*HTMLExtractor) Extract

func (e *HTMLExtractor) Extract(ctx context.Context, absPath string) (string, error)

Extract reads the HTML file at absPath and returns its visible text content.

func (*HTMLExtractor) Supports

func (e *HTMLExtractor) Supports(ext string) bool

Supports returns true for .html and .htm files.

type HwpxExtractor

type HwpxExtractor struct{}

HwpxExtractor extracts plain text from Hancom HWPX (.hwpx) files. HWPX uses the OWPML (Open Word Processor Markup Language) format — a ZIP archive containing XML section files at Contents/section<N>.xml. Each section element uses the hp: namespace (http://www.hancom.co.kr/hwpml/2011/paragraph). Text is extracted from <hp:t> nodes; a newline is emitted for every </hp:p>.

func (*HwpxExtractor) Extract

func (e *HwpxExtractor) Extract(ctx context.Context, absPath string) (string, error)

Extract reads the hwpx file at absPath and returns its plain-text content.

func (*HwpxExtractor) Supports

func (e *HwpxExtractor) Supports(ext string) bool

Supports returns true for .hwpx files.

type PDFExtractor

type PDFExtractor struct{}

PDFExtractor extracts plain text from PDF files using a multi-stage fallback chain designed to handle image-based and Korean-language PDFs:

  1. ledongthuc/pdf — pure-Go parser; fast, no external deps.
  2. pdftotext — poppler-utils CLI; handles many more PDF variants.
  3. ocrmypdf — OCR via Tesseract; catches image-only pages. ocrmypdf is always co-installed in the collector image and is the only OCR path; tesseract cannot read PDF input directly.
  4. pdfinfo — metadata fallback; ensures the document is at least indexable even when no text layer or OCR is possible.

Each external stage is skipped gracefully when the required binary is not found in PATH, so the extractor is fully usable in environments that only have the Go runtime installed.

Every external command is bound to the caller's context so that timeouts and cancellations propagate correctly.

func (*PDFExtractor) Extract

func (e *PDFExtractor) Extract(ctx context.Context, absPath string) (string, error)

Extract reads the PDF at absPath and returns its plain-text content. It runs the fallback chain and applies SanitizeText + TruncateUTF8 to the first stage that produces sufficient text.

func (*PDFExtractor) Supports

func (e *PDFExtractor) Supports(ext string) bool

Supports returns true for .pdf files.

type PptxExtractor

type PptxExtractor struct{}

PptxExtractor extracts plain text from Microsoft PowerPoint (.pptx) files. It reads each ppt/slides/slide*.xml file and concatenates <a:t> text nodes.

func (*PptxExtractor) Extract

func (e *PptxExtractor) Extract(ctx context.Context, absPath string) (string, error)

Extract reads the pptx file at absPath and returns its plain-text content.

func (*PptxExtractor) Supports

func (e *PptxExtractor) Supports(ext string) bool

Supports returns true for .pptx files.

type Registry

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

Registry holds a list of extractors and picks the first one that supports the requested extension.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns a Registry pre-loaded with all built-in extractors.

func (*Registry) Find

func (r *Registry) Find(ext string) Extractor

Find returns the first extractor that supports ext, or nil if none does.

type XlsxExtractor

type XlsxExtractor struct{}

XlsxExtractor extracts structured text from Microsoft Excel (.xlsx) files. Each sheet is emitted as a TSV block preceded by a "##SHEET <name>" header. Rows are newline-separated; cells within a row are tab-separated.

func (*XlsxExtractor) Extract

func (e *XlsxExtractor) Extract(ctx context.Context, absPath string) (string, error)

Extract reads the xlsx file at absPath and returns its TSV-formatted content.

func (*XlsxExtractor) Supports

func (e *XlsxExtractor) Supports(ext string) bool

Supports returns true for .xlsx files.

Jump to

Keyboard shortcuts

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