larablog 0.1.0

Markdown blogs for Laravel Rust in several languages, where every language has its own URL.
Documentation
//! What the views receive.
//!
//! PHP hands its Blade views objects and the views call their methods. A MiniJinja template cannot
//! call a Rust method, so every view gets plain data instead: what a method returns is a field of
//! the same name (`post.url`, `post.author.name`, `post.category.name`), and the frontmatter's own
//! `author` and `category` keys are `author_key` and `category_key`.

use serde::Serialize;
use serde_json::{Map, Value};

use crate::markdown::Heading;
use crate::{Author, Blog, Category, Post, Tag, support};

/// The collection, as a view sees it.
#[derive(Debug, Clone, Serialize)]
pub struct BlogView {
    /// Its name.
    pub name: String,
    /// Its languages, the default first.
    pub locales: Vec<String>,
    /// The listing of the page's language.
    pub url: String,
    /// The feed of the page's language, when it has one.
    pub feed_url: Option<String>,
}

impl BlogView {
    /// The collection in the language of the page.
    pub fn of(blog: &Blog, locale: &str) -> Self {
        Self {
            name: blog.name().to_owned(),
            locales: blog.locales(),
            url: blog.url("index", Some(locale), &[], true),
            feed_url: blog
                .settings()
                .feed
                .enabled
                .then(|| blog.url("feed", Some(locale), &[], true)),
        }
    }
}

/// A category, as a view sees it.
#[derive(Debug, Clone, Serialize)]
pub struct CategoryView {
    /// Its key.
    pub key: String,
    /// Its name.
    pub name: String,
    /// Its URL segment.
    pub slug: String,
    /// What it is about.
    pub description: Option<String>,
    /// A colour or a class name.
    pub color: Option<String>,
    /// Its address.
    pub url: String,
    /// Every other field of its entry.
    pub meta: Map<String, Value>,
}

impl From<&Category> for CategoryView {
    fn from(category: &Category) -> Self {
        Self {
            key: category.key.clone(),
            name: category.name.clone(),
            slug: category.slug.clone(),
            description: category.description.clone(),
            color: category.color.clone(),
            url: category.url(),
            meta: category
                .meta
                .keys()
                .filter_map(|key| Some((key.clone(), category.meta(key)?)))
                .collect(),
        }
    }
}

/// A tag, as a view sees it.
#[derive(Debug, Clone, Serialize)]
pub struct TagView {
    /// Its slug.
    pub slug: String,
    /// Its name.
    pub name: String,
    /// How many published posts carry it.
    pub count: usize,
    /// Its address.
    pub url: String,
}

impl TagView {
    /// A tag of the collection in a language.
    pub fn of(blog: &Blog, tag: &Tag, locale: &str) -> Self {
        Self {
            slug: tag.slug.clone(),
            name: tag.name.clone(),
            count: tag.count,
            url: blog.url("tag", Some(locale), &[("tag", &tag.slug)], true),
        }
    }
}

/// A post, as a view sees it. A listing gets the summary, which leaves the body out; the post's own
/// page gets the whole of it.
#[derive(Debug, Clone, Serialize)]
pub struct PostView {
    /// The folder.
    pub id: String,
    /// The language.
    pub locale: String,
    /// The slug of this language.
    pub slug: String,
    /// Its address.
    pub url: String,
    /// The title.
    pub title: String,
    /// The description.
    pub description: Option<String>,
    /// When it goes out, as an ISO 8601 date.
    pub date: String,
    /// `date` in the language of the page: `1 de septiembre de 2026`.
    pub date_formatted: String,
    /// When it last changed.
    pub updated: Option<String>,
    /// The category's key.
    pub category_key: Option<String>,
    /// The category.
    pub category: Option<CategoryView>,
    /// The tags, as written.
    pub tags: Vec<String>,
    /// The author's key.
    pub author_key: Option<String>,
    /// The author.
    pub author: Option<Author>,
    /// The picture of the frontmatter.
    pub image: Option<String>,
    /// The picture for the post and its social preview.
    pub image_url: Option<String>,
    /// The picture's alternative text.
    pub image_alt: Option<String>,
    /// Whether it is featured.
    pub featured: bool,
    /// Whether it is a draft.
    pub draft: bool,
    /// Minutes to read.
    pub reading_time: u32,
    /// Every other frontmatter field.
    pub meta: Map<String, Value>,
    /// The body as HTML, which a template prints with `|safe`. Only on the post's own page.
    pub html: Option<String>,
    /// The `h2` and `h3` of the body. Only on the post's own page.
    pub headings: Option<Vec<Heading>>,
    /// Language => address of the same post, this one included. Only on the post's own page.
    pub alternates: Option<Vec<(String, String)>>,
}

impl PostView {
    /// What a listing needs.
    pub fn summary(post: &Post) -> Self {
        Self {
            id: post.id.clone(),
            locale: post.locale.clone(),
            slug: post.slug.clone(),
            url: post.url(),
            title: post.title.clone(),
            description: post.description.clone(),
            date: support::atom(post.date),
            date_formatted: support::long_date(post.date, &post.locale),
            updated: post.updated.map(support::atom),
            category_key: post.category.clone(),
            category: post.category().as_ref().map(CategoryView::from),
            tags: post.tags.clone(),
            author_key: post.author.clone(),
            author: post.author(),
            image: post.image.clone(),
            image_url: post.image_url(),
            image_alt: post.image_alt.clone(),
            featured: post.featured,
            draft: post.draft,
            reading_time: post.reading_time,
            meta: post.meta.clone(),
            html: None,
            headings: None,
            alternates: None,
        }
    }

    /// What the post's own page needs: the summary, the body, its headings and its alternates.
    pub fn full(post: &Post) -> Self {
        Self {
            html: Some(post.html()),
            headings: Some(post.headings()),
            alternates: Some(post.alternates()),
            ..Self::summary(post)
        }
    }
}