summaryrefslogtreecommitdiff
path: root/src/works/stories.rs
diff options
context:
space:
mode:
authorWriterV2 <primary-v2@protonmail.com>2023-02-11 19:38:14 +0100
committerWriterV2 <primary-v2@protonmail.com>2023-02-11 19:38:14 +0100
commite4fe6e056a0c7b707ee0520780eab3e41d3e6d94 (patch)
tree253b80a856f52c5e0fdd8b854fb48c82c9a6aeb6 /src/works/stories.rs
parentdeda52fe10fffe5eb4701566c5b0ada5402c675d (diff)
Add extracted text from story document to single page
Diffstat (limited to 'src/works/stories.rs')
-rw-r--r--src/works/stories.rs37
1 files changed, 30 insertions, 7 deletions
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<String> {
+ // 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