-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactored codebase and added more links
- Loading branch information
Showing
7 changed files
with
102 additions
and
68 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use std::fs; | ||
|
||
use chrono::{TimeZone, Utc}; | ||
use sailfish::TemplateOnce; | ||
|
||
fn get_age() -> u8 { | ||
let utc = Utc; | ||
let dob = utc | ||
.with_ymd_and_hms(2001, 7, 30, 0, 0, 0) | ||
.single() | ||
.expect("Failed to parse birthday"); | ||
let age = Utc::now().signed_duration_since(dob).num_days() as f64 / 365.25; | ||
age as u8 | ||
} | ||
|
||
#[derive(TemplateOnce)] | ||
#[template(path = "index.stpl")] | ||
struct IndexPage { | ||
age: u8, | ||
title: &'static str, | ||
build_time: String, | ||
} | ||
|
||
pub fn build_index() -> std::io::Result<()> { | ||
let ctx = IndexPage { | ||
title: "Luke Carr", | ||
build_time: Utc::now().to_rfc3339(), | ||
age: get_age(), | ||
}; | ||
fs::write("out/index.html", ctx.render_once().unwrap()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use std::fs; | ||
|
||
use chrono::Utc; | ||
use sailfish::TemplateOnce; | ||
|
||
#[derive(TemplateOnce)] | ||
#[template(path = "links.stpl")] | ||
struct LinksPage { | ||
title: &'static str, | ||
build_time: String, | ||
links: Vec<Link>, | ||
} | ||
|
||
pub struct Link { | ||
pub href: &'static str, | ||
pub text: &'static str, | ||
} | ||
|
||
pub fn build_links(links: Vec<Link>) -> std::io::Result<()> { | ||
let ctx = LinksPage { | ||
title: "Links :: Luke Carr", | ||
build_time: Utc::now().to_rfc3339(), | ||
links, | ||
}; | ||
fs::write("out/links.html", ctx.render_once().unwrap()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,35 @@ | ||
use chrono::offset::Utc; | ||
use std::fs; | ||
use std::time::Instant; | ||
use chrono::TimeZone; | ||
use sailfish::TemplateOnce; | ||
|
||
fn main() { | ||
let now = Instant::now(); | ||
let build_time = Utc::now(); | ||
mod index; | ||
use index::build_index; | ||
mod links; | ||
use links::{build_links, Link}; | ||
mod robots; | ||
use robots::build_robots; | ||
|
||
fn main() { | ||
fs::create_dir_all("out").expect("Failed to create ouput directory"); | ||
|
||
let index = IndexPage { | ||
age: get_age(), | ||
title: "Luke Carr".to_owned(), | ||
build_time: build_time.to_rfc3339(), | ||
}; | ||
build_index(index).expect("Failed to build index page"); | ||
|
||
let links = LinksPage { | ||
title: "Links :: Luke Carr".to_owned(), | ||
build_time: build_time.to_rfc3339(), | ||
}; | ||
build_links(links).expect("Failed to build links page"); | ||
|
||
build_index().expect("Failed to build index page"); | ||
build_robots().expect("Failed to build robots.txt"); | ||
|
||
println!("Compiled site successfully in {:.1}ms!", (now.elapsed().as_nanos() as f64) / 1e6); | ||
} | ||
|
||
fn get_age() -> u8 { | ||
let utc = Utc; | ||
let dob = utc.ymd(2001, 7, 30); | ||
let age = Utc::today().signed_duration_since(dob).num_days() as f64 / 365.25; | ||
return age as u8; | ||
} | ||
|
||
#[derive(TemplateOnce)] | ||
#[template(path = "index.stpl")] | ||
struct IndexPage { | ||
age: u8, | ||
title: String, | ||
build_time: String, | ||
} | ||
|
||
fn build_index(ctx: IndexPage) -> std::io::Result<()> { | ||
fs::write("out/index.html", ctx.render_once().unwrap()) | ||
} | ||
|
||
#[derive(TemplateOnce)] | ||
#[template(path = "links.stpl")] | ||
struct LinksPage { | ||
title: String, | ||
build_time: String, | ||
} | ||
|
||
fn build_links(ctx: LinksPage) -> std::io::Result<()> { | ||
fs::write("out/links.html", ctx.render_once().unwrap()) | ||
} | ||
|
||
fn build_robots() -> std::io::Result<()> { | ||
fs::write("out/robots.txt", concat!("User-agent: *", "Allow: /")) | ||
let links = vec![ | ||
Link { | ||
href: "https://sr.ht/~carr", | ||
text: "Sourcehut (~carr)", | ||
}, | ||
Link { | ||
href: "https://github.com/lukecarr", | ||
text: "GitHub (lukecarr)", | ||
}, | ||
Link { | ||
href: "https://gitlab.com/lukecarr", | ||
text: "GitLab (lukecarr)", | ||
}, | ||
Link { | ||
href: "https://linkedin.com/in/luke-j-carr", | ||
text: "LinkedIn (luke-j-carr)", | ||
}, | ||
]; | ||
build_links(links).expect("Failed to build links page"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
use std::fs; | ||
|
||
pub fn build_robots() -> std::io::Result<()> { | ||
fs::write("out/robots.txt", concat!("User-agent: *", "Allow: /")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters