1use crate::fn_path_utils::to_display_path;
2use crate::fn_pathtype::get_file_type;
3use crate::fn_files_blacklist::is_blacklisted_extension;
4use std::collections::HashMap;
5use std::fs;
6use std::path::PathBuf;
7use std::io;
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() { continue; }
33
34 let display_path = to_display_path(path, ¤t_dir);
35
36 if path.exists() {
37 let original_id = id_map.get(path).cloned().unwrap_or_else(|| "BrakID".to_string());
38
39 let header_name = match id_style {
41 "id-num" => format!("Plik-{:03}", file_counter),
42 "id-non" => "Plik".to_string(),
43 _ => format!("Plik-{}", original_id),
44 };
45 file_counter += 1;
46
47 let ext = path.extension().unwrap_or_default().to_string_lossy().to_lowercase();
48 let lang = get_file_type(&ext).md_lang;
49
50 if is_blacklisted_extension(&ext) {
52 content.push_str(&format!("## {}: `{}`\n\n> *(Plik binarny/graficzny - pominięto zawartość)*\n\n", header_name, display_path));
53 continue;
54 }
55
56 match fs::read_to_string(path) {
58 Ok(file_content) => {
59 if lang == "markdown" {
60 content.push_str(&format!("## {}: `{}`\n\n", header_name, display_path));
61 for line in file_content.lines() {
62 if line.trim().is_empty() {
63 content.push_str(">\n");
64 } else {
65 content.push_str(&format!("> {}\n", line));
66 }
67 }
68 content.push_str("\n\n");
69 } else {
70 content.push_str(&format!(
71 "## {}: `{}`\n\n```{}\n{}\n```\n\n",
72 header_name, display_path, lang, file_content
73 ));
74 }
75 }
76 Err(_) => {
77 content.push_str(&format!("## {}: `{}`\n\n> *(Nie można odczytać pliku jako tekst UTF-8 - pominięto)*\n\n", header_name, display_path));
79 }
80 }
81 } else {
82 content.push_str(&format!("## BŁĄD: `{}` (Plik nie istnieje)\n\n", display_path));
83 }
84 }
85
86 fs::write(out_path, &content)?;
87 Ok(())
88}