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.

104 lines
3.2KB

  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. /// Any extra parameter present in the front matter
  36. pub extra: Option<HashMap<String, Value>>,
  37. }
  38. impl SectionFrontMatter {
  39. pub fn parse(toml: &str) -> Result<SectionFrontMatter> {
  40. let mut f: SectionFrontMatter = match toml::from_str(toml) {
  41. Ok(d) => d,
  42. Err(e) => bail!(e),
  43. };
  44. if f.paginate_path.is_none() {
  45. f.paginate_path = Some(DEFAULT_PAGINATE_PATH.to_string());
  46. }
  47. if f.render.is_none() {
  48. f.render = Some(true);
  49. }
  50. if f.sort_by.is_none() {
  51. f.sort_by = Some(SortBy::None);
  52. }
  53. if f.insert_anchor.is_none() {
  54. f.insert_anchor = Some(InsertAnchor::None);
  55. }
  56. Ok(f)
  57. }
  58. /// Returns the current sorting method, defaults to `None` (== no sorting)
  59. pub fn sort_by(&self) -> SortBy {
  60. self.sort_by.unwrap()
  61. }
  62. /// Only applies to section, whether it is paginated or not.
  63. pub fn is_paginated(&self) -> bool {
  64. match self.paginate_by {
  65. Some(v) => v > 0,
  66. None => false
  67. }
  68. }
  69. pub fn should_render(&self) -> bool {
  70. self.render.unwrap()
  71. }
  72. }
  73. impl Default for SectionFrontMatter {
  74. fn default() -> SectionFrontMatter {
  75. SectionFrontMatter {
  76. title: None,
  77. description: None,
  78. sort_by: Some(SortBy::None),
  79. template: None,
  80. paginate_by: None,
  81. paginate_path: Some(DEFAULT_PAGINATE_PATH.to_string()),
  82. render: Some(true),
  83. insert_anchor: Some(InsertAnchor::None),
  84. extra: None,
  85. }
  86. }
  87. }