main.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. use std::path::Path;
  2. use std::{fs, vec::Vec};
  3. use comrak::{markdown_to_html, ComrakOptions};
  4. fn list_markdown_files(path: &Path) -> Vec<String> {
  5. let mut files = Vec::<String>::new();
  6. let dir_entries = fs::read_dir(path);
  7. match dir_entries {
  8. Ok(dir) => {
  9. for entry in dir {
  10. match entry {
  11. Ok(entry) => {
  12. if entry.file_type().unwrap().is_dir() {
  13. let mut recursively_obtained = list_markdown_files(&entry.path());
  14. files.append(&mut recursively_obtained);
  15. } else {
  16. let path = entry.path();
  17. let extension_wrapped = path.extension();
  18. match extension_wrapped {
  19. Some(extension) => {
  20. if extension == "md" {
  21. files.push(entry.path().to_str().expect("Couldn't use file path.").to_owned());
  22. }
  23. },
  24. None => {}
  25. }
  26. }
  27. },
  28. Err(_) => {
  29. println!("Invalid entry found.");
  30. }
  31. }
  32. }
  33. },
  34. Err(err) => {
  35. println!("Error while opening directory: {}", err.to_string());
  36. }
  37. }
  38. files
  39. }
  40. fn md_to_html(file: String) -> Result<String, String> {
  41. let content = fs::read_to_string(file);
  42. match content {
  43. Ok(content) => Ok(markdown_to_html(&content, &ComrakOptions::default())),
  44. Err(err) => Err(err.to_string())
  45. }
  46. }
  47. #[cfg(test)]
  48. mod tests_listing {
  49. use std::env;
  50. use std::path::Path;
  51. use crate::list_markdown_files;
  52. #[test]
  53. fn list_files_recursively() {
  54. let directory_path = env::var("TEST_DIRECTORY_PATH").unwrap_or("".to_owned());
  55. let files_list = list_markdown_files(Path::new(&directory_path));
  56. assert_eq!(files_list.len(), 2);
  57. }
  58. #[test]
  59. fn on_nonexistant_dir_empty_list() {
  60. let files_list = list_markdown_files(Path::new("/usr/fake/path"));
  61. assert_eq!(files_list.len(), 0);
  62. }
  63. }
  64. fn main() -> Result<(), Box<dyn std::error::Error + 'static>> {
  65. let files = list_markdown_files(Path::new(""));
  66. let base_directory = "";
  67. let header_path = format!("{}/header.html", base_directory);
  68. let header_path = Path::new(&header_path);
  69. let footer_path = format!("{}/footer.html", base_directory);
  70. let footer_path = Path::new(&footer_path);
  71. if !header_path.exists() {
  72. println!("[warning] No header.html found.");
  73. }
  74. if footer_path.exists() {
  75. println!("[warning] No footer.html found.");
  76. }
  77. let header_content = fs::read_to_string(header_path)?;
  78. let footer_content = fs::read_to_string(footer_path)?;
  79. // TODO:
  80. // 1. Get working directory from args program -d <input_directory> -o <output_directory>
  81. // 2. Get relative paths from input_directory. e.g.: /this/is/folder/[get/this/part/only]
  82. // to be able to construct same relative paths in output.
  83. // 3. Convert content to new content and save file. (md_to_html should do it)
  84. //
  85. for file in files {
  86. let html_content = md_to_html(file)?;
  87. let assembled = format!("{}{}{}", header_content, html_content, footer_content);
  88. if fs::write(Path::new(base_directory), assembled).is_err() {
  89. println!("Couldn't not write to file.");
  90. }
  91. // Minification of HTML?
  92. }
  93. Ok(())
  94. }