Skip to content

Commit

Permalink
feat: refactored codebase and added more links
Browse files Browse the repository at this point in the history
  • Loading branch information
lukecarr committed Jan 2, 2023
1 parent 4735f05 commit 6c92606
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 68 deletions.
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ name = "www-carr-sh"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
chrono = "0.4.22"
sailfish = "0.4.0"
chrono = "0.4"
sailfish = "0.5"
31 changes: 31 additions & 0 deletions src/index.rs
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())
}
26 changes: 26 additions & 0 deletions src/links.rs
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())
}
82 changes: 27 additions & 55 deletions src/main.rs
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");
}
5 changes: 5 additions & 0 deletions src/robots.rs
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: /"))
}
4 changes: 3 additions & 1 deletion templates/links.stpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
<% include!("./header.stpl"); %>
<h2>links</h2>
<ul>
<li><a href="https://sr.ht/~carr" target="_blank" rel="noopener">Sourcehut (~carr)</a></li>
<% for link in &links { %>
<li><a href="<%= link.href %>" target="_blank" rel="noopener"><%= link.text %></a></li>
<% } %>
</ul>
<% include!("./footer.stpl"); %>
</body>
Expand Down

0 comments on commit 6c92606

Please sign in to comment.