Skip to content

Commit

Permalink
Merge pull request #47 from onlyduyy/main
Browse files Browse the repository at this point in the history
Added `epub_direction()` (Update Page Direction using PageDirection enum instead of `metadata()`) & `add_metadata_opf()` (Add custom `<meta>` to the `content.opf` file)
  • Loading branch information
crowdagger authored Oct 5, 2024
2 parents 14faaee + 6f662c9 commit 31815e7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/epub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,28 @@ pub enum PageDirection {
Rtl,
}


/// Represents the EPUB `<meta>` content inside `content.opf` file.
///
/// <meta name="" content="">
///
#[derive(Debug)]
pub struct MetadataOpf {
/// Name of the `<meta>` tag
pub name: String,
/// Content of the `<meta>` tag
pub content: String
}

impl MetadataOpf {
/// Create new instance
///
///
pub fn new(&self, meta_name: String, meta_content: String) -> Self {
Self { name: meta_name, content: meta_content }
}
}

impl ToString for PageDirection {
fn to_string(&self) -> String {
match &self {
Expand Down Expand Up @@ -144,27 +166,31 @@ impl Content {
#[derive(Debug)]
pub struct EpubBuilder<Z: Zip> {
version: EpubVersion,
direction: PageDirection,
zip: Z,
files: Vec<Content>,
metadata: Metadata,
toc: Toc,
stylesheet: bool,
inline_toc: bool,
escape_html: bool,
meta_opf: Vec<MetadataOpf>
}

impl<Z: Zip> EpubBuilder<Z> {
/// Create a new default EPUB Builder
pub fn new(zip: Z) -> Result<EpubBuilder<Z>> {
let mut epub = EpubBuilder {
version: EpubVersion::V20,
direction: PageDirection::Ltr,
zip,
files: vec![],
metadata: Metadata::default(),
toc: Toc::new(),
stylesheet: false,
inline_toc: false,
escape_html: true,
meta_opf: Vec::new()
};

epub.zip
Expand All @@ -187,6 +213,36 @@ impl<Z: Zip> EpubBuilder<Z> {
self.version = version;
self
}

/// Set EPUB Direction (default: Ltr)
///
/// * `Ltr`: Left-To-Right
/// * `Rtl`: Right-To-Left
///
///
pub fn epub_direction(&mut self, direction: PageDirection) -> &mut Self {
self.direction = direction;
self
}


/// Add custom <meta> to `content.opf`
/// Syntax: `self.add_metadata_opf(name, content)`
///
/// ### Example
/// If you wanna add `<meta name="primary-writing-mode" content="vertical-rl"/>` into `content.opf`
///
/// ```rust
/// self.add_metadata_opf(MetadataOpf {
/// name: String::from("primary-writing-mode"),
/// content: String::from("vertical-rl")
/// })
/// ```
///
pub fn add_metadata_opf(&mut self, item: MetadataOpf) -> &mut Self {
self.meta_opf.push(item);
self
}

/// Set some EPUB metadata
///
Expand Down Expand Up @@ -550,6 +606,14 @@ impl<Z: Zip> EpubBuilder<Z> {
common::encode_html(rights, self.escape_html),
));
}
for meta in &self.meta_opf{
optional.push(format!(
"<meta name=\"{}\" content=\"{}\"/>",
common::encode_html(&meta.name, self.escape_html),
common::encode_html(&meta.content, self.escape_html),
));
}

let date_modified = self
.metadata
.date_modified
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ mod zip_library;

pub use epub::EpubBuilder;
pub use epub::EpubVersion;
pub use epub::MetadataOpf;
pub use epub::PageDirection;
pub use epub_content::EpubContent;
pub use epub_content::ReferenceType;
Expand Down

0 comments on commit 31815e7

Please sign in to comment.