implement post page rendering

This commit is contained in:
Peter Cai 2020-04-10 21:10:30 +08:00
parent 1110a2614b
commit bbcb48beeb
No known key found for this signature in database
GPG Key ID: 71F5FB4E4F3FD54F
3 changed files with 49 additions and 1 deletions

View File

@ -148,7 +148,7 @@ async fn default_route(_req: Request, url: Url) -> MyResult<Response> {
} else {
// TODO: Actually render the page...
return Response::new_with_opt_str_and_init(
Some(&blog::PostContentCache::find_or_render(&post).await.content),
Some(&render::render_post(post).await?),
ResponseInit::new()
.status(200)
.headers(headers!{

View File

@ -60,6 +60,15 @@ struct HomePageContext {
next: Option<String>
}
#[derive(Serialize)]
struct PostContext {
blog: &'static BlogRootContext,
title: String,
url: String,
timestamp: u64,
content: String
}
lazy_static! {
static ref THEME_CONFIG: serde_json::Value = serde_json::from_str(
include_str!("../theme_config.json")).unwrap();
@ -157,3 +166,18 @@ pub async fn render_homepage(url: Url) -> MyResult<String> {
hbs.render("home.hbs", &context)
.map_err(|e| Error::BadRequest(format!("{:#?}", e)))
}
pub async fn render_post(post: blog::Post) -> MyResult<String> {
let hbs = build_handlebars();
let post_cache = blog::PostContentCache::find_or_render(&post).await;
let context = PostContext {
blog: &ROOT_CONTEXT,
title: post.title,
url: post.url,
timestamp: post.timestamp,
content: post_cache.content
};
hbs.render("post.hbs", &context)
.map_err(|e| Error::BadRequest(format!("{:#?}", e)))
}

24
theme/default/post.hbs Normal file
View File

@ -0,0 +1,24 @@
<html lang="{{ blog.lang }}">
<head>
<meta charset="UTF-8">
<title>{{ title }} - {{ blog.title }}</title>
<link rel="stylesheet" href="/static/style.css?ver={{ build_num 0 }}"/>
</head>
<body>
<div class="page-wrapper">
{{> sidebar.hbs }}
<article class="content">
<h1>{{ title }}</h1>
<span class="date">{{ format_date timestamp "%e %b, %Y" }}</span>
{{{ content }}}
</article>
</div>
<div class="toc-wrapper hidden">
<div class="toc">
<!-- This will be generated dynamically by JavaScript -->
<!-- We don't handle this in backend -->
</div>
</div>
<script src="/static/script.js?ver={{ build_num 0 }}"></script>
</body>
</html>