rustyphoenixlecture 1.2.0

This project aims to provide a simple a powerfull lecture compilation to generate html web sites
/***************************************
	Auteur : Pierre Aubert
	Mail : pierre.aubert@lapp.in2p3.fr
	Licence : CeCILL-C
****************************************/

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
};


///Manager of all environement of the lecture
pub struct PEnvironmentManager{
	///Map of all loaded environment of the lecture
	p_map_environment: HashMap<String, PEnvironment>,
	///Vector of the environments names
	p_vec_env_name: Vec<String>,
}

impl PEnvironmentManager {
	///Constructor of the PEnvironmentManager
	/// # Parameters
	/// - `vec_env_directory` : vector of directories where to get all configuration of all environment to be used
	/// # Returns
	/// Initialised 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;
	}
	///Load the PEnvironmentManager from a configuration file
	/// # Parameters
	/// - `vec_config_directory` : vector of directories where to get all configuration of all PEnvironment to be used
	fn load_from_dir(&mut self, vec_config_directory: &Vec<PathBuf>){
		//Let's iterate on all configuration directories
		for config_directory in vec_config_directory.iter() {
			//Let's get all the toml files in this directory
			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();
	}
	///Write the CSS file of the environment
	/// # Parameters
	/// - `output_css_file` : name of the css file to write
	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 {
				//Let's define the paragraph style inside this environment
				buffer.write(&String::from(&format!(".{} p {{\nmargin-top: 3px;\n\tmargin-bottom: 3px;\n}}\n\n", env.name)));
			}
		}
	}
	///Get the map of available environements
	/// # Returns
	/// Map of available environements
	pub fn get_map_environment(&self) -> &HashMap<String, PEnvironment>{
		&self.p_map_environment
	}
	///Get the vector of environements name
	/// # Returns
	/// Vector of environements names
	pub fn get_vec_env_name(&self) -> &Vec<String>{
		&self.p_vec_env_name
	}
}