From e4fe6e056a0c7b707ee0520780eab3e41d3e6d94 Mon Sep 17 00:00:00 2001 From: WriterV2 Date: Sat, 11 Feb 2023 19:38:14 +0100 Subject: Add extracted text from story document to single page --- src/works/stories.rs | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/works/stories.rs b/src/works/stories.rs index 22ad1dc..092b721 100644 --- a/src/works/stories.rs +++ b/src/works/stories.rs @@ -25,15 +25,17 @@ impl super::Works for Stories { tera_instance: &tera::Tera, template_name: &str, ) -> anyhow::Result<()> { - // TODO: Extract story pdf text let mut result = Vec::new(); for story in self.0.iter() { - let mut context = tera::Context::new(); - context.insert("story", story); - result.push(std::fs::write( - story.get_html_path()?.as_str(), - tera_instance.render(format!("{}.html", template_name).as_str(), &context)?, - )); + if story.document_modified()? { + let mut context = tera::Context::new(); + context.insert("story", story); + context.insert("text", &story.get_text()?); + result.push(std::fs::write( + story.get_html_path()?.as_str(), + tera_instance.render(format!("{}.html", template_name).as_str(), &context)?, + )); + } } Ok(()) } @@ -56,6 +58,27 @@ impl Story { self.path_to_document.replace(".pdf", "") )) } + + // extract text from pdf document + fn get_text(&self) -> anyhow::Result { + // TODO: + // - Clean and optimise + // - Find a solution where you can use 1-2 digit numbers in the texts + // - You should add linebreaks between paragraphs + let mut text = + pdf_extract::extract_text(format!("docs/stories/{}", &self.path_to_document))?; + let re = regex::Regex::new(r"\d{1,2}")?; + let re2 = regex::Regex::new(r"-\d{1,2}\n+ ?")?; + let re3 = regex::Regex::new(r"-\n+")?; + let re4 = regex::Regex::new(r"\n\n")?; + let re5 = regex::Regex::new(r"\n")?; + text = re2.replace_all(&text, "").to_string(); + text = re.replace_all(&text, "").to_string(); + text = re3.replace_all(&text, "").to_string(); + text = re4.replace_all(&text, " ").to_string(); + text = re5.replace_all(&text, "").to_string(); + Ok(text) + } } // language of story content -- cgit v1.2.3