From 3d54b73266646f73e855a175dbfd86ca2d50ba96 Mon Sep 17 00:00:00 2001 From: A Farzat Date: Wed, 4 Nov 2026 15:25:31 +0300 Subject: Record the O'Reilly bookid in the generated EPUB Store the O'Reilly bookid as EPUB metadata instead of relying solely on the filename. Preserving the original bookid makes it possible to look up additional information from the upstream O'Reilly resource later, even if the EPUB is renamed or imported into a library manager. --- src/xml.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'src/xml.rs') diff --git a/src/xml.rs b/src/xml.rs index 239fac3..f37b169 100644 --- a/src/xml.rs +++ b/src/xml.rs @@ -124,6 +124,7 @@ pub fn build_epub_chapter( pub fn write_modified_opf( opf_input: R, mut out: &mut W, + bookid: &str, description: &str, ) -> Result<()> { // Setup the XML Reader and Writer. @@ -146,11 +147,21 @@ pub fn write_modified_opf( writer.write_event(Event::Start(tag_data))?; } Ok(Event::End(tag_data)) => { - // Write description if end of metadata is reached without finding one. - if tag_data.name().as_ref() == b"metadata" && !desc_found { - writer.write_event(Event::Start(BytesStart::new("dc:description")))?; - writer.write_event(Event::Text(BytesText::new(description)))?; - writer.write_event(Event::End(BytesEnd::new("dc:description")))?; + // Extra attributes are added just before the closing metadata tag. + if tag_data.name().as_ref() == b"metadata" { + // A new dc:identifier records the O'Reilly resource ID used to obtain the book. + let mut start_elem = BytesStart::new("dc:identifier"); + start_elem.push_attribute(("id", "oreilly-id")); + writer.write_event(Event::Start(start_elem))?; + let oreilly_id = &format!("urn:orm:book:{bookid}"); + writer.write_event(Event::Text(BytesText::new(oreilly_id)))?; + writer.write_event(Event::End(BytesEnd::new("dc:identifier")))?; + // Write description if end of metadata is reached without finding one. + if !desc_found { + writer.write_event(Event::Start(BytesStart::new("dc:description")))?; + writer.write_event(Event::Text(BytesText::new(description)))?; + writer.write_event(Event::End(BytesEnd::new("dc:description")))?; + } } // Pass through unmodified. writer.write_event(Event::End(tag_data))?; -- cgit v1.3.1