rustyphoenixlecture 1.12.1

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
	p_highlighted_code: String,
	///Number of lines of code
	p_nb_line: usize,
	///True if this environment needs line number print, false otherwise
	p_is_with_line_number: bool,
	///Display always as a block even if there is one single line of code
	p_is_display_block: bool,
	///Initial source without highlighting
	p_source: String,
	///True if the parser has a copy button, false otherwise
	p_is_copy_button: 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
	/// - `source` : initial source without highlighting
	/// - `is_copy_button` : true if the parser has a copy button, false otherwise
	/// # Returns
	/// Initialised PContentParser
	pub fn new(code: &String, is_with_line_number: bool, is_display_block: bool, source: &String, is_copy_button: bool) -> Self{
		let mut other = PContentParser{
			p_highlighted_code: code.clone(),
			p_nb_line: 0,
			p_is_with_line_number: is_with_line_number,
			p_is_display_block: is_display_block,
			p_source: source.clone(),
			p_is_copy_button: is_copy_button,
		};
		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.p_is_display_block
	}
	///Update the number of lines
	fn update_nb_line(&mut self){
		let trim_code: String = String::from(self.p_highlighted_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.p_highlighted_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
	{
		if self.p_is_copy_button && !self.p_source.is_empty(){
			backend.write(&String::from(format!("<div class=\"codecopy\"><button type=\"button\" onclick=\"phoenix_writeClipboardText('{}')\">copy</button></div>\n", self.p_source.replace("\n", "\\n").replace("'", "\\'").replace("\"", "&quot;"))));
		}
		if self.p_is_with_line_number {
			if self.p_nb_line > 1 || self.p_is_display_block || (!self.p_source.is_empty() && self.p_is_copy_button) {
				backend.write(&String::from(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{
					backend.write(&String::from(format!("{}\n", line_number + 1)));
				}
				backend.write(&String::from(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.p_highlighted_code)));
			}else{
				backend.write(&String::from(format!("<span id=\"{}\">{}</span>", id.get_id(), self.p_highlighted_code)));
			}
		}else{
			if self.p_nb_line > 1 || self.p_is_display_block || (!self.p_source.is_empty() && self.p_is_copy_button) {
				backend.write(&String::from(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.p_highlighted_code.replace("\n", "</p>\n<p>"))));
			}else{
				backend.write(&String::from(format!("<span id=\"{}\">{}</span>", id.get_id(), self.p_highlighted_code)));
			}
		}
	}
}

#[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, &String::from(""), 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, &String::from(""), 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, &String::from(""), 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, &String::from(""), 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 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"));
	}
	///Test the PContentParser with line number in multiple line mode
	#[test]
	fn test_content_parser_line_number_multiple_line_copy(){
		let text: PContentParser = PContentParser::new(&String::from("\nint val = 0;\nstd::string str(\"\");"), true, true, &String::from("\nint val = 0;\nstd::string str(\"\");"), true);
		let mut backend = PStrBackend::new();
		text.to_html(&mut backend, &PLabelId::new(42));
		assert_eq!(backend.get_body(), &String::from("<div class=\"codecopy\"><button type=\"button\" onclick=\"phoenix_writeClipboardText('\\nint val = 0;\\nstd::string str(&quot;&quot;);')\">copy</button></div>\n<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"));
	}
}