46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
package parser
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// Parser interface for all document parsers.
|
|
type Parser interface {
|
|
SupportedExtensions() []string
|
|
ParseToMarkdown(path string) (string, error)
|
|
}
|
|
|
|
// Registry holds registered parsers keyed by extension.
|
|
type Registry struct {
|
|
parsers map[string]Parser
|
|
}
|
|
|
|
// NewRegistry creates a parser registry with all built-in parsers.
|
|
func NewRegistry() *Registry {
|
|
r := &Registry{parsers: make(map[string]Parser)}
|
|
r.Register(&ExcelParser{})
|
|
r.Register(&PDFParser{})
|
|
r.Register(&CADParser{})
|
|
r.Register(&WordParser{})
|
|
return r
|
|
}
|
|
|
|
// Register adds a parser for its supported extensions.
|
|
func (r *Registry) Register(p Parser) {
|
|
for _, ext := range p.SupportedExtensions() {
|
|
r.parsers[strings.ToLower(ext)] = p
|
|
}
|
|
}
|
|
|
|
// Parse dispatches to the appropriate parser based on file extension.
|
|
func (r *Registry) Parse(path string) (string, error) {
|
|
ext := strings.ToLower(filepath.Ext(path))
|
|
p, ok := r.parsers[ext]
|
|
if !ok {
|
|
return "", fmt.Errorf("unsupported file type: %s", ext)
|
|
}
|
|
return p.ParseToMarkdown(path)
|
|
}
|