1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use serde::Serialize;
use serde_json::{Map, Value};
use crate::support;
/// A category in one language: the same `key` everywhere, a name and a slug of its own.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Category {
/// The collection it belongs to.
pub collection: String,
/// As written in the posts' frontmatter.
pub key: String,
/// The language this is the category in.
pub locale: 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 for your views.
pub color: Option<String>,
/// Any other field of its entry in `_categories.yml`.
pub meta: Map<String, Value>,
}
impl Category {
/// A field of the category's entry that has no property of its own, in this language when it is
/// written per language. `accent` is also what the social card is tinted with.
pub fn meta(&self, key: &str) -> Option<Value> {
let value = support::data_get(&Value::Object(self.meta.clone()), key)?.clone();
match &value {
Value::Object(map) if map.contains_key(&self.locale) => map.get(&self.locale).cloned(),
_ => Some(value),
}
}
/// Its address, absolute.
pub fn url(&self) -> String {
self.url_with(true)
}
/// Its address, absolute or just the path.
pub fn url_with(&self, absolute: bool) -> String {
crate::collection(&self.collection).map_or_else(
|_| String::new(),
|blog| blog.url("category", Some(&self.locale), &[("category", &self.slug)], absolute),
)
}
}