Skip to content

Commit

Permalink
remove PageValues Struct
Browse files Browse the repository at this point in the history
Signed-off-by: karthik2804 <[email protected]>
  • Loading branch information
karthik2804 committed Jul 15, 2023
1 parent 52c3341 commit 449fa08
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 32 deletions.
7 changes: 3 additions & 4 deletions src/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use chrono::{DateTime, Utc};
use pulldown_cmark as markdown;

use crate::rhai_engine::custom_rhai_engine_init;
use crate::template::PageValues;

use handlebars::Handlebars;

Expand Down Expand Up @@ -86,7 +85,7 @@ pub fn all_pages(
dir: PathBuf,
show_unpublished: bool,
skip_cache: bool,
) -> anyhow::Result<BTreeMap<String, PageValues>> {
) -> anyhow::Result<BTreeMap<String, Content>> {
if skip_cache {
let index_cache: IndexCache = all_pages_load(dir, show_unpublished)?;
return Ok(index_cache.contents);
Expand Down Expand Up @@ -163,7 +162,7 @@ pub fn all_pages(
}

pub struct IndexCache {
contents: BTreeMap<String, PageValues>,
contents: BTreeMap<String, Content>,
cache_expiration: Option<DateTime<Utc>>,
}

Expand All @@ -190,7 +189,7 @@ pub fn all_pages_load(dir: PathBuf, show_unpublished: bool) -> anyhow::Result<In
match raw_data.parse::<Content>() {
Ok(content) => {
if show_unpublished || content.published {
contents.insert(f.to_string_lossy().to_string(), content.into());
contents.insert(f.to_string_lossy().to_string(), content);
} else {
// find earliest unpublished article to save timestamp to refresh cache
let article_date = content.head.date;
Expand Down
36 changes: 8 additions & 28 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub struct SiteInfo {
#[derive(Serialize)]
pub struct TemplateContext {
request: BTreeMap<String, String>,
page: PageValues,
page: Content,
site: SiteValues,
/// A copy of the environment variables
///
Expand All @@ -69,26 +69,7 @@ pub struct TemplateContext {
#[derive(Serialize)]
pub struct SiteValues {
info: SiteInfo,
pages: BTreeMap<String, PageValues>,
}

/// The structured values sent to the template renderer.
/// The body should be legal HTML that can be inserted within the <body> tag.
#[derive(Serialize, Deserialize)]
pub struct PageValues {
pub head: Head,
pub body: String,
pub published: bool,
}

impl From<Content> for PageValues {
fn from(c: Content) -> Self {
PageValues {
body: c.body,
head: c.head,
published: c.published,
}
}
pages: BTreeMap<String, Content>,
}

/// Renderer can execute a handlebars template and render the results into HTML.
Expand Down Expand Up @@ -190,14 +171,13 @@ impl<'a> Renderer<'a> {
}

/// Given values and a site object, render a template.
pub fn render_template<T: Into<PageValues>>(
pub fn render_template(
&self,
values: T,
content: Content,
info: SiteInfo,
request: HeaderMap,
) -> anyhow::Result<String> {
let page: PageValues = values.into();
let tpl = page
let tpl = content
.head
.template
.clone()
Expand All @@ -210,7 +190,7 @@ impl<'a> Renderer<'a> {
}

let ctx = TemplateContext {
page,
page: content,
request: request_headers,
site: SiteValues {
// Right now, we literally include ALL OF THE CONTENT in its rendered
Expand Down Expand Up @@ -279,8 +259,8 @@ fn pages_helper(
///
/// It should be assumed that all data passed into this function will be visible to the
/// end user.
pub fn error_values(title: &str, msg: &str) -> PageValues {
PageValues {
pub fn error_values(title: &str, msg: &str) -> Content {
Content {
head: Head {
title: title.to_string(),
date: Some(chrono::Utc::now()),
Expand Down

0 comments on commit 449fa08

Please sign in to comment.