about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorgennyble <gen@nyble.dev>2024-05-03 10:43:53 -0500
committergennyble <gen@nyble.dev>2024-05-03 10:43:53 -0500
commitb0f6242b8b936d47b32c227cab5b18b4902cf9c4 (patch)
tree07645afa7286b3fa7f9555742a1ddba1bb6ba126 /src
parent71a9330b728c1b7d7688d3aac2c6b4e8adffb1b0 (diff)
downloadawake-b0f6242b8b936d47b32c227cab5b18b4902cf9c4.tar.gz
awake-b0f6242b8b936d47b32c227cab5b18b4902cf9c4.zip
opengraph embeds :)
Diffstat (limited to 'src')
-rw-r--r--src/fs.rs12
-rw-r--r--src/main.rs37
-rw-r--r--src/settings.rs1
3 files changed, 45 insertions, 5 deletions
diff --git a/src/fs.rs b/src/fs.rs
index 7dc94d3..831aa86 100644
--- a/src/fs.rs
+++ b/src/fs.rs
@@ -57,6 +57,18 @@ impl Webpath {
 	pub fn as_dir(&self) -> String {
 		format!("/{}/", self.webcanon)
 	}
+
+	/// Returns the first directory of the Webpath. Assumes the path is a
+	/// directory if it ends in a `/`, otherwise pops the last component.
+	pub fn first_dir(&self) -> String {
+		if self.is_dir {
+			self.webcanon.to_string()
+		} else {
+			let mut dir = self.webcanon.clone();
+			dir.pop();
+			dir.to_string()
+		}
+	}
 }
 
 impl Deref for Webpath {
diff --git a/src/main.rs b/src/main.rs
index 57c373e..0c61389 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -18,7 +18,7 @@ use axum::{
 	routing::get,
 	Extension, Router,
 };
-use bempline::{Document, Options};
+use bempline::{variables, Document, Options};
 use camino::Utf8PathBuf;
 use confindent::Confindent;
 pub use error::RuntimeError;
@@ -54,6 +54,7 @@ async fn main() {
 	let conf = Confindent::from_file(std::env::args().nth(2).unwrap()).unwrap();
 	let webroot: Utf8PathBuf = conf.child_parse("Webroot").unwrap();
 	let templates = conf.child_value("Templates").unwrap();
+	let hostname = conf.child_owned("Hostname").unwrap();
 
 	let fs = Filesystem::new(&webroot);
 
@@ -61,6 +62,7 @@ async fn main() {
 		template_dir: Utf8PathBuf::from(webroot.join(templates))
 			.canonicalize_utf8()
 			.unwrap(),
+		hostname,
 	};
 
 	let app = Router::new()
@@ -213,10 +215,35 @@ async fn send_template(
 	)
 	.unwrap();
 
-	template.set(
-		"title",
-		templated.frontmatter.get("title").unwrap_or(filename),
-	);
+	let title = templated.frontmatter.get("title").unwrap_or(filename);
+
+	template.set("title", title);
+
+	if let Some(og_description) = templated.frontmatter.get("description") {
+		let og_title = title;
+		let og_url = format!("https://{}{}", &settings.hostname, webpath);
+
+		if let Some(art_relpath) = templated.frontmatter.get("art") {
+			let serving_dir = Utf8PathBuf::from(webpath.first_dir());
+			let art_path = serving_dir.join(art_relpath);
+
+			let og_image_alt = match templated.frontmatter.get("art_alt") {
+				Some(alt) => alt,
+				None => {
+					tracing::warn!("{} has art but no alt", resolve.filepath);
+					""
+				}
+			};
+
+			let og_image = format!("https://{}/{}", &settings.hostname, art_path);
+
+			variables!(template, og_image, og_image_alt);
+		}
+
+		let og_site_name = &settings.hostname;
+
+		variables!(template, og_title, og_url, og_description, og_site_name);
+	}
 
 	// styles the templated stuff wants
 	let style_pattern = template.get_pattern("styles").unwrap();
diff --git a/src/settings.rs b/src/settings.rs
index 9a31d56..06abc18 100644
--- a/src/settings.rs
+++ b/src/settings.rs
@@ -3,4 +3,5 @@ use camino::Utf8PathBuf;
 #[derive(Clone, Debug)]
 pub struct Settings {
 	pub template_dir: Utf8PathBuf,
+	pub hostname: String,
 }