Skip to content

Commit

Permalink
Generate index page
Browse files Browse the repository at this point in the history
  • Loading branch information
raffomania committed Jul 9, 2023
1 parent 91f36fa commit ca927a3
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 49 deletions.
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,11 @@ pub fn run(config: Config) -> Result<()> {
)]
let total_pages = (f64::from(total_posts) / f64::from(page_size)).ceil() as usize;
for (page, page_posts) in posts_to_render.chunks(page_size.try_into()?).enumerate() {
render::page(page_posts, page, total_pages)?;
render::listing(page_posts, page, total_pages)?;
}

render::index()?;

Ok(())
}

Expand Down
21 changes: 17 additions & 4 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ pub struct Post {
}

#[derive(Template)]
#[template(path = "index.jinja")]
struct IndexTemplate<'a> {
#[template(path = "listing.jinja")]
struct ListingTemplate<'a> {
posts: &'a [Post],
page: usize,
last_page: usize,
next_page: Option<usize>,
previous_page: Option<usize>,
}

pub fn page(posts: &[Post], page: usize, total_pages: usize) -> Result<()> {
pub fn listing(posts: &[Post], page: usize, total_pages: usize) -> Result<()> {
let last_page = total_pages - 1;
let next_page = (page < last_page).then(|| page + 1);
let previous_page = (page > 0).then(|| page - 1);
let template = IndexTemplate {
let template = ListingTemplate {
posts,
page,
last_page,
Expand Down Expand Up @@ -60,3 +60,16 @@ pub fn post(post: &Post) -> Result<()> {

Ok(())
}

#[derive(Template)]
#[template(path = "index.jinja")]
struct IndexTemplate;

pub fn index() -> Result<()> {
let template = IndexTemplate {};

let output = template.render()?;
std::fs::write("output/index.html", output)?;

Ok(())
}
46 changes: 2 additions & 44 deletions templates/index.jinja
Original file line number Diff line number Diff line change
@@ -1,46 +1,4 @@
{% extends "layout.jinja" %}

{% block content %}

{% include "nav.jinja" %}

{% for post in posts %}
<div class="mb-8 prose break-words">
<a href="/posts/{{post.id}}.html" class="no-underline">
<h3>{{ post.title }}</h3>
</a>
<p>{% include "post_text.jinja" %}</p>
<p class="text-sm flex justify-between">
<a href="/posts/{{post.id}}.html">{{ post.real_num_comments }}
Answers</a>
<span class="text-gray-500">{{post.created_at.format("%F")}}</span>
</p>
</div>
{% endfor %}

<div class="flex justify-center">
{% if let Some(previous_page) = previous_page %}
<a class="text-gray-700 px-2" href="/pages/{{previous_page}}.html">⯇</a>
{% else %}
<span class="text-gray-400 px-2 cursor-default">⯇</span>
{% endif %}

<span class="font-bold px-1">{{page}}</span>
<span>/</span>
<a class="px-1" href="/pages/{{last_page}}.html">{{last_page}}</a>

{% if let Some(next_page) = next_page %}
<a class="text-gray-700 px-2" href="/pages/{{next_page}}.html">⯈</a>
{% else %}
<span class="text-gray-400 px-2 cursor-default">⯈</span>
{% endif %}

</div>

{% if page > 1 %}
<p class="text-center mt-2">
<a class="text-sm underline text-gray-700" href="/pages/{{0}}.html">Back to start</a>
</p>
{% endif %}

{% block head %}
<meta http-equiv="refresh" content="0; url=/pages/0.html" />
{% endblock %}
1 change: 1 addition & 0 deletions templates/layout.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<title>AskHistorians Archive</title>
<meta name="viewport" content="width=device-width">
<meta charset="utf-8">
{% block head %}{% endblock %}
</head>

<body class="m-4">
Expand Down
46 changes: 46 additions & 0 deletions templates/listing.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{% extends "layout.jinja" %}

{% block content %}

{% include "nav.jinja" %}

{% for post in posts %}
<div class="mb-8 prose break-words">
<a href="/posts/{{post.id}}.html" class="no-underline">
<h3>{{ post.title }}</h3>
</a>
<p>{% include "post_text.jinja" %}</p>
<p class="text-sm flex justify-between">
<a href="/posts/{{post.id}}.html">{{ post.real_num_comments }}
Answers</a>
<span class="text-gray-500">{{post.created_at.format("%F")}}</span>
</p>
</div>
{% endfor %}

<div class="flex justify-center">
{% if let Some(previous_page) = previous_page %}
<a class="text-gray-700 px-2" href="/pages/{{previous_page}}.html">⯇</a>
{% else %}
<span class="text-gray-400 px-2 cursor-default">⯇</span>
{% endif %}

<span class="font-bold px-1">{{page}}</span>
<span>/</span>
<a class="px-1" href="/pages/{{last_page}}.html">{{last_page}}</a>

{% if let Some(next_page) = next_page %}
<a class="text-gray-700 px-2" href="/pages/{{next_page}}.html">⯈</a>
{% else %}
<span class="text-gray-400 px-2 cursor-default">⯈</span>
{% endif %}

</div>

{% if page > 1 %}
<p class="text-center mt-2">
<a class="text-sm underline text-gray-700" href="/pages/{{0}}.html">Back to start</a>
</p>
{% endif %}

{% endblock %}

0 comments on commit ca927a3

Please sign in to comment.