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
/***************************************
Auteur : Pierre Aubert
Mail : pierre.aubert@lapp.in2p3.fr
Licence : CeCILL-C
****************************************/
use crate::{
phighlighter::PLocation,
pcontent::{
pabstractcontent::{PAbstractContent, PAbstractLectureBackend},
plabeler::PLabelId,
preferenceurl::PReferenceUrl,
pveccontent::PVecContent
}
};
///Content object of the lecture
#[derive(Debug, Clone, Default, PartialEq)]
pub struct PContentEnvironment{
///Name of the environment
p_name: String,
///Main balise to use around this environment
p_balise: String,
///Vector of children
p_content: PVecContent,
///Image of the environement
p_image: String,
///Location where the PContentEvironement is defined in the lecture
p_location: PLocation,
}
impl PContentEnvironment {
///Constructor of the PContentEnvironment
/// # Parameters
/// - `name` : name of the environement
/// - `balise` : balise to use around this environement
/// - `image` : image to illustrate the environment (could be empty if there is no image)
/// - `location` : location where the current environement is defined
/// # Returns
/// Initialised PContentEnvironment
pub fn new(name: &String, balise: &String, image: &String, location: &PLocation) -> Self{
PContentEnvironment {
p_name: name.clone(),
p_balise: balise.clone(),
p_content: Default::default(),
p_image: image.clone(),
p_location: location.clone(),
}
}
///Get the content of the PContentEnvironmentWorkInProgress
/// # Returns
/// Content of the PContentEnvironmentWorkInProgress
pub fn get_content(&self) -> &PVecContent {
&self.p_content
}
///Get the mutable content of the PContentEnvironmentWorkInProgress
/// # Returns
/// Mutable content of the PContentEnvironmentWorkInProgress
pub fn get_content_mut(&mut self) -> &mut PVecContent {
&mut self.p_content
}
///Get the name of the current environment
/// # Returns
/// Name of the current environment
pub fn get_name(&self) -> &String{
&self.p_name
}
///Get the location of the current environment
/// # Returns
/// PLocation of the current environment
pub fn get_location(&self) -> &PLocation{
&self.p_location
}
}
impl PAbstractContent for PContentEnvironment{
///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{
false
}
///Get the reference url of the current PContent
/// # Parameters
/// - `current_file` : current output file of the PContent
/// - `id` : id of the current PContent
/// # Returns
/// Corresponding PReferenceUrl
fn get_reference_url(&self, current_file: &String, id: usize) -> PReferenceUrl{
PReferenceUrl::from_text(current_file, id, &String::from(&format!("PContentEnvironment({}) {}", self.p_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
{
if self.p_image.is_empty() {
backend.write(&String::from(&format!("<{} id=\"{}\" class=\"{}\">", self.p_balise, id.get_id(), self.p_name)));
self.p_content.to_html(backend, id);
backend.write(&String::from(&format!("</{}>\n", self.p_balise)));
}else{
backend.write(&format!("\n<div id=\"{}\" class=\"workinprogress\">\n", id.get_id()));
backend.write(&String::from(format!("\t<div class=\"workinprogressimage\"><img src=\"{}\" alt=\"wip\" /></div>\n", self.p_image)));
backend.write(&String::from(&format!("<{} id=\"{}\" class=\"{}\">", self.p_balise, id.get_id(), self.p_name)));
self.p_content.to_html(backend, id);
backend.write(&String::from(format!("</{}>\n</div>\n", self.p_balise)));
// backend.write(&format!("\n<table id=\"{}\" class=\"workinprogress\">\n", id.get_id()));
// backend.write(&String::from(format!("\t<tr><td class=\"workinprogressimage\"><img src=\"{}\" alt=\"wip\" /></td></tr>\n", self.p_image)));
// backend.write(&String::from(format!("\t<tr><td><{} class=\"{}\">", self.p_balise, self.p_name)));
// self.p_content.to_html(backend, id);
// backend.write(&String::from(format!("</{}></td></tr>\n</table>\n", self.p_balise)));
}
}
}
#[cfg(test)]
mod tests{
use super::*;
use crate::pcontent::{
pstrbackend::PStrBackend,
PContentType,
};
///Test the PContentEnvironment
#[test]
fn test_content_environment(){
let mut environement: PContentEnvironment = PContentEnvironment::new(&String::from("test"), &String::from("div"), &String::from(""), &Default::default());
environement.get_content_mut().add_child(&PContentType::from_text(43, &String::from("Some text.")));
let mut backend = PStrBackend::new();
environement.to_html(&mut backend, &PLabelId::new(42));
assert_eq!(backend.get_body(), &String::from("<div id=\"42\" class=\"test\">Some text.</div>\n"));
}
}