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.

141 lines
4.4KB

  1. use std::borrow::Cow;
  2. use std::hash::{Hash, Hasher};
  3. use std::collections::{HashSet};
  4. use tera::{Map, Value};
  5. use config::{Config};
  6. use library::{Library, Taxonomy};
  7. use std::cmp::Ordering;
  8. /// The sitemap only needs links, potentially date and extra for pages in case of updates
  9. /// for examples so we trim down all entries to only that
  10. #[derive(Debug, Serialize)]
  11. pub struct SitemapEntry<'a> {
  12. permalink: Cow<'a, str>,
  13. date: Option<String>,
  14. extra: Option<&'a Map<String, Value>>,
  15. }
  16. // Hash/Eq is not implemented for tera::Map but in our case we only care about the permalink
  17. // when comparing/hashing so we implement it manually
  18. impl<'a> Hash for SitemapEntry<'a> {
  19. fn hash<H: Hasher>(&self, state: &mut H) {
  20. self.permalink.hash(state);
  21. }
  22. }
  23. impl<'a> PartialEq for SitemapEntry<'a> {
  24. fn eq(&self, other: &SitemapEntry) -> bool {
  25. self.permalink == other.permalink
  26. }
  27. }
  28. impl<'a> Eq for SitemapEntry<'a> {}
  29. impl<'a> SitemapEntry<'a> {
  30. pub fn new(permalink: Cow<'a, str>, date: Option<String>) -> Self {
  31. SitemapEntry { permalink, date, extra: None }
  32. }
  33. pub fn add_extra(&mut self, extra: &'a Map<String, Value>) {
  34. self.extra = Some(extra);
  35. }
  36. }
  37. impl<'a> PartialOrd for SitemapEntry<'a> {
  38. fn partial_cmp(&self, other: &SitemapEntry) -> Option<Ordering> {
  39. Some(self.permalink.as_ref().cmp(other.permalink.as_ref()))
  40. }
  41. }
  42. impl<'a> Ord for SitemapEntry<'a> {
  43. fn cmp(&self, other: &SitemapEntry) -> Ordering {
  44. self.permalink.as_ref().cmp(other.permalink.as_ref())
  45. }
  46. }
  47. /// Finds out all the links to put in a sitemap from the pages/sections/taxonomies
  48. /// There are no duplicate permalinks in the output vec
  49. pub fn find_entries<'a>(library: &'a Library, taxonomies: &'a [Taxonomy], config: &'a Config) -> Vec<SitemapEntry<'a>> {
  50. let pages = library
  51. .pages_values()
  52. .iter()
  53. .filter(|p| !p.is_draft())
  54. .map(|p| {
  55. let date = match p.meta.date {
  56. Some(ref d) => Some(d.to_string()),
  57. None => None,
  58. };
  59. let mut entry = SitemapEntry::new(Cow::Borrowed(&p.permalink), date);
  60. entry.add_extra(&p.meta.extra);
  61. entry
  62. })
  63. .collect::<Vec<_>>();
  64. let mut sections = library
  65. .sections_values()
  66. .iter()
  67. .filter(|s| s.meta.render)
  68. .map(|s| SitemapEntry::new(Cow::Borrowed(&s.permalink), None))
  69. .collect::<Vec<_>>();
  70. for section in library
  71. .sections_values()
  72. .iter()
  73. .filter(|s| s.meta.paginate_by.is_some())
  74. {
  75. let number_pagers = (section.pages.len() as f64
  76. / section.meta.paginate_by.unwrap() as f64)
  77. .ceil() as isize;
  78. for i in 1..=number_pagers {
  79. let permalink =
  80. format!("{}{}/{}/", section.permalink, section.meta.paginate_path, i);
  81. sections.push(SitemapEntry::new(Cow::Owned(permalink), None))
  82. }
  83. }
  84. let mut taxonomies_entries = vec![];
  85. for taxonomy in taxonomies {
  86. let name = &taxonomy.kind.name;
  87. let mut terms = vec![];
  88. terms.push(SitemapEntry::new(Cow::Owned(config.make_permalink(name)), None));
  89. for item in &taxonomy.items {
  90. terms.push(SitemapEntry::new(
  91. Cow::Owned(config.make_permalink(&format!("{}/{}", name, item.slug))),
  92. None,
  93. ));
  94. if taxonomy.kind.is_paginated() {
  95. let number_pagers = (item.pages.len() as f64
  96. / taxonomy.kind.paginate_by.unwrap() as f64)
  97. .ceil() as isize;
  98. for i in 1..=number_pagers {
  99. let permalink = config.make_permalink(&format!(
  100. "{}/{}/{}/{}",
  101. name,
  102. item.slug,
  103. taxonomy.kind.paginate_path(),
  104. i
  105. ));
  106. terms.push(SitemapEntry::new(Cow::Owned(permalink), None))
  107. }
  108. }
  109. }
  110. taxonomies_entries.push(terms);
  111. }
  112. let mut all_sitemap_entries = HashSet::new();
  113. for p in pages {
  114. all_sitemap_entries.insert(p);
  115. }
  116. for s in sections {
  117. all_sitemap_entries.insert(s);
  118. }
  119. for terms in taxonomies_entries {
  120. for term in terms {
  121. all_sitemap_entries.insert(term);
  122. }
  123. }
  124. all_sitemap_entries.into_iter().collect::<Vec<_>>()
  125. }