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.

111 lines
3.4KB

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