use crate::pcontent::{
pabstractcontent::{PAbstractContent, PAbstractLectureBackend},
pcontenttext::PContentText,
pidcounter::PIdCounter,
plabeler::{PLabelId, PLabeler},
preferenceurl::PReferenceUrl,
pveccontent::{PContentType, PVecContent}
};
#[derive(Debug, Clone, Default, PartialEq)]
pub struct PContentParagraph{
content: PVecContent,
}
impl PContentParagraph {
pub fn new() -> Self{
PContentParagraph {
content: Default::default(),
}
}
pub fn get_content(&self) -> &PVecContent {
&self.content
}
pub fn get_content_mut(&mut self) -> &mut PVecContent {
&mut self.content
}
pub fn add_text(&mut self, id: &mut PIdCounter, text: &String){
let trim_text = String::from(text.trim_matches('\n'));
if trim_text.is_empty() { return;
}
match self.get_content_mut().get_vec_child_mut().last_mut() {
Some(last_child_paragraph) => {
match last_child_paragraph {
PContentType::Text(child_text) => child_text.get_data_mut().add_text(&trim_text),
_ => {
self.create_text(id, &trim_text);
}
}
},
None =>{
self.create_text(id, &trim_text);
}
}
}
fn create_text(&mut self, id: &mut PIdCounter, trim_text: &String){
let text_content = PContentType::Text(PLabeler::new(id.get_id(), &PContentText::new(&trim_text)));
self.get_content_mut().add_child(&text_content);
}
}
impl PAbstractContent for PContentParagraph{
fn has_embeded_label(&self) -> bool{
true
}
fn get_reference_url(&self, current_file: &String, id: usize) -> PReferenceUrl{
PReferenceUrl::from_text(current_file, id, &String::from(&format!("PContentParagraph {}", id)))
}
fn to_html<TLectureBackend>(&self, backend: &mut TLectureBackend, id: &PLabelId)
where TLectureBackend: PAbstractLectureBackend
{
if self.content.len() == 0 {
return;
}
backend.write(&format!("<p id=\"{}\">", id.get_id()));
self.content.to_html(backend, id);
id.to_html(backend, false);
backend.write(&String::from("</p>\n"))
}
}
#[cfg(test)]
mod tests{
use super::*;
use crate::pcontent::{pcontentstyle::PContentStyle, plabeler::PLabeler};
#[test]
fn test_pcontent_paragraph_add_text(){
let id = &mut PIdCounter::new(0); let mut paragraph = PContentParagraph::new();
paragraph.add_text(id, &String::from("Patricia mon petit"));
assert_eq!(paragraph.get_content().len(), 1);
paragraph.add_text(id, &String::from(", l'homme de la pampa parfois rude reste toujours courtois."));
assert_eq!(paragraph.get_content().len(), 1);
let mut text_style = PContentStyle::new(&String::from("bold"));
let text_id = id.get_id();
text_style.get_content_mut().add_child(&PContentType::Text(PLabeler::new(text_id, &PContentText::new(&String::from("Mais l'honneteté m'oblige à te le dire :")))));
let text_style_id = id.get_id();
paragraph.get_content_mut().add_child(&PContentType::TextStyle(PLabeler::new(text_style_id, &text_style)));
assert_eq!(paragraph.get_content().len(), 2);
paragraph.add_text(id, &String::from(" ton Antoine commence à me les briser menu."));
assert_eq!(paragraph.get_content().len(), 3);
}
}