|
| 1 | +//! Book list and detail page generation. |
| 2 | +
|
| 3 | +use super::SiteGenerator; |
| 4 | +use crate::models::{Book, BookStatus, StatisticsData}; |
| 5 | +use crate::statistics::BookStatistics; |
| 6 | +use crate::templates::{IndexTemplate, BookTemplate, BookMarkdownTemplate}; |
| 7 | +use anyhow::Result; |
| 8 | +use askama::Template; |
| 9 | +use log::info; |
| 10 | +use std::fs; |
| 11 | + |
| 12 | +impl SiteGenerator { |
| 13 | + pub(crate) async fn generate_book_list(&self, books: &[Book], recap_latest_href: Option<String>) -> Result<()> { |
| 14 | + info!("Generating book list page..."); |
| 15 | + |
| 16 | + let mut reading_books = Vec::new(); |
| 17 | + let mut completed_books = Vec::new(); |
| 18 | + let mut abandoned_books = Vec::new(); |
| 19 | + let mut unread_books = Vec::new(); |
| 20 | + |
| 21 | + for book in books { |
| 22 | + match book.status() { |
| 23 | + BookStatus::Reading => reading_books.push(book.clone()), |
| 24 | + BookStatus::Complete => completed_books.push(book.clone()), |
| 25 | + BookStatus::Abandoned => abandoned_books.push(book.clone()), |
| 26 | + BookStatus::Unknown => { |
| 27 | + // If it has KoReader metadata, add to reading; otherwise check if we should include unread |
| 28 | + if book.koreader_metadata.is_some() { |
| 29 | + reading_books.push(book.clone()); |
| 30 | + } else if self.include_unread { |
| 31 | + unread_books.push(book.clone()); |
| 32 | + } |
| 33 | + // If include_unread is false, we skip books without metadata |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // Sort by title |
| 39 | + reading_books.sort_by(|a, b| a.epub_info.title.cmp(&b.epub_info.title)); |
| 40 | + completed_books.sort_by(|a, b| a.epub_info.title.cmp(&b.epub_info.title)); |
| 41 | + abandoned_books.sort_by(|a, b| a.epub_info.title.cmp(&b.epub_info.title)); |
| 42 | + unread_books.sort_by(|a, b| a.epub_info.title.cmp(&b.epub_info.title)); |
| 43 | + |
| 44 | + // ------------------------------------------------------------------ |
| 45 | + // Generate books manifest JSON categorized by reading status. |
| 46 | + // NOTE: This manifest is not consumed by the frontend code – it is |
| 47 | + // generated purely for the convenience of users who may want a |
| 48 | + // machine-readable list of all books and their export paths. |
| 49 | + // ------------------------------------------------------------------ |
| 50 | + |
| 51 | + use serde_json::json; |
| 52 | + |
| 53 | + let to_manifest_entry = |b: &Book| { |
| 54 | + json!({ |
| 55 | + "id": b.id.clone(), |
| 56 | + "title": b.epub_info.title.clone(), |
| 57 | + "authors": b.epub_info.authors.clone(), |
| 58 | + "json_path": format!("/books/{}/details.json", b.id), |
| 59 | + "html_path": format!("/books/{}/index.html", b.id), |
| 60 | + }) |
| 61 | + }; |
| 62 | + |
| 63 | + let reading_json: Vec<_> = reading_books.iter().map(to_manifest_entry).collect(); |
| 64 | + let completed_json: Vec<_> = completed_books.iter().map(to_manifest_entry).collect(); |
| 65 | + let abandoned_json: Vec<_> = abandoned_books.iter().map(to_manifest_entry).collect(); |
| 66 | + let new_json: Vec<_> = unread_books.iter().map(to_manifest_entry).collect(); |
| 67 | + |
| 68 | + let manifest = json!({ |
| 69 | + "reading": reading_json, |
| 70 | + "completed": completed_json, |
| 71 | + "abandoned": abandoned_json, |
| 72 | + "new": new_json, |
| 73 | + "generated_at": self.get_last_updated(), |
| 74 | + }); |
| 75 | + |
| 76 | + fs::write( |
| 77 | + self.books_dir().join("list.json"), |
| 78 | + serde_json::to_string_pretty(&manifest)?, |
| 79 | + )?; |
| 80 | + |
| 81 | + // ------------------------------------------------------------------ |
| 82 | + // Render book list HTML |
| 83 | + // ------------------------------------------------------------------ |
| 84 | + |
| 85 | + let template = IndexTemplate { |
| 86 | + site_title: self.site_title.clone(), |
| 87 | + reading_books, |
| 88 | + completed_books, |
| 89 | + abandoned_books, |
| 90 | + unread_books, |
| 91 | + version: self.get_version(), |
| 92 | + last_updated: self.get_last_updated(), |
| 93 | + navbar_items: self.create_navbar_items_with_recap("books", recap_latest_href.as_deref()), |
| 94 | + }; |
| 95 | + |
| 96 | + let html = template.render()?; |
| 97 | + self.write_minify_html(self.output_dir.join("index.html"), &html)?; |
| 98 | + |
| 99 | + Ok(()) |
| 100 | + } |
| 101 | + |
| 102 | + pub(crate) async fn generate_book_pages(&self, books: &[Book], stats_data: &mut Option<StatisticsData>, recap_latest_href: Option<String>) -> Result<()> { |
| 103 | + info!("Generating book detail pages..."); |
| 104 | + |
| 105 | + for book in books { |
| 106 | + // Try to find matching statistics by MD5 |
| 107 | + let book_stats = stats_data.as_ref().and_then(|stats| { |
| 108 | + // Try to match using the partial_md5_checksum from KoReader metadata |
| 109 | + book.koreader_metadata |
| 110 | + .as_ref() |
| 111 | + .and_then(|metadata| metadata.partial_md5_checksum.as_ref()) |
| 112 | + .and_then(|md5| stats.stats_by_md5.get(md5)) |
| 113 | + .cloned() |
| 114 | + }); |
| 115 | + |
| 116 | + // Calculate session statistics if we have book stats |
| 117 | + let session_stats = match (stats_data.as_ref(), &book_stats) { |
| 118 | + (Some(stats), Some(book_stat)) => Some(book_stat.calculate_session_stats(&stats.page_stats, &self.time_config)), |
| 119 | + _ => None, |
| 120 | + }; |
| 121 | + |
| 122 | + let template = BookTemplate { |
| 123 | + site_title: self.site_title.clone(), |
| 124 | + book: book.clone(), |
| 125 | + book_stats: book_stats.clone(), |
| 126 | + session_stats: session_stats.clone(), |
| 127 | + version: self.get_version(), |
| 128 | + last_updated: self.get_last_updated(), |
| 129 | + navbar_items: self.create_navbar_items_with_recap("books", recap_latest_href.as_deref()), |
| 130 | + }; |
| 131 | + |
| 132 | + let html = template.render()?; |
| 133 | + let book_dir = self.books_dir().join(&book.id); |
| 134 | + fs::create_dir_all(&book_dir)?; |
| 135 | + let book_path = book_dir.join("index.html"); |
| 136 | + self.write_minify_html(book_path, &html)?; |
| 137 | + |
| 138 | + // Generate Markdown export |
| 139 | + let md_template = BookMarkdownTemplate { |
| 140 | + book: book.clone(), |
| 141 | + book_stats: book_stats.clone(), |
| 142 | + session_stats: session_stats.clone(), |
| 143 | + version: self.get_version(), |
| 144 | + last_updated: self.get_last_updated(), |
| 145 | + }; |
| 146 | + let markdown = md_template.render()?; |
| 147 | + fs::write(book_dir.join("details.md"), markdown)?; |
| 148 | + |
| 149 | + // Generate JSON export / not used by the frontend code - only for the user's convenience |
| 150 | + let json_data = serde_json::json!({ |
| 151 | + "book": { |
| 152 | + "title": book.epub_info.title, |
| 153 | + "authors": book.epub_info.authors, |
| 154 | + "series": book.series_display(), |
| 155 | + "language": book.language(), |
| 156 | + "publisher": book.publisher(), |
| 157 | + "description": book.epub_info.sanitized_description(), |
| 158 | + "rating": book.rating(), |
| 159 | + "review_note": book.review_note(), |
| 160 | + "status": book.status().to_string(), |
| 161 | + "progress_percentage": book.progress_percentage(), |
| 162 | + "subjects": book.subjects(), |
| 163 | + "identifiers": book.identifiers().iter().map(|id| { |
| 164 | + serde_json::json!({ |
| 165 | + "scheme": id.scheme, |
| 166 | + "value": id.value, |
| 167 | + "display_scheme": id.display_scheme(), |
| 168 | + "url": id.url() |
| 169 | + }) |
| 170 | + }).collect::<Vec<_>>() |
| 171 | + }, |
| 172 | + "annotations": book.koreader_metadata.as_ref().map(|m| &m.annotations).unwrap_or(&vec![]), |
| 173 | + "statistics": { |
| 174 | + "book_stats": book_stats, |
| 175 | + "session_stats": session_stats, |
| 176 | + "completions": book_stats.as_ref().and_then(|stats| stats.completions.as_ref()) |
| 177 | + }, |
| 178 | + "export_info": { |
| 179 | + "generated_by": "KoShelf", |
| 180 | + "version": self.get_version(), |
| 181 | + "generated_at": self.get_last_updated() |
| 182 | + } |
| 183 | + }); |
| 184 | + let json_str = serde_json::to_string_pretty(&json_data)?; |
| 185 | + fs::write(book_dir.join("details.json"), json_str)?; |
| 186 | + } |
| 187 | + |
| 188 | + Ok(()) |
| 189 | + } |
| 190 | +} |
0 commit comments