refactor all the things
This commit is contained in:
parent
1c702242d6
commit
b1ef76e2de
14
Cargo.lock
generated
14
Cargo.lock
generated
@ -759,13 +759,7 @@ dependencies = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rustc-demangle"
|
name = "rust-ssg"
|
||||||
version = "0.1.23"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "rustylinks"
|
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
@ -775,6 +769,12 @@ dependencies = [
|
|||||||
"serde_yaml",
|
"serde_yaml",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rustc-demangle"
|
||||||
|
version = "0.1.23"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ryu"
|
name = "ryu"
|
||||||
version = "1.0.15"
|
version = "1.0.15"
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "rustylinks"
|
name = "rust-ssg"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
|
40
src/file_utils.rs
Normal file
40
src/file_utils.rs
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
use std::path::Path;
|
||||||
|
use std::{fs, io};
|
||||||
|
|
||||||
|
pub fn copy_recursively(source: impl AsRef<Path>, destination: impl AsRef<Path>) -> io::Result<()> {
|
||||||
|
// Credit: https://nick.groenen.me/notes/recursively-copy-files-in-rust/
|
||||||
|
fs::create_dir_all(&destination)?;
|
||||||
|
for entry in fs::read_dir(source)? {
|
||||||
|
let entry = entry?;
|
||||||
|
let filetype = entry.file_type()?;
|
||||||
|
if filetype.is_dir() {
|
||||||
|
copy_recursively(entry.path(), destination.as_ref().join(entry.file_name()))?;
|
||||||
|
} else {
|
||||||
|
fs::copy(entry.path(), destination.as_ref().join(entry.file_name()))?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write_file(html: String) {
|
||||||
|
// copy all files in static to public
|
||||||
|
if Path::new("./public/links").exists() {
|
||||||
|
copy_recursively("./static", "./public/links").expect("Could not copy static directory");
|
||||||
|
}
|
||||||
|
fs::write("./public/links/index.html", html).expect("Could not write to index.html");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_dirs() {
|
||||||
|
if !Path::new("./public").exists() {
|
||||||
|
fs::create_dir("./public").expect("Could not create public directory");
|
||||||
|
}
|
||||||
|
if !Path::new("./public/links").exists() {
|
||||||
|
fs::create_dir("./public/links").expect("Could not create public directory");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn delete_public_dir() {
|
||||||
|
if Path::new("./public").exists() {
|
||||||
|
fs::remove_dir_all("./public").expect("could not remove directory");
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,7 @@
|
|||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use serde_yaml::{self};
|
||||||
|
use minijinja::Environment;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct RustyLinks {
|
pub struct RustyLinks {
|
||||||
@ -36,3 +39,26 @@ pub struct Link {
|
|||||||
copy: Option<String>,
|
copy: Option<String>,
|
||||||
rels: Option<String>,
|
rels: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn load_links(file_name: &str) -> RustyLinks {
|
||||||
|
let links_yaml = std::fs::File::open(file_name).expect("Could not find file");
|
||||||
|
|
||||||
|
let mut rusty_links: RustyLinks = serde_yaml::from_reader(links_yaml).expect("Could not read values");
|
||||||
|
|
||||||
|
// Set last updated time to now.
|
||||||
|
let now: DateTime<Utc> = Utc::now();
|
||||||
|
let metadata: MetaData = MetaData { last_updated: now.to_rfc2822() };
|
||||||
|
rusty_links.metadata = Some(metadata);
|
||||||
|
|
||||||
|
rusty_links
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn render_links(rusty_links: RustyLinks) -> String {
|
||||||
|
let main = std::fs::read_to_string("templates/links/links.html").expect("Could not find links.html");
|
||||||
|
|
||||||
|
let mut env = Environment::new();
|
||||||
|
env.add_template("main", &*main).unwrap();
|
||||||
|
let tmpl = env.get_template("main").unwrap();
|
||||||
|
tmpl.render(rusty_links).unwrap()
|
||||||
|
}
|
||||||
|
80
src/main.rs
80
src/main.rs
@ -1,75 +1,15 @@
|
|||||||
mod links;
|
mod links;
|
||||||
|
mod file_utils;
|
||||||
|
use crate::links::{load_links,render_links};
|
||||||
|
|
||||||
use links::{RustyLinks, MetaData};
|
|
||||||
|
|
||||||
use std::{fs, io};
|
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
use chrono::{DateTime, Utc};
|
|
||||||
use serde_yaml::{self};
|
|
||||||
use minijinja::{Environment};
|
|
||||||
|
|
||||||
|
|
||||||
pub fn copy_recursively(source: impl AsRef<Path>, destination: impl AsRef<Path>) -> io::Result<()> {
|
|
||||||
// Credit: https://nick.groenen.me/notes/recursively-copy-files-in-rust/
|
|
||||||
fs::create_dir_all(&destination)?;
|
|
||||||
for entry in fs::read_dir(source)? {
|
|
||||||
let entry = entry?;
|
|
||||||
let filetype = entry.file_type()?;
|
|
||||||
if filetype.is_dir() {
|
|
||||||
copy_recursively(entry.path(), destination.as_ref().join(entry.file_name()))?;
|
|
||||||
} else {
|
|
||||||
fs::copy(entry.path(), destination.as_ref().join(entry.file_name()))?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn load_links(file_name: &str) -> RustyLinks {
|
|
||||||
let links_yaml = std::fs::File::open(file_name).expect("Could not find file");
|
|
||||||
|
|
||||||
let mut rusty_links: RustyLinks = serde_yaml::from_reader(links_yaml).expect("Could not read values");
|
|
||||||
|
|
||||||
// Set last updated time to now.
|
|
||||||
let now: DateTime<Utc> = Utc::now();
|
|
||||||
let metadata: MetaData = MetaData { last_updated: now.to_rfc2822() };
|
|
||||||
rusty_links.metadata = Some(metadata);
|
|
||||||
|
|
||||||
rusty_links
|
|
||||||
}
|
|
||||||
|
|
||||||
fn render_links(rusty_links: RustyLinks) -> String {
|
|
||||||
let main = std::fs::read_to_string("templates/links/links.html").expect("Could not find links.html");
|
|
||||||
|
|
||||||
let mut env = Environment::new();
|
|
||||||
env.add_template("main", &*main).unwrap();
|
|
||||||
let tmpl = env.get_template("main").unwrap();
|
|
||||||
tmpl.render(rusty_links).unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write_file(html: String) {
|
|
||||||
if !Path::new("./public").exists() {
|
|
||||||
fs::create_dir("./public").expect("Could not create public directory");
|
|
||||||
}
|
|
||||||
if !Path::new("./public/links").exists() {
|
|
||||||
fs::create_dir("./public/links").expect("Could not create public directory");
|
|
||||||
}
|
|
||||||
// copy all files in static to public
|
|
||||||
if Path::new("./public/links").exists() {
|
|
||||||
copy_recursively("./static", "./public/links").expect("Could not copy static directory");
|
|
||||||
}
|
|
||||||
fs::write("./public/links/index.html", html).expect("Could not write to index.html");
|
|
||||||
}
|
|
||||||
|
|
||||||
fn delete_public_dir() {
|
|
||||||
if Path::new("./public").exists() {
|
|
||||||
fs::remove_dir_all("./public").expect("could not remove directory");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Write Links
|
file_utils::delete_public_dir();
|
||||||
delete_public_dir();
|
file_utils::create_dirs();
|
||||||
write_file(render_links(load_links("data/links/links.yaml")));
|
|
||||||
|
// Generate and Write Links
|
||||||
|
let links_input = load_links("data/links/links.yaml");
|
||||||
|
let links_output = render_links(links_input);
|
||||||
|
|
||||||
|
file_utils::write_file(links_output);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user