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.

321 lines
11KB

  1. use std::collections::{HashMap, HashSet};
  2. use std::path::{Path, PathBuf};
  3. use slotmap::{DenseSlotMap, Key};
  4. use front_matter::SortBy;
  5. use content::{Page, Section};
  6. use sorting::{find_siblings, sort_pages_by_date, sort_pages_by_weight};
  7. /// Houses everything about pages and sections
  8. /// Think of it as a database where each page and section has an id (Key here)
  9. /// that can be used to find the actual value
  10. /// Sections and pages can then refer to other elements by those keys, which are very cheap to
  11. /// copy.
  12. /// We can assume the keys are always existing as removing a page/section deletes all references
  13. /// to that key.
  14. #[derive(Debug)]
  15. pub struct Library {
  16. /// All the pages of the site
  17. pages: DenseSlotMap<Page>,
  18. /// All the sections of the site
  19. sections: DenseSlotMap<Section>,
  20. /// A mapping path -> key for pages so we can easily get their key
  21. paths_to_pages: HashMap<PathBuf, Key>,
  22. /// A mapping path -> key for sections so we can easily get their key
  23. pub paths_to_sections: HashMap<PathBuf, Key>,
  24. }
  25. impl Library {
  26. pub fn new(cap_pages: usize, cap_sections: usize) -> Self {
  27. Library {
  28. pages: DenseSlotMap::with_capacity(cap_pages),
  29. sections: DenseSlotMap::with_capacity(cap_sections),
  30. paths_to_pages: HashMap::with_capacity(cap_pages),
  31. paths_to_sections: HashMap::with_capacity(cap_sections),
  32. }
  33. }
  34. /// Add a section and return its Key
  35. pub fn insert_section(&mut self, section: Section) -> Key {
  36. let path = section.file.path.clone();
  37. let key = self.sections.insert(section);
  38. self.paths_to_sections.insert(path, key);
  39. key
  40. }
  41. /// Add a page and return its Key
  42. pub fn insert_page(&mut self, page: Page) -> Key {
  43. let path = page.file.path.clone();
  44. let key = self.pages.insert(page);
  45. self.paths_to_pages.insert(path, key);
  46. key
  47. }
  48. pub fn pages(&self) -> &DenseSlotMap<Page> {
  49. &self.pages
  50. }
  51. pub fn pages_mut(&mut self) -> &mut DenseSlotMap<Page> {
  52. &mut self.pages
  53. }
  54. pub fn pages_values(&self) -> Vec<&Page> {
  55. self.pages.values().collect::<Vec<_>>()
  56. }
  57. pub fn sections(&self) -> &DenseSlotMap<Section> {
  58. &self.sections
  59. }
  60. pub fn sections_mut(&mut self) -> &mut DenseSlotMap<Section> {
  61. &mut self.sections
  62. }
  63. pub fn sections_values(&self) -> Vec<&Section> {
  64. self.sections.values().collect::<Vec<_>>()
  65. }
  66. /// Find out the direct subsections of each subsection if there are some
  67. /// as well as the pages for each section
  68. pub fn populate_sections(&mut self) {
  69. let (root_path, index_path) = self
  70. .sections
  71. .values()
  72. .find(|s| s.is_index())
  73. .map(|s| (s.file.parent.clone(), s.file.path.clone()))
  74. .unwrap();
  75. let root_key = self.paths_to_sections[&index_path];
  76. // We are going to get both the ancestors and grandparents for each section in one go
  77. let mut ancestors: HashMap<PathBuf, Vec<_>> = HashMap::new();
  78. let mut subsections: HashMap<PathBuf, Vec<_>> = HashMap::new();
  79. for section in self.sections.values_mut() {
  80. // Make sure the pages of a section are empty since we can call that many times on `serve`
  81. section.pages = vec![];
  82. section.ignored_pages = vec![];
  83. if let Some(ref grand_parent) = section.file.grand_parent {
  84. subsections
  85. .entry(grand_parent.join("_index.md"))
  86. .or_insert_with(|| vec![])
  87. .push(section.file.path.clone());
  88. }
  89. // Index has no ancestors, no need to go through it
  90. if section.is_index() {
  91. ancestors.insert(section.file.path.clone(), vec![]);
  92. continue;
  93. }
  94. let mut path = root_path.clone();
  95. // Index section is the first ancestor of every single section
  96. let mut parents = vec![root_key];
  97. for component in &section.file.components {
  98. path = path.join(component);
  99. // Skip itself
  100. if path == section.file.parent {
  101. continue;
  102. }
  103. if let Some(section_key) = self.paths_to_sections.get(&path.join("_index.md")) {
  104. parents.push(*section_key);
  105. }
  106. }
  107. ancestors.insert(section.file.path.clone(), parents);
  108. }
  109. for (key, page) in &mut self.pages {
  110. let parent_section_path = page.file.parent.join("_index.md");
  111. if let Some(section_key) = self.paths_to_sections.get(&parent_section_path) {
  112. self.sections.get_mut(*section_key).unwrap().pages.push(key);
  113. page.ancestors =
  114. ancestors.get(&parent_section_path).cloned().unwrap_or_else(|| vec![]);
  115. // Don't forget to push the actual parent
  116. page.ancestors.push(*section_key);
  117. }
  118. }
  119. self.sort_sections_pages();
  120. let sections = self.paths_to_sections.clone();
  121. let mut sections_weight = HashMap::new();
  122. for (key, section) in &self.sections {
  123. sections_weight.insert(key, section.meta.weight);
  124. }
  125. for section in self.sections.values_mut() {
  126. if let Some(ref children) = subsections.get(&section.file.path) {
  127. let mut children: Vec<_> = children.iter().map(|p| sections[p]).collect();
  128. children.sort_by(|a, b| sections_weight[a].cmp(&sections_weight[b]));
  129. section.subsections = children;
  130. }
  131. section.ancestors =
  132. ancestors.get(&section.file.path).cloned().unwrap_or_else(|| vec![]);
  133. }
  134. }
  135. /// Sort all sections pages
  136. pub fn sort_sections_pages(&mut self) {
  137. let mut updates = HashMap::new();
  138. for (key, section) in &self.sections {
  139. let (sorted_pages, cannot_be_sorted_pages) = match section.meta.sort_by {
  140. SortBy::None => continue,
  141. SortBy::Date => {
  142. let data = section
  143. .pages
  144. .iter()
  145. .map(|k| {
  146. if let Some(page) = self.pages.get(*k) {
  147. (k, page.meta.datetime, page.permalink.as_ref())
  148. } else {
  149. unreachable!("Sorting got an unknown page")
  150. }
  151. })
  152. .collect();
  153. sort_pages_by_date(data)
  154. }
  155. SortBy::Weight => {
  156. let data = section
  157. .pages
  158. .iter()
  159. .map(|k| {
  160. if let Some(page) = self.pages.get(*k) {
  161. (k, page.meta.weight, page.permalink.as_ref())
  162. } else {
  163. unreachable!("Sorting got an unknown page")
  164. }
  165. })
  166. .collect();
  167. sort_pages_by_weight(data)
  168. }
  169. };
  170. updates.insert(key, (sorted_pages, cannot_be_sorted_pages, section.meta.sort_by));
  171. }
  172. for (key, (sorted, cannot_be_sorted, sort_by)) in updates {
  173. // Find sibling between sorted pages first
  174. let with_siblings = find_siblings(
  175. sorted
  176. .iter()
  177. .map(|k| {
  178. if let Some(page) = self.pages.get(*k) {
  179. (k, page.is_draft())
  180. } else {
  181. unreachable!("Sorting got an unknown page")
  182. }
  183. })
  184. .collect(),
  185. );
  186. for (k2, val1, val2) in with_siblings {
  187. if let Some(page) = self.pages.get_mut(k2) {
  188. match sort_by {
  189. SortBy::Date => {
  190. page.earlier = val2;
  191. page.later = val1;
  192. }
  193. SortBy::Weight => {
  194. page.lighter = val1;
  195. page.heavier = val2;
  196. }
  197. SortBy::None => unreachable!("Impossible to find siblings in SortBy::None"),
  198. }
  199. } else {
  200. unreachable!("Sorting got an unknown page")
  201. }
  202. }
  203. if let Some(s) = self.sections.get_mut(key) {
  204. s.pages = sorted;
  205. s.ignored_pages = cannot_be_sorted;
  206. }
  207. }
  208. }
  209. /// Find all the orphan pages: pages that are in a folder without an `_index.md`
  210. pub fn get_all_orphan_pages(&self) -> Vec<&Page> {
  211. let pages_in_sections =
  212. self.sections.values().flat_map(|s| &s.pages).collect::<HashSet<_>>();
  213. self.pages
  214. .iter()
  215. .filter(|(key, _)| !pages_in_sections.contains(&key))
  216. .map(|(_, page)| page)
  217. .collect()
  218. }
  219. pub fn find_parent_section(&self, path: &Path) -> Option<&Section> {
  220. let page_key = self.paths_to_pages[path];
  221. for s in self.sections.values() {
  222. if s.pages.contains(&page_key) {
  223. return Some(s);
  224. }
  225. }
  226. None
  227. }
  228. /// Only used in tests
  229. pub fn get_section_key(&self, path: &PathBuf) -> Option<&Key> {
  230. self.paths_to_sections.get(path)
  231. }
  232. pub fn get_section(&self, path: &PathBuf) -> Option<&Section> {
  233. self.sections.get(self.paths_to_sections.get(path).cloned().unwrap_or_default())
  234. }
  235. pub fn get_section_mut(&mut self, path: &PathBuf) -> Option<&mut Section> {
  236. self.sections.get_mut(self.paths_to_sections.get(path).cloned().unwrap_or_default())
  237. }
  238. pub fn get_section_by_key(&self, key: Key) -> &Section {
  239. self.sections.get(key).unwrap()
  240. }
  241. pub fn get_section_mut_by_key(&mut self, key: Key) -> &mut Section {
  242. self.sections.get_mut(key).unwrap()
  243. }
  244. pub fn get_section_path_by_key(&self, key: Key) -> &str {
  245. &self.get_section_by_key(key).file.relative
  246. }
  247. pub fn get_page(&self, path: &PathBuf) -> Option<&Page> {
  248. self.pages.get(self.paths_to_pages.get(path).cloned().unwrap_or_default())
  249. }
  250. pub fn get_page_by_key(&self, key: Key) -> &Page {
  251. self.pages.get(key).unwrap()
  252. }
  253. pub fn remove_section(&mut self, path: &PathBuf) -> Option<Section> {
  254. if let Some(k) = self.paths_to_sections.remove(path) {
  255. self.sections.remove(k)
  256. } else {
  257. None
  258. }
  259. }
  260. pub fn remove_page(&mut self, path: &PathBuf) -> Option<Page> {
  261. if let Some(k) = self.paths_to_pages.remove(path) {
  262. self.pages.remove(k)
  263. } else {
  264. None
  265. }
  266. }
  267. /// Used in rebuild, to check if we know it already
  268. pub fn contains_section(&self, path: &PathBuf) -> bool {
  269. self.paths_to_sections.contains_key(path)
  270. }
  271. /// Used in rebuild, to check if we know it already
  272. pub fn contains_page(&self, path: &PathBuf) -> bool {
  273. self.paths_to_pages.contains_key(path)
  274. }
  275. }