feat: render push objects

This commit is contained in:
Aravinth Manivannan 2024-12-11 17:31:02 +05:30
parent 9d0efee9aa
commit fffaedaeb3
Signed by: realaravinth
GPG key ID: F8F50389936984FF
6 changed files with 117 additions and 28 deletions

View file

@ -1,11 +1,15 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use std::cell::RefCell;
use derive_builder::*;
use derive_more::*;
use serde::*;
use tera::Context;
use url::Url;
use super::utils::*;
pub use super::*;
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize, Builder, Clone)]
@ -18,3 +22,67 @@ pub struct Post {
pub post_content: String,
pub post_html_url: Url,
}
pub const POST_PAGE: TemplateFile = TemplateFile::new("post_page", "post_page.html");
pub const POST_SEGMENT: TemplateFile = TemplateFile::new("post_segment.html", "post_segment.html");
pub const POSTS_SEGMENT: TemplateFile =
TemplateFile::new("posts_segment.html", "posts_segment.html");
pub fn register_templates(tera: &mut tera::Tera) {
for t in [POST_SEGMENT, POSTS_SEGMENT, POST_PAGE].iter() {
t.register(tera).unwrap();
}
}
pub struct PostPage {
ctx: RefCell<Context>,
}
impl PostPage {
pub fn new(p: &Post) -> Self {
let mut ctx = context();
ctx.insert(PAYLOAD_KEY, p);
let ctx = RefCell::new(ctx);
Self { ctx }
}
pub fn render(&self) -> String {
TEMPLATES
.render(POST_PAGE.name, &self.ctx.borrow())
.unwrap()
}
pub fn page(p: &Post) -> String {
let p = Self::new(p);
p.render()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn render_page_and_write() {
const FILE: &str = "./tmp/post_page.html";
let post = PostBuilder::default()
.author_name("Batman".into())
.author_preferred_username("@batman@dc.com".into())
.author_html_url(Url::parse("https://batman.dc.com").unwrap())
.post_published("Now".into())
.post_content("I'm batman".into())
.post_html_url(Url::parse("https://batman.dc.com/posts/1").unwrap())
.build()
.unwrap();
let tw = TestWriterBuilder::default()
.file(FILE.into())
.contents(PostPage::page(&post))
.build()
.unwrap();
tw.write();
}
}

View file

@ -46,8 +46,6 @@ impl TemplateFile {
pub const PAYLOAD_KEY: &str = "payload";
pub const POST_SEGMENT: TemplateFile = TemplateFile::new("post_segment.html", "post_segment.html");
pub const NAV_SEGMENT: TemplateFile = TemplateFile::new("nav_segment.html", "nav_segment.html");
pub const TAILWIND_CONFIG: TemplateFile =
@ -61,13 +59,15 @@ pub const TAILWIND_CONFIG: TemplateFile =
lazy_static! {
pub static ref TEMPLATES: Tera = {
let mut tera = Tera::default();
for t in [POST_SEGMENT, NAV_SEGMENT, TAILWIND_CONFIG].iter() {
for t in [NAV_SEGMENT, TAILWIND_CONFIG].iter() {
t.register(&mut tera).unwrap();
}
// errors::register_templates(&mut tera);
tera.autoescape_on(vec![".html", ".sql"]);
crate::person_homepage::register_templates(&mut tera);
crate::person_followers::register_templates(&mut tera);
crate::posts::register_templates(&mut tera);
crate::commits::register_templates(&mut tera);
// auth::register_templates(&mut tera);
// gists::register_templates(&mut tera);
tera

View file

@ -41,6 +41,6 @@
</div>
{% if payload.posts %} {% set posts = payload.posts %} {% include
"post_segment.html" %} {% endif %}
"posts_segment.html" %} {% endif %}
</body>
</html>

View file

@ -0,0 +1,20 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title> Post | {{ payload.author_name }} </title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-backdrop">
{% include "tailwind_config.html" %} {% include "nav_segment.html" %}
<section class="w-2/4 m-auto mt-9 items-center">
{% set post = payload %}
{% include "post_segment.html" %}
</section>
</body>
</html>

View file

@ -1,24 +1,20 @@
<section class="w-2/4 m-auto mt-9 items-center">
{% for post in posts %}
<div class="m-auto w-full bg-lightest p-2 rounded-md mb-3">
<div class="flex flex-row items-center">
<span class="">
<a
class="text-2xl text-darkest font-bold w-full"
href="{{ post.author_html_url }}"
>
{{ post.author_name }}</a
>
<p class="text-sm text-dark mt-1 w-full">
{{ post.author_preferred_username }}
</p>
</span>
<span class="grow"></span>
<p class="text-sm text-dark">{{ post.post_published }}</p>
</div>
<div class="mt-3">
<p class="text-lg text-darkest">{{ post.post_content }}</p>
</div>
</div>
{% endfor %}
</section>
<div class="m-auto w-full bg-lightest p-2 rounded-md mb-3">
<div class="flex flex-row items-center">
<span class="">
<a
class="text-2xl text-darkest font-bold w-full"
href="{{ post.author_html_url }}"
>
{{ post.author_name }}</a
>
<p class="text-sm text-dark mt-1 w-full">
{{ post.author_preferred_username }}
</p>
</span>
<span class="grow"></span>
<p class="text-sm text-dark">{{ post.post_published }}</p>
</div>
<div class="mt-3">
<p class="text-lg text-darkest">{{ post.post_content }}</p>
</div>
</div>

View file

@ -0,0 +1,5 @@
<section class="w-2/4 m-auto mt-9 items-center">
{% for post in posts %}
{% include "post_segment.html" %}
{% endfor %}
</section>