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
use crate::types::{Image, Metadata, Page, TocEntry};
use crate::utils::{ZipHandler, preprocess_html_entities};
use ordered_hash_map::OrderedHashMap;
use quick_xml::events::Event;
use std::io::Cursor;
use std::path::{Path, PathBuf};
/// A parsed EPUB e-book representation.
///
/// This struct contains all the extracted data from an EPUB file including:
/// - Metadata (title, author, publisher, etc.)
/// - Table of contents (hierarchical navigation)
/// - Text content (pages in reading order)
/// - Images (including cover image)
///
/// # Example
///
/// ```
/// use epub_parser::Epub;
/// use std::path::Path;
///
/// let epub = Epub::parse(Path::new("book.epub"))?;
///
/// // Access metadata
/// if let Some(title) = &epub.metadata.title {
/// println!("Title: {}", title);
/// }
///
/// // Access all pages
/// for page in &epub.pages {
/// println!("Page {}: {} chars", page.index, page.content.len());
/// }
///
/// // Access images
/// for image in &epub.images {
/// println!("Image: {} ({})", image.href, image.media_type);
/// }
/// ```
#[derive(Debug)]
pub struct Epub {
/// The Dublin Core metadata extracted from the EPUB.
pub metadata: Metadata,
/// The hierarchical table of contents from the NCX file.
pub toc: Vec<TocEntry>,
/// The text content of each page in reading order.
pub pages: Vec<Page>,
/// All images found in the EPUB (including cover).
pub images: Vec<Image>,
}
/// Errors that can occur while parsing an EPUB file.
#[derive(Debug)]
pub enum Error {
/// The EPUB file is invalid or corrupted.
InvalidEpub(String),
/// An I/O error occurred while reading the file.
IoError(std::io::Error),
/// An error occurred while reading the ZIP archive.
ZipError(zip::result::ZipError),
/// An error occurred while parsing XML.
XmlError(String),
/// The META-INF/container.xml file is missing.
MissingContainer,
/// The OPF package file is missing or not found.
MissingOpf,
/// The NCX table of contents file is missing.
MissingNcx,
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::InvalidEpub(msg) => write!(f, "Invalid EPUB: {}", msg),
Error::IoError(e) => write!(f, "I/O error: {}", e),
Error::ZipError(e) => write!(f, "ZIP error: {}", e),
Error::XmlError(e) => write!(f, "XML error: {}", e),
Error::MissingContainer => write!(f, "Missing container.xml"),
Error::MissingOpf => write!(f, "Missing OPF file"),
Error::MissingNcx => write!(f, "Missing NCX file"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::IoError(e) => Some(e),
Error::ZipError(e) => Some(e),
_ => None,
}
}
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Error::IoError(err)
}
}
impl From<zip::result::ZipError> for Error {
fn from(err: zip::result::ZipError) -> Self {
Error::ZipError(err)
}
}
impl From<quick_xml::Error> for Error {
fn from(err: quick_xml::Error) -> Self {
Error::XmlError(err.to_string())
}
}
impl Epub {
/// Parse an EPUB file from a file path.
///
/// # Arguments
///
/// * `path` - The path to the EPUB file.
///
/// # Returns
///
/// Returns `Ok(Epub)` on success, or an `Error` if parsing fails.
///
/// # Errors
///
/// This function will return an error if:
/// - The file does not exist
/// - The file is not a valid ZIP archive
/// - The EPUB structure is invalid
///
/// # Example
///
/// ```
/// use epub_parser::Epub;
/// use std::path::Path;
///
/// let epub = Epub::parse(Path::new("book.epub"))?;
/// println!("Parsed: {}", epub.metadata.title.unwrap_or_default());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn parse(path: &Path) -> Result<Self, Error> {
let mut zip_handler = ZipHandler::new(path)?;
Self::parse_from_handler(&mut zip_handler)
}
/// Parse an EPUB file from a byte buffer.
///
/// This is useful when you have the EPUB data in memory, for example
/// when downloading from a network or reading from a database.
///
/// # Arguments
///
/// * `buffer` - The raw bytes of the EPUB file.
///
/// # Returns
///
/// Returns `Ok(Epub)` on success, or an `Error` if parsing fails.
///
/// # Example
///
/// ```
/// use epub_parser::Epub;
///
/// let bytes = std::fs::read("book.epub")?;
/// let epub = Epub::parse_from_buffer(&bytes)?;
/// println!("Parsed: {}", epub.metadata.title.unwrap_or_default());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn parse_from_buffer(buffer: &[u8]) -> Result<Self, Error> {
let cursor = Cursor::new(buffer.to_vec());
let mut zip_handler = ZipHandler::new_from_reader(cursor)?;
Self::parse_from_handler(&mut zip_handler)
}
fn parse_from_handler<R: std::io::Read + std::io::Seek>(
zip_handler: &mut ZipHandler<R>,
) -> Result<Self, Error> {
let opf_path = zip_handler.get_opf_path()?;
let opf_content = zip_handler.read_file(&opf_path)?;
let (metadata, manifest, spine, ncx_path) = Self::parse_opf(&opf_content)?;
let toc = if let Some(ncx_ref) = ncx_path {
let ncx_path_full = Self::resolve_path(&opf_path, &ncx_ref);
let ncx_content = zip_handler.read_file(&ncx_path_full)?;
Self::parse_ncx(&ncx_content)?
} else {
Vec::new()
};
let mut pages = Vec::new();
for itemref in spine {
if let Some(manifest_item) = manifest.get(&itemref) {
let content_path = Self::resolve_path(&opf_path, &manifest_item.href);
match zip_handler.read_file(&content_path) {
Ok(content) => {
if let Ok(text) = Self::extract_text_from_html(&content) {
pages.push(Page {
index: pages.len(),
content: text,
});
}
}
Err(e) => {
eprintln!(
"Warning: Could not read content file '{}': {}",
content_path, e
);
}
}
}
}
let mut images = Vec::new();
for (id, item) in &manifest {
if item._media_type.to_lowercase().starts_with("image/") {
let image_path = Self::resolve_path(&opf_path, &item.href);
if let Ok(bytes) = zip_handler.read_file_as_bytes(&image_path) {
if id.to_lowercase().contains("cover") {
images.insert(
0,
Image {
id: id.clone(),
href: item.href.clone(),
media_type: item._media_type.clone(),
content: bytes,
},
);
} else {
images.push(Image {
id: id.clone(),
href: item.href.clone(),
media_type: item._media_type.clone(),
content: bytes,
});
}
}
}
}
Ok(Epub {
metadata,
toc,
pages,
images,
})
}
fn parse_opf(
content: &str,
) -> Result<
(
Metadata,
OrderedHashMap<String, ManifestItem>,
Vec<String>,
Option<String>,
),
Error,
> {
let content = preprocess_html_entities(content);
let mut reader = quick_xml::Reader::from_str(&content);
let mut metadata = Metadata::new();
let mut manifest: OrderedHashMap<String, ManifestItem> = OrderedHashMap::new();
let mut spine: Vec<String> = Vec::new();
let mut ncx_path: Option<String> = None;
let mut current_text_tag: Option<String> = None;
let mut buf = Vec::new();
loop {
match reader.read_event_into(&mut buf) {
Ok(Event::Start(ref e)) => {
let name = String::from_utf8_lossy(e.name().as_ref()).to_string();
if name.contains("title") {
current_text_tag = Some("title".to_string());
} else if name.contains("creator") {
current_text_tag = Some("author".to_string());
} else if name.contains("publisher") {
current_text_tag = Some("publisher".to_string());
} else if name.contains("language") {
current_text_tag = Some("language".to_string());
} else if name.contains("identifier") {
current_text_tag = Some("identifier".to_string());
} else if name.contains("date") {
current_text_tag = Some("date".to_string());
} else if name.contains("rights") {
current_text_tag = Some("rights".to_string());
}
}
Ok(Event::Empty(ref e)) => {
let name = String::from_utf8_lossy(e.name().as_ref()).to_string();
if name.contains("item") && !name.contains("itemref") {
let mut id = String::new();
let mut href = String::new();
let mut media_type = String::new();
for attr_result in e.attributes() {
if let Ok(attr) = attr_result {
let attr_name =
String::from_utf8_lossy(attr.key.as_ref()).to_string();
if attr_name == "id" || attr_name.ends_with(":id") {
if let Some(val) =
attr.decode_and_unescape_value(reader.decoder()).ok()
{
id = val.to_string();
}
} else if attr_name == "href" || attr_name.ends_with(":href") {
href = attr
.decode_and_unescape_value(reader.decoder())?
.to_string();
} else if attr_name == "media-type"
|| attr_name.ends_with(":media-type")
{
media_type = attr
.decode_and_unescape_value(reader.decoder())?
.to_string();
}
}
}
if !id.is_empty() && !href.is_empty() {
if media_type == "application/x-dtbncx+xml" {
ncx_path = Some(href.clone());
}
manifest.insert(
id.clone(),
ManifestItem {
_id: id.clone(),
href,
_media_type: media_type,
},
);
}
} else if name.contains("itemref") {
let mut idref = String::new();
for attr_result in e.attributes() {
if let Ok(attr) = attr_result {
let attr_name =
String::from_utf8_lossy(attr.key.as_ref()).to_string();
if attr_name == "idref" || attr_name.ends_with(":idref") {
if let Some(val) =
attr.decode_and_unescape_value(reader.decoder()).ok()
{
idref = val.to_string();
}
break;
}
}
}
if !idref.is_empty() {
spine.push(idref);
}
}
}
Ok(Event::Text(e)) => {
if let Some(tag) = ¤t_text_tag {
let text = e
.unescape()
.unwrap_or_else(|_| {
std::str::from_utf8(e.as_ref()).unwrap_or_default().into()
})
.into_owned()
.trim()
.to_string();
if !text.is_empty() {
match tag.as_str() {
"title" => metadata.title = Some(text),
"author" => metadata.author = Some(text),
"publisher" => metadata.publisher = Some(text),
"language" => metadata.language = Some(text),
"identifier" => metadata.identifier = Some(text),
"date" => metadata.date = Some(text),
"rights" => metadata.rights = Some(text),
_ => {}
}
}
current_text_tag = None;
}
}
Ok(Event::End(_)) => {
current_text_tag = None;
}
Ok(Event::Eof) => break,
Err(e) => return Err(Error::XmlError(e.to_string())),
_ => {}
}
buf.clear();
}
Ok((metadata, manifest, spine, ncx_path))
}
fn parse_ncx(content: &str) -> Result<Vec<TocEntry>, Error> {
let content = preprocess_html_entities(content);
let mut reader = quick_xml::Reader::from_str(&content);
let mut toc = Vec::new();
let mut stack: Vec<TocEntry> = Vec::new();
let mut buf = Vec::new();
let mut in_nav_label = false;
let mut in_text = false;
loop {
match reader.read_event_into(&mut buf) {
Ok(Event::Start(ref e)) => {
let name = String::from_utf8_lossy(e.name().as_ref()).to_string();
if name == "navPoint" {
let entry = TocEntry {
label: String::new(),
href: String::new(),
children: Vec::new(),
};
stack.push(entry);
} else if name == "navLabel" {
in_nav_label = true;
} else if name == "text" && in_nav_label {
in_text = true;
}
}
Ok(Event::End(ref e)) => {
let name = String::from_utf8_lossy(e.name().as_ref()).to_string();
if name == "navPoint" {
if let Some(entry) = stack.pop() {
if let Some(parent) = stack.last_mut() {
parent.children.push(entry);
} else {
toc.push(entry);
}
}
} else if name == "navLabel" {
in_nav_label = false;
} else if name == "text" && in_nav_label {
in_text = false;
}
}
Ok(Event::Text(e)) => {
if in_text {
if let Some(entry) = stack.last_mut() {
entry.label = e
.unescape()
.unwrap_or_else(|_| {
std::str::from_utf8(e.as_ref()).unwrap_or_default().into()
})
.into_owned();
}
}
}
Ok(Event::Empty(ref e)) => {
let name = String::from_utf8_lossy(e.name().as_ref()).to_string();
if name == "content" {
if let Some(src) = e.try_get_attribute("src")? {
if let Some(entry) = stack.last_mut() {
entry.href =
src.decode_and_unescape_value(reader.decoder())?.to_string();
}
}
}
}
Ok(Event::Eof) => break,
Err(e) => return Err(Error::XmlError(e.to_string())),
_ => {}
}
buf.clear();
}
Ok(toc)
}
fn extract_text_from_html(content: &str) -> Result<String, Error> {
let content = preprocess_html_entities(content);
let mut reader = quick_xml::Reader::from_str(&content);
let mut text = String::new();
let skip_tags: Vec<Vec<u8>> = vec![b"script".to_vec(), b"style".to_vec(), b"head".to_vec()];
let mut in_skip_tag = false;
let mut buf = Vec::new();
loop {
match reader.read_event_into(&mut buf) {
Ok(Event::Start(ref e)) => {
let tag = e.name().as_ref().to_vec();
if skip_tags.contains(&tag) {
in_skip_tag = true;
} else if tag.as_slice() == b"p"
|| tag.as_slice() == b"div"
|| tag.as_slice() == b"br"
|| tag.as_slice() == b"li"
{
text.push('\n');
}
}
Ok(Event::End(ref e)) => {
let tag = e.name().as_ref().to_vec();
if skip_tags.contains(&tag) {
in_skip_tag = false;
}
}
Ok(Event::Text(e)) => {
if !in_skip_tag {
let unescaped = e.unescape().unwrap_or_else(|_| {
std::str::from_utf8(e.as_ref()).unwrap_or_default().into()
});
let t = unescaped.into_owned();
let trimmed: String = t.chars().filter(|c| !c.is_control()).collect();
text.push_str(&trimmed);
text.push(' ');
}
}
Ok(Event::Eof) => break,
Err(e) => {
eprintln!(
"Warning: XML parse error in HTML content, continuing: {}",
e
);
break;
}
_ => {}
}
buf.clear();
}
Ok(text
.lines()
.map(|l| l.trim())
.filter(|l| !l.is_empty())
.collect::<Vec<_>>()
.join("\n"))
}
fn resolve_path(base_path: &str, href: &str) -> String {
let base = PathBuf::from(base_path);
let parent = base.parent().unwrap_or(base.as_path());
let resolved = parent.join(href);
resolved.to_string_lossy().to_string().replace('\\', "/")
}
}
#[derive(Debug, Clone)]
struct ManifestItem {
_id: String,
href: String,
_media_type: String,
}