From 655cd12e1d6dc0b68695e888ab1919e0387ee0ce Mon Sep 17 00:00:00 2001 From: WriterV2 Date: Mon, 10 Apr 2023 13:21:07 +0200 Subject: Add last modified date automation --- docs/stories.html | 28 ++++++++++++++-------------- src/works/stories.rs | 21 +++++++++++++++++++++ stories.json | 9 +++------ 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/docs/stories.html b/docs/stories.html index 4d3894f..f724dd3 100644 --- a/docs/stories.html +++ b/docs/stories.html @@ -39,18 +39,6 @@ Despite my linguistic difficulties to express myself, my love for story telling
- -
-

The fallen dragons of Bavaria

-

In his new life in Germany Kazuyuki is needed by dragons.

-
-
- 3 Pages - English - Last Update: 2023-04-02 -
-
-

Das schweigende Spiegelbild

@@ -59,7 +47,7 @@ Despite my linguistic difficulties to express myself, my love for story telling
2 Pages German - Last Update: 2023-01-28 + Last Update: 2023-04-10
@@ -71,7 +59,19 @@ Despite my linguistic difficulties to express myself, my love for story telling
7 Pages German - Last Update: 2023-01-28 + Last Update: 2023-04-10 +
+ + + +
+

The fallen dragons of Bavaria

+

In his new life in Germany Kazuyuki is needed by dragons.

+
+
+ 3 Pages + English + Last Update: 2023-04-02
diff --git a/src/works/stories.rs b/src/works/stories.rs index 9db3ec3..c2b4b8f 100644 --- a/src/works/stories.rs +++ b/src/works/stories.rs @@ -15,6 +15,7 @@ struct Story { path_to_document: String, number_of_pages: u16, language: Language, + #[serde(default)] last_update: NaiveDate, } @@ -40,6 +41,16 @@ impl super::Works for Stories { } anyhow::Ok(()).with_context(|| "Failed to render single story pages") } + + // create stories from stories.json and add last modification date + fn new_from_file(file: std::fs::File) -> anyhow::Result { + let mut stories: Stories = serde_json::from_reader(std::io::BufReader::new(&file))?; + for story in stories.0.iter_mut() { + story.get_last_modified()?; + } + // add error context + Ok(stories) + } } impl Story { @@ -87,6 +98,16 @@ impl Story { let text = std::fs::read_to_string(self.get_content_html_path())?; anyhow::Ok(text).with_context(|| format!("Failed to extract text for {}", self.title)) } + + // get date of last modification of pdf file + fn get_last_modified(&mut self) -> anyhow::Result { + use std::os::linux::fs::MetadataExt; + let pdf_last_modification = std::fs::metadata(self.get_pdf_path())?.st_mtime(); + let html_last_modification = std::fs::metadata(self.get_html_path())?.st_mtime(); + let date = chrono::NaiveDateTime::from_timestamp(std::cmp::max(pdf_last_modification, html_last_modification), 0).date(); + self.last_update = date; + Ok(date) + } } // language of story content diff --git a/stories.json b/stories.json index 2fec76b..e9b10a9 100644 --- a/stories.json +++ b/stories.json @@ -4,23 +4,20 @@ "description": "Der gefährliche Zauber von Leon wird von seinem wegweisenden Freund durchschaut. Kann er ihn stoppen?", "path_to_document": "mit_gutem_gewissen.pdf", "number_of_pages": 7, - "language": "German", - "last_update": "2023-01-28" + "language": "German" }, { "title": "Das schweigende Spiegelbild", "description": "Das Spiegelbild verzweifelt in seiner Hilflosigkeit gegenüber seinem Ich.", "path_to_document": "das_schweigende_spiegelbild.pdf", "number_of_pages": 2, - "language": "German", - "last_update": "2023-01-28" + "language": "German" }, { "title": "The fallen dragons of Bavaria", "description": "In his new life in Germany Kazuyuki is needed by dragons.", "path_to_document": "the_fallen_dragons_of_bavaria.pdf", "number_of_pages": 3, - "language": "English", - "last_update": "2023-04-02" + "language": "English" } ] -- cgit v1.2.3