From 7d208b4609ea569c0c513d06adc8f5814d48c53b Mon Sep 17 00:00:00 2001 From: Folyd Date: Fri, 10 Mar 2023 22:09:58 +0800 Subject: [PATCH] Fix default author avatar missing (#199) --- src/entity/author.rs | 20 +++----------------- src/entity/zine.rs | 31 +++++++++++++++++++++---------- templates/author.jinja | 4 ++-- 3 files changed, 26 insertions(+), 29 deletions(-) diff --git a/src/entity/author.rs b/src/entity/author.rs index 87ba42b..2b24502 100644 --- a/src/entity/author.rs +++ b/src/entity/author.rs @@ -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. @@ -30,8 +30,8 @@ pub struct Author { pub bio: Option, /// Whether the author is an editor. #[serde(default)] - #[serde(rename(deserialize = "editor"))] - pub is_editor: bool, + #[serde(rename = "editor")] + pub editor: bool, } impl AuthorId { @@ -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( diff --git a/src/entity/zine.rs b/src/entity/zine.rs index b87b0fa..fad3b0b 100644 --- a/src/entity/zine.rs +++ b/src/entity/zine.rs @@ -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}; @@ -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) @@ -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() { diff --git a/templates/author.jinja b/templates/author.jinja index bd1a8ce..b4672eb 100644 --- a/templates/author.jinja +++ b/templates/author.jinja @@ -19,7 +19,7 @@
Avatar - {% if author.is_editor -%} + {% if author.editor -%}
-
{{ author_name }}
+
{{ author_name }}
{% if author.bio -%}
{{ markdown_to_html(markdown = author.bio) | safe }}
{% endif -%}