rustyphoenixlecture 1.7.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 crate::{
	phighlighter::PLocation,
	pcontent::{
		pabstractcontent::{PAbstractContent, PAbstractLectureBackend},
		plabeler::PLabelId,
		preferenceurl::PReferenceUrl,
		pveccontent::PVecContent
	}
};

///Content object of the lecture
#[derive(Debug, Clone, Default, PartialEq)]
pub struct PContentEnvironment{
	///Name of the environment
	p_name: String,
	///Main balise to use around this environment
	p_balise: String,
	///Vector of children
	p_content: PVecContent,
	///Image of the environement
	p_image: String,
	///Location where the PContentEvironement is defined in the lecture
	p_location: PLocation,
}

impl PContentEnvironment {
	///Constructor of the PContentEnvironment
	/// # Parameters
	/// - `name` : name of the environement
	/// - `balise` : balise to use around this environement
	/// - `image` : image to illustrate the environment (could be empty if there is no image)
	/// - `location` : location where the current environement is defined
	/// # Returns
	/// Initialised PContentEnvironment
	pub fn new(name: &String, balise: &String, image: &String, location: &PLocation) -> Self{
		PContentEnvironment {
			p_name: name.clone(),
			p_balise: balise.clone(),
			p_content: Default::default(),
			p_image: image.clone(),
			p_location: location.clone(),
		}
	}
	///Get the content of the PContentEnvironmentWorkInProgress
	/// # Returns
	/// Content of the PContentEnvironmentWorkInProgress
	pub fn get_content(&self) -> &PVecContent {
		&self.p_content
	}
	///Get the mutable content of the PContentEnvironmentWorkInProgress
	/// # Returns
	/// Mutable content of the PContentEnvironmentWorkInProgress
	pub fn get_content_mut(&mut self) -> &mut PVecContent {
		&mut self.p_content
	}
	///Get the name of the current environment
	/// # Returns
	/// Name of the current environment
	pub fn get_name(&self) -> &String{
		&self.p_name
	}
	///Get the location of the current environment
	/// # Returns
	/// PLocation of the current environment
	pub fn get_location(&self) -> &PLocation{
		&self.p_location
	}
}

impl PAbstractContent for PContentEnvironment{
	///Say if the PContent has an embeded label
	/// # Returns
	/// True if the PContent has an embeded label, false otherwise
	fn has_embeded_label(&self) -> bool{
		false
	}
	///Get the reference url of the current PContent
	/// # Parameters
	/// - `current_file` : current output file of the PContent
	/// - `id` : id of the current PContent
	/// # Returns
	/// Corresponding PReferenceUrl
	fn get_reference_url(&self, current_file: &String, id: usize) -> PReferenceUrl{
		PReferenceUrl::from_text(current_file, id, &String::from(&format!("PContentEnvironment({}) {}", self.p_name, id)))
	}
	///Convert the current struct into html
	/// # Parameters
	/// - `backend` : backend which write a lecture in files
	/// - `id` : id of the current PContent
	fn to_html<TLectureBackend>(&self, backend: &mut TLectureBackend, id: &PLabelId)
		where TLectureBackend: PAbstractLectureBackend
	{
		if self.p_image.is_empty() {
			backend.write(&String::from(&format!("<{} id=\"{}\" class=\"{}\">", self.p_balise, id.get_id(), self.p_name)));
			self.p_content.to_html(backend, id);
			backend.write(&String::from(&format!("</{}>\n", self.p_balise)));
		}else{
			backend.write(&format!("\n<div id=\"{}\" class=\"workinprogress\">\n", id.get_id()));
			backend.write(&String::from(format!("\t<div class=\"workinprogressimage\"><img src=\"{}\" alt=\"wip\" /></div>\n", self.p_image)));
			backend.write(&String::from(&format!("<{} id=\"{}\" class=\"{}\">", self.p_balise, id.get_id(), self.p_name)));
			self.p_content.to_html(backend, id);
			backend.write(&String::from(format!("</{}>\n</div>\n", self.p_balise)));
			
			// backend.write(&format!("\n<table id=\"{}\" class=\"workinprogress\">\n", id.get_id()));
			// backend.write(&String::from(format!("\t<tr><td class=\"workinprogressimage\"><img src=\"{}\" alt=\"wip\" /></td></tr>\n", self.p_image)));
			// backend.write(&String::from(format!("\t<tr><td><{} class=\"{}\">", self.p_balise, self.p_name)));
			// self.p_content.to_html(backend, id);
			// backend.write(&String::from(format!("</{}></td></tr>\n</table>\n", self.p_balise)));
		}
	}
}

#[cfg(test)]
mod tests{
	use super::*;
	use crate::pcontent::{
		pstrbackend::PStrBackend,
		PContentType,
	};
	///Test the PContentEnvironment
	#[test]
	fn test_content_environment(){
		let mut environement: PContentEnvironment = PContentEnvironment::new(&String::from("test"), &String::from("div"), &String::from(""), &Default::default());
		environement.get_content_mut().add_child(&PContentType::from_text(43, &String::from("Some text.")));
		let mut backend = PStrBackend::new();
		environement.to_html(&mut backend, &PLabelId::new(42));
		assert_eq!(backend.get_body(), &String::from("<div id=\"42\" class=\"test\">Some text.</div>\n"));
	}
}