use std::path::PathBuf;
use std::collections::HashMap;
use glob::glob;
use crate::pcontent::PAbstractLectureBackend;
use crate::plectureparser::{
penvironment_configuration::{
PEnvironment,
update_map_environment
},
plecturebuffer::PLectureBuffer
};
pub struct PEnvironmentManager{
p_map_environment: HashMap<String, PEnvironment>,
p_vec_env_name: Vec<String>,
}
impl PEnvironmentManager {
pub fn new(vec_env_directory: &Vec<PathBuf>) -> Self{
let mut other = PEnvironmentManager{
p_map_environment: Default::default(),
p_vec_env_name: Default::default(),
};
other.load_from_dir(vec_env_directory);
return other;
}
fn load_from_dir(&mut self, vec_config_directory: &Vec<PathBuf>){
for config_directory in vec_config_directory.iter() {
let search_pattern = format!("{}/*.toml", config_directory.to_str().unwrap());
match glob(&search_pattern) {
Ok(fileiter) => {
for config_name in fileiter.into_iter() {
match config_name {
Ok(working_file) => {
update_map_environment(&mut self.p_map_environment, &mut self.p_vec_env_name, &working_file);
},
Err(err) => panic!("PEnvironmentManager::load_from_dir : Cannot use file, error '{}'", err)
}
}
},
Err(_) => panic!("PEnvironmentManager::load_from_dir : Pattern '{}' not found for *.toml configuration", search_pattern)
};
}
self.p_vec_env_name.sort();
}
pub fn write_css(&self, output_css_file: &PathBuf){
let mut buffer = PLectureBuffer::new(output_css_file);
buffer.write(&String::from(format!("/* CSS style of all environements ({}) of the lecture */\n\n", self.p_map_environment.len())));
for (_, env) in self.p_map_environment.iter(){
buffer.write(&String::from(&format!(".{} {{\n{}\n}}\n\n", env.name, env.style.trim())));
if env.is_paragraph_allowed {
buffer.write(&String::from(&format!(".{} p {{\nmargin-top: 3px;\n\tmargin-bottom: 3px;\n}}\n\n", env.name)));
}
}
}
pub fn get_map_environment(&self) -> &HashMap<String, PEnvironment>{
&self.p_map_environment
}
pub fn get_vec_env_name(&self) -> &Vec<String>{
&self.p_vec_env_name
}
}