use crate::phighlighter::{
pfileparser::PFileParserIter,
lecture_str_to_html::lecture_str_to_html,
};
#[derive(Debug,Default,Clone,PartialEq, Eq)]
pub enum PTypeHighLight{
#[default]
None,
Token,
Match,
GetUntil(String),
Replace(String),
GetUntilReplace(String, String, String),
}
#[derive(Debug,Default,Clone)]
pub struct PActionHighLight{
p_style: String,
p_type: PTypeHighLight,
}
impl PActionHighLight{
pub fn new(style: &String, highlight_type: &PTypeHighLight) -> Self{
PActionHighLight{
p_style: style.clone(),
p_type: highlight_type.clone()
}
}
pub fn run(&self, iter: &mut PFileParserIter, prev_str: &String, token_charset: &String) -> String{
match &self.p_type {
PTypeHighLight::None => prev_str.clone(),
PTypeHighLight::GetUntil(pattern) => {
let mut final_pattern = pattern.clone();
let mut after_final_pattern = String::from("");
if *pattern == String::from("\n") {
after_final_pattern = pattern.clone();
final_pattern = String::from("");
}
format!("<span class=\"{}\">{}{}{}</span>{}",
self.p_style, &lecture_str_to_html(&prev_str), &lecture_str_to_html(&iter.get_until(&pattern)), &lecture_str_to_html(&final_pattern), &after_final_pattern)
},
PTypeHighLight::Replace(replace_str) => {
let extra_token_str: String = iter.get_str_of(token_charset);
if extra_token_str.is_empty() { format!("{}", &replace_str)
}else{ format!("{}{}", &lecture_str_to_html(&prev_str), &lecture_str_to_html(&extra_token_str))
}
},
PTypeHighLight::Token => {
let extra_token_str: String = iter.get_str_of(token_charset);
if extra_token_str.is_empty() { format!("<span class=\"{}\">{}</span>", self.p_style, &lecture_str_to_html(&prev_str))
}else{ format!("{}{}", &lecture_str_to_html(&prev_str), &lecture_str_to_html(&extra_token_str))
}
},
PTypeHighLight::Match => {
format!("<span class=\"{}\">{}</span>", self.p_style, &lecture_str_to_html(&prev_str))
},
PTypeHighLight::GetUntilReplace(pattern, start_replace, end_replace) => format!("{}{}{}", start_replace, &lecture_str_to_html(&iter.get_until(&pattern)), end_replace),
}
}
}
#[cfg(test)]
mod tests{
use super::*;
use crate::phighlighter::pfileparser::PFileParser;
#[test]
fn test_action_highlight_none(){
let action = PActionHighLight::new(&String::from("string"), &PTypeHighLight::None);
let parser = PFileParser::from_content(&String::from("just some text"));
let mut iter = &mut parser.iter(false);
assert_eq!(action.run(&mut iter, &String::from("just some text"), &String::from("")), String::from("just some text"));
}
#[test]
fn test_action_highlight_getuntil(){
let action = PActionHighLight::new(&String::from("string"), &PTypeHighLight::GetUntil(String::from("\"")));
let parser = PFileParser::from_content(&String::from("end of a string\""));
let mut iter = &mut parser.iter(false);
assert_eq!(action.run(&mut iter, &String::from("\""), &String::from("")), String::from("<span class=\"string\">"end of a string"</span>"));
}
#[test]
fn test_action_highlight_replace(){
let action = PActionHighLight::new(&String::from("keyword"), &PTypeHighLight::Replace(String::from("%23")));
let parser = PFileParser::from_content(&String::from("#"));
let mut iter = &mut parser.iter(false);
assert_eq!(action.run(&mut iter, &String::from("#"), &String::from("")), String::from("%23"));
}
#[test]
fn test_action_highlight_token(){
let action = PActionHighLight::new(&String::from("keyword"), &PTypeHighLight::Token);
let parser = PFileParser::from_content(&String::from("return"));
let mut iter = &mut parser.iter(false);
assert_eq!(action.run(&mut iter, &String::from("return"), &String::from("")), String::from("<span class=\"keyword\">return</span>"));
}
#[test]
fn test_action_highlight_getuntilreplace(){
let action = PActionHighLight::new(&String::from("title"), &PTypeHighLight::GetUntilReplace(String::from("\n"), String::from("<h1>"), String::from("</h1>")));
let parser = PFileParser::from_content(&String::from("Some title\n"));
let mut iter = &mut parser.iter(false);
assert_eq!(action.run(&mut iter, &String::from("\""), &String::from("")), String::from("<h1>Some title</h1>"));
}
}