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 ¶
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 ¶
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:
- Replace every 0x00 byte with a space (preserves word boundaries).
- Replace any remaining invalid UTF-8 sequences with U+FFFD (replacement character) so the result is always valid UTF-8.
- Collapse runs of whitespace-only lines to a single blank line to keep the output readable without inflating storage.
func TruncateUTF8 ¶
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 ¶
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 ¶
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 ¶
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:
- ledongthuc/pdf — pure-Go parser; fast, no external deps.
- pdftotext — poppler-utils CLI; handles many more PDF variants.
- 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.
- 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 ¶
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 ¶
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.
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 ¶
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.