use crate::pcontent::{
pabstractcontent::{PAbstractContent, PAbstractLectureBackend},
pidcounter::PIdCounter,
plabeler::{PLabelId, PLabeler},
preferenceurl::PReferenceUrl,
pveccontent::{PContentType, PVecContent}
};
#[derive(Debug, Clone, Default, PartialEq)]
pub enum PItemType{
#[default]
Item,
List
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct PContentItem{
p_content: PVecContent,
p_indentation_level: usize,
p_type: PItemType,
}
pub fn list_item_from_item(item: &PLabeler<PContentItem>) -> PContentItem{
let mut listitem = PContentItem::new(item.get_data().get_indentation_level(), &PItemType::List);
listitem.get_content_mut().add_child(&PContentType::ListItem(item.clone()));
return listitem;
}
impl PContentItem {
pub fn new(indentation_level: usize, item_type: &PItemType) -> Self{
PContentItem {
p_indentation_level: indentation_level,
p_type: item_type.clone(),
p_content: Default::default(),
}
}
pub fn get_type(&self) -> &PItemType{
&self.p_type
}
pub fn get_indentation_level(&self) -> usize{
self.p_indentation_level
}
pub fn get_content(&self) -> &PVecContent {
&self.p_content
}
pub fn get_content_mut(&mut self) -> &mut PVecContent {
&mut self.p_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_item) => {
match last_child_item {
PContentType::Paragraph(last_paragraph) => last_paragraph.get_data_mut().add_text(id, &trim_text),
PContentType::ListItem(last_child_item) => last_child_item.get_data_mut().add_text(id, &trim_text),
_ => self.get_content_mut().create_paragraph_with_text(id, &trim_text),
}
},
None => self.get_content_mut().create_paragraph_with_text(id, &trim_text),
}
}
}
impl PAbstractContent for PContentItem{
fn has_embeded_label(&self) -> bool{
true
}
fn get_reference_url(&self, current_file: &String, id: usize) -> PReferenceUrl{
let name: String = match self.p_type {
PItemType::Item => String::from("Item"),
PItemType::List => String::from("List")
};
PReferenceUrl::from_text(current_file, id, &String::from(&format!("PContent{} {}", name, id)))
}
fn to_html<TLectureBackend>(&self, backend: &mut TLectureBackend, id: &PLabelId)
where TLectureBackend: PAbstractLectureBackend
{
let delimiter: String = match self.p_type {
PItemType::Item => String::from("li"),
PItemType::List => String::from("lu")
};
backend.write(&String::from(&format!("<{}>", delimiter)));
self.p_content.to_html(backend, id);
id.to_html(backend, false);
backend.write(&String::from(&format!("</{}>\n", delimiter)));
}
}