rustyphoenixlecture 1.1.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},
	pidcounter::PIdCounter,
	plabeler::{PLabelId, PLabeler},
	preferenceurl::PReferenceUrl,
	pveccontent::{PContentType, PVecContent}
};

///Type of the current PContentItem
#[derive(Debug, Clone, Default, PartialEq)]
pub enum PItemType{
	///Item of a list
	#[default]
	Item,
	///List of item
	List
}

///Content object of the lecture
#[derive(Debug, Clone, Default, PartialEq)]
pub struct PContentItem{
	///Vector of children
	p_content: PVecContent,
	///Indentation level of the current item
	p_indentation_level: usize,
	///Type of the current PContentItem (PContentItem: item or PContentList: list)
	p_type: PItemType,
}

///Create a PContentItem list from a PContentItem item
/// # Parameters
/// - `item ` : item to create list from
/// # Returns
/// Corresponding PContentItem list
pub fn list_item_from_item(item: &PLabeler<PContentItem>) -> PContentItem{
	let mut listitem = PContentItem::new(item.get_data().get_indentation_level(), &PItemType::List);
	listitem.get_content_mut().add_child(&PContentType::ListItem(item.clone()));
	return listitem;
}

impl PContentItem {
	///Constructor of the PContentItem
	/// # Parameters
	/// - `indentation_level` : indentation level of the current item
	/// - `item_type` : type of the current PContentItem (item or list)
	/// # Returns
	/// Initialised PContentItem
	pub fn new(indentation_level: usize, item_type: &PItemType) -> Self{
		PContentItem {
			p_indentation_level: indentation_level,
			p_type: item_type.clone(),
			p_content: Default::default(),
		}
	}
	///Get the type of the PContentItem
	/// # Returns
	/// Type of the PContentItem (Item or List)
	pub fn get_type(&self) -> &PItemType{
		&self.p_type
	}
	///Get the indentation level of the current item
	/// # Returns
	/// Indentation level of the current item
	pub fn get_indentation_level(&self) -> usize{
		self.p_indentation_level
	}
	///Get the content of the PContentItem
	/// # Returns
	/// Content of the PContentItem
	pub fn get_content(&self) -> &PVecContent {
		&self.p_content
	}
	///Get the mutable content of the PContentItem
	/// # Returns
	/// Mutable content of the PContentItem
	pub fn get_content_mut(&mut self) -> &mut PVecContent {
		&mut self.p_content
	}
	///Add text in the PContentItem
	/// # Parameters
	/// - `id` : id manager
	/// - `text` : text to be added to the PContentItem
	/// This method is called by the PVecContent::add_text
	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_item) => {
				//If the last child is a text
				match last_child_item {
					PContentType::Paragraph(last_paragraph) => last_paragraph.get_data_mut().add_text(id, &trim_text),
					PContentType::ListItem(last_child_item) => last_child_item.get_data_mut().add_text(id, &trim_text),
					_ => self.get_content_mut().create_paragraph_with_text(id, &trim_text),
				}
			},
			None => self.get_content_mut().create_paragraph_with_text(id, &trim_text),
		}
	}
}

impl PAbstractContent for PContentItem{
	///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 PContentItem
	/// # Parameters
	/// - `current_file` : current output file of the PContentItem
	/// - `id` : id of the current PContent
	/// # Returns
	/// Corresponding PReferenceUrl
	fn get_reference_url(&self, current_file: &String, id: usize) -> PReferenceUrl{
		let name: String = match self.p_type {
			PItemType::Item => String::from("Item"),
			PItemType::List => String::from("List")
		};
		PReferenceUrl::from_text(current_file, id, &String::from(&format!("PContent{} {}", 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
	{
		let delimiter: String = match self.p_type {
			PItemType::Item => String::from("li"),
			PItemType::List => String::from("lu")
		};
		backend.write(&String::from(&format!("<{}>", delimiter)));
		self.p_content.to_html(backend, id);
		id.to_html(backend, false);
		backend.write(&String::from(&format!("</{}>\n", delimiter)));
	}
}