index must always be written, so write to it before exiting
This commit is contained in:
parent
0ff1d85535
commit
e4da3ce475
75
src/main.rs
75
src/main.rs
|
@ -45,7 +45,7 @@ fn main() -> Result<(), Error> {
|
||||||
}
|
}
|
||||||
let hash_body = read_to_string(hash_file.clone()).unwrap_or(String::new());
|
let hash_body = read_to_string(hash_file.clone()).unwrap_or(String::new());
|
||||||
let hash_body: HashFile = toml::from_str(&*hash_body).unwrap_or(HashFile{hashes: HashMap::new()});
|
let hash_body: HashFile = toml::from_str(&*hash_body).unwrap_or(HashFile{hashes: HashMap::new()});
|
||||||
let in_hashes: HashMap<String, [u8; 128]> = hash_body.hashes.iter().map(|(k, v)| {
|
let in_hashes: HashMap<String, [u8; 128]> = hash_body.hashes.par_iter().map(|(k, v)| {
|
||||||
let mut array = [0u8; 128];
|
let mut array = [0u8; 128];
|
||||||
let decoded = base64::decode(v).unwrap();
|
let decoded = base64::decode(v).unwrap();
|
||||||
let slice = decoded.as_slice();
|
let slice = decoded.as_slice();
|
||||||
|
@ -81,6 +81,7 @@ fn main() -> Result<(), Error> {
|
||||||
println!("Read configuration successfully! Converting markdown files to html...");
|
println!("Read configuration successfully! Converting markdown files to html...");
|
||||||
|
|
||||||
let mut posts: Vec<Post> = vec![];
|
let mut posts: Vec<Post> = vec![];
|
||||||
|
let mut posts_to_index: Vec<Post> = vec![];
|
||||||
let input_files: Vec<PathBuf> = fs::read_dir(cwd)?
|
let input_files: Vec<PathBuf> = fs::read_dir(cwd)?
|
||||||
.filter(|file| {
|
.filter(|file| {
|
||||||
let file = file.as_ref().unwrap();
|
let file = file.as_ref().unwrap();
|
||||||
|
@ -104,21 +105,55 @@ fn main() -> Result<(), Error> {
|
||||||
published: NaiveDateTime::parse_from_str(&*settings.published, &*config.time_format)
|
published: NaiveDateTime::parse_from_str(&*settings.published, &*config.time_format)
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
};
|
};
|
||||||
|
posts_to_index.push(post.clone());
|
||||||
if in_hashes.get(&*post.id).unwrap_or(&[0; 128]) != &hash || config_update {
|
if in_hashes.get(&*post.id).unwrap_or(&[0; 128]) != &hash || config_update {
|
||||||
out_hashes.insert(post.id.clone(), hash);
|
out_hashes.insert(post.id.clone(), hash);
|
||||||
posts.push(post);
|
posts.push(post.clone());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let mut write_hashes = in_hashes.clone();
|
let mut write_hashes = in_hashes.clone();
|
||||||
write_hashes.extend(out_hashes.iter().map(|(k, v)| {(k.clone(), v.clone())}));
|
write_hashes.par_extend(out_hashes.par_iter().map(|(k, v)| {(k.clone(), v.clone())}));
|
||||||
println!("Writing hash table...");
|
println!("Writing hash table...");
|
||||||
let toml = toml::to_string(&HashFile { hashes: write_hashes.iter().map(|(k, v)| {
|
let toml = toml::to_string(&HashFile { hashes: write_hashes.iter().map(|(k, v)| {
|
||||||
(k.clone(), base64::encode(v))
|
(k.clone(), base64::encode(v))
|
||||||
}).collect::<HashMap<String, String>>()}).unwrap();
|
}).collect::<HashMap<String, String>>()}).unwrap();
|
||||||
let mut file = File::create(hash_file)?;
|
let mut file = File::create(hash_file)?;
|
||||||
file.write_all(toml.as_bytes())?;
|
file.write_all(toml.as_bytes())?;
|
||||||
|
|
||||||
|
println!("Writing index...");
|
||||||
|
posts_to_index.par_sort_by(|a, b| b.published.cmp(&a.published));
|
||||||
|
println!(
|
||||||
|
"Found {} post lists",
|
||||||
|
index.select(r#"meta[typeset="index-entry""#).length()
|
||||||
|
);
|
||||||
|
for index_ref in index.select(r#"meta[typeset="index-entry""#).iter() {
|
||||||
|
let n = index_ref
|
||||||
|
.attr("content")
|
||||||
|
.unwrap_or("".to_tendril())
|
||||||
|
.parse()
|
||||||
|
.unwrap_or(0);
|
||||||
|
let parent = nth_parent(index_ref.clone(), n);
|
||||||
|
let parent_html = parent.html();
|
||||||
|
for post in posts_to_index.iter() {
|
||||||
|
let html = parent_html.to_string();
|
||||||
|
|
||||||
|
parent.parent().append_html(html);
|
||||||
|
nth_children(parent.parent(), n)
|
||||||
|
.select(r#"meta[typeset="index-entry""#)
|
||||||
|
.replace_with_html(
|
||||||
|
format!("<a href=\"./{}.html\">{}</a>\n", post.id, post.title).as_str(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
parent.parent().children().iter().nth(0).unwrap().remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
let output = index.html().to_string();
|
||||||
|
let path = out_path.join(PathBuf::from("index.html"));
|
||||||
|
let mut file = File::create(path)?;
|
||||||
|
file.write_all(output.as_bytes())?;
|
||||||
|
|
||||||
if out_hashes.len() == 0 {
|
if out_hashes.len() == 0 {
|
||||||
println!("Nothing to do");
|
println!("Have a nice day :)");
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
println!("Successfully converted to HTML! Creating {} document(s)...", posts.len());
|
println!("Successfully converted to HTML! Creating {} document(s)...", posts.len());
|
||||||
|
@ -134,7 +169,7 @@ fn main() -> Result<(), Error> {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.replace("$", post.title.as_str());
|
.replace("$", post.title.as_str());
|
||||||
|
|
||||||
element.replace_with_html(format!(r#"<title>{title}</title>"#));
|
element.replace_with_html(format!("<title>{}</title>", title));
|
||||||
}
|
}
|
||||||
"title" => {
|
"title" => {
|
||||||
element.replace_with_html(post.title.to_string());
|
element.replace_with_html(post.title.to_string());
|
||||||
|
@ -154,37 +189,7 @@ fn main() -> Result<(), Error> {
|
||||||
let mut file = File::create(path)?;
|
let mut file = File::create(path)?;
|
||||||
file.write_all(output.as_bytes())?;
|
file.write_all(output.as_bytes())?;
|
||||||
}
|
}
|
||||||
println!("Writing index...");
|
|
||||||
posts.par_sort_by(|a, b| b.published.cmp(&a.published));
|
|
||||||
println!(
|
|
||||||
"Found {} post lists",
|
|
||||||
index.select(r#"meta[typeset="index-entry""#).length()
|
|
||||||
);
|
|
||||||
for index_ref in index.select(r#"meta[typeset="index-entry""#).iter() {
|
|
||||||
let n = index_ref
|
|
||||||
.attr("content")
|
|
||||||
.unwrap_or("".to_tendril())
|
|
||||||
.parse()
|
|
||||||
.unwrap_or(0);
|
|
||||||
let parent = nth_parent(index_ref.clone(), n);
|
|
||||||
let parent_html = parent.html();
|
|
||||||
for post in posts.iter() {
|
|
||||||
let html = parent_html.to_string();
|
|
||||||
|
|
||||||
parent.parent().append_html(html);
|
|
||||||
nth_children(parent.parent(), n)
|
|
||||||
.select(r#"meta[typeset="index-entry""#)
|
|
||||||
.replace_with_html(
|
|
||||||
format!("<a href=\"./{}.html\">{}</a>\n", post.id, post.title).as_str(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
parent.parent().children().iter().nth(0).unwrap().remove();
|
|
||||||
}
|
|
||||||
|
|
||||||
let output = index.html().to_string();
|
|
||||||
let path = out_path.join(PathBuf::from("index.html"));
|
|
||||||
let mut file = File::create(path)?;
|
|
||||||
file.write_all(output.as_bytes())?;
|
|
||||||
println!("Have a nice day :)");
|
println!("Have a nice day :)");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue