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
//! KFX format importer.
//!
//! KFX is Amazon's Kindle Format 10, using Ion binary data format.
//!
//! This module handles I/O operations for reading KFX containers.
//! Pure parsing functions are in `crate::kfx::container`.
use std::collections::HashMap;
use std::io;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use crate::book::{CollectionInfo, Contributor, Landmark, Metadata, TocEntry};
use crate::import::{ChapterId, Importer, SpineEntry};
use crate::io::{ByteSource, FileSource};
use crate::ir::IRChapter;
use crate::kfx::container::{
self, ContainerError, EntityLoc, extract_doc_symbols, get_field, get_symbol_text,
parse_container_header, parse_container_info, parse_index_table, skip_enty_header,
};
use crate::kfx::ion::{IonParser, IonValue};
use crate::kfx::schema::schema;
use crate::kfx::storyline::parse_storyline_to_ir;
use crate::kfx::symbols::KfxSymbol;
/// Shorthand for getting a KfxSymbol as u32 for field lookups.
macro_rules! sym {
($variant:ident) => {
KfxSymbol::$variant as u64
};
}
/// KFX format importer.
pub struct KfxImporter {
/// Random-access byte source.
source: Arc<dyn ByteSource>,
/// Container header length (offset to entity data).
#[allow(dead_code)]
header_len: usize,
/// Entity index: maps (type_id, entity_idx) -> EntityLoc
entities: Vec<EntityLoc>,
/// Document-specific symbols (extended symbol table).
doc_symbols: Vec<String>,
/// Book metadata.
metadata: Metadata,
/// Table of contents.
toc: Vec<TocEntry>,
/// Landmarks (structural navigation points).
landmarks: Vec<Landmark>,
/// Reading order (spine).
spine: Vec<SpineEntry>,
/// Section names for spine entries.
section_names: Vec<String>,
/// Cache: section name -> storyline EntityLoc (lazily populated)
section_storylines: HashMap<String, EntityLoc>,
/// Whether section→storyline mapping has been built
section_storylines_indexed: bool,
/// Resources: name -> EntityLoc (lazily populated)
resources: HashMap<String, EntityLoc>,
/// Whether resources have been indexed
resources_indexed: bool,
/// Content cache: name -> list of strings (lazily populated)
content_cache: HashMap<String, Vec<String>>,
/// Anchor map: anchor_name -> uri (for external link resolution)
anchors: HashMap<String, String>,
/// Whether anchors have been indexed
anchors_indexed: bool,
/// Style map: style_name -> KFX style properties (for style resolution)
styles: HashMap<String, Vec<(u64, IonValue)>>,
/// Whether styles have been indexed
styles_indexed: bool,
}
impl From<ContainerError> for io::Error {
fn from(e: ContainerError) -> Self {
io::Error::new(io::ErrorKind::InvalidData, e.to_string())
}
}
impl Importer for KfxImporter {
fn open(path: &Path) -> io::Result<Self> {
let file = std::fs::File::open(path)?;
let source = Arc::new(FileSource::new(file)?);
Self::from_source(source)
}
fn metadata(&self) -> &Metadata {
&self.metadata
}
fn toc(&self) -> &[TocEntry] {
&self.toc
}
fn landmarks(&self) -> &[Landmark] {
&self.landmarks
}
fn spine(&self) -> &[SpineEntry] {
&self.spine
}
fn source_id(&self, id: ChapterId) -> Option<&str> {
self.section_names.get(id.0 as usize).map(|s| s.as_str())
}
fn load_chapter(&mut self, id: ChapterId) -> io::Result<IRChapter> {
// Ensure anchors and styles are indexed
self.index_anchors()?;
self.index_styles()?;
let section_name = self
.section_names
.get(id.0 as usize)
.ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "Chapter not found"))?
.clone();
// Get storyline location
let storyline_loc = self.resolve_section_to_storyline(§ion_name)?;
// Parse storyline entity
let storyline_ion = self.parse_entity_ion(storyline_loc)?;
// Clone doc_symbols, anchors, and styles to avoid borrow conflict with content lookup closure
let doc_symbols = self.doc_symbols.clone();
let anchors = self.anchors.clone();
let styles = self.styles.clone();
// Parse storyline and build IR using schema-driven tokenization
let mut chapter = parse_storyline_to_ir(
&storyline_ion,
&doc_symbols,
Some(&anchors),
Some(&styles),
|name, index| self.lookup_content_text(name, index),
);
// Run optimization passes (KFX builds IR directly, not through compile_html)
crate::compiler::optimizer::optimize(&mut chapter);
Ok(chapter)
}
fn load_raw(&mut self, id: ChapterId) -> io::Result<Vec<u8>> {
let section_name = self
.section_names
.get(id.0 as usize)
.ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "Chapter not found"))?
.clone();
// Find section entity and resolve to storyline
let storyline_loc = self.resolve_section_to_storyline(§ion_name)?;
self.read_entity(storyline_loc)
}
fn list_assets(&self) -> Vec<PathBuf> {
// Return entity IDs for bcRawMedia (actual asset data)
self.entities
.iter()
.filter(|e| e.type_id == KfxSymbol::Bcrawmedia as u32)
.map(|e| PathBuf::from(format!("#{}", e.id)))
.collect()
}
fn load_asset(&mut self, path: &Path) -> io::Result<Vec<u8>> {
let name = path.to_string_lossy();
// Handle direct entity ID lookup (e.g., "#1102" from list_assets)
if let Some(id_str) = name.strip_prefix('#') {
if let Ok(id) = id_str.parse::<u32>() {
// Find entity by ID
if let Some(loc) = self.entities.iter().find(|e| e.id == id) {
return self.read_entity(*loc);
}
}
return Err(io::Error::new(io::ErrorKind::NotFound, "Entity not found"));
}
// Ensure resources are indexed for name-based lookup
if !self.resources_indexed {
self.index_resources()?;
}
let loc = self
.resources
.get(name.as_ref())
.ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "Asset not found"))?;
self.read_entity(*loc)
}
fn requires_normalized_export(&self) -> bool {
// KFX load_raw returns binary Ion data, not HTML
true
}
}
impl KfxImporter {
/// Create an importer from a ByteSource.
pub fn from_source(source: Arc<dyn ByteSource>) -> io::Result<Self> {
// Read and parse container header (18 bytes)
let header_data = source.read_at(0, 18)?;
let header = parse_container_header(&header_data)?;
// Read and parse container info
let container_info_data = source.read_at(
header.container_info_offset as u64,
header.container_info_length,
)?;
let container_info = parse_container_info(&container_info_data)?;
// Get index table location (required)
let (index_offset, index_length) = container_info.index.ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidData,
"Missing index table in container",
)
})?;
// Read and parse document symbols (optional)
let doc_symbols = if let Some((offset, length)) = container_info.doc_symbols {
if length > 0 {
let doc_sym_data = source.read_at(offset as u64, length)?;
extract_doc_symbols(&doc_sym_data)
} else {
Vec::new()
}
} else {
Vec::new()
};
// Read and parse index table
let index_data = source.read_at(index_offset as u64, index_length)?;
let entities = parse_index_table(&index_data, header.header_len);
let mut importer = Self {
source,
header_len: header.header_len,
entities,
doc_symbols,
metadata: Metadata::default(),
toc: Vec::new(),
landmarks: Vec::new(), // TODO: Parse from KFX landmarks nav_container
spine: Vec::new(),
section_names: Vec::new(),
section_storylines: HashMap::new(),
section_storylines_indexed: false,
resources: HashMap::new(),
resources_indexed: false,
content_cache: HashMap::new(),
anchors: HashMap::new(),
anchors_indexed: false,
styles: HashMap::new(),
styles_indexed: false,
};
// Parse metadata (only reads needed entities)
importer.parse_metadata()?;
// Parse navigation (TOC)
importer.parse_navigation()?;
// Build section→storyline map (needed for spine sizes and load_raw)
importer.index_section_storylines()?;
// Parse spine from reading order (uses section→storyline map for sizes)
importer.parse_spine()?;
Ok(importer)
}
/// Read an entity's raw data (after ENTY header).
fn read_entity(&self, loc: EntityLoc) -> io::Result<Vec<u8>> {
let entity_data = self.source.read_at(loc.offset as u64, loc.length)?;
// Use pure function to skip ENTY header
let payload = skip_enty_header(&entity_data);
if payload.len() != entity_data.len() {
Ok(payload.to_vec())
} else {
Ok(entity_data)
}
}
/// Parse an entity as Ion and return the parsed value.
fn parse_entity_ion(&self, loc: EntityLoc) -> io::Result<IonValue> {
let ion_data = self.read_entity(loc)?;
let mut parser = IonParser::new(&ion_data);
parser.parse()
}
/// Get a symbol's text from an IonValue (handles both Symbol and String).
fn get_symbol_text<'a>(&'a self, value: &'a IonValue) -> Option<&'a str> {
get_symbol_text(value, &self.doc_symbols)
}
/// Parse book metadata.
fn parse_metadata(&mut self) -> io::Result<()> {
// Find book_metadata entity
let loc = self
.entities
.iter()
.find(|e| e.type_id == KfxSymbol::BookMetadata as u32)
.copied();
if let Some(loc) = loc {
let elem = self.parse_entity_ion(loc)?;
if let Some(fields) = elem.as_struct()
&& let Some(list) =
get_field(fields, sym!(CategorisedMetadata)).and_then(|m| m.as_list())
{
for category_elem in list {
if let Some(cat_fields) = category_elem.as_struct() {
let category = get_field(cat_fields, sym!(Category))
.and_then(|v| self.get_symbol_text(v))
.unwrap_or("");
if category == "kindle_title_metadata"
&& let Some(metadata_list) =
get_field(cat_fields, sym!(Metadata)).and_then(|v| v.as_list())
{
for meta in metadata_list {
let Some(meta_fields) = meta.as_struct() else {
continue;
};
let key = get_field(meta_fields, sym!(Key))
.and_then(|v| v.as_string())
.unwrap_or("");
let value = get_field(meta_fields, sym!(Value))
.and_then(|v| v.as_string())
.unwrap_or("");
match key {
"title" => self.metadata.title = value.to_string(),
"author" => self.metadata.authors.push(value.to_string()),
"publisher" => {
self.metadata.publisher = Some(value.to_string())
}
"language" => self.metadata.language = value.to_string(),
"description" => {
self.metadata.description = Some(value.to_string())
}
"book_id" => self.metadata.identifier = value.to_string(),
"issue_date" => self.metadata.date = Some(value.to_string()),
"cover_image" => {
let value_elem = get_field(meta_fields, sym!(Value));
if let Some(cover) = self.resolve_cover_value(value_elem) {
self.metadata.cover_image = Some(cover);
}
}
"modified_date" => {
self.metadata.modified_date = Some(value.to_string())
}
"translator" => self.metadata.contributors.push(Contributor {
name: value.to_string(),
file_as: None,
role: Some("trl".to_string()),
}),
"title_pronunciation" => {
self.metadata.title_sort = Some(value.to_string())
}
"author_pronunciation" => {
self.metadata.author_sort = Some(value.to_string())
}
"series_name" => {
if let Some(ref mut coll) = self.metadata.collection {
coll.name = value.to_string();
} else {
self.metadata.collection = Some(CollectionInfo {
name: value.to_string(),
collection_type: Some("series".to_string()),
position: None,
});
}
}
"series_position" => {
if let Ok(pos) = value.parse::<f64>() {
if let Some(ref mut coll) = self.metadata.collection {
coll.position = Some(pos);
} else {
self.metadata.collection = Some(CollectionInfo {
name: String::new(),
collection_type: Some("series".to_string()),
position: Some(pos),
});
}
}
}
_ => {}
}
}
}
}
}
}
}
Ok(())
}
/// Parse book navigation (TOC and landmarks).
fn parse_navigation(&mut self) -> io::Result<()> {
// Find book_navigation entity
let loc = self
.entities
.iter()
.find(|e| e.type_id == KfxSymbol::BookNavigation as u32)
.copied();
if let Some(loc) = loc {
let elem = self.parse_entity_ion(loc)?;
// book_navigation is a list of reading orders
if let Some(list) = elem.as_list() {
for reading_order in list {
if let Some(ro_fields) = reading_order.as_struct() {
// Look for nav_containers
if let Some(containers) =
get_field(ro_fields, sym!(NavContainers)).and_then(|v| v.as_list())
{
for container in containers {
// Unwrap annotation if present
let inner = container.unwrap_annotated();
if let Some(container_fields) = inner.as_struct() {
// Check nav_type
let nav_type = get_field(container_fields, sym!(NavType))
.and_then(|v| self.get_symbol_text(v));
match nav_type {
Some("toc") => {
self.toc = self.parse_nav_entries(container_fields);
}
Some("landmarks") => {
self.landmarks =
self.parse_landmark_entries(container_fields);
}
_ => {}
}
}
}
}
}
}
}
}
Ok(())
}
/// Parse landmark entries from a landmarks nav_container.
fn parse_landmark_entries(&self, container: &[(u64, IonValue)]) -> Vec<Landmark> {
let mut landmarks = Vec::new();
if let Some(entry_list) = get_field(container, sym!(Entries)).and_then(|v| v.as_list()) {
for entry in entry_list {
// Unwrap annotation if present
let inner = entry.unwrap_annotated();
if let Some(entry_fields) = inner.as_struct() {
// Get landmark_type symbol and convert via schema
let landmark_type =
get_field(entry_fields, sym!(LandmarkType)).and_then(|v| match v {
IonValue::Symbol(id) => schema().landmark_from_kfx(*id),
_ => None,
});
// Skip unknown landmark types
let Some(landmark_type) = landmark_type else {
continue;
};
// Get label from representation.label
let label = get_field(entry_fields, sym!(Representation))
.and_then(|v| v.as_struct())
.and_then(|s| get_field(s, sym!(Label)))
.and_then(|v| v.as_string())
.unwrap_or("")
.to_string();
// Get target position (id and offset)
let target_pos =
get_field(entry_fields, sym!(TargetPosition)).and_then(|v| v.as_struct());
let href = if let Some(pos) = target_pos {
let id = get_field(pos, sym!(Id)).and_then(|v| v.as_int());
let offset = get_field(pos, sym!(Offset)).and_then(|v| v.as_int());
match (id, offset) {
(Some(id), Some(off)) if off > 0 => format!("#{}:{}", id, off),
(Some(id), _) => format!("#{}", id),
_ => String::new(),
}
} else {
String::new()
};
landmarks.push(Landmark {
landmark_type,
href,
label,
});
}
}
}
landmarks
}
/// Recursively parse nav entries into a tree structure.
fn parse_nav_entries(&self, container: &[(u64, IonValue)]) -> Vec<TocEntry> {
let mut entries = Vec::new();
if let Some(entry_list) = get_field(container, sym!(Entries)).and_then(|v| v.as_list()) {
for entry in entry_list {
// Unwrap annotation if present (nav_unit::...)
let inner = entry.unwrap_annotated();
if let Some(entry_fields) = inner.as_struct() {
// Get label (try representation.label first, then direct label)
let label = get_field(entry_fields, sym!(Representation))
.and_then(|v| v.as_struct())
.and_then(|s| get_field(s, sym!(Label)))
.and_then(|v| v.as_string())
.or_else(|| {
get_field(entry_fields, sym!(Label)).and_then(|v| v.as_string())
})
.unwrap_or("Untitled");
// Skip placeholder labels
if label == "heading-nav-unit" || label == "Untitled" {
continue;
}
// Get target position (includes id and offset for within-section navigation)
let target_pos =
get_field(entry_fields, sym!(TargetPosition)).and_then(|v| v.as_struct());
let href = if let Some(pos) = target_pos {
let id = get_field(pos, sym!(Id)).and_then(|v| v.as_int());
let offset = get_field(pos, sym!(Offset)).and_then(|v| v.as_int());
match (id, offset) {
(Some(id), Some(off)) if off > 0 => format!("#{}:{}", id, off),
(Some(id), _) => format!("#{}", id),
_ => String::new(),
}
} else {
String::new()
};
// Recursively parse children
let children = self.parse_nav_entries(entry_fields);
entries.push(TocEntry {
title: label.to_string(),
href,
children,
play_order: None,
});
}
}
}
entries
}
/// Parse spine from reading_orders.
///
/// Uses the section→storyline cache to get size estimates.
fn parse_spine(&mut self) -> io::Result<()> {
let section_names = self.get_reading_order_sections()?;
for (idx, name) in section_names.into_iter().enumerate() {
// Get size from cached storyline location
let size_estimate = self
.section_storylines
.get(&name)
.map(|loc| loc.length)
.unwrap_or(0);
self.section_names.push(name);
self.spine.push(SpineEntry {
id: ChapterId(idx as u32),
size_estimate,
});
}
Ok(())
}
/// Resolve a section name to its storyline entity location.
fn resolve_section_to_storyline(&self, section_name: &str) -> io::Result<EntityLoc> {
self.section_storylines
.get(section_name)
.copied()
.ok_or_else(|| {
io::Error::new(
io::ErrorKind::NotFound,
format!("Could not resolve section: {}", section_name),
)
})
}
/// Build the section name → storyline location cache.
fn index_section_storylines(&mut self) -> io::Result<()> {
if self.section_storylines_indexed {
return Ok(());
}
// First, build a map of story_name → storyline EntityLoc
let mut storyline_map: HashMap<String, EntityLoc> = HashMap::new();
for loc in &self.entities {
if loc.type_id == KfxSymbol::Storyline as u32
&& let Ok(elem) = self.parse_entity_ion(*loc)
&& let Some(fields) = elem.as_struct()
&& let Some(name) =
get_field(fields, sym!(StoryName)).and_then(|v| self.get_symbol_text(v))
{
storyline_map.insert(name.to_string(), *loc);
}
}
// Then, map each section to its storyline
for loc in &self.entities {
if loc.type_id == KfxSymbol::Section as u32
&& let Ok(elem) = self.parse_entity_ion(*loc)
&& let Some(fields) = elem.as_struct()
{
let section_name =
get_field(fields, sym!(SectionName)).and_then(|v| self.get_symbol_text(v));
let story_name = get_field(fields, sym!(PageTemplates))
.and_then(|v| v.as_list())
.and_then(|templates| templates.first())
.and_then(|t| t.as_struct())
.and_then(|f| get_field(f, sym!(StoryName)))
.and_then(|v| self.get_symbol_text(v));
if let (Some(sec_name), Some(story_name)) = (section_name, story_name)
&& let Some(storyline_loc) = storyline_map.get(story_name)
{
self.section_storylines
.insert(sec_name.to_string(), *storyline_loc);
}
}
}
self.section_storylines_indexed = true;
Ok(())
}
/// Extract section names from reading_orders in document_data or metadata.
///
/// Prefers the "default" reading order if multiple are present.
fn get_reading_order_sections(&self) -> io::Result<Vec<String>> {
// Try document_data ($538) first, then metadata ($258)
let doc_data_loc = self
.entities
.iter()
.find(|e| e.type_id == KfxSymbol::DocumentData as u32)
.copied();
let metadata_loc = self
.entities
.iter()
.find(|e| e.type_id == KfxSymbol::Metadata as u32)
.copied();
for loc in [doc_data_loc, metadata_loc].into_iter().flatten() {
if let Ok(elem) = self.parse_entity_ion(loc)
&& let Some(fields) = elem.as_struct()
&& let Some(orders) =
get_field(fields, sym!(ReadingOrders)).and_then(|v| v.as_list())
{
// First pass: look for "default" reading order
for order in orders {
if let Some(order_fields) = order.as_struct() {
let order_name = get_field(order_fields, sym!(ReadingOrderName))
.and_then(|v| self.get_symbol_text(v));
if order_name == Some("default")
&& let Some(sections) = self.extract_sections(order_fields)
{
return Ok(sections);
}
}
}
// Second pass: take first reading order with sections
for order in orders {
if let Some(order_fields) = order.as_struct()
&& let Some(sections) = self.extract_sections(order_fields)
{
return Ok(sections);
}
}
}
}
Ok(Vec::new())
}
/// Extract section names from a reading order struct.
fn extract_sections(&self, order_fields: &[(u64, IonValue)]) -> Option<Vec<String>> {
let sections = get_field(order_fields, sym!(Sections))?.as_list()?;
let mut section_names = Vec::new();
for section in sections {
if let Some(name) = self.get_symbol_text(section) {
section_names.push(name.to_string());
}
}
if section_names.is_empty() {
None
} else {
Some(section_names)
}
}
/// Resolve cover_image value which can be a string or list with symbol/string reference.
fn resolve_cover_value(&self, value: Option<&IonValue>) -> Option<String> {
let value = value?;
// Format 1: Direct string
if let Some(s) = value.as_string() {
return Some(s.to_string());
}
// Format 2: List containing a symbol or string reference
if let Some(list) = value.as_list()
&& let Some(first) = list.first()
&& let Some(text) = self.get_symbol_text(first)
{
return Some(text.to_string());
}
None
}
/// Look up text content by name and index.
///
/// Lazily loads and caches content entities as needed.
fn lookup_content_text(&mut self, name: &str, index: usize) -> Option<String> {
// Check cache first
if let Some(content_list) = self.content_cache.get(name) {
return content_list.get(index).cloned();
}
// Load and cache the content entity
if let Some(content_list) = self.load_content_entity(name) {
let result = content_list.get(index).cloned();
self.content_cache.insert(name.to_string(), content_list);
return result;
}
None
}
/// Load a content entity by name and return its string list.
fn load_content_entity(&self, name: &str) -> Option<Vec<String>> {
// Find content entity with matching name
for loc in &self.entities {
if loc.type_id == KfxSymbol::Content as u32
&& let Ok(elem) = self.parse_entity_ion(*loc)
&& let Some(fields) = elem.as_struct()
{
// Check if name matches
let entity_name =
get_field(fields, sym!(Name)).and_then(|v| self.get_symbol_text(v));
if entity_name == Some(name)
&& let Some(list) =
get_field(fields, sym!(ContentList)).and_then(|v| v.as_list())
{
return Some(
list.iter()
.filter_map(|v| v.as_string().map(|s| s.to_string()))
.collect(),
);
}
}
}
None
}
/// Index external resources.
fn index_resources(&mut self) -> io::Result<()> {
if self.resources_indexed {
return Ok(());
}
// Collect entities to process to avoid borrow conflicts
let locs: Vec<_> = self
.entities
.iter()
.filter(|e| e.type_id == KfxSymbol::ExternalResource as u32)
.copied()
.collect();
for loc in locs {
if let Ok(elem) = self.parse_entity_ion(loc)
&& let Some(fields) = elem.as_struct()
{
// Use location as key (e.g., "resource/rsrc7")
let location = get_field(fields, sym!(Location))
.and_then(|v| v.as_string())
.map(|s| s.to_string());
// Also index by resource_name (e.g., "eF") for cover lookup
let name = get_field(fields, sym!(ResourceName))
.and_then(|v| container::get_symbol_text(v, &self.doc_symbols))
.map(|s| s.to_string());
if let Some(loc_str) = &location
&& !loc_str.is_empty()
{
self.resources.insert(loc_str.clone(), loc);
}
if let Some(name_str) = &name
&& !name_str.is_empty()
&& Some(name_str) != location.as_ref()
{
self.resources.insert(name_str.clone(), loc);
}
}
}
self.resources_indexed = true;
Ok(())
}
/// Index anchor entities to build anchor_name → uri map.
///
/// This enables resolution of external links where `link_to` contains
/// an anchor name that maps to an external URI.
fn index_anchors(&mut self) -> io::Result<()> {
if self.anchors_indexed {
return Ok(());
}
// Find all anchor entities (type $266)
let locs: Vec<_> = self
.entities
.iter()
.filter(|e| e.type_id == KfxSymbol::Anchor as u32)
.copied()
.collect();
for loc in locs {
if let Ok(elem) = self.parse_entity_ion(loc)
&& let Some(fields) = elem.as_struct()
{
// Get anchor_name
let anchor_name = get_field(fields, sym!(AnchorName))
.and_then(|v| container::get_symbol_text(v, &self.doc_symbols))
.map(|s| s.to_string());
// Get uri (only present for external links)
let uri = get_field(fields, sym!(Uri))
.and_then(|v| v.as_string())
.map(|s| s.to_string());
// If we have both anchor_name and uri, add to map
if let (Some(name), Some(uri)) = (anchor_name, uri) {
self.anchors.insert(name, uri);
}
}
}
self.anchors_indexed = true;
Ok(())
}
/// Index style entities to build style_name → properties map.
///
/// This enables resolution of style references in storyline elements.
/// Style entities ($157) contain properties like font_weight, text_alignment, margins, etc.
fn index_styles(&mut self) -> io::Result<()> {
if self.styles_indexed {
return Ok(());
}
// Find all style entities (type $157)
let locs: Vec<_> = self
.entities
.iter()
.filter(|e| e.type_id == KfxSymbol::Style as u32)
.copied()
.collect();
for loc in locs {
if let Ok(elem) = self.parse_entity_ion(loc)
&& let Some(fields) = elem.as_struct()
{
// Get style_name
let style_name = get_field(fields, sym!(StyleName))
.and_then(|v| container::get_symbol_text(v, &self.doc_symbols))
.map(|s| s.to_string());
if let Some(name) = style_name {
// Store all fields (cloned) for later interpretation
let props: Vec<(u64, IonValue)> = fields
.iter()
.filter(|(k, _)| *k != sym!(StyleName)) // Exclude the name itself
.map(|(k, v)| (*k, v.clone()))
.collect();
self.styles.insert(name, props);
}
}
}
self.styles_indexed = true;
Ok(())
}
}