package parse import ( "bufio" "bytes" "fmt" "io" "git.sr.ht/~rjarry/aerc/lib/log" ) // ExtractImages scans a reader for OSC 9 image markers, replaces them with // null-byte-delimited placeholders (\x00IMG:N\x00), and returns the cleaned // reader plus a slice of ImageRef structs. // // Lines containing valid image markers are replaced entirely with the // placeholder. Lines with invalid markers (e.g. missing path) pass through // unchanged. func ExtractImages(r io.Reader) (io.Reader, []ImageRef) { var images []ImageRef buf := bytes.NewBuffer(nil) scanner := bufio.NewScanner(r) scanner.Buffer(nil, 1024*1024*1024) for scanner.Scan() { line := scanner.Bytes() if bytes.Contains(line, oscImagePrefix) { if ref, ok := ParseImageOSC(line); ok { ref.Index = len(images) images = append(images, *ref) fmt.Fprintf(buf, "\x00IMG:%d\x00\n", ref.Index) continue } } buf.Write(line) buf.WriteByte('\n') } if err := scanner.Err(); err != nil { log.Warnf("ExtractImages: scan error: %v", err) } return buf, images }