rustyphoenixlecture 1.6.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},
	plabeler::PLabelId,
	preferenceurl::PReferenceUrl,
};

///Content object of the lecture
#[derive(Debug, Clone, PartialEq)]
pub struct PContentParser{
	///Program to be highligthed
	code: String,
	///Number of lines of code
	p_nb_line: usize,
	///True if this environment needs line number print, false otherwise
	is_with_line_number: bool,
	///Display always as a block even if there is one single line of code
	is_display_block: bool,
}

impl PContentParser {
	///Constructor of the PContentParser
	/// # Parameters
	/// - `code` : highlighted code
	/// - `is_with_line_number` : true if we have to show the line numbers, false if not
	/// - `is_display_block` : display always as a block even if there is one single line of code
	/// # Returns
	/// Initialised PContentParser
	pub fn new(code: &String, is_with_line_number: bool, is_display_block: bool) -> Self{
		let mut other = PContentParser{
			code: code.clone(),
			p_nb_line: 0,
			is_with_line_number: is_with_line_number,
			is_display_block: is_display_block,
		};
		other.update_nb_line();
		return other;
	}
	///Say if the current Parser is inlined or not
	/// # Returns
	/// True if the current Parser is inlined, false is not
	pub fn is_inlined(&self) -> bool{
		self.p_nb_line <= 1 && !self.is_display_block
	}
	///Update the number of lines
	fn update_nb_line(&mut self){
		let trim_code: String = String::from(self.code.trim_matches('\n'));
		let mut nb_line: usize = trim_code.matches("\n").count();
		if nb_line == 0 {
			nb_line = 1;
		}else{
			nb_line += 1;
		}
		self.p_nb_line = nb_line;
		self.code = trim_code.clone();
	}
}

impl PAbstractContent for PContentParser{
	///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!("PContentParser {}", 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 mut body = String::from("");
		if self.is_with_line_number {
			if self.p_nb_line > 1 || self.is_display_block {
				body += &format!("<table id=\"{}\" class=\"code\">\n\t<tbody>\n\t\t<tr>\n\t\t\t<td class=\"colLineNumbers\">\n\t\t\t\t<pre class=\"lineNumbers\">", id.get_id());
				for line_number in 0..self.p_nb_line{
					body += &format!("{}\n", line_number + 1);
				}
				body += &format!("</pre>\n\t\t\t</td>\n\t\t\t<td>\n\t\t\t\t<pre class=\"source\">{}</pre>\n\t\t\t</td>\n\t\t</tr>\n\t</tbody>\n</table>\n", self.code);
			}else{
				body = format!("<span id=\"{}\">{}</span>", id.get_id(), self.code);
			}
		}else{
			if self.p_nb_line > 1 || self.is_display_block {
				body = format!("<table id=\"{}\" class=\"code\">\n\t<tbody>\n\t\t<tr>\n\t\t\t<td>\n\t\t\t\t<div class=\"source\">\n<p>{}</p></div>\n\t\t\t</td>\n\t\t</tr>\n\t</tbody>\n</table>\n", id.get_id(), self.code.replace("\n", "</p>\n<p>"));
			}else{
				body = format!("<span id=\"{}\">{}</span>", id.get_id(), self.code);
			}
		}
		backend.write(&body);
	}
}

#[cfg(test)]
mod tests{
	use super::*;
	use crate::pcontent::pstrbackend::PStrBackend;
	
	///Test the PContentParser without line number in single line mode
	#[test]
	fn test_content_parser_no_line_number_single_line(){
		let text: PContentParser = PContentParser::new(&String::from("int val = 0"), false, false);
		let mut backend = PStrBackend::new();
		text.to_html(&mut backend, &PLabelId::new(42));
		assert_eq!(backend.get_body(), &String::from("<span id=\"42\">int val = 0</span>"));
	}
	///Test the PContentParser without line number in multiple line mode
	#[test]
	fn test_content_parser_no_line_number_multiple_line(){
		let text: PContentParser = PContentParser::new(&String::from("\nint val = 0;\nstd::string str(\"\");"), false, false);
		let mut backend = PStrBackend::new();
		text.to_html(&mut backend, &PLabelId::new(42));
		assert_eq!(backend.get_body(), &String::from("<table id=\"42\" class=\"code\">\n\t<tbody>\n\t\t<tr>\n\t\t\t<td>\n\t\t\t\t<div class=\"source\">\n<p>int val = 0;</p>\n<p>std::string str(\"\");</p></div>\n\t\t\t</td>\n\t\t</tr>\n\t</tbody>\n</table>\n"));
	}
	///Test the PContentParser with line number in single line mode
	#[test]
	fn test_content_parser_line_number_single_line(){
		let text: PContentParser = PContentParser::new(&String::from("int val = 0"), true, false);
		let mut backend = PStrBackend::new();
		text.to_html(&mut backend, &PLabelId::new(42));
		assert_eq!(backend.get_body(), &String::from("<span id=\"42\">int val = 0</span>"));
	}
	///Test the PContentParser with line number in multiple line mode
	#[test]
	fn test_content_parser_line_number_multiple_line(){
		let text: PContentParser = PContentParser::new(&String::from("\nint val = 0;\nstd::string str(\"\");"), true, true);
		let mut backend = PStrBackend::new();
		text.to_html(&mut backend, &PLabelId::new(42));
		assert_eq!(backend.get_body(), &String::from("<table id=\"42\" class=\"code\">\n\t<tbody>\n\t\t<tr>\n\t\t\t<td class=\"colLineNumbers\">\n\t\t\t\t<pre class=\"lineNumbers\">1\n2\n</pre>\n\t\t\t</td>\n\t\t\t<td>\n\t\t\t\t<pre class=\"source\">int val = 0;\nstd::string str(\"\");</pre>\n\t\t\t</td>\n\t\t</tr>\n\t</tbody>\n</table>\n"));
	}
}