Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 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 Expand Up @@ -300,6 +299,7 @@ fn translate_relative_links(event: markdown::Event, path_info: String) -> markdo

/// The envelope for a page's content
/// The head contains front matter, while the body is the markdown body of the document.
#[derive(Serialize, Deserialize)]
pub struct Content {
/// The front matter for this content.
pub head: Head,
Expand Down
44 changes: 16 additions & 28 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use {
use crate::rhai_engine::custom_rhai_engine_init;

use super::content::{Content, Head};
use handlebars::handlebars_helper;
use serde::{Deserialize, Serialize};

/// The name of the default template.
Expand Down Expand Up @@ -46,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 @@ -68,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(mut c: Content) -> Self {
PageValues {
body: c.render_markdown(&None),
head: c.head,
published: c.published,
}
}
pages: BTreeMap<String, Content>,
}

/// Renderer can execute a handlebars template and render the results into HTML.
Expand All @@ -102,6 +84,11 @@ pub struct Renderer<'a> {
handlebars: handlebars::Handlebars<'a>,
}

handlebars_helper!(render_markdown: |content: Content|{
let mut content = content;
content.render_markdown(&None)
});

#[cfg(feature = "server")]
impl<'a> Renderer<'a> {
/// Create a new renderer with the necessary directories attached.
Expand Down Expand Up @@ -136,6 +123,8 @@ impl<'a> Renderer<'a> {
pub fn load_template_dir(&mut self) -> Result<(), anyhow::Error> {
#[cfg(feature = "server")]
self.register_helpers();
self.handlebars
.register_helper("render_markdown", Box::new(render_markdown));

// If there is a theme, load the templates provided by it first
// Allows for user defined templates to take precedence
Expand Down Expand Up @@ -182,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 @@ -202,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 @@ -271,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