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.

94 lines
3.4KB

  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. /// Any extra parameter present in the front matter
  49. pub extra: HashMap<String, Value>,
  50. }
  51. impl SectionFrontMatter {
  52. pub fn parse(toml: &str) -> Result<SectionFrontMatter> {
  53. let f: SectionFrontMatter = match toml::from_str(toml) {
  54. Ok(d) => d,
  55. Err(e) => bail!(e),
  56. };
  57. Ok(f)
  58. }
  59. /// Only applies to section, whether it is paginated or not.
  60. pub fn is_paginated(&self) -> bool {
  61. match self.paginate_by {
  62. Some(v) => v > 0,
  63. None => false,
  64. }
  65. }
  66. }
  67. impl Default for SectionFrontMatter {
  68. fn default() -> SectionFrontMatter {
  69. SectionFrontMatter {
  70. title: None,
  71. description: None,
  72. sort_by: SortBy::None,
  73. weight: 0,
  74. template: None,
  75. paginate_by: None,
  76. paginate_path: DEFAULT_PAGINATE_PATH.to_string(),
  77. render: true,
  78. redirect_to: None,
  79. insert_anchor_links: InsertAnchor::None,
  80. in_search_index: true,
  81. extra: HashMap::new(),
  82. }
  83. }
  84. }