You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
4.3KB

  1. use serde_derive::{Deserialize, Serialize};
  2. use tera::{Map, Value};
  3. use toml;
  4. use super::{InsertAnchor, SortBy};
  5. use errors::{bail, Result};
  6. use utils::de::fix_toml_dates;
  7. static DEFAULT_PAGINATE_PATH: &str = "page";
  8. /// The front matter of every section
  9. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
  10. #[serde(default)]
  11. pub struct SectionFrontMatter {
  12. /// <title> of the page
  13. pub title: Option<String>,
  14. /// Description in <meta> that appears when linked, e.g. on twitter
  15. pub description: Option<String>,
  16. /// Whether to sort by "date", "order", "weight" or "none". Defaults to `none`.
  17. #[serde(skip_serializing)]
  18. pub sort_by: SortBy,
  19. /// Used by the parent section to order its subsections.
  20. /// Higher values means it will be at the end. Defaults to `0`
  21. #[serde(skip_serializing)]
  22. pub weight: usize,
  23. /// Optional template, if we want to specify which template to render for that section
  24. #[serde(skip_serializing)]
  25. pub template: Option<String>,
  26. /// How many pages to be displayed per paginated page. No pagination will happen if this isn't set
  27. #[serde(skip_serializing)]
  28. pub paginate_by: Option<usize>,
  29. /// Path to be used by pagination: the page number will be appended after it. Defaults to `page`.
  30. #[serde(skip_serializing)]
  31. pub paginate_path: String,
  32. /// Whether to insert a link for each header like the ones you can see in this site if you hover one
  33. /// The default template can be overridden by creating a `anchor-link.html` in the `templates` directory
  34. pub insert_anchor_links: InsertAnchor,
  35. /// Whether to render that section or not. Defaults to `true`.
  36. /// Useful when the section is only there to organize things but is not meant
  37. /// to be used directly, like a posts section in a personal site
  38. #[serde(skip_serializing)]
  39. pub render: bool,
  40. /// Whether to redirect when landing on that section. Defaults to `None`.
  41. /// Useful for the same reason as `render` but when you don't want a 404 when
  42. /// landing on the root section page
  43. #[serde(skip_serializing)]
  44. pub redirect_to: Option<String>,
  45. /// Whether the section content and its pages/subsections are included in the index.
  46. /// Defaults to `true` but is only used if search if explicitly enabled in the config.
  47. #[serde(skip_serializing)]
  48. pub in_search_index: bool,
  49. /// Whether the section should pass its pages on to the parent section. Defaults to `false`.
  50. /// Useful when the section shouldn't split up the parent section, like
  51. /// sections for each year under a posts section.
  52. #[serde(skip_serializing)]
  53. pub transparent: bool,
  54. /// Optional template for all pages in this section (including the pages of children section)
  55. #[serde(skip_serializing)]
  56. pub page_template: Option<String>,
  57. /// All aliases for that page. Zola will create HTML templates that will
  58. /// redirect to this
  59. #[serde(skip_serializing)]
  60. pub aliases: Vec<String>,
  61. /// Any extra parameter present in the front matter
  62. pub extra: Map<String, Value>,
  63. }
  64. impl SectionFrontMatter {
  65. pub fn parse(toml: &str) -> Result<SectionFrontMatter> {
  66. let mut f: SectionFrontMatter = match toml::from_str(toml) {
  67. Ok(d) => d,
  68. Err(e) => bail!(e),
  69. };
  70. f.extra = match fix_toml_dates(f.extra) {
  71. Value::Object(o) => o,
  72. _ => unreachable!("Got something other than a table in section extra"),
  73. };
  74. Ok(f)
  75. }
  76. /// Only applies to section, whether it is paginated or not.
  77. pub fn is_paginated(&self) -> bool {
  78. match self.paginate_by {
  79. Some(v) => v > 0,
  80. None => false,
  81. }
  82. }
  83. }
  84. impl Default for SectionFrontMatter {
  85. fn default() -> SectionFrontMatter {
  86. SectionFrontMatter {
  87. title: None,
  88. description: None,
  89. sort_by: SortBy::None,
  90. weight: 0,
  91. template: None,
  92. paginate_by: None,
  93. paginate_path: DEFAULT_PAGINATE_PATH.to_string(),
  94. render: true,
  95. redirect_to: None,
  96. insert_anchor_links: InsertAnchor::None,
  97. in_search_index: true,
  98. transparent: false,
  99. page_template: None,
  100. aliases: Vec::new(),
  101. extra: Map::new(),
  102. }
  103. }
  104. }