feat: clean up yomitoku page images (_pN.jpg) after OCR

yomitoku always generates per-page JPEG files that cannot be suppressed
via CLI options. Remove these after successful OCR while preserving the
markdown output and figures/ directory (referenced by markdown).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
dj-oyu 2026-03-19 23:09:53 +09:00
parent 51f08e7f50
commit b4b6c110cc

View file

@ -568,6 +568,11 @@ func (al *AgentLoop) ocrPDF(
return fmt.Sprintf("[file:%s]", pdfPath)
}
// Clean up page images (_pN.jpg) generated by yomitoku.
// These are always created and cannot be suppressed via CLI options.
// Keep: .md files, figures/ directory (referenced by markdown output).
cleanupOCRPageImages(outputDir, pdfPath)
// Find the output markdown file
mdPath := findOCROutput(outputDir, pdfPath)
if mdPath == "" {
@ -669,3 +674,39 @@ func ocrEnvSlice(env map[string]string) []string {
}
return result
}
// cleanupOCRPageImages removes _pN.jpg files generated by yomitoku.
// These per-page images are always created by the CLI and cannot be suppressed.
// Only top-level _pN.jpg files matching the PDF basename are removed;
// the figures/ subdirectory and .md files are preserved.
func cleanupOCRPageImages(outputDir, pdfPath string) {
base := strings.TrimSuffix(filepath.Base(pdfPath), filepath.Ext(pdfPath))
entries, err := os.ReadDir(outputDir)
if err != nil {
return
}
removed := 0
for _, e := range entries {
if e.IsDir() {
continue
}
name := e.Name()
// Match pattern: <dirname>_<basename>_pN.jpg
if !strings.HasSuffix(strings.ToLower(name), ".jpg") {
continue
}
if !strings.Contains(name, base+"_p") {
continue
}
if rmErr := os.Remove(filepath.Join(outputDir, name)); rmErr == nil {
removed++
}
}
if removed > 0 {
logger.DebugCF("agent", "Cleaned up OCR page images", map[string]any{
"dir": outputDir,
"removed": removed,
})
}
}