use crate::plectureparser::pabstractcontent::{PAbstractContent, PAbstractLectureBackend};
use crate::plectureparser::plabeler::PLabelId;
use crate::plectureparser::{
PReferenceUrl,
};
#[derive(Debug, Clone, PartialEq)]
pub struct PContentParser{
code: String,
p_nb_line: usize,
is_with_line_number: bool,
is_display_block: bool,
}
impl 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;
}
pub fn is_inlined(&self) -> bool{
self.p_nb_line <= 1 && !self.is_display_block
}
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{
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
{
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::plectureparser::pstrbackend::PStrBackend;
#[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]
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]
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]
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"));
}
}