rustyphoenixlecture 1.4.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::pcontent::{
	pabstractcontent::{PAbstractContent, PAbstractLectureBackend},
	PReferenceUrl,
	phoenix_html_label::phoenix_html_label,
};

///Manage a label and an id
pub struct PLabelId{
	///Id of the PLabelId
	p_id: usize,
	///Label of the PLabelId
	p_label: String,
}

impl PLabelId{
	///Constructor of a PLabelId
	/// # Parameters
	/// - `id` : id of the current PContent
	/// # Returns
	/// Initialised PLabelId
	pub fn new(id: usize) -> Self{
		PLabelId {
			p_id: id,
			p_label: String::from(""),
		}
	}
	///Get the Id of the current PLabelId
	/// # Returns
	/// Id of the current PLabelId
	pub fn get_id(&self) -> usize{
		self.p_id
	}
	///Get the Label of the current PLabelId
	/// # Returns
	/// Label of the current PLabelId
	pub fn get_label(&self) -> &String{
		&self.p_label
	}
	///Convert the current struct into html
	/// # Parameters
	/// - `backend` : backend which write a lecture in files
	/// - `with_id` : true if we need to put the id in the label name (such as (id) )
	pub fn to_html<TLectureBackend>(&self, backend: &mut TLectureBackend, with_id: bool)
		where TLectureBackend: PAbstractLectureBackend
	{
		if with_id {
			backend.write(&phoenix_html_label(&self.p_label, Some(self.p_id)));
		}else{
			backend.write(&phoenix_html_label(&self.p_label, None));
		}
	}
}

///Manage label and id of all PContent to avoid code dupplication
#[derive(Debug, Clone, PartialEq)]
pub struct PLabeler<TData>{
	///Id of the PLabeler
	p_id: usize,
	///Label of the PLabeler
	p_label: String,
	///Data of the PLabeler
	p_data: TData,
}

impl<TData> PLabeler<TData>
	where TData: Clone + PAbstractContent
{
	///Constructor of a PLabeler
	/// # Parameters
	/// - `id` : id of the current PContent
	/// # Returns
	/// Initialised PLabeler
	pub fn new(id: usize, data: &TData) -> Self{
		PLabeler {
			p_id: id,
			p_label: String::from(""),
			p_data: data.clone(),
		}
	}
	///Set the label of the current PLabeler
	/// # Parameters
	/// - `label` : label of the current PLabeler
	pub fn set_label(&mut self, label: &String){
		self.p_label = label.clone();
	}
	///Get the label of the current PLabeler
	/// # Returns
	/// Label of the current PLabeler
	pub fn get_label(&self) -> &String{
		&self.p_label
	}
	///Get the Id of the current PLabeler
	/// # Returns
	/// Id of the current PLabeler
	pub fn get_id(&self) -> usize{
		self.p_id
	}
	///Get the data of the current PLabeler
	/// # Returns
	/// Data of the current PLabeler
	pub fn get_data(&self) -> &TData{
		&self.p_data
	}
	///Get the muttable data of the current PLabeler
	/// # Returns
	/// Muttable data of the current PLabeler
	pub fn get_data_mut(&mut self) -> &mut TData{
		&mut self.p_data
	}
	///Get the reference url of the current PContent
	/// # Parameters
	/// - `current_file` : current output file of the PContent
	/// # Returns
	/// Corresponding PReferenceUrl
	pub fn get_reference_url(&self, current_file: &String) -> PReferenceUrl{
		self.p_data.get_reference_url(current_file, self.p_id)
	}
	///Convert the current struct into html
	/// # Parameters
	/// - `backend` : backend which write a lecture in files
	pub fn to_html<TLectureBackend>(&self, backend: &mut TLectureBackend)
		where TLectureBackend: PAbstractLectureBackend
	{
		self.p_data.to_html(backend, &PLabelId { p_id: self.p_id, p_label: self.p_label.clone() });
		if !self.p_data.has_embeded_label() {
			backend.write(&phoenix_html_label(&self.p_label, None));
		}
	}
}

#[cfg(test)]
mod tests{
	use crate::pcontent::{
		pstrbackend::PStrBackend,
		pcontenttext::PContentText
	};

	use super::*;

	///Test the PLabeler add content
	#[test]
	fn test_plabeler(){
		let mut labeler: PLabeler<PContentText> = PLabeler::new(42, &PContentText::new(&String::from("Some text")));
		labeler.set_label(&String::from("somelabel"));
		
		assert_eq!(labeler.get_id(), 42);
		assert_eq!(labeler.get_label(), &String::from("somelabel"));
		
		let mut backend = PStrBackend::new();
		labeler.to_html(&mut backend);
		assert_eq!(backend.get_body(), &String::from("Some text<span class=\"redirectIcon\"><a href=\"redirection.html?label=somelabel\"><img src=\"book/images/iconlink_32.png\"></a></span>"));
	}
}