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.

104 lines
3.9KB

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