remove intermediate template and cache rendered markdown directly instead

This commit is contained in:
slonkazoid 2024-05-02 20:17:46 +03:00
parent ad2a8c6ba4
commit 8678758440
Signed by: slonk
SSH key fingerprint: SHA256:tbZfJX4IOvZ0LGWOWu5Ijo8jfMPi78TU7x1VoEeCIjM
5 changed files with 56 additions and 72 deletions

View file

@ -59,8 +59,8 @@ struct IndexTemplate {
} }
#[derive(Template)] #[derive(Template)]
#[template(path = "view_post.html")] #[template(path = "post.html")]
struct ViewPostTemplate { struct PostTemplate {
meta: PostMetadata, meta: PostMetadata,
rendered: String, rendered: String,
rendered_in: RenderStats, rendered_in: RenderStats,
@ -176,7 +176,7 @@ async fn post(State(state): State<ArcState>, Path(name): Path<String>) -> AppRes
Ok(([("content-type", "text/plain")], buf).into_response()) Ok(([("content-type", "text/plain")], buf).into_response())
} else { } else {
let post = state.posts.get_post(&name).await?; let post = state.posts.get_post(&name).await?;
let page = ViewPostTemplate { let page = PostTemplate {
meta: post.0, meta: post.0,
rendered: post.1, rendered: post.1,
rendered_in: post.2, rendered_in: post.2,

View file

@ -8,7 +8,7 @@ use crate::config::RenderConfig;
use crate::post::PostMetadata; use crate::post::PostMetadata;
/// do not persist cache if this version number changed /// do not persist cache if this version number changed
pub const CACHE_VERSION: u16 = 1; pub const CACHE_VERSION: u16 = 2;
#[derive(Serialize, Deserialize, Clone)] #[derive(Serialize, Deserialize, Clone)]
pub struct CacheValue { pub struct CacheValue {

View file

@ -5,7 +5,6 @@ use std::io;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::time::{Duration, Instant, SystemTime}; use std::time::{Duration, Instant, SystemTime};
use askama::Template;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use fronma::parser::{parse, ParsedData}; use fronma::parser::{parse, ParsedData};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -63,14 +62,6 @@ pub struct PostMetadata {
pub tags: Vec<String>, pub tags: Vec<String>,
} }
use crate::filters;
#[derive(Template)]
#[template(path = "post.html")]
struct Post<'a> {
pub meta: &'a PostMetadata,
pub rendered_markdown: String,
}
#[allow(unused)] #[allow(unused)]
pub enum RenderStats { pub enum RenderStats {
Cached(Duration), Cached(Duration),
@ -127,12 +118,7 @@ impl PostManager {
let parsing = parsing_start.elapsed(); let parsing = parsing_start.elapsed();
let before_render = Instant::now(); let before_render = Instant::now();
let rendered_markdown = render(body, &self.config); let post = render(body, &self.config);
let post = Post {
meta: &metadata,
rendered_markdown,
}
.render()?;
let rendering = before_render.elapsed(); let rendering = before_render.elapsed();
if let Some(cache) = self.cache.as_ref() { if let Some(cache) = self.cache.as_ref() {

View file

@ -1,16 +1,52 @@
{%- import "macros.askama" as macros -%} {%- import "macros.askama" as macros -%}
<h1 class="post-title"> <!DOCTYPE html>
{{ meta.title }} <html lang="en">
<span class="post-author">- by {{ meta.author }}</span> <head>
</h1> <head>
<p class="post-desc">{{ meta.description }}</p> <meta charset="UTF-8" />
<p> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- prettier-ignore --> <meta name="description" content="{{ meta.title }}" />
<div> <meta property="og:title" content="{{ meta.title }}" />
{% call macros::table(meta) %} <meta property="og:description" content="{{ meta.description }}" />
</div> {% match meta.icon %} {% when Some with (url) %}
<a href="/posts/{{ meta.name }}">link</a><br /> <meta property="og:image" content="{{ url }}" />
<a href="/">back to home</a> <link rel="shortcut icon" href="{{ url }}" />
</p> {% when None %} {% endmatch %}
<hr /> <title>{{ meta.title }}</title>
{{ rendered_markdown|escape("none") }} <link rel="stylesheet" href="/static/style.css" />
<link rel="stylesheet" href="/static/post.css" />
</head>
</head>
<body>
<main>
<h1 class="post-title">
{{ meta.title }}
<span class="post-author">- by {{ meta.author }}</span>
</h1>
<p class="post-desc">{{ meta.description }}</p>
<div class="" post>
<!-- prettier-ignore -->
<div>
{% call macros::table(meta) %}
</div>
<a href="/posts/{{ meta.name }}">link</a><br />
<a href="/">back to home</a>
</div>
<hr />
{{ rendered|escape("none") }}
</main>
<!-- prettier-ignore -->
<footer>
{% match rendered_in %}
{% when RenderStats::ParsedAndRendered(total, parsing, rendering) %}
<span class="tooltipped" title="parsing took {{ parsing|duration }}">parsed</span> and
<span class="tooltipped" title="rendering took {{ rendering|duration }}">rendered</span> in {{ total|duration }}
{% when RenderStats::Cached(total) %}
retrieved from cache in {{ total|duration }}
{% endmatch %}
{% if markdown_access %}
- <a href="/posts/{{ meta.name }}.md">view raw</a>
{% endif %}
</footer>
</body>
</html>

View file

@ -1,38 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<meta name="description" content="{{ meta.title }}" />
<meta property="og:title" content="{{ meta.title }}" />
<meta property="og:description" content="{{ meta.description }}" />
{% match meta.icon %} {% when Some with (url) %}
<meta property="og:image" content="{{ url }}" />
<link rel="shortcut icon" href="{{ url }}" />
{% when None %} {% endmatch %}
<title>{{ meta.title }}</title>
<link rel="stylesheet" href="/static/style.css" />
<link rel="stylesheet" href="/static/post.css" />
</head>
</head>
<body>
<main>{{ rendered|escape("none") }}</main>
<!-- prettier-ignore -->
<footer>
{% match rendered_in %}
{% when RenderStats::ParsedAndRendered(total, parsing, rendering) %}
<span class="tooltipped" title="parsing took {{ parsing|duration }}">parsed</span> and
<span class="tooltipped" title="rendering took {{ rendering|duration }}">rendered</span> in {{ total|duration }}
{% when RenderStats::Cached(total) %}
retrieved from cache in {{ total|duration }}
{% endmatch %}
{% if markdown_access %}
- <a href="/posts/{{ meta.name }}.md">view raw</a>
{% endif %}
</footer>
</body>
</html>