Very bad take on a list of recently updated chapters
parent
ea94977ec5
commit
e5a0ccdb53
|
@ -209,14 +209,16 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "chrono"
|
name = "chrono"
|
||||||
version = "0.4.26"
|
version = "0.4.31"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ec837a71355b28f6556dbd569b37b3f363091c0bd4b2e735674521b4c5fd9bc5"
|
checksum = "7f2c685bad3eb3d45a01354cedb7d5faa66194d1d58ba6e267a8de788f79db38"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"android-tzdata",
|
"android-tzdata",
|
||||||
"iana-time-zone",
|
"iana-time-zone",
|
||||||
|
"js-sys",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"winapi",
|
"wasm-bindgen",
|
||||||
|
"windows-targets 0.48.1",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -263,6 +265,12 @@ version = "1.0.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
|
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "concat-string"
|
||||||
|
version = "1.0.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7439becb5fafc780b6f4de382b1a7a3e70234afe783854a4702ee8adbb838609"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "core-foundation-sys"
|
name = "core-foundation-sys"
|
||||||
version = "0.8.4"
|
version = "0.8.4"
|
||||||
|
@ -898,7 +906,9 @@ dependencies = [
|
||||||
name = "mdbook-last-changed"
|
name = "mdbook-last-changed"
|
||||||
version = "0.1.4"
|
version = "0.1.4"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"chrono",
|
||||||
"clap",
|
"clap",
|
||||||
|
"concat-string",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
"log",
|
"log",
|
||||||
"mdbook",
|
"mdbook",
|
||||||
|
|
|
@ -15,3 +15,5 @@ clap = { version = "4.0.29", features = ["cargo"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
toml = "0.5.11"
|
toml = "0.5.11"
|
||||||
xshell = "0.2.2"
|
xshell = "0.2.2"
|
||||||
|
chrono = "0.4.31"
|
||||||
|
concat-string = "1.0.1"
|
87
src/lib.rs
87
src/lib.rs
|
@ -1,5 +1,9 @@
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
use chrono::NaiveDate;
|
||||||
|
use concat_string::concat_string;
|
||||||
use mdbook::book::{Book, BookItem, Chapter};
|
use mdbook::book::{Book, BookItem, Chapter};
|
||||||
use mdbook::errors::Result;
|
use mdbook::errors::Result;
|
||||||
use mdbook::preprocess::{Preprocessor, PreprocessorContext};
|
use mdbook::preprocess::{Preprocessor, PreprocessorContext};
|
||||||
|
@ -7,6 +11,12 @@ use xshell::{cmd, Shell};
|
||||||
|
|
||||||
pub struct LastChanged;
|
pub struct LastChanged;
|
||||||
|
|
||||||
|
#[derive(Debug, Eq, PartialEq, Hash)]
|
||||||
|
struct LiteLink {
|
||||||
|
name: String,
|
||||||
|
path: Option<PathBuf>,
|
||||||
|
}
|
||||||
|
|
||||||
impl Preprocessor for LastChanged {
|
impl Preprocessor for LastChanged {
|
||||||
fn name(&self) -> &str {
|
fn name(&self) -> &str {
|
||||||
"last-changed"
|
"last-changed"
|
||||||
|
@ -43,6 +53,7 @@ impl Preprocessor for LastChanged {
|
||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let mut map: HashMap<LiteLink, String> = HashMap::new();
|
||||||
let mut res = None;
|
let mut res = None;
|
||||||
book.for_each_mut(|item: &mut BookItem| {
|
book.for_each_mut(|item: &mut BookItem| {
|
||||||
if let Some(Err(_)) = res {
|
if let Some(Err(_)) = res {
|
||||||
|
@ -52,13 +63,53 @@ impl Preprocessor for LastChanged {
|
||||||
|
|
||||||
if let BookItem::Chapter(ref mut chapter) = *item {
|
if let BookItem::Chapter(ref mut chapter) = *item {
|
||||||
res = Some(
|
res = Some(
|
||||||
last_changed(&git_root, &src_root, repository_string, chapter).map(|md| {
|
last_changed(&git_root, &src_root, repository_string, chapter, &mut map).map(
|
||||||
chapter.content = md;
|
|md| {
|
||||||
}),
|
chapter.content = md;
|
||||||
|
},
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let mut vec: Vec<(LiteLink, NaiveDate)> = map
|
||||||
|
.into_iter()
|
||||||
|
.map(|e| (e.0, NaiveDate::from_str(&e.1).unwrap()))
|
||||||
|
.collect();
|
||||||
|
// Sorting alphabetically and then by date, this means same date entries will be sorted alphabetically, otherwise the end result will be randomized when truncating
|
||||||
|
vec.sort_by(|a, b| a.0.name.cmp(&b.0.name));
|
||||||
|
vec.sort_by(|a, b| b.1.cmp(&a.1));
|
||||||
|
vec.truncate(5);
|
||||||
|
|
||||||
|
let text: String = vec
|
||||||
|
.into_iter()
|
||||||
|
.map(|f| {
|
||||||
|
format!(
|
||||||
|
"<li><a href=\"{}\">{}</a></li>",
|
||||||
|
f.0.path.unwrap().display(),
|
||||||
|
f.0.name
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
let text = concat_string!("## Recently Updated:\n", "<ul>", text, "</ul>");
|
||||||
|
|
||||||
|
book.for_each_mut(|item: &mut BookItem| {
|
||||||
|
if let Some(Err(_)) = res {
|
||||||
|
log::trace!("Error when changing the book. Last result: {res:?}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let BookItem::Chapter(ref mut chapter) = *item {
|
||||||
|
if let Some(ch) = &chapter.number {
|
||||||
|
if ch.0[0] == 1 {
|
||||||
|
// let content = &chapter.content;
|
||||||
|
// chapter.content = format!("{}\n{}", text, content);
|
||||||
|
chapter.content = chapter.content.replace("⁂⁂⁂", &text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
res.unwrap_or(Ok(())).map(|_| book)
|
res.unwrap_or(Ok(())).map(|_| book)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -68,6 +119,7 @@ fn last_changed(
|
||||||
src_root: &Path,
|
src_root: &Path,
|
||||||
base_url: Option<&str>,
|
base_url: Option<&str>,
|
||||||
chapter: &mut Chapter,
|
chapter: &mut Chapter,
|
||||||
|
map: &mut HashMap<LiteLink, String>,
|
||||||
) -> Result<String> {
|
) -> Result<String> {
|
||||||
let content = &chapter.content;
|
let content = &chapter.content;
|
||||||
|
|
||||||
|
@ -95,18 +147,25 @@ fn last_changed(
|
||||||
|
|
||||||
let modification = get_last_modification(git_root, &path);
|
let modification = get_last_modification(git_root, &path);
|
||||||
let text = match modification {
|
let text = match modification {
|
||||||
Ok((date, commit)) => match base_url {
|
Ok((date, commit)) => {
|
||||||
Some(url) => {
|
let lite_link = LiteLink {
|
||||||
let url = format!("{}/commit/{}", url, commit);
|
name: chapter.name.clone(),
|
||||||
format!(
|
path: chapter.path.clone(),
|
||||||
"Last change: {}, commit: <a href=\"{}\">{}</a>",
|
};
|
||||||
date, url, commit
|
map.insert(lite_link, date.clone());
|
||||||
)
|
match base_url {
|
||||||
|
Some(url) => {
|
||||||
|
let url = format!("{}/commit/{}", url, commit);
|
||||||
|
format!(
|
||||||
|
"Last change: {}, commit: <a href=\"{}\">{}</a>",
|
||||||
|
date, url, commit
|
||||||
|
)
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
format!("Last change: {}", date)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
None => {
|
}
|
||||||
format!("Last change: {}", date)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::trace!("No modification found for {path:?}. Error: {e:?}");
|
log::trace!("No modification found for {path:?}. Error: {e:?}");
|
||||||
return Ok(content.into());
|
return Ok(content.into());
|
||||||
|
|
Loading…
Reference in New Issue