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.

118 lines
3.8KB

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