1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/***************************************
Auteur : Pierre Aubert
Mail : pierre.aubert@lapp.in2p3.fr
Licence : LGPL-3.0-only
****************************************/
use crate::pcontent::{
pabstractcontent::{PAbstractContent, PAbstractLectureBackend},
plabeler::{PLabelId, PLabeler},
preferenceurl::PReferenceUrl,
pveccontent::{PContentType, PVecContent}
};
///Type of the current PContentItem
#[derive(Debug, Clone, Default, PartialEq)]
pub enum PItemType{
///Item of a list
#[default]
Item,
///List of item
List
}
///Content object of the lecture
#[derive(Debug, Clone, Default, PartialEq)]
pub struct PContentItem{
///Vector of children
p_content: PVecContent,
///Indentation level of the current item
p_indentation_level: usize,
///Type of the current PContentItem (PContentItem: item or PContentList: list)
p_type: PItemType,
}
///Create a PContentItem list from a PContentItem item
/// # Parameters
/// - `item ` : item to create list from
/// # Returns
/// Corresponding PContentItem list
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 {
///Constructor of the PContentItem
/// # Parameters
/// - `indentation_level` : indentation level of the current item
/// - `item_type` : type of the current PContentItem (item or list)
/// # Returns
/// Initialised 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(),
}
}
///Get the type of the PContentItem
/// # Returns
/// Type of the PContentItem (Item or List)
pub fn get_type(&self) -> &PItemType{
&self.p_type
}
///Get the indentation level of the current item
/// # Returns
/// Indentation level of the current item
pub fn get_indentation_level(&self) -> usize{
self.p_indentation_level
}
///Get the content of the PContentItem
/// # Returns
/// Content of the PContentItem
pub fn get_content(&self) -> &PVecContent {
&self.p_content
}
///Get the mutable content of the PContentItem
/// # Returns
/// Mutable content of the PContentItem
pub fn get_content_mut(&mut self) -> &mut PVecContent {
&mut self.p_content
}
///Add text in the PContentItem
/// # Parameters
/// - `text` : text to be added to the PContentItem
/// This method is called by the PVecContent::add_text
pub fn add_text(&mut self, text: &String){
//First we trim the text
let trim_text = String::from(text.trim_matches('\n'));
if trim_text.is_empty() { //If the text is empty, we do nothing
return;
}
match self.get_content_mut().get_vec_child_mut().last_mut() {
Some(last_child_item) => {
//If the last child is a text
match last_child_item {
PContentType::Paragraph(last_paragraph) => last_paragraph.get_data_mut().add_text(&trim_text),
PContentType::ListItem(last_child_item) => last_child_item.get_data_mut().add_text(&trim_text),
_ => self.get_content_mut().create_paragraph_with_text(&trim_text),
}
},
None => self.get_content_mut().create_paragraph_with_text(&trim_text),
}
}
}
impl PAbstractContent for PContentItem{
///Say if the PContent has an embeded label
/// # Returns
/// True if the PContent has an embeded label, false otherwise
fn has_embeded_label(&self) -> bool{
true
}
///Get the reference url of the current PContentItem
/// # Parameters
/// - `current_file` : current output file of the PContentItem
/// - `id` : id of the current PContent
/// # Returns
/// Corresponding PReferenceUrl
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)))
}
///Convert the current struct into html
/// # Parameters
/// - `backend` : backend which write a lecture in files
/// - `id` : id of the current PContent
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("ul")
};
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)));
}
}