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.

110 lines
3.5KB

  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" or "none". Defaults to `none`.
  15. #[serde(skip_serializing)]
  16. pub sort_by: Option<SortBy>,
  17. /// Optional template, if we want to specify which template to render for that page
  18. #[serde(skip_serializing)]
  19. pub template: Option<String>,
  20. /// How many pages to be displayed per paginated page. No pagination will happen if this isn't set
  21. #[serde(skip_serializing)]
  22. pub paginate_by: Option<usize>,
  23. /// Path to be used by pagination: the page number will be appended after it. Defaults to `page`.
  24. #[serde(skip_serializing)]
  25. pub paginate_path: Option<String>,
  26. /// Whether to insert a link for each header like in Github READMEs. Defaults to false
  27. /// The default template can be overridden by creating a `anchor-link.html` template and CSS will need to be
  28. /// written if you turn that on.
  29. pub insert_anchor: Option<InsertAnchor>,
  30. /// Whether to render that section or not. Defaults to `true`.
  31. /// Useful when the section is only there to organize things but is not meant
  32. /// to be used directly, like a posts section in a personal site
  33. #[serde(skip_serializing)]
  34. pub render: Option<bool>,
  35. /// Whether to redirect when landing on that section. Defaults to `None`.
  36. /// Useful for the same reason as `render` but when you don't want a 404 when
  37. /// landing on the root section page
  38. #[serde(skip_serializing)]
  39. pub redirect_to: Option<String>,
  40. /// Any extra parameter present in the front matter
  41. pub extra: Option<HashMap<String, Value>>,
  42. }
  43. impl SectionFrontMatter {
  44. pub fn parse(toml: &str) -> Result<SectionFrontMatter> {
  45. let mut f: SectionFrontMatter = match toml::from_str(toml) {
  46. Ok(d) => d,
  47. Err(e) => bail!(e),
  48. };
  49. if f.paginate_path.is_none() {
  50. f.paginate_path = Some(DEFAULT_PAGINATE_PATH.to_string());
  51. }
  52. if f.render.is_none() {
  53. f.render = Some(true);
  54. }
  55. if f.sort_by.is_none() {
  56. f.sort_by = Some(SortBy::None);
  57. }
  58. if f.insert_anchor.is_none() {
  59. f.insert_anchor = Some(InsertAnchor::None);
  60. }
  61. Ok(f)
  62. }
  63. /// Returns the current sorting method, defaults to `None` (== no sorting)
  64. pub fn sort_by(&self) -> SortBy {
  65. self.sort_by.unwrap()
  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. pub fn should_render(&self) -> bool {
  75. self.render.unwrap()
  76. }
  77. }
  78. impl Default for SectionFrontMatter {
  79. fn default() -> SectionFrontMatter {
  80. SectionFrontMatter {
  81. title: None,
  82. description: None,
  83. sort_by: Some(SortBy::None),
  84. template: None,
  85. paginate_by: None,
  86. paginate_path: Some(DEFAULT_PAGINATE_PATH.to_string()),
  87. render: Some(true),
  88. redirect_to: None,
  89. insert_anchor: Some(InsertAnchor::None),
  90. extra: None,
  91. }
  92. }
  93. }