|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- //! What we are sending to the templates when rendering them
- use std::collections::HashMap;
-
- use tera::{Map, Value};
-
- use content::{Page, Section};
- use library::Library;
-
- #[derive(Clone, Debug, PartialEq, Serialize)]
- pub struct TranslatedContent<'a> {
- lang: &'a str,
- permalink: &'a str,
- title: &'a Option<String>,
- }
-
- impl<'a> TranslatedContent<'a> {
- // copypaste eh, not worth creating an enum imo
- pub fn find_all_sections(section: &'a Section, library: &'a Library) -> Vec<Self> {
- let mut translations = vec![];
-
- for key in §ion.translations {
- let other = library.get_section_by_key(*key);
- translations.push(TranslatedContent {
- lang: &other.lang,
- permalink: &other.permalink,
- title: &other.meta.title,
- });
- }
-
- translations
- }
-
- pub fn find_all_pages(page: &'a Page, library: &'a Library) -> Vec<Self> {
- let mut translations = vec![];
-
- for key in &page.translations {
- let other = library.get_page_by_key(*key);
- translations.push(TranslatedContent {
- lang: &other.lang,
- permalink: &other.permalink,
- title: &other.meta.title,
- });
- }
-
- translations
- }
- }
-
- #[derive(Clone, Debug, PartialEq, Serialize)]
- pub struct SerializingPage<'a> {
- relative_path: &'a str,
- content: &'a str,
- permalink: &'a str,
- slug: &'a str,
- ancestors: Vec<String>,
- title: &'a Option<String>,
- description: &'a Option<String>,
- date: &'a Option<String>,
- year: Option<i32>,
- month: Option<u32>,
- day: Option<u32>,
- taxonomies: &'a HashMap<String, Vec<String>>,
- extra: &'a Map<String, Value>,
- path: &'a str,
- components: &'a [String],
- summary: &'a Option<String>,
- word_count: Option<usize>,
- reading_time: Option<usize>,
- assets: &'a [String],
- draft: bool,
- lang: &'a str,
- lighter: Option<Box<SerializingPage<'a>>>,
- heavier: Option<Box<SerializingPage<'a>>>,
- earlier: Option<Box<SerializingPage<'a>>>,
- later: Option<Box<SerializingPage<'a>>>,
- translations: Vec<TranslatedContent<'a>>,
- }
-
- impl<'a> SerializingPage<'a> {
- /// Grabs all the data from a page, including sibling pages
- pub fn from_page(page: &'a Page, library: &'a Library) -> Self {
- let mut year = None;
- let mut month = None;
- let mut day = None;
- if let Some(d) = page.meta.datetime_tuple {
- year = Some(d.0);
- month = Some(d.1);
- day = Some(d.2);
- }
- let pages = library.pages();
- let lighter = page
- .lighter
- .map(|k| Box::new(Self::from_page_basic(pages.get(k).unwrap(), Some(library))));
- let heavier = page
- .heavier
- .map(|k| Box::new(Self::from_page_basic(pages.get(k).unwrap(), Some(library))));
- let earlier = page
- .earlier
- .map(|k| Box::new(Self::from_page_basic(pages.get(k).unwrap(), Some(library))));
- let later = page
- .later
- .map(|k| Box::new(Self::from_page_basic(pages.get(k).unwrap(), Some(library))));
- let ancestors = page
- .ancestors
- .iter()
- .map(|k| library.get_section_by_key(*k).file.relative.clone())
- .collect();
-
- let translations = TranslatedContent::find_all_pages(page, library);
-
- SerializingPage {
- relative_path: &page.file.relative,
- ancestors,
- content: &page.content,
- permalink: &page.permalink,
- slug: &page.slug,
- title: &page.meta.title,
- description: &page.meta.description,
- extra: &page.meta.extra,
- date: &page.meta.date,
- year,
- month,
- day,
- taxonomies: &page.meta.taxonomies,
- path: &page.path,
- components: &page.components,
- summary: &page.summary,
- word_count: page.word_count,
- reading_time: page.reading_time,
- assets: &page.serialized_assets,
- draft: page.is_draft(),
- lang: &page.lang,
- lighter,
- heavier,
- earlier,
- later,
- translations,
- }
- }
-
- /// Same as from_page but does not fill sibling pages
- pub fn from_page_basic(page: &'a Page, library: Option<&'a Library>) -> Self {
- let mut year = None;
- let mut month = None;
- let mut day = None;
- if let Some(d) = page.meta.datetime_tuple {
- year = Some(d.0);
- month = Some(d.1);
- day = Some(d.2);
- }
- let ancestors = if let Some(ref lib) = library {
- page.ancestors
- .iter()
- .map(|k| lib.get_section_by_key(*k).file.relative.clone())
- .collect()
- } else {
- vec![]
- };
-
- let translations = if let Some(ref lib) = library {
- TranslatedContent::find_all_pages(page, lib)
- } else {
- vec![]
- };
-
- SerializingPage {
- relative_path: &page.file.relative,
- ancestors,
- content: &page.content,
- permalink: &page.permalink,
- slug: &page.slug,
- title: &page.meta.title,
- description: &page.meta.description,
- extra: &page.meta.extra,
- date: &page.meta.date,
- year,
- month,
- day,
- taxonomies: &page.meta.taxonomies,
- path: &page.path,
- components: &page.components,
- summary: &page.summary,
- word_count: page.word_count,
- reading_time: page.reading_time,
- assets: &page.serialized_assets,
- draft: page.is_draft(),
- lang: &page.lang,
- lighter: None,
- heavier: None,
- earlier: None,
- later: None,
- translations,
- }
- }
- }
-
- #[derive(Clone, Debug, PartialEq, Serialize)]
- pub struct SerializingSection<'a> {
- relative_path: &'a str,
- content: &'a str,
- permalink: &'a str,
- ancestors: Vec<String>,
- title: &'a Option<String>,
- description: &'a Option<String>,
- extra: &'a HashMap<String, Value>,
- path: &'a str,
- components: &'a [String],
- word_count: Option<usize>,
- reading_time: Option<usize>,
- lang: &'a str,
- assets: &'a [String],
- pages: Vec<SerializingPage<'a>>,
- subsections: Vec<&'a str>,
- translations: Vec<TranslatedContent<'a>>,
- }
-
- impl<'a> SerializingSection<'a> {
- pub fn from_section(section: &'a Section, library: &'a Library) -> Self {
- let mut pages = Vec::with_capacity(section.pages.len());
- let mut subsections = Vec::with_capacity(section.subsections.len());
-
- for k in §ion.pages {
- pages.push(library.get_page_by_key(*k).to_serialized_basic(library));
- }
-
- for k in §ion.subsections {
- subsections.push(library.get_section_path_by_key(*k));
- }
-
- let ancestors = section
- .ancestors
- .iter()
- .map(|k| library.get_section_by_key(*k).file.relative.clone())
- .collect();
- let translations = TranslatedContent::find_all_sections(section, library);
-
- SerializingSection {
- relative_path: §ion.file.relative,
- ancestors,
- content: §ion.content,
- permalink: §ion.permalink,
- title: §ion.meta.title,
- description: §ion.meta.description,
- extra: §ion.meta.extra,
- path: §ion.path,
- components: §ion.components,
- word_count: section.word_count,
- reading_time: section.reading_time,
- assets: §ion.serialized_assets,
- lang: §ion.lang,
- pages,
- subsections,
- translations,
- }
- }
-
- /// Same as from_section but doesn't fetch pages and sections
- pub fn from_section_basic(section: &'a Section, library: Option<&'a Library>) -> Self {
- let ancestors = if let Some(ref lib) = library {
- section
- .ancestors
- .iter()
- .map(|k| lib.get_section_by_key(*k).file.relative.clone())
- .collect()
- } else {
- vec![]
- };
-
- let translations = if let Some(ref lib) = library {
- TranslatedContent::find_all_sections(section, lib)
- } else {
- vec![]
- };
-
- SerializingSection {
- relative_path: §ion.file.relative,
- ancestors,
- content: §ion.content,
- permalink: §ion.permalink,
- title: §ion.meta.title,
- description: §ion.meta.description,
- extra: §ion.meta.extra,
- path: §ion.path,
- components: §ion.components,
- word_count: section.word_count,
- reading_time: section.reading_time,
- assets: §ion.serialized_assets,
- lang: §ion.lang,
- pages: vec![],
- subsections: vec![],
- translations,
- }
- }
- }
|