kindling-mobi 0.14.5

Kindle MOBI/AZW3 builder for dictionaries, books, and comics. Drop-in kindlegen replacement.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
/// kindling - Kindle MOBI builder for dictionaries and books
///
/// Usage:
///     kindling build input.opf -o output.mobi
///     kindling build input.epub -o output.mobi
///
/// Kindlegen-compatible usage:
///     kindling input.epub
///     kindling input.opf -o output.mobi -dont_append_source -verbose

use kindling::{
    comic, epub, extracted::ExtractedEpub, kdp_rules, mobi, mobi_check, mobi_dump, mobi_rewrite,
    opf, repair, validate,
};

use std::path::PathBuf;
use std::process;

use clap::{Parser, Subcommand};

#[derive(Parser)]
#[command(name = "kindling", about = "Kindle MOBI builder for dictionaries and books", version)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Build MOBI file from OPF or EPUB
    #[command(version)]
    Build {
        /// Input OPF or EPUB file
        input: PathBuf,

        /// Output MOBI file
        #[arg(short, long)]
        output: Option<PathBuf>,

        /// Skip PalmDOC compression (faster builds, larger files)
        #[arg(long)]
        no_compress: bool,

        /// Only index headwords (no inflected forms in orth index)
        #[arg(long)]
        headwords_only: bool,

        /// Skip embedding the EPUB source in the MOBI (saves space, breaks Kindle Previewer)
        #[arg(long)]
        no_embed_source: bool,

        /// Include a CMET (compilation metadata) record
        #[arg(long)]
        include_cmet: bool,

        /// Disable HD image container (CONT/CRES) for book MOBIs
        #[arg(long)]
        no_hd_images: bool,

        /// Identify as kindling in EXTH metadata instead of kindlegen
        #[arg(long)]
        creator_tag: bool,

        /// Build legacy dual-format MOBI7+KF8 (.mobi) instead of the modern
        /// KF8-only (.azw3) default. Only useful for pre-2012 Kindles; modern
        /// devices prefer KF8-only output. Dictionaries always use legacy
        /// MOBI7 because Kindle's lookup popup requires the MOBI7 INDX format,
        /// so this flag is a no-op on dictionary builds.
        #[arg(long)]
        legacy_mobi: bool,

        /// Deprecated no-op. KF8-only (.azw3) is now the default for
        /// non-dictionary builds; pass `--legacy-mobi` to opt back into the
        /// old dual MOBI7+KF8 behavior. Kept so existing scripts that pass
        /// `--kf8-only` keep working.
        #[arg(long, hide = true)]
        kf8_only: bool,

        /// Enforce Kindle publishing limits: split HTML chunks >30MB at entry/paragraph
        /// boundaries, warn if >300 HTML files. ON by default for dictionaries, OFF for books.
        /// Use --no-kindle-limits to disable for dictionaries, --kindle-limits to enable for books.
        #[arg(long, overrides_with = "no_kindle_limits")]
        kindle_limits: bool,

        /// Disable Kindle publishing limits enforcement (see --kindle-limits)
        #[arg(long, overrides_with = "kindle_limits")]
        no_kindle_limits: bool,

        /// Skip the automatic Kindle Publishing Guidelines pre-flight check.
        /// Validation runs by default before every build; use this flag to
        /// bypass it (e.g. when a known-benign finding would otherwise abort).
        #[arg(long)]
        no_validate: bool,

        /// Skip the build-time HTML self-check on the MOBI text blob.
        /// The self-check runs by default and prints a warning (without
        /// aborting the build) if it detects malformed HTML, unbalanced
        /// tags, or `<hr/` corruption. Overhead is typically 50-200 ms.
        #[arg(long)]
        no_self_check: bool,

        /// Strict accent matching for dictionary lookups. By default
        /// kindling embeds the kindlegen-derived ORDT/SPL collation tables
        /// in the orth INDX, which makes Kindle fold diacritics at lookup
        /// time ("meme" matches "même"). With this flag, the collation
        /// blob is omitted: Kindle falls back to raw UTF-16BE ordering,
        /// so exact-accent headwords always beat unaccented homographs
        /// when both exist in the dictionary. No effect on book builds.
        #[arg(long)]
        strict_accents: bool,
    },

    /// Convert comic images/CBZ/CBR/EPUB to Kindle-optimized MOBI
    #[command(version)]
    Comic {
        /// Input image folder, CBZ file, CBR file, or EPUB file
        input: PathBuf,

        /// Output MOBI file
        #[arg(short, long)]
        output: Option<PathBuf>,

        /// Target Kindle device profile
        #[arg(short, long, default_value = "paperwhite")]
        device: String,

        /// Right-to-left reading mode (manga). Reverses page order and split order.
        #[arg(long)]
        rtl: bool,

        /// Disable double-page spread detection and splitting
        #[arg(long)]
        no_split: bool,

        /// Rotate double-page spreads 90 degrees clockwise instead of splitting.
        /// Gives a full-page spread view, useful for tablet users.
        #[arg(long)]
        rotate_spreads: bool,

        /// Crop mode: 0=disabled, 1=margins only, 2=margins+page numbers (default)
        #[arg(long, default_value = "2", value_parser = clap::value_parser!(u8).range(0..=2))]
        crop: u8,

        /// Deprecated: equivalent to --crop 0
        #[arg(long, hide = true)]
        no_crop: bool,

        /// Disable auto-contrast and gamma correction
        #[arg(long)]
        no_enhance: bool,

        /// Force webtoon mode (vertical strip merge + gutter-aware split)
        #[arg(long)]
        webtoon: bool,

        /// Disable Kindle Panel View (tap-to-zoom panels). Panel View is ON by default.
        #[arg(long)]
        no_panel_view: bool,

        /// Panel View reading order: controls which panel is shown first when
        /// tapping to zoom. Options: horizontal-lr (left-to-right, default for
        /// Western comics), horizontal-rl (right-to-left, default for manga
        /// when --rtl is set), vertical-lr (top-to-bottom then left-to-right,
        /// for 4-koma), vertical-rl (top-to-bottom then right-to-left).
        /// If omitted, auto-detected from --rtl flag.
        #[arg(long)]
        panel_reading_order: Option<String>,

        /// JPEG encoding quality (1-100). Lower values produce smaller files.
        /// Some Kindle devices may show blank pages with very high quality JPEGs,
        /// so 70-80 can be a workaround.
        #[arg(long, default_value = "85", value_parser = clap::value_parser!(u8).range(1..=100))]
        jpeg_quality: u8,

        /// Maximum pixel height for merged webtoon strips. If the merged strip
        /// exceeds this, it is split into chunks processed independently.
        /// Prevents OOM on large webtoon directories.
        #[arg(long, default_value = "65536")]
        max_height: u32,

        /// Embed the intermediate EPUB source as a SRCS record inside the
        /// MOBI. Off by default for comics: the SRCS record contains a zipped
        /// copy of every image in the comic, which for a large book produces
        /// a single PalmDB record well over 100 MB. That oversize record is
        /// what triggered the "Unable to Open Item" failure on Vader Down,
        /// even though the Kindle indexer accepted the file. Only enable
        /// this when you need Kindle Previewer to round-trip to EPUB.
        #[arg(long)]
        embed_source: bool,

        /// Deprecated no-op. Comics no longer embed the EPUB source by
        /// default; pass `--embed-source` to opt back in. Kept so existing
        /// scripts that pass `--no-embed-source` keep working.
        #[arg(long, hide = true)]
        no_embed_source: bool,

        /// Document type: "ebok" (Books shelf) or "pdoc" (Documents shelf, default).
        /// WARNING: Amazon may auto-delete sideloaded EBOK files when Kindle connects to WiFi.
        #[arg(long, default_value = "pdoc")]
        doc_type: String,

        /// Override the title from ComicInfo.xml
        #[arg(long)]
        title: Option<String>,

        /// Override the author from ComicInfo.xml
        #[arg(long)]
        author: Option<String>,

        /// Language code for OPF metadata (e.g. "ja", "en", "ko").
        /// Important for CJK content where language affects font selection on Kindle.
        #[arg(long)]
        language: Option<String>,

        /// Cover image: a page number (1-based) or a file path.
        /// When provided, use that image as the cover instead of the first page.
        #[arg(long)]
        cover: Option<String>,

        /// Center-crop the cover image to fill the device screen exactly.
        /// Removes letterbox borders by cropping to the device's aspect ratio.
        #[arg(long)]
        cover_fill: bool,

        /// Build legacy dual-format MOBI7+KF8 (.mobi) instead of the modern
        /// KF8-only (.azw3) default. Only useful for pre-2012 Kindles; modern
        /// devices prefer KF8-only output.
        #[arg(long)]
        legacy_mobi: bool,

        /// Deprecated no-op. KF8-only (.azw3) is now the default for comics;
        /// pass `--legacy-mobi` to opt back into the old dual MOBI7+KF8
        /// behavior. Kept so existing scripts that pass `--kf8-only` keep
        /// working.
        #[arg(long, hide = true)]
        kf8_only: bool,

        /// Enforce Kindle publishing limits: warn if >300 HTML files.
        /// OFF by default for comics. Use --kindle-limits to enable.
        #[arg(long, overrides_with = "no_kindle_limits")]
        kindle_limits: bool,

        /// Disable Kindle publishing limits enforcement (see --kindle-limits)
        #[arg(long, overrides_with = "kindle_limits")]
        no_kindle_limits: bool,

        /// Skip the build-time HTML self-check on the comic's MOBI text
        /// blob. See `kindling build --help` for details.
        #[arg(long)]
        no_self_check: bool,

        /// Emit KF8 comic output that matches kindlegen's byte-level
        /// shape. Off by default (kindling emits its normal pretty-
        /// printed "better than kindlegen" form). Turn this on for
        /// parity tests or when producing reference builds — byte
        /// differences between kindling and kindlegen shrink to the
        /// unavoidable ones (compression seeds, timestamps, UIDs).
        #[arg(long)]
        kindlegen_parity: bool,
    },

    /// Validate an OPF manuscript against the Amazon Kindle Publishing Guidelines (2026.1).
    ///
    /// Runs a set of pre-flight checks (cover image, NCX, HTML/CSS hygiene,
    /// image formats/sizes, table size, unsupported tags) and prints one line
    /// per finding with severity, KPG rule id, section, PDF page, and message.
    /// Exits 0 if there are no errors, 1 otherwise. With `--strict`, exits 1
    /// on any warning too.
    #[command(version)]
    Validate {
        /// Input OPF file
        input: PathBuf,

        /// Treat warnings as errors (exit non-zero on any warning).
        #[arg(long)]
        strict: bool,
    },

    /// Repair an EPUB file for cleaner Kindle ingest.
    ///
    /// Applies the fix list from innocenat/kindle-epub-fix (public domain):
    /// prepend missing XML declarations, rewrite body-id hyperlinks that
    /// Kindle drops, inject a fallback dc:language, and delete stray <img>
    /// tags with no src. Byte-stable on clean input, idempotent on broken
    /// input, rejects DRM-protected files.
    #[command(version)]
    Repair {
        /// Input EPUB file
        input: PathBuf,

        /// Output EPUB file. Defaults to `<stem>-fixed.epub` next to the input.
        #[arg(short, long)]
        output: Option<PathBuf>,

        /// Emit the full RepairReport as JSON on stdout.
        #[arg(long)]
        report_json: bool,

        /// Detect fixes without writing an output file.
        #[arg(long)]
        dry_run: bool,
    },

    /// Rewrite MOBI/AZW3 metadata in place without rebuilding from source.
    ///
    /// Takes an existing MOBI or AZW3 file and replaces the EXTH metadata
    /// records (title, authors, publisher, description, language, ISBN,
    /// ASIN, publication date, tags, series, cover image) according to
    /// the provided flags. Book content records (text, non-cover images,
    /// indices) are never touched. Byte-stable on no-op, idempotent, and
    /// refuses DRM-encrypted files.
    #[command(version)]
    RewriteMetadata {
        /// Input MOBI or AZW3 file.
        input: PathBuf,

        /// Output MOBI/AZW3 file. Defaults to `<stem>-meta.<ext>` next to
        /// the input.
        #[arg(short, long)]
        output: Option<PathBuf>,

        /// Emit the full RewriteReport as JSON on stdout.
        #[arg(long)]
        report_json: bool,

        /// Scan and plan changes without writing an output file.
        #[arg(long)]
        dry_run: bool,

        /// New title (EXTH 503 plus full_name).
        #[arg(long)]
        title: Option<String>,

        /// New author. Pass multiple times for multiple creators.
        #[arg(long = "author", action = clap::ArgAction::Append)]
        authors: Vec<String>,

        /// New publisher (EXTH 101).
        #[arg(long)]
        publisher: Option<String>,

        /// New description (EXTH 103).
        #[arg(long)]
        description: Option<String>,

        /// New primary writing language, BCP47 code (EXTH 524).
        #[arg(long)]
        language: Option<String>,

        /// New ISBN (EXTH 104).
        #[arg(long)]
        isbn: Option<String>,

        /// New ASIN (EXTH 504).
        #[arg(long)]
        asin: Option<String>,

        /// New publication date (EXTH 106).
        #[arg(long = "publication-date")]
        publication_date: Option<String>,

        /// New subject/tag. Pass multiple times for multiple tags.
        #[arg(long = "subject", action = clap::ArgAction::Append)]
        subjects: Vec<String>,

        /// New series name (EXTH 112).
        #[arg(long)]
        series: Option<String>,

        /// New series index (EXTH 113).
        #[arg(long = "series-index")]
        series_index: Option<String>,

        /// Path to a new cover image (JPEG, PNG, or GIF). Replaces the
        /// existing cover image record in place. The input file must
        /// already have a cover.
        #[arg(long)]
        cover: Option<PathBuf>,
    },

    /// Dump the structural contents of a MOBI/AZW3 file to stdout.
    ///
    /// Emits one line per parsed field in `section.field = value` form so
    /// `diff -u` between two dumps surfaces semantic differences (EXTH
    /// records, MOBI header fields, INDX / ORDT2 tables, entry labels)
    /// without drowning in absolute-offset cascades. Text and image
    /// record contents are summarized (length + magic only) to keep the
    /// diff focused on structure.
    #[command(version)]
    Dump {
        /// Input MOBI or AZW3 file.
        input: PathBuf,
    },
}

/// Check if the first argument looks like a file path (kindlegen compat mode)
/// rather than a subcommand like "build".
fn is_kindlegen_compat_mode() -> bool {
    let args: Vec<String> = std::env::args().collect();
    if args.len() < 2 {
        return false;
    }
    let first_arg = &args[1];
    // If first arg ends with .opf or .epub, treat as kindlegen compat mode
    let lower = first_arg.to_lowercase();
    lower.ends_with(".opf") || lower.ends_with(".epub")
}

/// Parse kindlegen-compatible arguments.
/// Accepts: kindling <input_file> [-o <filename>] [-dont_append_source] [-locale <value>]
///          [-c0] [-c1] [-c2] [-verbose] [-no_validate | --no-validate]
///          [-no_self_check | --no-self-check]
/// Returns (input, output_override, no_validate, no_self_check)
fn parse_kindlegen_args() -> (PathBuf, Option<String>, bool, bool) {
    let args: Vec<String> = std::env::args().collect();
    let input = PathBuf::from(&args[1]);
    let mut output_name: Option<String> = None;
    let mut no_validate = false;
    let mut no_self_check = false;
    let mut i = 2;
    while i < args.len() {
        match args[i].as_str() {
            "-o" => {
                if i + 1 < args.len() {
                    output_name = Some(args[i + 1].clone());
                    i += 2;
                } else {
                    i += 1;
                }
            }
            "-locale" => {
                // Silently ignore -locale <value>
                i += 2;
            }
            "-dont_append_source" | "-c0" | "-c1" | "-c2" | "-verbose" => {
                // Silently ignore these flags
                i += 1;
            }
            "-no_validate" | "--no-validate" => {
                no_validate = true;
                i += 1;
            }
            "--no-self-check" | "-no_self_check" => {
                no_self_check = true;
                i += 1;
            }
            _ => {
                // Unknown flag, skip
                i += 1;
            }
        }
    }
    (input, output_name, no_validate, no_self_check)
}

/// Resolve the output path for a build.
///
/// If an explicit output is given, use it verbatim (the user's extension
/// choice always wins, even if it does not match the actual container
/// format). If no output is specified, derive a default filename by
/// replacing the input extension with `.azw3` for KF8-only builds or
/// `.mobi` for dual-format (legacy MOBI7+KF8 and dictionary) builds.
fn resolve_output_path(input: &PathBuf, output: Option<PathBuf>, kf8_only: bool) -> PathBuf {
    match output {
        Some(p) => p,
        None => {
            let ext = if kf8_only { "azw3" } else { "mobi" };
            input.with_extension(ext)
        }
    }
}

/// Best-effort detection of whether the build input describes a dictionary.
///
/// For `.opf` inputs this parses the OPF directly and consults
/// `OPFData::is_dictionary()`. For `.epub` inputs this extracts the archive
/// into a temporary directory, parses the inner OPF, and cleans up. Any
/// error (parse failure, missing metadata) is treated as "not a dictionary",
/// which is the safer default since the worst case is a non-dictionary book
/// accidentally built as dual-format MOBI7+KF8 instead of KF8-only.
fn detect_is_dictionary(input: &std::path::Path) -> bool {
    let is_epub = input
        .extension()
        .map(|ext| ext.eq_ignore_ascii_case("epub"))
        .unwrap_or(false);

    if is_epub {
        match epub::extract_epub(input) {
            Ok((temp_dir, opf_path)) => {
                let result = opf::OPFData::parse(&opf_path)
                    .map(|data| data.is_dictionary())
                    .unwrap_or(false);
                epub::cleanup_temp_dir(&temp_dir);
                result
            }
            Err(_) => false,
        }
    } else {
        opf::OPFData::parse(input)
            .map(|data| data.is_dictionary())
            .unwrap_or(false)
    }
}

#[allow(clippy::too_many_arguments)]
fn do_build(
    input: &PathBuf,
    output_path: &PathBuf,
    no_compress: bool,
    headwords_only: bool,
    embed_source: bool,
    include_cmet: bool,
    no_hd_images: bool,
    creator_tag: bool,
    kf8_only: bool,
    kindle_limits: bool,
    no_validate: bool,
    self_check: bool,
    strict_accents: bool,
) {
    let is_epub = input
        .extension()
        .map(|ext| ext.eq_ignore_ascii_case("epub"))
        .unwrap_or(false);

    // Read the EPUB bytes for SRCS embedding if requested and input is EPUB
    let srcs_data = if embed_source && is_epub {
        match std::fs::read(input) {
            Ok(data) => {
                eprintln!("SRCS: embedding {} bytes of EPUB source", data.len());
                Some(data)
            }
            Err(e) => {
                eprintln!("Warning: could not read EPUB for SRCS embedding: {}", e);
                None
            }
        }
    } else {
        if embed_source && !is_epub {
            eprintln!("Note: EPUB source embedding skipped for non-EPUB input");
        }
        None
    };

    // Capture the OPF so we can round-trip its title/author through the
    // post-build MOBI readback check.
    let mut opf_snapshot: Option<(String, String, bool)> = None;

    // Resolve the EPUB-or-OPF input down to an OPF path, unzipping the EPUB
    // into a temp directory if needed. The temp directory (when present) is
    // returned alongside the path so we can clean it up on every exit branch.
    let (temp_dir, opf_path): (Option<std::path::PathBuf>, std::path::PathBuf) = if is_epub {
        match epub::extract_epub(input) {
            Ok((dir, path)) => (Some(dir), path),
            Err(e) => {
                eprintln!("Error extracting EPUB: {}", e);
                println!("Error(prcgen):E24000: Could not process input file");
                process::exit(1);
            }
        }
    } else {
        (None, input.clone())
    };

    // Build the ExtractedEpub once; pre-flight validation, build_mobi, and
    // the post-build readback metadata snapshot all share this instance so
    // we parse the OPF a single time per `kindling build` invocation. If the
    // OPF cannot be parsed we still hand the path to run_preflight_validation
    // so the existing "couldn't parse" warning + soft-pass behavior is
    // preserved verbatim, then let the builder surface its own parse error.
    let extracted: Option<ExtractedEpub> = match ExtractedEpub::from_opf_path(&opf_path) {
        Ok(e) => Some(e),
        Err(_) => None,
    };

    // Pre-flight KDP validation. Use the shared ExtractedEpub when we have
    // one; otherwise fall back to the path-based entry point so the warning
    // message matches the original wording exactly.
    let preflight = match extracted.as_ref() {
        Some(e) => kindling::run_preflight_validation_on_extracted(e, no_validate),
        None => kindling::run_preflight_validation(&opf_path, no_validate),
    };
    if let Err(errors) = preflight {
        if let Some(ref dir) = temp_dir {
            epub::cleanup_temp_dir(dir);
        }
        eprintln!(
            "Build aborted: {} validation errors. Run with --no-validate to skip.",
            errors
        );
        println!("Error(prcgen):E24000: Could not build Mobi file");
        process::exit(1);
    }

    // Snapshot OPF metadata for the post-build readback check. Reuse the
    // already-parsed ExtractedEpub when available; otherwise re-parse so
    // that the OPF's title/author still round-trip when validation was
    // skipped or the OPF parser failed earlier (the builder will then
    // produce the canonical error itself).
    if let Some(ref e) = extracted {
        opf_snapshot = Some((
            e.opf.title.clone(),
            e.opf.author.clone(),
            e.opf.is_dictionary(),
        ));
    } else if let Ok(parsed) = opf::OPFData::parse(&opf_path) {
        opf_snapshot = Some((parsed.title.clone(), parsed.author.clone(), parsed.is_dictionary()));
    }

    let result = match extracted.as_ref() {
        Some(e) => mobi::build_mobi_from_extracted(
            e, output_path, no_compress, headwords_only,
            srcs_data.as_deref(), include_cmet, no_hd_images, creator_tag, kf8_only, None, kindle_limits, self_check,
            false, // kindlegen_parity: only meaningful for the comic path
            strict_accents,
        ),
        None => mobi::build_mobi(
            &opf_path, output_path, no_compress, headwords_only,
            srcs_data.as_deref(), include_cmet, no_hd_images, creator_tag, kf8_only, None, kindle_limits, self_check,
            false, // kindlegen_parity: only meaningful for the comic path
            strict_accents,
        ),
    };

    if let Some(ref dir) = temp_dir {
        epub::cleanup_temp_dir(dir);
    }

    match result {
        Ok(()) => {
            // Post-build MOBI readback check. This is the only thing between
            // a broken library entry and a happy user, so don't skip it by
            // default.
            let (title, author, is_dictionary) = opf_snapshot
                .as_ref()
                .map(|(t, a, d)| (t.as_str(), a.as_str(), *d))
                .unwrap_or(("", "", false));
            let expected = mobi_check::ExpectedMetadata {
                title: if title.is_empty() { None } else { Some(title) },
                author: if author.is_empty() { None } else { Some(author) },
                is_comic: false,
                is_dictionary,
            };
            match mobi_check::check_mobi_file(output_path, &expected) {
                Ok(report) => {
                    if let Err(e) = mobi_check::report_result(output_path, &report) {
                        eprintln!("Error: {}", e);
                        println!("Error(prcgen):E24000: Could not build Mobi file");
                        process::exit(1);
                    }
                }
                Err(e) => {
                    eprintln!("Warning: MOBI post-build check could not run: {}", e);
                }
            }
            println!("Info(prcgen):I1036: Mobi file built successfully");
        }
        Err(e) => {
            eprintln!("Error: {}", e);
            // Check if this looks like a file-too-big error
            let err_str = format!("{}", e);
            if err_str.contains("too big") || err_str.contains("too large") {
                println!("Error(prcgen):E23026: File too big");
            } else {
                println!("Error(prcgen):E24000: Could not build Mobi file");
            }
            process::exit(1);
        }
    }
}

fn main() {
    if is_kindlegen_compat_mode() {
        // Kindlegen-compatible invocation: kindling <file> [-o name] [flags...]
        let (input, output_name, no_validate, no_self_check) = parse_kindlegen_args();

        // In kindlegen compat mode, -o specifies just a filename next to the input
        let output_path = if let Some(name) = output_name {
            let parent = input.parent().unwrap_or(std::path::Path::new("."));
            parent.join(name)
        } else {
            input.with_extension("mobi")
        };

        do_build(&input, &output_path, false, false, true, false, false, false, false, true, no_validate, !no_self_check, false);
    } else {
        let cli = Cli::parse();

        match cli.command {
            Commands::Build {
                input,
                output,
                no_compress,
                headwords_only,
                no_embed_source,
                include_cmet,
                no_hd_images,
                creator_tag,
                legacy_mobi,
                kf8_only,
                kindle_limits,
                no_kindle_limits,
                no_validate,
                no_self_check,
                strict_accents,
            } => {
                // Default: ON for dictionaries, OFF for books.
                // Since we don't know the content type yet at parse time, we pass
                // a tri-state: if user explicitly set a flag, use that; otherwise
                // let build_mobi decide based on content type detection.
                // We use a simple heuristic: if neither flag is set, pass true
                // (the dictionary default, which is more conservative). The book
                // path will only warn, not restructure, so it's safe.
                let effective_kindle_limits = if no_kindle_limits {
                    false
                } else if kindle_limits {
                    true
                } else {
                    // Default: true (dictionary default, harmless for books since
                    // books only warn, not split)
                    true
                };

                // Deprecated: `--kf8-only` is now the default for non-dict
                // builds and a no-op for dicts. Note this loudly so users
                // update their scripts, but do not fail.
                if kf8_only {
                    eprintln!(
                        "Note: --kf8-only is now the default for non-dictionary \
                         builds and has no effect. Dictionaries still build as \
                         dual-format MOBI7+KF8 because Kindle's lookup popup \
                         requires the MOBI7 INDX format."
                    );
                }

                // Detect dictionary vs non-dictionary up front so we can flip
                // the kf8_only default correctly and pick the right output
                // extension when the user has not passed -o.
                let is_dictionary = detect_is_dictionary(&input);

                // Dictionaries always build as dual-format MOBI7+KF8.
                // Non-dictionaries default to KF8-only (.azw3); the user can
                // opt back into legacy dual-format via --legacy-mobi.
                let effective_kf8_only = if is_dictionary {
                    if legacy_mobi {
                        eprintln!(
                            "Note: --legacy-mobi is implicit for dictionary \
                             builds (dictionaries always use MOBI7 INDX)."
                        );
                    }
                    false
                } else if legacy_mobi {
                    eprintln!(
                        "Building dual-format MOBI7+KF8 (.mobi). Modern Kindles \
                         prefer KF8-only .azw3. Drop --legacy-mobi to use the \
                         modern default."
                    );
                    false
                } else {
                    true
                };

                let output_path = resolve_output_path(&input, output, effective_kf8_only);
                do_build(&input, &output_path, no_compress, headwords_only, !no_embed_source, include_cmet, no_hd_images, creator_tag, effective_kf8_only, effective_kindle_limits, no_validate, !no_self_check, strict_accents);
            }
            Commands::Comic {
                input,
                output,
                device,
                rtl,
                no_split,
                rotate_spreads,
                crop,
                no_crop,
                no_enhance,
                webtoon,
                no_panel_view,
                jpeg_quality,
                max_height,
                embed_source,
                no_embed_source,
                doc_type,
                title,
                author,
                language,
                cover,
                cover_fill,
                panel_reading_order,
                legacy_mobi,
                kf8_only,
                kindle_limits,
                no_kindle_limits,
                no_self_check,
                kindlegen_parity,
            } => {
                let profile = match comic::get_profile(&device) {
                    Some(p) => p,
                    None => {
                        eprintln!("Error: unknown device '{}'. Valid devices: {}", device, comic::valid_device_names());
                        process::exit(1);
                    }
                };

                // Deprecated: `--kf8-only` is now the default for comic
                // builds. Keep the flag as a hidden no-op so existing scripts
                // keep working, but nudge users toward dropping it.
                if kf8_only {
                    eprintln!(
                        "Note: --kf8-only is now the default for comic builds \
                         and has no effect. Pass --legacy-mobi for the old \
                         dual MOBI7+KF8 behavior."
                    );
                }

                // Comics default to KF8-only (.azw3). --legacy-mobi opts back
                // into dual-format MOBI7+KF8 for pre-2012 Kindles.
                let effective_kf8_only = if legacy_mobi {
                    eprintln!(
                        "Building dual-format MOBI7+KF8 (.mobi). Modern Kindles \
                         prefer KF8-only .azw3. Drop --legacy-mobi to use the \
                         modern default."
                    );
                    false
                } else {
                    true
                };

                let output_path = match output {
                    Some(p) => p,
                    None => {
                        let ext = if effective_kf8_only { "azw3" } else { "mobi" };
                        input.with_extension(ext)
                    }
                };

                // Parse doc_type flag
                let doc_type_value = match doc_type.to_lowercase().as_str() {
                    "ebok" => Some("EBOK".to_string()),
                    "pdoc" => None, // None means default PDOC
                    other => {
                        eprintln!("Warning: unknown --doc-type '{}', using default 'pdoc'", other);
                        None
                    }
                };

                // Parse cover flag: either a page number or a file path
                let cover_source = cover.map(|c| {
                    if let Ok(page_num) = c.parse::<usize>() {
                        if page_num >= 1 {
                            comic::CoverSource::PageNumber(page_num)
                        } else {
                            eprintln!("Warning: cover page number must be >= 1, treating as file path");
                            comic::CoverSource::FilePath(PathBuf::from(c))
                        }
                    } else {
                        comic::CoverSource::FilePath(PathBuf::from(c))
                    }
                });

                // Comic defaults to OFF for kindle_limits
                let effective_kindle_limits = kindle_limits && !no_kindle_limits;

                // `--no-embed-source` is accepted for backward compatibility
                // but has been a no-op since comics stopped defaulting to
                // embed-source in v0.7.7. Only `--embed-source` turns it on.
                if no_embed_source {
                    eprintln!("Note: --no-embed-source is now the default for comics and has no effect");
                }
                let effective_embed_source = embed_source;

                // --no-crop is a deprecated alias for --crop 0
                let effective_crop = if no_crop { 0 } else { crop };

                let options = comic::ComicOptions {
                    rtl,
                    split: !no_split,
                    crop: effective_crop,
                    enhance: !no_enhance,
                    webtoon,
                    panel_view: !no_panel_view,
                    jpeg_quality,
                    max_height,
                    embed_source: effective_embed_source,
                    doc_type: doc_type_value,
                    title_override: title,
                    author_override: author,
                    language,
                    cover: cover_source,
                    rotate_spreads,
                    panel_reading_order,
                    cover_fill,
                    kindle_limits: effective_kindle_limits,
                    kf8_only: effective_kf8_only,
                    self_check: !no_self_check,
                    kindlegen_parity,
                };

                match comic::build_comic_with_options(&input, &output_path, &profile, &options) {
                    Ok(()) => {
                        let format_name = if effective_kf8_only { "AZW3" } else { "MOBI" };
                        eprintln!("Comic {} built successfully: {}", format_name, output_path.display());
                    }
                    Err(e) => {
                        eprintln!("Error: {}", e);
                        process::exit(1);
                    }
                }
            }
            Commands::Validate { input, strict } => {
                do_validate(&input, strict);
            }
            Commands::Repair {
                input,
                output,
                report_json,
                dry_run,
            } => {
                do_repair(&input, output.as_ref(), report_json, dry_run);
            }
            Commands::RewriteMetadata {
                input,
                output,
                report_json,
                dry_run,
                title,
                authors,
                publisher,
                description,
                language,
                isbn,
                asin,
                publication_date,
                subjects,
                series,
                series_index,
                cover,
            } => {
                do_rewrite_metadata(
                    &input,
                    output.as_ref(),
                    report_json,
                    dry_run,
                    title,
                    authors,
                    publisher,
                    description,
                    language,
                    isbn,
                    asin,
                    publication_date,
                    subjects,
                    series,
                    series_index,
                    cover.as_ref(),
                );
            }
            Commands::Dump { input } => {
                do_dump(&input);
            }
        }
    }
}

/// Parse a MOBI/AZW3 file and print a structural dump to stdout.
fn do_dump(path: &PathBuf) {
    match mobi_dump::dump_mobi(path) {
        Ok(s) => {
            print!("{}", s);
        }
        Err(e) => {
            eprintln!("Error: could not dump {}: {}", path.display(), e);
            process::exit(1);
        }
    }
}

/// Run the validator on an OPF and print the report.
///
/// Exits 0 if there are no errors (and no warnings when `strict` is set),
/// 1 otherwise.
fn do_validate(opf_path: &PathBuf, strict: bool) {
    println!(
        "Validating {} against Kindle Publishing Guidelines v{}",
        opf_path.display(),
        kdp_rules::KPG_VERSION
    );

    let epub = match kindling::extracted::ExtractedEpub::from_opf_path(opf_path) {
        Ok(e) => e,
        Err(e) => {
            eprintln!("Error: could not parse OPF {}: {}", opf_path.display(), e);
            process::exit(2);
        }
    };
    let report = validate::validate(&epub);

    for finding in &report.findings {
        println!("{}", finding);
    }

    let errors = report.error_count();
    let warnings = report.warning_count();
    let infos = report.info_count();
    println!("{} errors, {} warnings, {} info", errors, warnings, infos);

    let fail = errors > 0 || (strict && warnings > 0);
    if fail {
        process::exit(1);
    }
}

/// Run the EPUB repair pass and print the result.
///
/// `input` is an EPUB file to repair. When `output` is `None`, the repaired
/// copy is written to `<stem>-fixed.epub` in the same directory.
///
/// With `report_json`, the full [`repair::RepairReport`] is emitted as JSON
/// on stdout. Otherwise a human-readable summary is written to stderr with
/// one line per fix and a final count.
///
/// With `dry_run`, the input is only scanned: no output file is written,
/// and the summary is prefixed with "(dry-run)".
///
/// Exit codes:
///   * 0 on success, even if fixes were applied. Callers wanting to know
///     whether the file was already clean should check the report or JSON.
///   * 1 on any `RepairError` (including DRM rejection and non-EPUB input).
fn do_repair(input: &PathBuf, output: Option<&PathBuf>, report_json: bool, dry_run: bool) {
    let default_output;
    let output_path: PathBuf = if dry_run {
        input.clone()
    } else if let Some(p) = output {
        p.clone()
    } else {
        let stem = input
            .file_stem()
            .map(|s| s.to_string_lossy().into_owned())
            .unwrap_or_else(|| "repaired".to_string());
        let parent = input.parent().unwrap_or(std::path::Path::new("."));
        default_output = parent.join(format!("{}-fixed.epub", stem));
        default_output
    };

    let result = if dry_run {
        repair::scan_epub(input)
    } else {
        repair::repair_epub(input, &output_path)
    };

    let report = match result {
        Ok(r) => r,
        Err(e) => {
            eprintln!("Error: {}", e);
            process::exit(1);
        }
    };

    if report_json {
        println!("{}", report.to_json());
    } else {
        let prefix = if dry_run { "(dry-run) " } else { "" };
        for fix in &report.fixes_applied {
            eprintln!("{}{}", prefix, fix.describe());
        }
        for warn in &report.warnings {
            eprintln!("{}warning: {}: {}", prefix, warn.file, warn.message);
        }
        if report.any_fixes() {
            eprintln!(
                "{}Repaired {} issue{} in {}",
                prefix,
                report.fix_count(),
                if report.fix_count() == 1 { "" } else { "s" },
                input.display()
            );
        } else {
            eprintln!("{}No repairs needed for {}", prefix, input.display());
        }
        if !dry_run {
            eprintln!("Output written to {}", output_path.display());
        }
    }
}

/// Run the in-place MOBI/AZW3 metadata rewrite and print the result.
///
/// `input` is a MOBI or AZW3 file whose EXTH metadata records (and
/// optionally the cover image record) are updated according to the
/// provided flags. When `output` is `None`, the rewritten copy is written
/// to `<stem>-meta.<ext>` in the same directory.
///
/// With `report_json`, the full [`mobi_rewrite::RewriteReport`] is emitted
/// as JSON on stdout. Otherwise a human-readable summary is written to
/// stderr listing each EXTH change and a final count.
///
/// With `dry_run`, the input is only scanned: no output file is written
/// and the summary is prefixed with "(dry-run)". Changes still reported.
///
/// Exit codes:
///   * 0 on success, even if no fields were actually changed. Callers
///     wanting to know whether the file was already current should check
///     `no_op` in the report or JSON.
///   * 1 on any `RewriteError` (including DRM rejection, non-MOBI input,
///     or a cover update on a file with no existing cover record).
#[allow(clippy::too_many_arguments)]
fn do_rewrite_metadata(
    input: &PathBuf,
    output: Option<&PathBuf>,
    report_json: bool,
    dry_run: bool,
    title: Option<String>,
    authors: Vec<String>,
    publisher: Option<String>,
    description: Option<String>,
    language: Option<String>,
    isbn: Option<String>,
    asin: Option<String>,
    publication_date: Option<String>,
    subjects: Vec<String>,
    series: Option<String>,
    series_index: Option<String>,
    cover: Option<&PathBuf>,
) {
    let default_output;
    let output_path: PathBuf = if dry_run {
        input.clone()
    } else if let Some(p) = output {
        p.clone()
    } else {
        let stem = input
            .file_stem()
            .map(|s| s.to_string_lossy().into_owned())
            .unwrap_or_else(|| "rewritten".to_string());
        let ext = input
            .extension()
            .map(|s| s.to_string_lossy().into_owned())
            .unwrap_or_else(|| "mobi".to_string());
        let parent = input.parent().unwrap_or(std::path::Path::new("."));
        default_output = parent.join(format!("{}-meta.{}", stem, ext));
        default_output
    };

    // Load cover image bytes up front so any I/O error is reported before
    // we touch the MOBI file.
    let cover_bytes = match cover {
        Some(p) => match std::fs::read(p) {
            Ok(b) => Some(b),
            Err(e) => {
                eprintln!("Error: could not read cover image {}: {}", p.display(), e);
                process::exit(1);
            }
        },
        None => None,
    };

    let updates = mobi_rewrite::MetadataUpdates {
        title,
        authors: if authors.is_empty() { None } else { Some(authors) },
        publisher,
        description,
        language,
        isbn,
        asin,
        publication_date,
        subjects: if subjects.is_empty() {
            None
        } else {
            Some(subjects)
        },
        series,
        series_index,
        cover_image: cover_bytes,
    };

    // Dry run: plan changes against a scratch output then discard. We do
    // this by writing into a temp file and removing it; the rewrite
    // function's byte-stable no-op path handles the case where nothing
    // actually changed.
    let report_result = if dry_run {
        let scratch = std::env::temp_dir().join(format!(
            "kindling_rewrite_metadata_dryrun_{}.bin",
            std::process::id()
        ));
        let r = mobi_rewrite::rewrite_mobi_metadata(input, &scratch, &updates);
        let _ = std::fs::remove_file(&scratch);
        r
    } else {
        mobi_rewrite::rewrite_mobi_metadata(input, &output_path, &updates)
    };

    let report = match report_result {
        Ok(r) => r,
        Err(e) => {
            eprintln!("Error: {}", e);
            process::exit(1);
        }
    };

    if report_json {
        println!("{}", rewrite_report_to_json(&report));
    } else {
        let prefix = if dry_run { "(dry-run) " } else { "" };
        if report.no_op {
            eprintln!("{}No metadata changes needed for {}", prefix, input.display());
        } else {
            for change in &report.changes {
                eprintln!("{}{}", prefix, describe_exth_change(change));
            }
            if report.cover_updated {
                eprintln!("{}Replaced cover image record", prefix);
            }
            let n = report.changes.len() + if report.cover_updated { 1 } else { 0 };
            eprintln!(
                "{}Rewrote {} metadata field{} in {}",
                prefix,
                n,
                if n == 1 { "" } else { "s" },
                input.display()
            );
        }
        if !dry_run {
            eprintln!("Output written to {}", output_path.display());
        }
    }
}

fn describe_exth_change(change: &mobi_rewrite::ExthChange) -> String {
    match change {
        mobi_rewrite::ExthChange::Added { exth_type, value } => {
            format!("added EXTH {} ({})", exth_type, preview_bytes(value))
        }
        mobi_rewrite::ExthChange::Replaced {
            exth_type,
            old_value,
            new_value,
        } => format!(
            "replaced EXTH {} ({} -> {})",
            exth_type,
            preview_bytes(old_value),
            preview_bytes(new_value)
        ),
        mobi_rewrite::ExthChange::Removed { exth_type, old_value } => {
            format!("removed EXTH {} ({})", exth_type, preview_bytes(old_value))
        }
    }
}

fn preview_bytes(b: &[u8]) -> String {
    const MAX: usize = 80;
    match std::str::from_utf8(b) {
        Ok(s) if s.chars().all(|c| !c.is_control() || c == '\n' || c == '\t') => {
            if s.len() <= MAX {
                format!("{:?}", s)
            } else {
                format!("{:?}...", &s[..MAX])
            }
        }
        _ => format!("{} bytes", b.len()),
    }
}

/// Serialize a [`mobi_rewrite::RewriteReport`] to a JSON string. Implemented
/// by hand so the module does not need a serde dependency.
fn rewrite_report_to_json(report: &mobi_rewrite::RewriteReport) -> String {
    let mut out = String::new();
    out.push('{');
    out.push_str(&format!(
        "\"input_path\":{},",
        json_string(&report.input_path.display().to_string())
    ));
    out.push_str(&format!(
        "\"output_path\":{},",
        json_string(&report.output_path.display().to_string())
    ));
    out.push_str(&format!("\"no_op\":{},", report.no_op));
    out.push_str(&format!("\"cover_updated\":{},", report.cover_updated));
    out.push_str("\"changes\":[");
    for (i, change) in report.changes.iter().enumerate() {
        if i > 0 {
            out.push(',');
        }
        out.push_str(&exth_change_to_json(change));
    }
    out.push(']');
    out.push('}');
    out
}

fn exth_change_to_json(change: &mobi_rewrite::ExthChange) -> String {
    match change {
        mobi_rewrite::ExthChange::Added { exth_type, value } => format!(
            "{{\"kind\":\"added\",\"exth_type\":{},\"value\":{}}}",
            exth_type,
            json_bytes(value)
        ),
        mobi_rewrite::ExthChange::Replaced {
            exth_type,
            old_value,
            new_value,
        } => format!(
            "{{\"kind\":\"replaced\",\"exth_type\":{},\"old_value\":{},\"new_value\":{}}}",
            exth_type,
            json_bytes(old_value),
            json_bytes(new_value)
        ),
        mobi_rewrite::ExthChange::Removed {
            exth_type,
            old_value,
        } => format!(
            "{{\"kind\":\"removed\",\"exth_type\":{},\"old_value\":{}}}",
            exth_type,
            json_bytes(old_value)
        ),
    }
}

fn json_bytes(b: &[u8]) -> String {
    match std::str::from_utf8(b) {
        Ok(s) => json_string(s),
        Err(_) => {
            // Fall back to a base64-free hex-ish notation so the JSON stays
            // self-contained without adding a base64 dependency.
            let mut out = String::from("{\"hex\":\"");
            for byte in b {
                out.push_str(&format!("{:02x}", byte));
            }
            out.push_str("\"}");
            out
        }
    }
}

fn json_string(s: &str) -> String {
    let mut out = String::with_capacity(s.len() + 2);
    out.push('"');
    for c in s.chars() {
        match c {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            c if (c as u32) < 0x20 => out.push_str(&format!("\\u{:04x}", c as u32)),
            c => out.push(c),
        }
    }
    out.push('"');
    out
}