init: initial commit

This commit is contained in:
Blizzard
2026-04-07 17:35:09 +08:00
commit 680ecc320f
129 changed files with 10562 additions and 0 deletions
+45
View File
@@ -0,0 +1,45 @@
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)
}