use crate::pcontent::{
pabstractcontent::{PAbstractContent, PAbstractLectureBackend},
plabeler::PLabelId,
preferenceurl::PReferenceUrl,
};
#[derive(Debug, Clone, PartialEq)]
pub struct PContentParser{
p_highlighted_code: String,
p_nb_line: usize,
p_is_with_line_number: bool,
p_is_display_block: bool,
p_source: String,
p_is_copy_button: bool,
}
impl 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;
}
pub fn is_inlined(&self) -> bool{
self.p_nb_line <= 1 && !self.p_is_display_block
}
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{
fn has_embeded_label(&self) -> bool{
false
}
fn get_reference_url(&self, current_file: &String, id: usize) -> PReferenceUrl{
PReferenceUrl::from_text(current_file, id, &String::from(&format!("PContentParser {}", id)))
}
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("\"", """))));
}
if self.p_is_with_line_number {
if self.p_nb_line > 1 || self.p_is_display_block {
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 {
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]
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]
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]
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]
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]
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("");')\">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"));
}
}