1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
use chrono::NaiveDate;
use serde::{Deserialize, Serialize};
// stories and their information
// derive from JSON
// process to context for HTML
#[derive(Debug, Deserialize, Serialize)]
pub struct Stories(Vec<Story>);
#[derive(Debug, Deserialize, Serialize)]
struct Story {
title: String,
description: String,
path_to_document: String,
number_of_pages: u16,
language: Language,
last_update: NaiveDate,
}
// see src/works/mod.rs for information of Works trait
impl super::Works for Stories {
// render single pages for every story with the extracted text and a download button
fn render_single_pages(
&self,
tera_instance: &tera::Tera,
template_name: &str,
) -> anyhow::Result<()> {
let mut result = Vec::new();
for story in self.0.iter() {
if story.update_available()? {
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(())
}
}
impl Story {
// check if it's necessary to re-compile the html single page
fn update_available(&self) -> anyhow::Result<bool> {
Ok(
// pdf document changes
std::fs::metadata(format!("docs/stories/{}", &self.path_to_document))?.modified()?
> std::fs::metadata(self.get_html_path()?)?.modified()?
// base template changes
|| std::fs::metadata("templates/base.html")?.modified()?
> std::fs::metadata(self.get_html_path()?)?.modified()?
// story template changes
||std::fs::metadata("templates/story.html")?.modified()? > std::fs::metadata(self.get_html_path()?)?.modified()?
// HTML file doesn't exist yet
|| !std::path::Path::new(&self.get_html_path()?).exists(),
)
}
// use story document name for the single page
// e.g. mit_gutem_gewissen.pdf -> mit_gutem_gewissen.html
fn get_html_path(&self) -> anyhow::Result<String> {
Ok(format!(
"docs/stories/{}.html",
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
#[derive(Deserialize, Serialize, Debug)]
pub enum Language {
German,
English,
}
|