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.

142 lines
4.4KB

  1. use std::borrow::Cow;
  2. use std::collections::HashSet;
  3. use std::hash::{Hash, Hasher};
  4. use config::Config;
  5. use library::{Library, Taxonomy};
  6. use std::cmp::Ordering;
  7. use tera::{Map, Value};
  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. pub permalink: Cow<'a, str>,
  13. pub date: Option<String>,
  14. pub 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>(
  50. library: &'a Library,
  51. taxonomies: &'a [Taxonomy],
  52. config: &'a Config,
  53. ) -> Vec<SitemapEntry<'a>> {
  54. let pages = library
  55. .pages_values()
  56. .iter()
  57. .map(|p| {
  58. let date = match p.meta.date {
  59. Some(ref d) => Some(d.to_string()),
  60. None => None,
  61. };
  62. let mut entry = SitemapEntry::new(Cow::Borrowed(&p.permalink), date);
  63. entry.add_extra(&p.meta.extra);
  64. entry
  65. })
  66. .collect::<Vec<_>>();
  67. let mut sections = library
  68. .sections_values()
  69. .iter()
  70. .filter(|s| s.meta.render)
  71. .map(|s| {
  72. let mut entry = SitemapEntry::new(Cow::Borrowed(&s.permalink), None);
  73. entry.add_extra(&s.meta.extra);
  74. entry
  75. })
  76. .collect::<Vec<_>>();
  77. for section in library.sections_values().iter().filter(|s| s.meta.paginate_by.is_some()) {
  78. let number_pagers =
  79. (section.pages.len() as f64 / section.meta.paginate_by.unwrap() as f64).ceil() as isize;
  80. for i in 1..=number_pagers {
  81. let permalink = format!("{}{}/{}/", section.permalink, section.meta.paginate_path, i);
  82. sections.push(SitemapEntry::new(Cow::Owned(permalink), None))
  83. }
  84. }
  85. let mut taxonomies_entries = vec![];
  86. for taxonomy in taxonomies {
  87. let name = &taxonomy.kind.name;
  88. let mut terms = vec![];
  89. terms.push(SitemapEntry::new(Cow::Owned(config.make_permalink(name)), None));
  90. for item in &taxonomy.items {
  91. terms.push(SitemapEntry::new(
  92. Cow::Owned(config.make_permalink(&format!("{}/{}", name, item.slug))),
  93. None,
  94. ));
  95. if taxonomy.kind.is_paginated() {
  96. let number_pagers = (item.pages.len() as f64
  97. / taxonomy.kind.paginate_by.unwrap() as f64)
  98. .ceil() as isize;
  99. for i in 1..=number_pagers {
  100. let permalink = config.make_permalink(&format!(
  101. "{}/{}/{}/{}",
  102. name,
  103. item.slug,
  104. taxonomy.kind.paginate_path(),
  105. i
  106. ));
  107. terms.push(SitemapEntry::new(Cow::Owned(permalink), None))
  108. }
  109. }
  110. }
  111. taxonomies_entries.push(terms);
  112. }
  113. let mut all_sitemap_entries = HashSet::new();
  114. for p in pages {
  115. all_sitemap_entries.insert(p);
  116. }
  117. for s in sections {
  118. all_sitemap_entries.insert(s);
  119. }
  120. for terms in taxonomies_entries {
  121. for term in terms {
  122. all_sitemap_entries.insert(term);
  123. }
  124. }
  125. all_sitemap_entries.into_iter().collect::<Vec<_>>()
  126. }