Skip to content

Commit

Permalink
Fix default author avatar missing (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
Folyd authored Mar 10, 2023
1 parent fc56538 commit 7d208b4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 29 deletions.
20 changes: 3 additions & 17 deletions src/entity/author.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::Result;
use serde::{de, ser::SerializeSeq, Deserialize, Serialize};
use tera::Context;

use crate::{data, engine, helpers::capitalize, html::Meta, markdown, Entity};
use crate::{engine, html::Meta, markdown, Entity};

/// AuthorId represents a single author or multiple co-authors.
/// Declared in `[[article]]` table.
Expand All @@ -30,8 +30,8 @@ pub struct Author {
pub bio: Option<String>,
/// Whether the author is an editor.
#[serde(default)]
#[serde(rename(deserialize = "editor"))]
pub is_editor: bool,
#[serde(rename = "editor")]
pub editor: bool,
}

impl AuthorId {
Expand All @@ -46,20 +46,6 @@ impl AuthorId {
}

impl Entity for Author {
fn parse(&mut self, _source: &Path) -> anyhow::Result<()> {
// Fallback to default zine avatar if neccessary.
if self.avatar.is_none() || matches!(&self.avatar, Some(avatar) if avatar.is_empty()) {
let data = data::read();
self.avatar = data.get_theme().default_avatar.clone();
}

// Fallback to capitalized id if missing.
if self.name.is_none() {
self.name = Some(capitalize(&self.id));
}
Ok(())
}

fn render(&self, mut context: Context, dest: &Path) -> anyhow::Result<()> {
let slug = format!("@{}", self.id.to_lowercase());
context.insert(
Expand Down
31 changes: 21 additions & 10 deletions src/entity/zine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::{
use tera::Context;
use walkdir::WalkDir;

use crate::{data, engine, error::ZineError, feed::FeedEntry, Entity};
use crate::{data, engine, error::ZineError, feed::FeedEntry, helpers::capitalize, Entity};

use super::{Author, Issue, List, MarkdownConfig, MetaArticle, Page, Site, Theme, Topic};

Expand Down Expand Up @@ -311,15 +311,6 @@ impl Entity for Zine {
fn parse(&mut self, source: &Path) -> Result<()> {
self.theme.parse(source)?;

if self.authors.is_empty() {
println!("Warning: no author specified in [authors] of root `zine.toml`.");
} else {
self.authors.iter_mut().try_for_each(|(id, author)| {
author.id = id.clone();
author.parse(source)
})?;
}

self.topics.iter_mut().try_for_each(|(id, topic)| {
topic.id = id.clone();
topic.parse(source)
Expand All @@ -340,6 +331,26 @@ impl Entity for Zine {
// Sort all issues by number.
self.issues.par_sort_unstable_by_key(|s| s.number);

if self.authors.is_empty() {
println!("Warning: no author specified in [authors] of root `zine.toml`.");
} else {
self.authors.iter_mut().try_for_each(|(id, author)| {
author.id = id.clone();
// Fallback to default zine avatar if neccessary.
if author.avatar.is_none()
|| matches!(&author.avatar, Some(avatar) if avatar.is_empty())
{
author.avatar = self.theme.default_avatar.clone();
}

// Fallback to capitalized id if missing.
if author.name.is_none() {
author.name = Some(capitalize(&author.id));
}
author.parse(source)
})?;
}

// Parse pages
let page_dir = source.join("pages");
if page_dir.exists() {
Expand Down
4 changes: 2 additions & 2 deletions templates/author.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<div class="flex-none m-4 sm:m-8 text-center">
<img class="zine-avatar mx-auto w-44 h-44 object-cover rounded-full border" src="{{ author.avatar }}"
alt="Avatar" loading="lazy">
{% if author.is_editor -%}
{% if author.editor -%}
<div class="flex justify-center items-center font-bold text-link py-2 px-4 mt-4">
<svg viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg"
fill="{{ theme.link_color }}" width="32" height="32" xmlns:xlink="http://www.w3.org/1999/xlink">
Expand All @@ -39,7 +39,7 @@
{% endif -%}
</div>
<div class="flex-1 prose my-2 sm:m-8">
<div class="zine-author-name text-center text-4xl font-bold py-2 text-center">{{ author_name }}</div>
<div class="zine-author-name text-center text-4xl font-bold py-2">{{ author_name }}</div>
{% if author.bio -%}
<div class="zine-author-bio">{{ markdown_to_html(markdown = author.bio) | safe }}</div>
{% endif -%}
Expand Down

0 comments on commit 7d208b4

Please sign in to comment.