mod package;
mod xhtml;
use super::zip::{Compression, ZipWriter};
use super::{
Equations, FlowCtx, HtmlOptions, ImageStore, Mode, PageImage, WidthUnit, arxiv_abs_link, authors_html, doc_title,
is_abstract_text, plain, render_flow, side_figures,
};
use crate::analysis::pipeline::Analysis;
use crate::backend::PdfFile;
use crate::common::geom::Rect;
use crate::model::{Block, BlockKind, Document, ImageSpec, SUP};
use package::{BookInfo, CONTAINER, CSS_FILE, ContentDoc, NAV_FILE, NavEntry, nav_xhtml, package_opf, toc_ncx};
use std::collections::HashMap;
const CSS: &str = include_str!("epub.css");
const SPLIT: char = '\u{2}';
const IMAGE_DIR: &str = "images";
const FIXED_MODIFIED: &str = "2000-01-01T00:00:00Z";
const BYLINE: &str = "by ";
const NAME_MAX_WORDS_COUNT: usize = 12;
const NAME_PARTICLES: &[&str] = &[
"van", "von", "der", "den", "de", "del", "della", "di", "da", "du", "la", "le", "bin", "binti", "y", "and", "&",
"et", "al.",
];
const HEADING_ANCHOR_PREFIX: &str = "toc-";
#[derive(Debug, Clone)]
pub struct EpubOptions {
pub scale: f32,
pub jobs: usize,
pub equations: Equations,
pub max_math_width_em: f32,
pub cover: bool,
pub modified: Option<String>,
}
impl Default for EpubOptions {
fn default() -> Self {
Self {
scale: 3.0,
jobs: 0,
equations: Equations::MathMl,
max_math_width_em: 20.0,
cover: true,
modified: None,
}
}
}
pub fn render_epub(analysis: &Analysis, pdf: &PdfFile, opts: &EpubOptions) -> Vec<u8> {
let doc = &analysis.doc;
let html_opts = HtmlOptions {
mode: Mode::Friendly,
inline_images: false,
asset_dir: IMAGE_DIR.into(),
scale: opts.scale,
page_image: PageImage::Svg,
jobs: opts.jobs,
embed_fonts: false,
equations: opts.equations,
};
let mut images = ImageStore {
opts: &html_opts,
jobs: Vec::new(),
};
let blocks = book_blocks(doc, opts.max_math_width_em);
let chapters = split_chapters(&blocks);
let all: Vec<&Block> = blocks.iter().collect();
let ctx = FlowCtx {
unit: WidthUnit::EmFit {
pt_per_em: doc.body_size.max(1.0),
},
offprint: true,
covers: (None, None),
sides: side_figures(doc, &all),
math: opts.equations == Equations::MathMl,
};
let mut text = String::new();
for (i, chapter) in chapters.iter().enumerate() {
if i > 0 {
text.push(SPLIT);
}
text.push_str(&chapter_html(doc, chapter, &mut images, &ctx));
}
let cover = (opts.cover && !doc.pages.is_empty()).then(|| {
text.push(SPLIT);
text.push_str(&images.region(&cover_spec(doc), "cover"));
});
let out = images.finish(pdf, text);
let mut parts: Vec<&str> = out.html.split(SPLIT).collect();
let cover_file = cover.and_then(|()| parts.pop()).filter(|src| !src.is_empty());
let bodies: Vec<String> = parts.iter().map(|part| xhtml::from_html(part)).collect();
let image_files: Vec<String> = out.files.iter().map(|(name, _)| name.clone()).collect();
let book = Book {
doc,
chapters: &chapters,
bodies,
cover: cover_file,
image_files: &image_files,
modified: opts.modified.as_deref().unwrap_or(FIXED_MODIFIED),
};
book.package(&out.files)
}
struct Book<'a> {
doc: &'a Document,
chapters: &'a [Vec<&'a Block>],
bodies: Vec<String>,
cover: Option<&'a str>,
image_files: &'a [String],
modified: &'a str,
}
impl Book<'_> {
fn package(&self, files: &[(String, Vec<u8>)]) -> Vec<u8> {
let doc = self.doc;
let title = doc_title(doc);
let identifier = identifier(doc);
let authors = authors(doc);
let source = doc
.meta
.arxiv_id
.as_ref()
.map(|id| format!("https://arxiv.org/abs/{id}"));
let info = BookInfo {
identifier: &identifier,
title: &title,
authors: &authors,
source: source.as_deref(),
modified: self.modified,
};
let mut place = HashMap::new();
for (i, body) in self.bodies.iter().enumerate() {
for id in xhtml::ids(body) {
place.entry(id).or_insert(i);
}
}
let entries = self.nav_entries(&place);
let mut zip = ZipWriter::default();
zip.add("mimetype", b"application/epub+zip", Compression::Stored);
zip.add("META-INF/container.xml", CONTAINER.as_bytes(), Compression::Deflated);
let mut docs = Vec::new();
for (i, body) in self.bodies.iter().enumerate() {
let body = xhtml::relink(body, i, &place, file_name);
let doc_title = self.chapters[i]
.iter()
.find(|block| matches!(block.kind, BlockKind::Heading { .. }))
.map(|block| plain(&block.spans))
.unwrap_or_else(|| title.clone());
let page = content_xhtml(&doc_title, &body);
zip.add(
&format!("EPUB/{}", file_name(i)),
page.as_bytes(),
Compression::Deflated,
);
docs.push(ContentDoc {
file: file_name(i),
mathml: body.contains("<math "),
});
}
let opf = package_opf(&info, &docs, self.image_files, self.cover);
zip.add("EPUB/package.opf", opf.as_bytes(), Compression::Deflated);
zip.add(
&format!("EPUB/{NAV_FILE}"),
nav_xhtml(&title, &entries).as_bytes(),
Compression::Deflated,
);
zip.add(
"EPUB/toc.ncx",
toc_ncx(&info, &entries).as_bytes(),
Compression::Deflated,
);
zip.add(&format!("EPUB/{CSS_FILE}"), CSS.as_bytes(), Compression::Deflated);
for (name, data) in files {
zip.add(&format!("EPUB/{name}"), data, Compression::Stored);
}
zip.finish()
}
fn nav_entries(&self, place: &HashMap<String, usize>) -> Vec<NavEntry> {
let mut entries = Vec::new();
let starts_with_heading = self
.chapters
.first()
.and_then(|chapter| chapter.first())
.is_some_and(|block| matches!(block.kind, BlockKind::Heading { .. }));
if !starts_with_heading {
entries.push(NavEntry {
depth: 0,
label: doc_title(self.doc),
href: file_name(0),
});
}
let headings = self.chapters.iter().flatten().filter_map(|block| match block.kind {
BlockKind::Heading { level } => Some((level, *block)),
_ => None,
});
let top = headings.clone().map(|(level, _)| level).min().unwrap_or(1);
for (level, block) in headings {
let label = plain(&block.spans);
let Some(anchor) = block.anchor.as_ref().filter(|_| !label.is_empty()) else {
continue;
};
let Some(&doc) = place.get(&super::esc(anchor)) else {
continue;
};
let max_depth = entries.last().map_or(0, |entry: &NavEntry| entry.depth + 1);
entries.push(NavEntry {
depth: usize::from(level - top).min(max_depth),
label,
href: format!("{}#{anchor}", file_name(doc)),
});
}
entries
}
}
fn file_name(index: usize) -> String {
format!("c{:03}.xhtml", index + 1)
}
fn content_xhtml(title: &str, body: &str) -> String {
format!(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE html>\n\
<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\" lang=\"en\" xml:lang=\"en\">\n\
<head>\n<meta charset=\"utf-8\"/>\n<title>{}</title>\n<link rel=\"stylesheet\" type=\"text/css\" href=\"{CSS_FILE}\"/>\n</head>\n\
<body>\n{body}</body>\n</html>\n",
super::esc(title)
)
}
fn book_blocks(doc: &Document, max_math_width_em: f32) -> Vec<Block> {
doc.blocks
.iter()
.map(|block| {
let mut block = block.clone();
if matches!(block.kind, BlockKind::Heading { .. }) && block.anchor.is_none() {
block.anchor = Some(format!("{HEADING_ANCHOR_PREFIX}{}", block.id));
}
if let BlockKind::Equation { image, .. } = &block.kind
&& image.rect.width() > max_math_width_em * doc.body_size
{
block.math = None;
}
block
})
.collect()
}
fn split_chapters(blocks: &[Block]) -> Vec<Vec<&Block>> {
let starts = |block: &Block| match block.kind {
BlockKind::Heading { level } => (!is_abstract_text(&block.spans)).then_some(level),
_ => None,
};
let top = blocks.iter().filter_map(starts).min();
let mut chapters: Vec<Vec<&Block>> = vec![Vec::new()];
for block in blocks {
let current = chapters.last_mut().expect("at least one chapter");
if top.is_some() && starts(block) == top && !current.is_empty() {
chapters.push(vec![block]);
} else {
current.push(block);
}
}
chapters
}
fn chapter_html(doc: &Document, blocks: &[&Block], images: &mut ImageStore<'_>, ctx: &FlowCtx) -> String {
fn run_kind(block: &Block) -> u8 {
match block.kind {
BlockKind::Title => 0,
BlockKind::Authors => 1,
_ => 2,
}
}
let mut out = String::new();
for run in blocks.chunk_by(|a, b| run_kind(a) == run_kind(b)) {
match run_kind(run[0]) {
0 => {
let parts: Vec<String> = run.iter().map(|block| super::spans_html(&block.spans)).collect();
out.push_str(&format!("<h1 class=\"title\">{}</h1>\n", parts.join(" ")));
let link = arxiv_abs_link(doc);
if !link.is_empty() {
out.push_str(&format!("<p class=\"source\">{link}</p>\n"));
}
}
1 => out.push_str(&authors_html(run)),
_ => out.push_str(&render_flow(run, images, ctx)),
}
}
out
}
fn cover_spec(doc: &Document) -> ImageSpec {
let page = doc.pages[0];
ImageSpec {
page: 0,
rect: Rect {
x0: 0.0,
y0: 0.0,
x1: page.width,
y1: page.height,
},
transparent: false,
}
}
fn authors(doc: &Document) -> Vec<String> {
doc.blocks
.iter()
.filter(|block| matches!(block.kind, BlockKind::Authors))
.filter_map(|block| block.rows.first())
.map(|row| {
let spans: Vec<_> = row.iter().filter(|span| span.style & SUP == 0).cloned().collect();
let name = plain(&spans);
let name = name.trim_end_matches([',', '*', 'â ', 'âĄ']).trim();
match name.get(..BYLINE.len()) {
Some(head) if head.eq_ignore_ascii_case(BYLINE) => name[BYLINE.len()..].trim().to_string(),
_ => name.to_string(),
}
})
.filter(|name| looks_like_name(name))
.collect()
}
fn looks_like_name(text: &str) -> bool {
let words: Vec<&str> = text.split_whitespace().collect();
!words.is_empty()
&& words.len() <= NAME_MAX_WORDS_COUNT
&& !text.contains(|ch: char| ch.is_ascii_digit() || ch == '@' || ch == '/')
&& words.iter().all(|word| {
word.starts_with(|ch: char| !ch.is_lowercase()) || NAME_PARTICLES.contains(&word.trim_end_matches(','))
})
}
fn identifier(doc: &Document) -> String {
let key = format!(
"{}\n{}\n{}",
doc_title(doc),
doc.meta.arxiv_id.as_deref().unwrap_or_default(),
doc.pages.len()
);
let words: Vec<u32> = (0u8..4)
.map(|salt| {
let mut hasher = crc32fast::Hasher::new();
hasher.update(&[salt]);
hasher.update(key.as_bytes());
hasher.finalize()
})
.collect();
let (time_hi, clock) = ((words[1] & 0x0fff) | 0x4000, ((words[2] >> 16) & 0x3fff) | 0x8000);
format!(
"urn:uuid:{:08x}-{:04x}-{time_hi:04x}-{clock:04x}-{:04x}{:08x}",
words[0],
words[1] >> 16,
words[2] & 0xffff,
words[3]
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn byline_is_not_part_of_the_author() {
let block = |text: &str| Block {
kind: BlockKind::Authors,
rows: vec![vec![crate::model::Span::plain(text)]],
..empty_block()
};
let doc = Document {
blocks: vec![block("By Jane Austen"), block("Bysshe Shelley"), block("Ada Lovelace*")],
..empty_doc()
};
assert_eq!(authors(&doc), ["Jane Austen", "Bysshe Shelley", "Ada Lovelace"]);
for not_a_name in [
"How to Think Like a Computer Scientist",
"2nd Edition, Version 2.4.1",
"The third edition of Think Python is available at thinkpython.com",
] {
assert!(!looks_like_name(not_a_name), "{not_a_name}");
}
for name in [
"Ludwig van Beethoven",
"Aidan N. Gomez",
"Ćukasz Kaiser",
"LIGO Scientific Collaboration",
"B. P. Abbott et al.",
"Shaoqing Ren, Kaiming He, Ross Girshick, and Jian Sun",
"Kaiming He Xiangyu Zhang Shaoqing Ren Jian Sun",
] {
assert!(looks_like_name(name), "{name}");
}
}
fn empty_doc() -> Document {
Document {
meta: Default::default(),
pages: Vec::new(),
body_size: 10.0,
leading: 12.0,
body_font: String::new(),
math_font: String::new(),
blocks: Vec::new(),
}
}
fn empty_block() -> Block {
Block {
id: 0,
kind: BlockKind::Paragraph { in_abstract: false },
page: 0,
bbox: Rect::default(),
spans: Vec::new(),
rows: Vec::new(),
anchor: None,
loc: Default::default(),
lines: Vec::new(),
line_texts: Vec::new(),
hlines: Vec::new(),
table: None,
inner_lines: Vec::new(),
inner_spans: Vec::new(),
eq_runs: Vec::new(),
math: None,
}
}
#[test]
fn identifier_is_a_uuid() {
let doc = Document {
meta: Default::default(),
pages: Vec::new(),
body_size: 10.0,
leading: 12.0,
body_font: String::new(),
math_font: String::new(),
blocks: Vec::new(),
};
let id = identifier(&doc);
let uuid = id.strip_prefix("urn:uuid:").unwrap();
let groups: Vec<usize> = uuid.split('-').map(str::len).collect();
assert_eq!(groups, [8, 4, 4, 4, 12]);
assert_eq!(&uuid[14..15], "4");
assert_eq!(identifier(&doc), id);
}
}