Skip to content

Commit

Permalink
Merge pull request #168 from karthik2804/alternate_dynamic_templating
Browse files Browse the repository at this point in the history
alternate approach to dynamic templating
  • Loading branch information
karthik2804 authored May 30, 2023
2 parents 321267a + a7ec7a1 commit 1440aa7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
54 changes: 26 additions & 28 deletions src/bartholomew.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::content;
use crate::response::{self, *};
use crate::template::{self, DynamicTemplateParams};
use crate::template::{self};
use anyhow::{anyhow, Result};
use spin_sdk::{
http::{Request, Response},
http_component,
};
use std::collections::HashMap;

use std::path::PathBuf;

/// The entry point to Bartholomew.
Expand Down Expand Up @@ -43,24 +43,6 @@ pub fn render(req: Request) -> Result<Response> {

// Load the site config.
let mut config: template::SiteInfo = toml::from_slice(&std::fs::read(CONFIG_FILE)?)?;
let dynamic_templates = config
.dynamic_templates
.clone()
.unwrap_or_default()
.into_iter()
.map(|t| {
(
t.dynamic_content_url,
DynamicTemplateParams {
dynamic_content_path: t.dynamic_content_path,
dynamic_template_name: t.dynamic_template_name,
},
)
})
.collect::<HashMap<_, _>>();

// check if path exists in dynamic templates list
let dynamic_template_params = dynamic_templates.get(&path_info as &str);

let base_url = std::env::var(BASE_URL_ENV);

Expand Down Expand Up @@ -98,17 +80,37 @@ pub fn render(req: Request) -> Result<Response> {
engine.load_script_dir()?;

// Load the content.
let content_path = match dynamic_template_params {
Some(val) => content::content_path(PathBuf::from(CONTENT_PATH), &val.dynamic_content_path),
None => content::content_path(PathBuf::from(CONTENT_PATH), &path_info),
};
let content_path = content::content_path(PathBuf::from(CONTENT_PATH), &path_info);

eprintln!("Path {}", content_path.to_string_lossy());

match std::fs::read_to_string(&content_path) {
Ok(full_document) => {
let mut doc: content::Content = full_document.parse()?;

// If a dynamic content source if specified get the body from that file instead
if let Some(val) = &doc.head.body_source {
match std::fs::read_to_string(content::content_path(
PathBuf::from(CONTENT_PATH),
val,
)) {
Ok(alternate_content) => {
let new_content: content::Content = alternate_content.parse()?;
doc.body = new_content.body;
}
Err(err) => {
eprintln!("Failed to read body source: {err}");
let err_vals = template::error_values(
"Not Found",
"The requested page was not found.",
);
let body =
engine.render_template(err_vals, config, req.headers().to_owned())?;
return response::not_found(path_info, body);
}
}
}

// Hide unpublished content unless PREVIEW_MODE is on.
if !doc.published && !preview_mode {
eprintln!(
Expand Down Expand Up @@ -139,10 +141,6 @@ pub fn render(req: Request) -> Result<Response> {
.clone()
.unwrap_or_else(|| DEFAULT_CONTENT_TYPE.to_owned());

if let Some(val) = dynamic_template_params {
doc.head.template = Some(val.dynamic_template_name.to_owned());
}

let data = engine
.render_template(doc, config, req.headers().to_owned())
.map_err(|e| anyhow!("Rendering {:?}: {}", &content_path, e))?;
Expand Down
3 changes: 3 additions & 0 deletions src/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ pub struct Head {
/// This provides the path info the file
/// If empty, the path info is filled in using the request url
pub path_info: Option<String>,
/// This provides the path from which to dynamicaly load content for the body
/// If empty, the body of the current file is used.
pub body_source: Option<String>,
/// A map of string/string pairs that are user-customizable.
pub extra: Option<HashMap<String, String>>,
}
Expand Down
1 change: 1 addition & 0 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ pub fn error_values(title: &str, msg: &str) -> PageValues {
redirect: None,
enable_shortcodes: None,
path_info: None,
body_source: None,
},
body: msg.to_string(),
published: true,
Expand Down

0 comments on commit 1440aa7

Please sign in to comment.