diff --git a/cli/src/lib.rs b/cli/src/lib.rs index c3d8282..b73b7f7 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -4,6 +4,7 @@ pub mod meta; pub mod outline; pub mod project; pub mod render; +pub mod search; pub mod theme; pub mod utils; pub mod version; diff --git a/cli/src/project.rs b/cli/src/project.rs index 1ef74c3..68870ad 100644 --- a/cli/src/project.rs +++ b/cli/src/project.rs @@ -405,22 +405,28 @@ impl Project { } pub fn compile_chapter(&mut self, _ch: DataDict, path: &str) -> ZResult { - let rel_data_path = std::path::Path::new(&self.path_to_root) + let file_name = std::path::Path::new(&self.path_to_root) .join(path) - .with_extension("") - .to_str() - .ok_or_else(|| error_once!("path_to_root is not a valid utf-8 string"))? - // windows - .replace('\\', "/"); + .with_extension(""); // todo: description for single document - let mut description = "".to_owned(); + let mut full_digest = "".to_owned(); if self.need_compile() { let doc = self.tr.compile_page(Path::new(path))?; - description = self.tr.generate_desc(&doc)?; + full_digest = self.tr.generate_desc(&doc)?; } + let description = match full_digest.char_indices().nth(512) { + Some((idx, _)) => full_digest[..idx].to_owned(), + None => full_digest, + }; + + let rel_data_path = file_name + .to_str() + .ok_or_else(|| error_once!("path_to_root is not a valid utf-8 string"))? + // windows + .replace('\\', "/"); - let dynamic_load_trampoline = self + let content = self .hr .handlebars .render( @@ -434,45 +440,44 @@ impl Project { ))?; Ok(ChapterArtifact { - content: dynamic_load_trampoline.to_owned(), - description: escape_str::( - &description.chars().take(512).collect::(), - ) - .into_owned(), + content, + description, }) } pub fn render_chapter(&mut self, chapter_data: DataDict, path: &str) -> ZResult { let instant = std::time::Instant::now(); - log::info!("rendering chapter {}", path); - // println!("RC = {:?}", rc); + + let file_path = std::path::Path::new(&self.path_to_root) + .join(path) + .with_extension(""); + + log::info!("rendering chapter {path}"); + + // Compiles the chapter + let art = self.compile_chapter(chapter_data, path)?; + self.index_search(&art.description, &file_path.with_extension("html")); + + // Creates the data to inject in the template let data = serde_json::to_value(self.book_meta.clone()) .map_err(map_string_err("render_chapter,convert_to"))?; let mut data: DataDict = serde_json::from_value(data) .map_err(map_string_err("render_chapter,convert_to"))?; - // inject chapters - data.insert("chapters".to_owned(), json!(self.chapters)); - + // Injects module path let renderer_module = format!("{}internal/typst_ts_renderer_bg.wasm", self.path_to_root); data.insert("renderer_module".to_owned(), json!(renderer_module)); - // inject content - - let art = self.compile_chapter(chapter_data, path)?; + // Injects description + let desc = escape_str::(&art.description).into_owned(); + data.insert("description".to_owned(), serde_json::Value::String(desc)); - // inject content - data.insert( - "description".to_owned(), - serde_json::Value::String(art.description), - ); + data.insert("chapters".to_owned(), json!(self.chapters)); data.insert("content".to_owned(), serde_json::Value::String(art.content)); - - // inject path_to_root data.insert("path_to_root".to_owned(), json!(self.path_to_root)); let index_html = self.hr.render_index(data, path); - log::info!("rendering chapter {} in {:?}", path, instant.elapsed()); + log::info!("rendering chapter {path} in {:?}", instant.elapsed()); Ok(index_html) } diff --git a/cli/src/search.rs b/cli/src/search.rs index e69de29..b405f45 100644 --- a/cli/src/search.rs +++ b/cli/src/search.rs @@ -0,0 +1,10 @@ +use std::path::Path; + +use crate::project::Project; + +impl Project { + pub fn index_search(&self, description: &str, page_path: &Path) { + let _ = description; + let _ = page_path; + } +}