Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@ repository = { workspace = true }
readme = { workspace = true }
keywords = { workspace = true }
rust-version = { workspace = true }

[dependencies]
zip = "6.0.0"
zip = "7.0.0"
anyhow = "1.0.100"
quick-xml = { version = "0.38.3" }
quick-xml = { version = "0.38.4" }
ab_glyph = { version = "0.2.32", optional = true }
imageproc = { version = "0.25.0", optional = true}
serde_json = { version = "1.0.145", optional = true }
imageproc = { version = "0.26.0", optional = true}
serde_json = { version = "1.0.149", optional = true }
iepub-derive = { path = "../derive", version = "1.2.6" }
serde = { version = "1.0.228", features = ["derive"], optional = true }
image = { version = "0.25.8", default-features = false, features = ["jpeg"], optional = true }
image = { version = "0.25.9", default-features = false, features = ["jpeg"], optional = true }
md-5 = {version = "0.10.6", optional = true }

[dev-dependencies]
reqwest = { version = "0.11", features = ["blocking"] }
reqwest = { version = "0.13.1", features = ["blocking"] }

[features]
no_nav=[]
Expand Down
94 changes: 94 additions & 0 deletions lib/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,100 @@ impl From<FromUtf8Error> for IError {
IError::Utf8(value)
}
}

/// 内容类型枚举
#[derive(Debug, Clone)]
pub enum ContentType {
/// 段落
Paragraph,
/// 标题 (level: 1-6)
Heading(u8),
/// 图片
Image,
/// 链接
Link,
/// 列表项
ListItem,
/// 引用块
BlockQuote,
/// 代码块
CodeBlock,
/// 分隔线
HorizontalRule,
/// 普通文本
Text,
/// 其他标签
Other(String),
}

/// 解析后的内容项
#[derive(Debug, Clone)]
pub struct ContentItem {
/// 内容类型
pub content_type: ContentType,
/// 文本内容
pub text: String,
/// 属性 (例如图片的 src, 链接的 href 等)
pub attributes: Vec<(String, String)>,
/// 子内容
pub children: Vec<ContentItem>,
}

impl ContentItem {
pub fn new(content_type: ContentType) -> Self {
Self {
content_type,
text: String::new(),
attributes: Vec::new(),
children: Vec::new(),
}
}

/// 添加属性
pub fn add_attribute(&mut self, key: String, value: String) {
self.attributes.push((key, value));
}

/// 添加子内容
pub fn add_child(&mut self, child: ContentItem) {
self.children.push(child);
}

/// 添加文本
pub fn add_text(&mut self, text: &str) {
self.text.push_str(text);
}

/// 格式化输出
pub fn format(&self, indent: usize) -> String {
let indent_str = " ".repeat(indent);
let mut result = format!("{}[{:?}]", indent_str, self.content_type);

if !self.text.is_empty() {
result.push_str(&format!(" 文本: \"{}\"", self.text.trim()));
}

if !self.attributes.is_empty() {
result.push_str(" 属性: {");
for (i, (key, value)) in self.attributes.iter().enumerate() {
if i > 0 {
result.push_str(", ");
}
result.push_str(&format!("{}: \"{}\"", key, value));
}
result.push('}');
}

result.push('\n');

for child in &self.children {
result.push_str(&child.format(indent + 1));
}

result
}
}

cache_struct! {
#[derive(Debug, Default)]
pub(crate) struct BookInfo {
Expand Down
20 changes: 18 additions & 2 deletions lib/src/epub/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::cache_struct;
use crate::common::{escape_xml, urldecode_enhanced, IError, IResult};
use crate::epub::common::LinkRel;
use crate::epub::html;
use crate::parser::HtmlParser;
crate::cache_enum! {
#[derive(Clone)]
pub enum Direction {
Expand Down Expand Up @@ -171,8 +172,11 @@ impl Debug for EpubHtml {
}

impl EpubHtml {
pub fn string_data(&self) -> String {
if let Some(data) = &self._data {
pub fn string_data(&mut self) -> String {
if self._data.is_none() {
self.data_mut();
}
if let Some(data) = &mut self._data {
String::from_utf8(data.clone()).unwrap_or_else(|_e| String::new())
} else {
String::new()
Expand All @@ -183,6 +187,18 @@ impl EpubHtml {
self._data.as_deref()
}

pub fn parser(&mut self) -> Option<HtmlParser> {
let mut obj = None;
let html = self.string_data();
if !html.is_empty() {
let mut parser = HtmlParser::new();
if parser.parse(&html).is_ok() {
obj = Some(parser);
}
}
obj
}

pub(crate) fn read_data(&mut self, reader: &mut impl EpubReaderTrait) {
let (id, origin) = if let Some(index) = self._file_name.find('#') {
(
Expand Down
7 changes: 5 additions & 2 deletions lib/src/epub/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,6 @@ html
assert_ne!(0, nav.len());
assert_ne!("", nav[0].title());
let mut chap = book.chapters_mut();

assert_eq!(75, chap.next().unwrap().data_mut().unwrap().len());

// println!("{}", String::from_utf8( chap.next().unwrap().data().unwrap().to_vec()).unwrap());
Expand All @@ -1208,11 +1207,15 @@ html
chap.next().unwrap().data_mut().unwrap().to_vec().len()
);

for i in chap {
if let Some(p) = i.parser() {
assert_ne!(0, p.extract_plain_text().len());
}
}
assert!(book.get_chapter("s04.xhtml#pgepubid00536").is_some());
// assert!(chap.next().is_some());
// chap.next();
// chap.next();

// assert_ne!("", chap.next().unwrap().title());
// assert_ne!(None, book.chapters_mut().next().unwrap().data());
}
Expand Down
3 changes: 2 additions & 1 deletion lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
#[allow(clippy::needless_range_loop)]
extern crate iepub_derive;
mod adapter;
mod common;
pub mod common;
mod cover;
mod epub;
mod mobi;
pub mod parser;
pub mod path;
pub use crate::common::DateTimeFormater;

Expand Down
Loading