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.

138 lines
4.3KB

  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. 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>(
  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| SitemapEntry::new(Cow::Borrowed(&s.permalink), None))
  72. .collect::<Vec<_>>();
  73. for section in library.sections_values().iter().filter(|s| s.meta.paginate_by.is_some()) {
  74. let number_pagers =
  75. (section.pages.len() as f64 / section.meta.paginate_by.unwrap() as f64).ceil() as isize;
  76. for i in 1..=number_pagers {
  77. let permalink = format!("{}{}/{}/", section.permalink, section.meta.paginate_path, i);
  78. sections.push(SitemapEntry::new(Cow::Owned(permalink), None))
  79. }
  80. }
  81. let mut taxonomies_entries = vec![];
  82. for taxonomy in taxonomies {
  83. let name = &taxonomy.kind.name;
  84. let mut terms = vec![];
  85. terms.push(SitemapEntry::new(Cow::Owned(config.make_permalink(name)), None));
  86. for item in &taxonomy.items {
  87. terms.push(SitemapEntry::new(
  88. Cow::Owned(config.make_permalink(&format!("{}/{}", name, item.slug))),
  89. None,
  90. ));
  91. if taxonomy.kind.is_paginated() {
  92. let number_pagers = (item.pages.len() as f64
  93. / taxonomy.kind.paginate_by.unwrap() as f64)
  94. .ceil() as isize;
  95. for i in 1..=number_pagers {
  96. let permalink = config.make_permalink(&format!(
  97. "{}/{}/{}/{}",
  98. name,
  99. item.slug,
  100. taxonomy.kind.paginate_path(),
  101. i
  102. ));
  103. terms.push(SitemapEntry::new(Cow::Owned(permalink), None))
  104. }
  105. }
  106. }
  107. taxonomies_entries.push(terms);
  108. }
  109. let mut all_sitemap_entries = HashSet::new();
  110. for p in pages {
  111. all_sitemap_entries.insert(p);
  112. }
  113. for s in sections {
  114. all_sitemap_entries.insert(s);
  115. }
  116. for terms in taxonomies_entries {
  117. for term in terms {
  118. all_sitemap_entries.insert(term);
  119. }
  120. }
  121. all_sitemap_entries.into_iter().collect::<Vec<_>>()
  122. }