fix(parser): keep PDFs on builtin instead of defaulting to anydoc

anydoc has no document model for PDF: Convert force-disables asset
extraction, and AnydocReader falls back to builtin only when the whole
file yields no text. A born-digital PDF with figures and tables that
still yields a few characters is therefore reported as parsed, with its
images, tables and layout silently dropped.

35275d56 widened the type-level preference from ppt/pptx to every type
anydoc claims, and pdf is on that list. The predicate asks whether
anydoc can convert a type, not whether it is the better parser for it.
60207997, one commit earlier, recorded the original intent: "Types
without a fallback (pdf, docx, ...) never consult this".

The builtin PDF parser classifies pages individually and routes scanned
ones through OCR/VLM, so it stays the default for PDF. Every other
anydoc format is unchanged, and an explicit pdf -> anydoc engine rule
still wins.
main
Lvcy_cd 2026-09-21 14:25:39 +08:00 committed by lyingbug
parent 56d075e213
commit cc82e75cb5
2 changed files with 33 additions and 6 deletions

View File

@ -33,7 +33,7 @@ func TestListAllEnginesBuiltinIncludesDocumentFormats(t *testing.T) {
}
func TestDefaultParserEnginePrefersAnydocWhenLinked(t *testing.T) {
cases := []string{"pptx", "ppt", "pdf", "docx"}
cases := []string{"pptx", "ppt", "docx"}
if anydoc.Available() {
for _, ft := range cases {
if got := types.DefaultParserEngine(ft); got != AnydocEngineName {
@ -43,15 +43,33 @@ func TestDefaultParserEnginePrefersAnydocWhenLinked(t *testing.T) {
if got := types.DefaultParserEngine("csv"); got != "" {
t.Errorf("DefaultParserEngine(csv) = %q, want empty so the Go simple reader stays default", got)
}
// anydoc claims pdf but cannot model one, so linking the binding must
// not move PDFs off builtin. This branch is where that is observable.
for _, ft := range []string{"pdf", ".PDF"} {
if got := types.DefaultParserEngine(ft); got != "" {
t.Errorf("DefaultParserEngine(%q) = %q, want empty so PDFs stay on builtin", ft, got)
}
}
return
}
if got := types.DefaultParserEngine("pptx"); got != "markitdown" {
t.Fatalf("DefaultParserEngine(pptx) = %q, want markitdown when anydoc is unavailable", got)
}
if got := types.DefaultParserEngine("pdf"); got != "" {
t.Fatalf("DefaultParserEngine(pdf) = %q, want empty when anydoc is unavailable", got)
}
if got := types.DefaultParserEngine("docx"); got != "" {
t.Fatalf("DefaultParserEngine(docx) = %q, want empty when anydoc is unavailable", got)
}
}
// The PDF exclusion only matters because anydoc still claims the type. If a
// future anydoc drops pdf, the exclusion becomes dead code and should go with
// it — this guard runs in every build, tagged or not.
func TestPreferAnydocSkipsPDF(t *testing.T) {
if !anydoc.Supports("pdf", "") {
t.Fatal("anydoc no longer claims pdf; drop the explicit exclusion in preferAnydocWhenAvailable")
}
for _, ft := range []string{"pdf", ".PDF", " Pdf "} {
if got := preferAnydocWhenAvailable(ft); got != "" {
t.Errorf("preferAnydocWhenAvailable(%q) = %q, want empty so PDFs stay on builtin", ft, got)
}
}
}

View File

@ -44,11 +44,20 @@ func init() {
// preferAnydocWhenAvailable is the type-level default override: when the
// anydoc binding is linked and converts this file type, use it instead of
// builtin or the markitdown fallback. Simple formats stay on the Go reader.
//
// PDF is excluded. anydoc has no document model for it, so Convert force-
// disables asset extraction, and AnydocReader only falls back to builtin
// when the whole file yields no text at all. A born-digital PDF that yields
// a few characters is therefore reported as parsed while its figures,
// tables and layout are gone. The builtin parser classifies pages
// individually and routes scanned ones through OCR/VLM, so it is the better
// default for the one type anydoc cannot model.
func preferAnydocWhenAvailable(fileType string) string {
if IsSimpleFormat(fileType) {
ft := strings.ToLower(strings.TrimPrefix(strings.TrimSpace(fileType), "."))
if IsSimpleFormat(ft) || ft == "pdf" {
return ""
}
if anydoc.Available() && anydoc.Supports(fileType, "") {
if anydoc.Available() && anydoc.Supports(ft, "") {
return AnydocEngineName
}
return ""