rustyphoenixlecture 1.7.2

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;

use crate::pcontent::{
	PReferenceUrl,
	pabstractcontent::{PAbstractContent, PAbstractLectureBackend},
	plabeler::PLabelId,
};

///Text Content object of the lecture
#[derive(Debug, Clone, PartialEq)]
pub struct PContentFormula{
	///Formula content
	p_formula: String,
	///Say if the current formula is inlined or not
	p_is_inlined: bool,
	///Location where the formula was defined
	p_location: PLocation,
}

impl PContentFormula {
	///Constructor of a PContentFormula
	/// # Parameters
	/// - `formula` : formula of the content
	/// - `is_inlined` : true if the current formula is inlined
	/// - `location` : location where the formula was defined in the lecture
	pub fn new(formula: &String, is_inlined: bool, location: &PLocation) -> Self{
		PContentFormula {
			p_formula: formula.clone(),
			p_is_inlined: is_inlined,
			p_location: location.clone(),
		}
	}
	///Say if the formula is inlined
	/// # Returns
	/// True if the formula is inlined, false otherwise
	pub fn is_inlined(&self) -> bool{
		self.p_is_inlined
	}
}

impl PAbstractContent for PContentFormula{
	///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 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!("Formula {}", 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_is_inlined {
			backend.write(&String::from(&format!("<table id=\"{}\" class=\"formula\"><tr><td>\n", id.get_id())));
		}else{
			backend.write(&String::from(&format!("<span id=\"{}\" class=\"inlineformula\">", id.get_id())));
		}
		backend.write_formula(&self.p_formula);
		if !id.get_label().is_empty() {
			if self.p_is_inlined{
				backend.write(&String::from(" "));
			}else{
				backend.write(&String::from("</td><td>"));
			}
		}
		id.to_html(backend, true);
		if !self.p_is_inlined {
			backend.write(&String::from("\n</td></tr>\n</table>\n"));
		}else{
			backend.write(&String::from("</span>"));
		}
	}
}

#[cfg(test)]
mod tests{
	use super::*;
	use std::path::PathBuf;

	use crate::pcontent::pstrbackend::PStrBackend;
	
	///Test the PContentFormula
	#[test]
	fn test_content_formula_inline(){
		let formula: PContentFormula = PContentFormula::new(&String::from("E = m c^2"), true, &PLocation::new(&PathBuf::from("index.md"), 1, 1));
		let mut backend = PStrBackend::new();
		formula.to_html(&mut backend, &PLabelId::new(42));
		assert_eq!(backend.get_body(), &String::from("<span id=\"42\" class=\"inlineformula\">E = m c^2</span>"));
	}
	///Test the PContentFormula
	#[test]
	fn test_content_formula(){
		let formula: PContentFormula = PContentFormula::new(&String::from("E = m c^2"), false, &PLocation::new(&PathBuf::from("index.md"), 1, 1));
		let mut backend = PStrBackend::new();
		formula.to_html(&mut backend, &PLabelId::new(42));
		assert_eq!(backend.get_body(), &String::from("<table id=\"42\" class=\"formula\"><tr><td>\nE = m c^2\n</td></tr>\n</table>\n"));
	}
}