1use crate::fn_files_blacklist::is_blacklisted_extension;
2use crate::fn_path_utils::to_display_path;
3use crate::fn_pathtype::get_file_type;
4use std::collections::HashMap;
5use std::fs;
6use std::io;
7use std::path::PathBuf;
8
9pub fn write_md(
10 out_path: &str,
11 files: &[PathBuf],
12 id_map: &HashMap<PathBuf, String>,
13 tree_text: Option<String>,
14 _stamp: &str,
15 id_style: &str,
16) -> io::Result<()> {
17 let mut content = String::new();
18
19 content.push_str(&format!("# RAPORT ({})\n\n", out_path));
20 if let Some(tree) = tree_text {
23 content.push_str("```text\n");
24 content.push_str(&tree);
25 content.push_str("```\n\n");
26 }
27
28 let current_dir = std::env::current_dir().unwrap_or_default();
29 let mut file_counter = 1;
30
31 for path in files {
32 if path.is_dir() {
33 continue;
34 }
35
36 let display_path = to_display_path(path, ¤t_dir);
37
38 if path.exists() {
39 let original_id = id_map
40 .get(path)
41 .cloned()
42 .unwrap_or_else(|| "BrakID".to_string());
43
44 let header_name = match id_style {
46 "id-num" => format!("Plik-{:03}", file_counter),
47 "id-non" => "Plik".to_string(),
48 _ => format!("Plik-{}", original_id),
49 };
50 file_counter += 1;
51
52 let ext = path
53 .extension()
54 .unwrap_or_default()
55 .to_string_lossy()
56 .to_lowercase();
57 let lang = get_file_type(&ext).md_lang;
58
59 if is_blacklisted_extension(&ext) {
61 content.push_str(&format!(
62 "## {}: `{}`\n\n> *(Plik binarny/graficzny - pominięto zawartość)*\n\n",
63 header_name, display_path
64 ));
65 continue;
66 }
67
68 match fs::read_to_string(path) {
70 Ok(file_content) => {
71 if lang == "markdown" {
72 content.push_str(&format!("## {}: `{}`\n\n", header_name, display_path));
73 for line in file_content.lines() {
74 if line.trim().is_empty() {
75 content.push_str(">\n");
76 } else {
77 content.push_str(&format!("> {}\n", line));
78 }
79 }
80 content.push_str("\n\n");
81 } else {
82 content.push_str(&format!(
83 "## {}: `{}`\n\n```{}\n{}\n```\n\n",
84 header_name, display_path, lang, file_content
85 ));
86 }
87 }
88 Err(_) => {
89 content.push_str(&format!("## {}: `{}`\n\n> *(Nie można odczytać pliku jako tekst UTF-8 - pominięto)*\n\n", header_name, display_path));
91 }
92 }
93 } else {
94 content.push_str(&format!(
95 "## BŁĄD: `{}` (Plik nie istnieje)\n\n",
96 display_path
97 ));
98 }
99 }
100
101 fs::write(out_path, &content)?;
102 Ok(())
103}