credence_lib/render/
default_preparer.rs

1use super::{super::util::*, context::*, preparer::*};
2
3use {axum::http::*, compris::normal::*, std::result::Result};
4
5/// Default [RenderPreparer].
6///
7/// If the following variables are not already set, will set them:
8///
9/// * `content`: will render it
10/// * `title`: will extract it from the `content` (the first heading)
11/// * `created`: will use the annotation if set
12/// * `updated`: will use the annotation if set
13/// * `up`: will use the original URI path's parent if it is not "/"
14pub struct DefaultRenderedPageHandler;
15
16impl RenderPreparer for DefaultRenderedPageHandler {
17    async fn prepare<'own>(&self, context: &mut RenderContext<'own>) -> Result<(), StatusCode> {
18        context.renderer.clone().prepare(context).await?;
19
20        if let Some(socket) = &context.socket {
21            context.variables.insert("socket".into(), socket.into());
22        }
23
24        if !context.variables.contains_key("title") {
25            let title = match context.variables.get("content") {
26                Some(content) => match content {
27                    Value::Text(content) => context.renderer.title_from_content(&content.value)?,
28                    _ => None,
29                },
30                None => None,
31            }
32            .unwrap_or("".into());
33
34            context.variables.insert("title".into(), title.into());
35        }
36
37        if !context.variables.contains_key("created") {
38            if let Some(created) = &context.rendered_page.annotations.created {
39                let created = created.value.timestamp();
40                context.variables.insert("created".into(), created.into());
41            }
42        }
43
44        if !context.variables.contains_key("updated") {
45            if let Some(updated) = &context.rendered_page.annotations.updated {
46                let updated = updated.value.timestamp();
47                context.variables.insert("updated".into(), updated.into());
48            }
49        }
50
51        if !context.variables.contains_key("up") {
52            if let Some(original_path) = &context.original_uri_path {
53                if original_path != PATH_SEPARATOR_STRING {
54                    let up = uri_path_parent(original_path);
55                    context.variables.insert("up".into(), up.into());
56                }
57            }
58        }
59
60        Ok(())
61    }
62}