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.

117 lines
3.6KB

  1. use std::path::{Path, PathBuf};
  2. /// Takes a full path to a file and returns only the components after the first `content` directory
  3. /// Will not return the filename as last component
  4. pub fn find_content_components<P: AsRef<Path>>(path: P) -> Vec<String> {
  5. let path = path.as_ref();
  6. let mut is_in_content = false;
  7. let mut components = vec![];
  8. for section in path.parent().unwrap().components() {
  9. let component = section.as_ref().to_string_lossy();
  10. if is_in_content {
  11. components.push(component.to_string());
  12. continue;
  13. }
  14. if component == "content" {
  15. is_in_content = true;
  16. }
  17. }
  18. components
  19. }
  20. /// Struct that contains all the information about the actual file
  21. #[derive(Debug, Clone, PartialEq)]
  22. pub struct FileInfo {
  23. /// The full path to the .md file
  24. pub path: PathBuf,
  25. /// The name of the .md file without the extension, always `_index` for sections
  26. pub name: String,
  27. /// The .md path, starting from the content directory, with `/` slashes
  28. pub relative: String,
  29. /// Path of the directory containing the .md file
  30. pub parent: PathBuf,
  31. /// Path of the grand parent directory for that file. Only used in sections to find subsections.
  32. pub grand_parent: Option<PathBuf>,
  33. /// The folder names to this section file, starting from the `content` directory
  34. /// For example a file at content/kb/solutions/blabla.md will have 2 components:
  35. /// `kb` and `solutions`
  36. pub components: Vec<String>,
  37. }
  38. impl FileInfo {
  39. pub fn new_page(path: &Path) -> FileInfo {
  40. let file_path = path.to_path_buf();
  41. let mut parent = file_path.parent().unwrap().to_path_buf();
  42. let name = path.file_stem().unwrap().to_string_lossy().to_string();
  43. let mut components = find_content_components(&file_path);
  44. let relative = format!("{}/{}.md", components.join("/"), name);
  45. // If we have a folder with an asset, don't consider it as a component
  46. if !components.is_empty() && name == "index" {
  47. components.pop();
  48. // also set parent_path to grandparent instead
  49. parent = parent.parent().unwrap().to_path_buf();
  50. }
  51. FileInfo {
  52. path: file_path,
  53. // We don't care about grand parent for pages
  54. grand_parent: None,
  55. parent,
  56. name,
  57. components,
  58. relative,
  59. }
  60. }
  61. pub fn new_section(path: &Path) -> FileInfo {
  62. let parent = path.parent().unwrap().to_path_buf();
  63. let components = find_content_components(path);
  64. let relative = if components.is_empty() {
  65. // the index one
  66. "_index.md".to_string()
  67. } else {
  68. format!("{}/_index.md", components.join("/"))
  69. };
  70. let grand_parent = parent.parent().map(|p| p.to_path_buf());
  71. FileInfo {
  72. path: path.to_path_buf(),
  73. parent,
  74. grand_parent,
  75. name: "_index".to_string(),
  76. components,
  77. relative,
  78. }
  79. }
  80. }
  81. #[doc(hidden)]
  82. impl Default for FileInfo {
  83. fn default() -> FileInfo {
  84. FileInfo {
  85. path: PathBuf::new(),
  86. parent: PathBuf::new(),
  87. grand_parent: None,
  88. name: String::new(),
  89. components: vec![],
  90. relative: String::new(),
  91. }
  92. }
  93. }
  94. #[cfg(test)]
  95. mod tests {
  96. use super::find_content_components;
  97. #[test]
  98. fn can_find_content_components() {
  99. let res = find_content_components("/home/vincent/code/site/content/posts/tutorials/python.md");
  100. assert_eq!(res, ["posts".to_string(), "tutorials".to_string()]);
  101. }
  102. }