2017-05-13 04:01:38 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use tera::Value;
|
|
|
|
use toml;
|
|
|
|
|
2017-07-01 07:47:41 +00:00
|
|
|
use errors::Result;
|
2017-05-13 04:01:38 +00:00
|
|
|
|
2018-10-31 07:18:57 +00:00
|
|
|
use super::{InsertAnchor, SortBy};
|
2017-05-13 04:01:38 +00:00
|
|
|
|
2017-07-01 07:47:41 +00:00
|
|
|
static DEFAULT_PAGINATE_PATH: &'static str = "page";
|
2017-05-13 04:01:38 +00:00
|
|
|
|
|
|
|
/// The front matter of every section
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
2018-03-14 17:22:24 +00:00
|
|
|
#[serde(default)]
|
2017-05-13 04:01:38 +00:00
|
|
|
pub struct SectionFrontMatter {
|
|
|
|
/// <title> of the page
|
|
|
|
pub title: Option<String>,
|
|
|
|
/// Description in <meta> that appears when linked, e.g. on twitter
|
|
|
|
pub description: Option<String>,
|
2017-09-26 08:04:18 +00:00
|
|
|
/// Whether to sort by "date", "order", "weight" or "none". Defaults to `none`.
|
2017-05-13 04:01:38 +00:00
|
|
|
#[serde(skip_serializing)]
|
2018-03-14 17:22:24 +00:00
|
|
|
pub sort_by: SortBy,
|
2017-09-27 14:37:17 +00:00
|
|
|
/// Used by the parent section to order its subsections.
|
2018-03-14 17:22:24 +00:00
|
|
|
/// Higher values means it will be at the end. Defaults to `0`
|
2017-09-26 08:04:18 +00:00
|
|
|
#[serde(skip_serializing)]
|
2018-03-14 17:22:24 +00:00
|
|
|
pub weight: usize,
|
2017-10-31 16:02:34 +00:00
|
|
|
/// Optional template, if we want to specify which template to render for that section
|
2017-05-13 04:01:38 +00:00
|
|
|
#[serde(skip_serializing)]
|
|
|
|
pub template: Option<String>,
|
|
|
|
/// How many pages to be displayed per paginated page. No pagination will happen if this isn't set
|
|
|
|
#[serde(skip_serializing)]
|
|
|
|
pub paginate_by: Option<usize>,
|
|
|
|
/// Path to be used by pagination: the page number will be appended after it. Defaults to `page`.
|
|
|
|
#[serde(skip_serializing)]
|
2018-03-14 17:22:24 +00:00
|
|
|
pub paginate_path: String,
|
2017-09-27 14:37:17 +00:00
|
|
|
/// Whether to insert a link for each header like the ones you can see in this site if you hover one
|
|
|
|
/// The default template can be overridden by creating a `anchor-link.html` in the `templates` directory
|
2018-03-14 17:22:24 +00:00
|
|
|
pub insert_anchor_links: InsertAnchor,
|
2017-05-13 04:01:38 +00:00
|
|
|
/// Whether to render that section or not. Defaults to `true`.
|
|
|
|
/// Useful when the section is only there to organize things but is not meant
|
|
|
|
/// to be used directly, like a posts section in a personal site
|
|
|
|
#[serde(skip_serializing)]
|
2018-03-14 17:22:24 +00:00
|
|
|
pub render: bool,
|
2017-07-25 07:56:13 +00:00
|
|
|
/// Whether to redirect when landing on that section. Defaults to `None`.
|
|
|
|
/// Useful for the same reason as `render` but when you don't want a 404 when
|
|
|
|
/// landing on the root section page
|
|
|
|
#[serde(skip_serializing)]
|
|
|
|
pub redirect_to: Option<String>,
|
2018-03-14 17:22:24 +00:00
|
|
|
/// Whether the section content and its pages/subsections are included in the index.
|
2018-03-14 21:03:06 +00:00
|
|
|
/// Defaults to `true` but is only used if search if explicitly enabled in the config.
|
2018-03-14 17:22:24 +00:00
|
|
|
#[serde(skip_serializing)]
|
|
|
|
pub in_search_index: bool,
|
2017-05-13 04:01:38 +00:00
|
|
|
/// Any extra parameter present in the front matter
|
2018-03-14 17:22:24 +00:00
|
|
|
pub extra: HashMap<String, Value>,
|
2017-05-13 04:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SectionFrontMatter {
|
|
|
|
pub fn parse(toml: &str) -> Result<SectionFrontMatter> {
|
2018-03-14 17:22:24 +00:00
|
|
|
let f: SectionFrontMatter = match toml::from_str(toml) {
|
2017-05-13 04:01:38 +00:00
|
|
|
Ok(d) => d,
|
|
|
|
Err(e) => bail!(e),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Only applies to section, whether it is paginated or not.
|
|
|
|
pub fn is_paginated(&self) -> bool {
|
|
|
|
match self.paginate_by {
|
|
|
|
Some(v) => v > 0,
|
2018-10-31 07:18:57 +00:00
|
|
|
None => false,
|
2017-05-13 04:01:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for SectionFrontMatter {
|
|
|
|
fn default() -> SectionFrontMatter {
|
|
|
|
SectionFrontMatter {
|
|
|
|
title: None,
|
|
|
|
description: None,
|
2018-03-14 17:22:24 +00:00
|
|
|
sort_by: SortBy::None,
|
|
|
|
weight: 0,
|
2017-05-13 04:01:38 +00:00
|
|
|
template: None,
|
|
|
|
paginate_by: None,
|
2018-03-14 17:22:24 +00:00
|
|
|
paginate_path: DEFAULT_PAGINATE_PATH.to_string(),
|
|
|
|
render: true,
|
2017-07-25 07:56:13 +00:00
|
|
|
redirect_to: None,
|
2018-03-14 17:22:24 +00:00
|
|
|
insert_anchor_links: InsertAnchor::None,
|
|
|
|
in_search_index: true,
|
|
|
|
extra: HashMap::new(),
|
2017-05-13 04:01:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|