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.

119 lines
3.9KB

  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. /// This can also be set in a section front-matter if you only want it for
  33. pub insert_anchor_links: Option<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: Option<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. /// Any extra parameter present in the front matter
  45. pub extra: Option<HashMap<String, Value>>,
  46. }
  47. impl SectionFrontMatter {
  48. pub fn parse(toml: &str) -> Result<SectionFrontMatter> {
  49. let mut f: SectionFrontMatter = match toml::from_str(toml) {
  50. Ok(d) => d,
  51. Err(e) => bail!(e),
  52. };
  53. if f.paginate_path.is_none() {
  54. f.paginate_path = Some(DEFAULT_PAGINATE_PATH.to_string());
  55. }
  56. if f.render.is_none() {
  57. f.render = Some(true);
  58. }
  59. if f.sort_by.is_none() {
  60. f.sort_by = Some(SortBy::None);
  61. }
  62. if f.insert_anchor_links.is_none() {
  63. f.insert_anchor_links = Some(InsertAnchor::None);
  64. }
  65. if f.weight.is_none() {
  66. f.weight = Some(0);
  67. }
  68. Ok(f)
  69. }
  70. /// Returns the current sorting method, defaults to `None` (== no sorting)
  71. pub fn sort_by(&self) -> SortBy {
  72. self.sort_by.unwrap()
  73. }
  74. /// Only applies to section, whether it is paginated or not.
  75. pub fn is_paginated(&self) -> bool {
  76. match self.paginate_by {
  77. Some(v) => v > 0,
  78. None => false
  79. }
  80. }
  81. pub fn should_render(&self) -> bool {
  82. self.render.unwrap()
  83. }
  84. }
  85. impl Default for SectionFrontMatter {
  86. fn default() -> SectionFrontMatter {
  87. SectionFrontMatter {
  88. title: None,
  89. description: None,
  90. sort_by: Some(SortBy::None),
  91. weight: Some(0),
  92. template: None,
  93. paginate_by: None,
  94. paginate_path: Some(DEFAULT_PAGINATE_PATH.to_string()),
  95. render: Some(true),
  96. redirect_to: None,
  97. insert_anchor_links: Some(InsertAnchor::None),
  98. extra: None,
  99. }
  100. }
  101. }