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.

100 lines
2.9KB

  1. use std::collections::HashMap;
  2. use tera::Value;
  3. use toml;
  4. use errors::{Result};
  5. static DEFAULT_PAGINATE_PATH: &'static str = "page";
  6. #[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
  7. #[serde(rename_all = "lowercase")]
  8. pub enum SortBy {
  9. Date,
  10. Order,
  11. None,
  12. }
  13. /// The front matter of every section
  14. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
  15. pub struct SectionFrontMatter {
  16. /// <title> of the page
  17. pub title: Option<String>,
  18. /// Description in <meta> that appears when linked, e.g. on twitter
  19. pub description: Option<String>,
  20. /// Whether to sort by "date", "order" or "none". Defaults to `none`.
  21. #[serde(skip_serializing)]
  22. pub sort_by: Option<SortBy>,
  23. /// Optional template, if we want to specify which template to render for that page
  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: Option<String>,
  32. /// Whether to render that section or not. Defaults to `true`.
  33. /// Useful when the section is only there to organize things but is not meant
  34. /// to be used directly, like a posts section in a personal site
  35. #[serde(skip_serializing)]
  36. pub render: Option<bool>,
  37. /// Any extra parameter present in the front matter
  38. pub extra: Option<HashMap<String, Value>>,
  39. }
  40. impl SectionFrontMatter {
  41. pub fn parse(toml: &str) -> Result<SectionFrontMatter> {
  42. let mut f: SectionFrontMatter = match toml::from_str(toml) {
  43. Ok(d) => d,
  44. Err(e) => bail!(e),
  45. };
  46. if f.paginate_path.is_none() {
  47. f.paginate_path = Some(DEFAULT_PAGINATE_PATH.to_string());
  48. }
  49. if f.render.is_none() {
  50. f.render = Some(true);
  51. }
  52. if f.sort_by.is_none() {
  53. f.sort_by = Some(SortBy::None);
  54. }
  55. Ok(f)
  56. }
  57. /// Returns the current sorting method, defaults to `None` (== no sorting)
  58. pub fn sort_by(&self) -> SortBy {
  59. self.sort_by.unwrap()
  60. }
  61. /// Only applies to section, whether it is paginated or not.
  62. pub fn is_paginated(&self) -> bool {
  63. match self.paginate_by {
  64. Some(v) => v > 0,
  65. None => false
  66. }
  67. }
  68. pub fn should_render(&self) -> bool {
  69. self.render.unwrap()
  70. }
  71. }
  72. impl Default for SectionFrontMatter {
  73. fn default() -> SectionFrontMatter {
  74. SectionFrontMatter {
  75. title: None,
  76. description: None,
  77. sort_by: None,
  78. template: None,
  79. paginate_by: None,
  80. paginate_path: Some(DEFAULT_PAGINATE_PATH.to_string()),
  81. render: Some(true),
  82. extra: None,
  83. }
  84. }
  85. }