rustyphoenixlecture 1.0.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::plectureparser::{
	pabstractcontent::{PAbstractContent, PAbstractLectureBackend},
	pcontenttext::PContentText,
	pidcounter::PIdCounter,
	plabeler::{PLabelId, PLabeler},
	preferenceurl::PReferenceUrl,
	pveccontent::{PContentType, PVecContent}
};

///Content object of the lecture
#[derive(Debug, Clone, Default, PartialEq)]
pub struct PContentParagraph{
	///Vector of children
	content: PVecContent,
}

impl PContentParagraph {
	///Constructor of the PContentParagraph
	/// # Returns
	/// Initialised PContentParagraph
	pub fn new() -> Self{
		PContentParagraph {
			content: Default::default(),
		}
	}
	///Get the content of the PContentParagraphWorkInProgress
	/// # Returns
	/// Content of the PContentParagraphWorkInProgress
	pub fn get_content(&self) -> &PVecContent {
		&self.content
	}
	///Get the mutable content of the PContentParagraphWorkInProgress
	/// # Returns
	/// Mutable content of the PContentParagraphWorkInProgress
	pub fn get_content_mut(&mut self) -> &mut PVecContent {
		&mut self.content
	}
	///Add text in the given paragraph
	/// # Parameters
	/// - `id` : id manager
	/// - `text` : text to be added in the paragraph
	pub fn add_text(&mut self, id: &mut PIdCounter, text: &String){
		//First we trim the text
		let trim_text = String::from(text.trim_matches('\n'));
		if trim_text.is_empty() {	//If the text is empty, we do nothing
			return;
		}
		match self.get_content_mut().get_vec_child_mut().last_mut() {
			Some(last_child_paragraph) => {
				//If the last child is a text
				match last_child_paragraph {
					PContentType::Text(child_text) => child_text.get_data_mut().add_text(&trim_text),
					//If not, we create the missing PContentText
					_ => {
						//We create the text
						self.create_text(id, &trim_text);
					}
				}
			},
			//If the paragraph is empty, we add the text in it
			None =>{
				//We create the text
				self.create_text(id, &trim_text);
			}
		}
	}
	///Add text in the given paragraph and create the missing PContentText
	/// # Parameters
	/// - `id` : id manager
	/// - `trim_text` : trimed text without newline to be added in the paragraph
	fn create_text(&mut self, id: &mut PIdCounter, trim_text: &String){
		let text_content = PContentType::Text(PLabeler::new(id.get_id(), &PContentText::new(&trim_text)));
		self.get_content_mut().add_child(&text_content);
	}
}

impl PAbstractContent for PContentParagraph{
	///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{
		true
	}
	///Get the reference url of the current PContentParagraph
	/// # Parameters
	/// - `current_file` : current output file of the PContentParagraph
	/// - `id` : id of the PContent
	/// # Returns
	/// Corresponding PReferenceUrl
	fn get_reference_url(&self, current_file: &String, id: usize) -> PReferenceUrl{
		PReferenceUrl::from_text(current_file, id, &String::from(&format!("PContentParagraph {}", id)))
	}
	///Convert the current struct into html
	/// # Parameters
	/// - `backend` : backend which write a lecture in files
	fn to_html<TLectureBackend>(&self, backend: &mut TLectureBackend, id: &PLabelId)
		where TLectureBackend: PAbstractLectureBackend
	{
		//If the paragraph is empty, we do not save it
		if self.content.len() == 0 {
			return;
		}
		backend.write(&format!("<p id=\"{}\">", id.get_id()));
		self.content.to_html(backend, id);
		id.to_html(backend, false);
		backend.write(&String::from("</p>\n"))
	}
}

#[cfg(test)]
mod tests{
	use super::*;
	use crate::plectureparser::{pcontentstyle::PContentStyle, plabeler::PLabeler};

	///Test the PContentParagraph add_text
	#[test]
	fn test_pcontent_paragraph_add_text(){
		let id = &mut PIdCounter::new(0);	//Just to avoid to have mut everywhere
		let mut paragraph = PContentParagraph::new();
		paragraph.add_text(id, &String::from("Patricia mon petit"));
		assert_eq!(paragraph.get_content().len(), 1);
		paragraph.add_text(id, &String::from(", l'homme de la pampa parfois rude reste toujours courtois."));
		assert_eq!(paragraph.get_content().len(), 1);
		
		let mut text_style = PContentStyle::new(&String::from("bold"));
		let text_id = id.get_id();
		text_style.get_content_mut().add_child(&PContentType::Text(PLabeler::new(text_id, &PContentText::new(&String::from("Mais l'honneteté m'oblige à te le dire :")))));
		let text_style_id = id.get_id();
		paragraph.get_content_mut().add_child(&PContentType::TextStyle(PLabeler::new(text_style_id, &text_style)));
		assert_eq!(paragraph.get_content().len(), 2);
		
		paragraph.add_text(id, &String::from(" ton Antoine commence à me les briser menu."));
		assert_eq!(paragraph.get_content().len(), 3);
	}
}