rustyphoenixlecture 1.4.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
****************************************/

pub use crate::phighlighter::{
	pnodehighlight::PNodeHighLight,
	pactionhighlight::{
		PTypeHighLight,
		PActionHighLight
	},
	phighlither_configuration::{
		PHighlighterGetUntil,
		PHighlighterGetUntilReplace,
		PHighlighterSequence
	},
	pfileparser::{PFileParser, PFileParserIter},
	planguage::PLanguage,
};

///Highlighter of any programming language
/// The PHighlighter manage all highlight for all supported languages even with composed highlighting (for example markdown which can also has c++, bash, rust, etc, blocks)
#[derive(Debug,Default,Clone)]
pub struct PHighlighter{
	///Language description
	p_language: PLanguage,
	///Node which starts the highlighting tree
	p_node: PNodeHighLight,
	///Vector of the different highlighting action for the current highlighter
	p_vec_action: Vec<PActionHighLight>,
}

impl PHighlighter{
	///Constructor of a PHighlighter
	/// # Returns
	/// Initialised PHighlighter
	pub fn new() -> Self{
		PHighlighter{
			p_language: Default::default(),
			p_node: Default::default(),
			//By default, there is nothing to perform
			p_vec_action: vec![PActionHighLight::new(&String::from(""), &PTypeHighLight::None)]
		}
	}
	///Set the language of the PHighlighter
	/// # Parameters
	/// - `language` : description of the language to be parsed by the current parser
	pub fn set_language(&mut self, language: &PLanguage){
		self.p_language = language.clone();
	}
	///Set the language of the PHighlighter
	/// # Returns
	/// Description of the language to be parsed by the current parser
	pub fn get_language(&self) -> &PLanguage{
		&self.p_language
	}
	///Perform the highlight the example
	/// # Returns
	/// Highlighted string
	pub fn highlight_example(&self) -> String{
		return self.highlight(&self.p_language.get_example());
	}
	///Perform the highlight the given input_str
	/// # Parameters
	/// - `input_str` : string to highlight
	/// # Returns
	/// Highlighted string
	pub fn highlight(&self, input_str: &String) -> String{
		if input_str.trim().is_empty() {
			return String::from("");
		}
		let parser: PFileParser = PFileParser::from_content(input_str);
		let mut it: PFileParserIter = parser.iter(self.p_language.get_is_escape_char());
		let mut body = String::from("");
		let mut current_match_token = String::from("");
		while !it.is_end_of_file() {
			self.p_node.highlight(&mut body, &mut current_match_token, &mut it, &self.p_vec_action, &self.p_language.get_token_charset());
		}
		return body;
	}
	///Perform the highlighting with an iterator
	/// # Parameters
	/// - `highlight_iter` : iterator on the file to be checked
	/// # Returns
	/// Higlighted string on success, empty string if no match was found
	pub fn highlight_iter(&self, file_iter: &mut PFileParserIter) -> String {
		let mut body = String::from("");
		let mut current_match_token = String::from("");
		self.p_node.highlight(&mut body, &mut current_match_token, file_iter, &self.p_vec_action, &self.p_language.get_token_charset());
		return body;
	}
	///Add a vector of token
	/// # Parameters
	/// - `vec_token` : vector of token to be added
	/// - `css_style` : add an action style
	pub fn add_vec_token(&mut self, vec_token: &Vec<String>, css_style: &String){
		//We create the action and add it into the PHighlighter
		let action_index: usize = self.add_style(css_style, &PTypeHighLight::Token);
		//Then we add all token in the node
		for token in vec_token.iter(){
			self.p_node.add_token(&token, &Some(action_index));
		}
	}
	///Add a vector of token
	/// # Parameters
	/// - `vec_match` : vector of match to be added (without the token check)
	/// - `css_style` : add an action style
	pub fn add_vec_match(&mut self, vec_match: &Vec<String>, css_style: &String){
		//We create the action and add it into the PHighlighter
		let action_index: usize = self.add_style(css_style, &PTypeHighLight::Match);
		//Then we add all token in the node
		for token in vec_match.iter(){
			self.p_node.add_token(&token, &Some(action_index));
		}
	}
	///Add replace action to the PHighlighter
	/// # Parameters
	/// - `pattern` : pattern to be replace when it is found
	/// - `replace` : replace string of the found pattern
	pub fn add_replace(&mut self, pattern: &String, replace: &String){
		let action_index: usize = self.add_style(&String::from(""), &PTypeHighLight::Replace(replace.clone()));
		self.p_node.add_token(&pattern, &Some(action_index));
	}
	///Add a vector of getuntil
	/// # Parameters
	/// - `vec_getuntil` : vector of getuntil
	pub fn add_vec_getuntil(&mut self, vec_getuntil: &Vec<PHighlighterGetUntil>){
		for getuntil in vec_getuntil.iter(){
			let action_index: usize = self.add_style(&getuntil.style, &PTypeHighLight::GetUntil(getuntil.end_token.clone()));
			self.p_node.add_getuntil(getuntil, action_index);
		}
	}
	///Add a vector of getuntil
	/// # Parameters
	/// - `vec_getuntilreplace` : vector of getuntilreplace
	pub fn add_vec_getuntilreplace(&mut self, vec_getuntilreplace: &Vec<PHighlighterGetUntilReplace>){
		for getuntilreplace in vec_getuntilreplace.iter(){
			let action_index: usize = self.add_style(&String::from(""),
				&PTypeHighLight::GetUntilReplace(
					getuntilreplace.end_token.clone(),
					getuntilreplace.replace_start.clone(),
					getuntilreplace.replace_end.clone()));
			self.p_node.add_getuntilreplace(getuntilreplace, action_index);
		}
	}
	///Add a vector of sequence
	/// # Parameters
	/// - `vec_sequence` : vector of sequence
	pub fn add_vec_sequence(&mut self, vec_sequence: &Vec<PHighlighterSequence>){
		for sequence in vec_sequence.iter() {
			//If the last step of the sequence is a token or a oneof, the action is a Token, if not it is a Match
			let type_highlight: PTypeHighLight = match sequence.vec_step.last() {
				Some(last_step) => {
					match &last_step.keyword {
						Some(_) => PTypeHighLight::Match,
						None => PTypeHighLight::Token
					}
				},
				None => PTypeHighLight::Token
			};
			let action_index: usize = self.add_style(&sequence.style, &type_highlight);
			self.p_node.add_sequence(sequence, &Some(action_index));
		}
	}
	///Add a style
	/// # Parameters
	/// - `css_style` : style of the highlight on match
	/// - `action_type` : type of the action to perform
	/// # Returns
	/// Corresponding index in the vector of action
	fn add_style(&mut self, css_style: &String, action_type: &PTypeHighLight) -> usize{
		let action: PActionHighLight = PActionHighLight::new(css_style, action_type);
		let action_index: usize = self.p_vec_action.len();
		self.p_vec_action.push(action);
		return action_index;
	}
}